Methods to Construct a Textual content-to-Voice Software With JavaScript

0
41
How to Build a Text-to-Voice Application With JavaScript


This tutorial will cowl convert textual content into speech utilizing JavaScript utilizing WebSpeechAPI. It would characteristic a easy interface the place the consumer provides the textual content to be spoken, then clicks a button to generate the corresponding speech.

Our Textual content-to-Speech Demo

Right here’s what we’re going to construct. Sort something you need within the textarea, choose the language you’ve written it in, and click on the button to listen to the end result!

HTML Construction

Okay, let’s begin constructing. The HTML Construction will encompass the next components:

  • a
    9
      
    
    10
      

    class="form-group">

    11
        
    
    12
        
    13
        
    
    14
      
    
    15
      
    
    16
      
    
    17
    
    

Further Styling with CSS

Bootstrap handles just about all of the styling for us. However let’s add some customized CSS properties to our design. These will give us a customized font, a container, some further spacing for the weather within the kind, and a rule to cover our alert message.

1
@import url("https://fonts.googleapis.com/css2?household=DM+Mono:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&show=swap");
2

3
physique {
4
  font-family: "DM Mono", monospace;
5
}
6
.container {
7
  width: 100%;
8
  max-width: 600px;
9
  padding: 2rem 0;
10
}
11
.form-group {
12
  margin: 2rem 0;
13
}
14
label {
15
  margin-bottom: 1rem;
16
}
17
.message{
18
    show: none;
19
}

Now we have set show:none to the alert part so that it’ll solely seem if there are error messages to show.

JavaScript Performance

As I defined within the introduction, we are able to get hold of voices utilizing the speechSynthesis.getVoices() technique; let’s begin by getting and storing them in an array like this.

1
const voices = [
2
  { name: "Google Deutsch", lang: "de-DE" },
3
  { name: "Google US English", lang: "en-US" },
4
  { name: "Google UK English Female", lang: "en-GB" },
5
  { name: "Google UK English Male", lang: "en-GB" },
6
  { name: "Google español", lang: "es-ES" },
7
  { name: "Google español de Estados Unidos", lang: "es-US" },
8
  { name: "Google français", lang: "fr-FR" },
9
  { name: "Google हिन्दी", lang: "hi-IN" },
10
  { name: "Google Bahasa Indonesia", lang: "id-ID" },
11
  { name: "Google italiano", lang: "it-IT" },
12
  { name: "Google 日本語", lang: "ja-JP" },
13
  { name: "Google 한국의", lang: "ko-KR" },
14
  { name: "Google Nederlands", lang: "nl-NL" },
15
  { name: "Google polski", lang: "pl-PL" },
16
  { name: "Google português do Brasil", lang: "pt-BR" },
17
  { name: "Google русский", lang: "ru-RU" },
18
  { name: "Google 普通话(中国大陆)", lang: "zh-CN" },
19
  { name: "Google 粤語(香港)", lang: "zh-HK" },
20
  { name: "Google 國語(臺灣)", lang: "zh-TW" }
21
];

Establish the Required Parts

Subsequent, use the Doc Object Mannequin (DOM) to acquire the alert, choose, and button components.

1
const optionsContainer = doc.querySelector(".select-voices");
2
const convertBtn = doc.querySelector(".convert");
3
const messageContainer = doc.querySelector(".message")

Create Voices Choice

The optionsContainer represents the

Please enter your comment!
Please enter your name here