2017-06-13 38 views
1

你好傢伙我想要在文本框中輸入的文本轉換爲瀏覽器中的語音。我試過兩種方式,我在下面提到,但它有一些問題。我需要一個更好的文本到語音的解決方案,也可以有任何想法以更好的方式實現它。文本到語音轉換爲web應用程序

方法1 ---

我已經試過它的FreeTTS在java中提供免費的文本轉換成.wave音頻文件。

使用下面的代碼,我創建了一個Web服務,只要用戶點擊一個web服務與將轉化爲.wave音頻文件,然後使用HTML5 <audio>標籤播放音頻文件在客戶端瀏覽器文本。

public static void main(String[] args) 
{ 
     FreeTTS freetts; 
     AudioPlayer audioPlayer = null; 
     System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory"); 
     String voiceName = "kevin16"; 

     System.out.println(); 
     System.out.println("Using voice: " + voiceName); 

     //The VoiceManager manages all the voices for FreeTTS. 

     VoiceManager voiceManager = VoiceManager.getInstance(); 
     Voice helloVoice = voiceManager.getVoice(voiceName); 

     if (helloVoice == null) 
     { 
     System.err.println(
      "Cannot find a voice named " 
      + voiceName + ". Please specify a different voice."); 
     System.exit(1); 
     } 

     helloVoice.allocate();//Allocates the resources for the voice. 
     audioPlayer = new SingleFileAudioPlayer("/home/rohit/voicefiles/file4",Type.WAVE); //create a audioplayer to dump the output file 
     helloVoice.setAudioPlayer(audioPlayer);//attach the audioplayer 
     helloVoice.speak("Hi this it rohit");//includes text to be converted in mp3 
     helloVoice.deallocate();//Clean up and leave. 
     audioPlayer.close();//don't forget to close the audioplayer otherwise file will not be saved 

     System.exit(0); 
} 

問題與上面的代碼是

  1. 如果有很多命中Web服務的明顯它創建 很多.wave音頻文件可以在一些 時間導致存儲問題。我已經通過執行標識爲否的批處理來解決此問題。的音頻文件已排除特定的編號。那麼我正在刪除舊文件,但這種解決方案並不是很好
  2. 此過程耗時,在用長文本訪問Web服務後,需要一段時間才能在客戶端瀏覽器播放語音。
  3. 在freeTTS中,我沒有找到任何女性聲音,我用男性聲音作爲kevin16,這聽起來不像用戶友好,聽起來像機器。

方法2 ---

的文本到語音轉換的另一種方法是使用外部API的的。我已經使用谷歌API,只與前端工作,這減少了服務器負載。

<html> 
<head> 
<script type="text/javascript"> 
    function textToVoice() 
    { 
     var msg = new SpeechSynthesisUtterance(); 
     var voices = window.speechSynthesis.getVoices(); 
     var text = document.getElementById("textToRead").value; 
     var lan = 'en-EN' 

     msg.voice = voices[10]; // Note: some voices don't support altering params 
     msg.voiceURI = 'native'; 
     msg.volume = 1; // 0 to 1 
     msg.rate = 1; // 0.1 to 10 
     msg.pitch = 1; //0 to 2 
     msg.text = text; 
     //msg.lang = 'hi-IN'; 
     msg.lang = lan; 
     msg.onend = function(e) { 
     console.log('Finished in ' + event.elapsedTime + ' seconds.'); 
     location.reload(); 
     }; 

     speechSynthesis.speak(msg); 
    } 
</script> 

</head> 
<body> 
    <center> 
    <textarea id="textToRead" rows="25" cols="100"></textarea> 
    <br> 
    <input type="button" value="Listen" onclick="textToVoice()" /> 
    </center> 
</body> 
</html> 

它產生良好的聲音與不同國家的具體口音,仍然有像

  1. 問題,它不是免費的
  2. 它需要有效的互聯網連接
  3. 它是特定的瀏覽器,它不在Internet Explorer中工作
+0

爲了便於使用,我建議使用https://responsivevoice.org/ - 但它再次不是免費的(令人驚訝的是有多少公司想賺錢,對吧?)。 –

+0

我已經檢查過responsivevoice.org它不是免費的,但它需要互聯網連接,這是我的項目不允許的 –

+0

雖然它在IE中不起作用,但W3C Web Speech API是免費的,並且可以離線工作。 https://dvcs.w3.org/hg/speech-api/raw-file/tip/webspeechapi.html#tts-section – Frazer

回答

0

嗨請通過此URL。我有這個解決方案。

+0

Stuart Ervine在評論中建議的同樣的事情reponsivevoice.js –

+0

哦。我沒有看到評論部分。但現在我明白了,但沒有互聯網連接,不可能將文本轉換爲語音。請在同一個博客中通過polly服務。 –