📜  使用 Google Chrome 的 Web Speech API 进行文本到语音的转换

📅  最后修改于: 2021-10-29 04:19:32             🧑  作者: Mango

创建一个将文本转换为语音的 Web 应用程序听起来很酷,如果所有这些功能都可用而不受任何第三方库的干扰,那么实现起来就更容易了。 Web 语音 API 提供了基本工具,可用于创建启用语音数据的交互式 Web 应用程序。我们创建了一个基本界面,其中包含一个简单的框,其中包含我们将在其中编写文本的文本输入部分,以及两个用于控制语音速度及其音调的滑块。然后我们有一个下拉菜单,其中包含所有受支持的语言以及其中提到的地区。

index.html这个 HTML 文件包含网页的布局。



  

    
  
    Text to speech!
  
    
    
      
    
    
      
    
      
    
    
      
    

  

    
        
            
                
                    
                        GeeksforGeeks Text-to-Speech Conversion                     
                                                                                 
            
        
                            
            
                
                                         
5
                                     
            
        
                            
            
                
                                         
5
                                     
            
        
                            
            
                
                                                                                                 
                                                                 
        
    
  

style.css这个文件用来给 HTML 文件添加一些 CSS 样式。

body {
    background: url('images/background.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
    background-attachment: fixed;
}
  
#front-text {
    font-size: 35px;
    color: white;
    font-weight: bolder;
    text-shadow: 1px 1px 1px black;
    display: block;
    position: relative;
    margin-bottom: 5%;
    margin-top: 15%;
}
  
#rate-value {
    float: right;
}
  
#pitch-value {
    float: right;
}
  
#foot {
    font-size: 20px;
    color: white;
    font-weight: bolder;
    display: block;
    position: relative;
    margin-top: 1%;
}

main.js JavaScript 文件用于将文本文件转换为语音。

// Initialising the speech API
const synth = window.speechSynthesis;
   
// Element initialization section
const form = document.querySelector('form');
const textarea = document.getElementById('maintext');
const voice_select = document.getElementById('voice-select');
const rate = document.getElementById('rate');
const pitch = document.getElementById('pitch');
const rateval = document.getElementById('rate-value');
const pitchval = document.getElementById('pitch-value');
   
// Retrieving the different voices and putting them as 
// options in our speech selection section
let voices = [];
const getVoice = () => {
      
    // This method retrieves voices and is asynchronously loaded
    voices = synth.getVoices();
    var option_string = "";
    voices.forEach(value => {
        var option = value.name + ' (' + value.lang + ') ';
        var newOption = "\n";
        option_string += newOption;
    });
      
    voice_select.innerHTML = option_string;
}
   
// Since synth.getVoices() is loaded asynchronously, this
// event gets fired when the return object of that 
// function has changed
synth.onvoiceschanged = function() {
    getVoice();
};
   
const speak = () => {
      
    // If the speech mode is on we dont want to load 
    // another speech
    if(synth.speaking) {
        alert('Already speaking....');
        return;
    }
       
    // If the text area is not empty that is if the input
    // is not empty
    if(textarea.value !== '') {
          
        // Creating an object of SpeechSynthesisUtterance with
        // the input value that represents a speech request
        const speakText = new SpeechSynthesisUtterance(textarea.value);
   
        // When the speaking is ended this method is fired
        speakText.onend = e => {
            console.log('Speaking is done!');
        };
          
        // When any error occurs this method is fired
        speakText.error = e=> {
            console.error('Error occured...');
        };
   
        // Selecting the voice for the speech from the selection DOM
        const id = voice_select.selectedIndex;
        const selectedVoice = 
            voice_select.selectedOptions[0].getAttribute('data-name');
    
        // Checking which voices has been chosen from the selection
        // and setting the voice to the chosen voice
        voices.forEach(voice => {
            if(voice.name === selectedVoice) {
                speakText.voice = voice;
            }
        });
   
        // Setting the rate and pitch of the voice
        speakText.rate = rate.value;
        speakText.pitch = pitch.value;
   
        // Finally calling the speech function that enables speech
        synth.speak(speakText);
    }
};
   
// This function updates the rate and pitch value to the
// value to display
rate.addEventListener('change', evt => rateval.innerHTML 
        = (Number.parseFloat(rate.value) * 10) + "");
  
pitch.addEventListener('change', evt => pitchval.innerHTML 
        = (Number.parseFloat(pitch.value) * 10) + "");
   
// This is the section when we assign the speak button, the
// speech function
form.addEventListener('submit', evt => {
    evt.preventDefault();
    speak();
    textarea.blur();
});

输出: