text to voice text to voice generator
Text-to-Speech Tool
const synth = window.speechSynthesis; let voices = []; // Fetch the list of available voices and populate the dropdown list function populateVoices() { voices = synth.getVoices(); const voiceSelect = document.getElementById("voice-select"); voices.forEach(voice => { const option = document.createElement("option"); option.textContent = `${voice.name} (${voice.lang})`; option.setAttribute("data-lang", voice.lang); option.setAttribute("data-name", voice.name); voiceSelect.appendChild(option); }); } // Speak the input text using the selected voice function speak() { const inputText = document.getElementById("text-input").value; const voiceSelect = document.getElementById("voice-select"); const selectedOption = voiceSelect.selectedOptions[0].getAttribute("data-name"); const utterance = new SpeechSynthesisUtterance(inputText); voices.forEach(voice => { if (voice.name === selectedOption) { utterance.voice = voice; } }); synth.speak(utterance); } // Stop speaking function stop() { synth.cancel(); } // Populate the list of available voices when the page loads populateVoices(); // Update the list of available voices if they change (e.g. if the user installs a new voice) synth.onvoiceschanged = populateVoices; // Bind the speak and stop buttons to their respective functions document.getElementById("speak-button").addEventListener("click", speak); document.getElementById("stop-button").addEventListener("click", stop);
Post a Comment