Here's an example.
<!DOCTYPE html> <html> <body> <!--Will read this content inside the P element, including the smiley face.--> <p id='intro'> Hello, I am Arun Banik and welcome to my blog. :-) </p> </body> <script> let text = document.getElementById('intro').innerHTML; const utterText = new SpeechSynthesisUtterance(text); window.speechSynthesis.speak(utterText); </script> </html>
It is not re-inventing the wheel. I know this feature is availble with many mordern browsers. Its just that you can have your own custom build text-to-speech functionality. You can customize text utterance by changing the pitch, speed, volume etc.
The SpeechSynthesis API instructs the browser to convert the texts into speech.
This API is supported by all morden browsers.
Here are few properties of the API that you might like to use.
1) Increase speed of text-to-speech rate
You can control the speed at which the text will be read by using the rate property. For example,
<script> let text = 'Hello world, I am Arun Banik'; const utterText = new SpeechSynthesisUtterance(text); utterText.rate = 2; // the speed at which the text will be read aloud. window.speechSynthesis.speak(utterText); </script>
Learn more about SpeechSynthesisUtterance interface.
2) Text-to-speech using different voices
The texts can be read using different voices like a male voice or a female voice and with different accent.
<!DOCTYPE html> <html> <body> <!--This drowdown will have different voices.--> <select id='selected_voice'></select> <p id='intro'> Hello, I am Arun Banik and welcome to my blog. </p> <input type='button' value='Speak' id='bt_speak' /> </body> <script> bt_speak.addEventListener("click", () => { let text = document.getElementById('intro').innerHTML; const utterText = new SpeechSynthesisUtterance(text); const selectedOption = selected_voice.selectedOptions[0].getAttribute("data-name"); for (let i = 0; i < voices.length; i++) { if (voices[i].name === selectedOption) { utterText.voice = voices[i]; // The voice property. } } window.speechSynthesis.speak(utterText); }); let voices = []; // Store available voices in an array. List will vary depending upon browser. let setVoices = () => { voices = window.speechSynthesis.getVoices(); for (let i = 0; i < voices.length; i++) { const option = document.createElement("option"); option.textContent = `${voices[i].name} (${voices[i].lang})`; if (voices[i].default) { option.textContent += " — DEFAULT"; } option.setAttribute("data-lang", voices[i].lang); option.setAttribute("data-name", voices[i].name); // add all available voices in the SELECT dropdown list. document.getElementById("selected_voice").appendChild(option); } } if ( typeof speechSynthesis !== "undefined" && speechSynthesis.onvoiceschanged !== undefined) { speechSynthesis.onvoiceschanged = setVoices; } </script> </html>