Web Text-to-Speech Tutorial using Web Speech API Synthesis

This is a simple tutorial that will teach how to convert Text into Speech in web applications. In order to do this, we are going to use the Web Speech API Interface Speech Synthesis. The Web Speech API will allow you to convert the text into incorporate voice data into web projects.

In this tutorial, I will be providing a simple web application that allows users to write a text message and converts it into speech. The application will re-display the text message below the form. Each word spoken will be highlighted.

Getting Started

The source code below uses Bootstrap v5 as the CSS Framework of the application user interface. Also, I have used jQuery Library for writing the JavaScript functions.

Compile the downloaded files of the library and CSS Framework into a directory that will serve as your source code directory.

Creating the Interface

The following script is an HTML File that contains the HTML Tags for the design of our web application. This file holds the Text Message Form and the Display Area.

Open a text editor such as (notepad++, Submlime Text, VS Code, or etc.). Create a new HTML File. Copy the script below and paste it into your new HTML File. Then, save the file into your source code directory and name the file as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Text-To-Speech</title>
  8.     <link rel="stylesheet" href="./css/bootstrap.min.css">
  9.     <script src="./js/jquery-3.6.0.min.js"></script>
  10.     <script src="./js/bootstrap.min.js"></script>
  11.     <script src="./js/script.js"></script>
  12.     <!-- Custom Css -->
  13.     <style>
  14.          :root {
  15.             --bs-success-rgb: 71, 222, 152 !important;
  16.         }
  17.        
  18.         html,
  19.         body {
  20.             height: 100%;
  21.             width: 100%;
  22.             font-family: Apple Chancery, cursive;
  23.         }
  24.        
  25.         mark {
  26.             background-color: yellow;
  27.         }
  28.     </style>
  29.     <!-- End of Custom Css -->
  30. </head>
  31.  
  32. <body class="bg-light">
  33.     <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  34.         <div class="container">
  35.             <a class="navbar-brand" href="https://sourcecodester.com">
  36.             Sourcecodester
  37.             </a>
  38.         </div>
  39.     </nav>
  40.     <div class="container py-3" id="page-container">
  41.         <h3>Text-To-Speech</h3>
  42.         <hr>
  43.         <small>This simple project converts your text to speech.</small>
  44.         <div class="content">
  45.             <div class="row justify-content-center">
  46.                 <div class="col-md-6">
  47.                     <!-- Text Area Form -->
  48.                     <form action="" id="text-form">
  49.                         <div class="form-goup">
  50.                             <label for="message" class="control-label">Enter you Message to Convert</label>
  51.                             <textarea name="message" id="message" rows="10" class="form-control rounded-0"></textarea>
  52.                         </div>
  53.                         <center>
  54.                             <button class="btn btn-primary py-2" id="submit">Speak</button>
  55.                         </center>
  56.                     </form>
  57.                     <!-- End of Text Area  Form-->
  58.                     <!-- Display Container of the Text -->
  59.                     <div id="display" class="py-3">
  60.  
  61.                     </div>
  62.                     <!-- End Display Container of the Text -->
  63.                 </div>
  64.             </div>
  65.         </div>
  66.     </div>
  67. </body>
  68.  
  69. </html>

Creating the Main Function

The following code/script is a JavaScript file which contains the codes of the function of form submission and speech synthesis configuration. Save this file as script.js.

  1. // setting Speech Synthesis
  2. let text_to_speech = new SpeechSynthesisUtterance();
  3. let synth = window.speechSynthesis;
  4. $(function() {
  5.     // SpeechSynthesisUtterance Configuration
  6.     text_to_speech.lang = 'en';
  7.     text_to_speech.voice = synth.getVoices()[0];
  8.  
  9.     // An Event that identifies the part of text has spoken
  10.     text_to_speech.onboundary = function(e) {
  11.         var message = $('#display').text()
  12.         var b_text = String(message).substring(0, e.charIndex + e.charLength)
  13.         var marked = $('<mark>')
  14.         marked.text(b_text)
  15.         $('#display').html('')
  16.         $('#display').append(marked)
  17.         $('#display').append(message.replace(b_text, ""))
  18.     }
  19.  
  20.     $('#text-form').submit(function(e) {
  21.         e.preventDefault();
  22.         // Set Text to be spoken
  23.         text_to_speech.text = $('#message').val();
  24.         $('#display').text($('#message').val())
  25.         $('#message').val('')
  26.             // Start to Speak the text
  27.         synth.speak(text_to_speech)
  28.         $('#display').animate({ scrollTop: 0 }, 'fast')
  29.     })
  30. })
Note: Check the CSS and Script Links imported in the index.html file if the file path location is right.

That's it! You can now test the source code on your end and see if the application is working as was planned. To run the application, browse the index.html file in a browser. If there's an error occurred on your end, check your source code and differentiate it from the source code I provided above. You can also download the working source code that I created for this tutorial. The download button is located below this article.

DEMO VIDEO

That is the end of this tutorial. I hope this will help you with what you are looking for and for your future Web Application Project. Explore more on this website for more tutorials and free source codes.

Happy Coding:)

Comments

Add new comment