• We just launched and are currently in beta. Join us as we build and grow the community.

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

Plenzy_

Domain Strategist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
53
Likes
195
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <head

    >
  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. html,
  18. body {
  19. height: 100%;
  20. width: 100%;
  21. font-family: Apple Chancery, cursive;
  22. }

  23. mark {
  24. background-color: yellow;
  25. }
  26. </

    style

    >
  27. <!-- End of Custom Css -->
  28. </

    head

    >

  29. <body

    class

    =

    "bg-light"

    >
  30. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    id

    =

    "topNavBar"

    >
  31. <div

    class

    =

    "container"

    >
  32. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  33. Sourcecodester
  34. </

    a

    >
  35. </

    div

    >
  36. </

    nav>
  37. <div

    class

    =

    "container py-3"

    id

    =

    "page-container"

    >
  38. <h3

    >

    Text-To-Speech</

    h3

    >
  39. <hr

    >
  40. <small

    >

    This simple project converts your text to speech.</

    small

    >
  41. <div

    class

    =

    "content"

    >
  42. <div

    class

    =

    "row justify-content-center"

    >
  43. <div

    class

    =

    "col-md-6"

    >
  44. <!-- Text Area Form -->
  45. <form

    action

    =

    ""

    id

    =

    "text-form"

    >
  46. <div

    class

    =

    "form-goup"

    >
  47. <label

    for

    =

    "message"

    class

    =

    "control-label"

    >

    Enter you Message to Convert</

    label

    >
  48. <textarea

    name

    =

    "message"

    id

    =

    "message"

    rows

    =

    "10"

    class

    =

    "form-control rounded-0"

    ></

    textarea

    >
  49. </

    div

    >
  50. <center

    >
  51. <button

    class

    =

    "btn btn-primary py-2"

    id

    =

    "submit"

    >

    Speak</

    button

    >
  52. </

    center

    >
  53. </

    form

    >
  54. <!-- End of Text Area Form-->
  55. <!-- Display Container of the Text -->
  56. <div

    id

    =

    "display"

    class

    =

    "py-3"

    >

  57. </

    div

    >
  58. <!-- End Display Container of the Text -->
  59. </

    div

    >
  60. </

    div

    >
  61. </

    div

    >
  62. </

    div

    >
  63. </

    body

    >

  64. </

    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. // An Event that identifies the part of text has spoken
  9. text_to_speech.onboundary

    =

    function

    (

    e)

    {
  10. var

    message =

    $(

    '#display'

    )

    .text

    (

    )
  11. var

    b_text =

    String

    (

    message)

    .substring

    (

    0

    ,

    e.charIndex

    +

    e.charLength

    )
  12. var

    marked =

    $(

    '<mark>'

    )
  13. marked.text

    (

    b_text)
  14. $(

    '#display'

    )

    .html

    (

    ''

    )
  15. $(

    '#display'

    )

    .append

    (

    marked)
  16. $(

    '#display'

    )

    .append

    (

    message.replace

    (

    b_text,

    ""

    )

    )
  17. }

  18. $(

    '#text-form'

    )

    .submit

    (

    function

    (

    e)

    {
  19. e.preventDefault

    (

    )

    ;
  20. // Set Text to be spoken
  21. text_to_speech.text

    =

    $(

    '#message'

    )

    .val

    (

    )

    ;
  22. $(

    '#display'

    )

    .text

    (

    $(

    '#message'

    )

    .val

    (

    )

    )
  23. $(

    '#message'

    )

    .val

    (

    ''

    )
  24. // Start to Speak the text
  25. synth.speak

    (

    text_to_speech)
  26. $(

    '#display'

    )

    .animate

    (

    {

    scrollTop:

    0

    }

    ,

    'fast'

    )
  27. }

    )
  28. }

    )

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:)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top