Untitled
unknown
javascript
2 years ago
2.2 kB
9
Indexable
// Create a promise that will resolve when the list of available voices is populated
const awaitVoices = new Promise(resolve => {
// Create a speech synthesis object
const synth = window.speechSynthesis;
// Retrieve the list of available voices
let voices = synth.getVoices();
// Define the voice names for Pavel and Irina
const pavelVoiceName = 'Microsoft Pavel - Russian (Russia)';
const irinaVoiceName = 'Microsoft Irina - Russian (Russia)';
// Find and store Pavel's voice
let pavelVoice = voices.find(voice => voice.name === pavelVoiceName);
// Find and store Irina's voice
let irinaVoice = voices.find(voice => voice.name === irinaVoiceName);
// If either voice is not found or the voices list is empty, wait for it to populate
if (!pavelVoice || !irinaVoice || voices.length === 0) {
synth.addEventListener('voiceschanged', () => {
voices = synth.getVoices();
pavelVoice = voices.find(voice => voice.name === pavelVoiceName);
irinaVoice = voices.find(voice => voice.name === irinaVoiceName);
// If both voices are found, continue with the initialization
if (pavelVoice && irinaVoice) {
// Define the utterance object as a global variable
const utterance = new SpeechSynthesisUtterance();
// Set the "lang" property of the utterance object to 'ru-RU'
utterance.lang = 'ru-RU';
// Set the "voice" property of the utterance object to Pavel's voice
utterance.voice = pavelVoice;
// Resolve the promise
resolve({ synth, utterance, voices, pavelVoice, irinaVoice });
}
});
} else {
// Define the utterance object as a global variable
const utterance = new SpeechSynthesisUtterance();
// Set the "lang" property of the utterance object to 'ru-RU'
utterance.lang = 'ru-RU';
// Set the "voice" property of the utterance object to (Needed) voice
utterance.voice = irinaVoice;
// Resolve the promise
resolve({ synth, utterance, voices, pavelVoice, irinaVoice });
}
});Editor is loading...
Leave a Comment