Text-to-Voice Generator
playPauseButton.addEventListener("click", () => { Const text = textInput.value.trim(); if (text) { if (!isPlaying) { loadingIndicator.style.display = "block"; playPauseButton.disabled = true; const speechSynthesis = window.speechSynthesis; const utterance = new SpeechSynthesisUtterance(text);
// Handle speech synthesis errors utterance.onerror = () => { loadingIndicator.style.display = "none"; playPauseButton.disabled = false; alert("Error occurred while converting text to speech."); };
// Handle audio playback audio.onended = () => { loadingIndicator.style.display = "none"; playPauseButton.disabled = false; isPlaying = false; playPauseButton.innerText = "Play"; };
speechSynthesis.speak(utterance); audio.src = `data:audio/wav;base64,${btoa(text)}`; audio.play(); isPlaying = true; playPauseButton.innerText = "Pause"; } else { audio.pause(); isPlaying = false; playPauseButton.innerText = "Play"; } } });
downloadButton.addEventListener("click", () => { const text = textInput.value.trim(); if (text) { // Create a Blob with the audio data const blob = new Blob([audio.src], { type: 'audio/wav' }); const url = window.URL.createObjectURL(blob);
// Create a download link and trigger the download
const a = document.createElement('a');
a.href = url;
a.download = 'generated_audio.wav';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
});
});