【发布时间】:2017-06-14 03:46:17
【问题描述】:
大家好,我想将在文本字段中输入的文本转换为浏览器中的语音。我尝试了两种我在下面提到的方法,但它有一些问题。我需要一个更好的文本到语音解决方案,并且任何人都可能有任何想法以更好的方式实现它。
方法一---
我尝试过 freeTTS,它可以在 java 中免费使用,并将文本转换为 .wave 音频文件。
使用下面的代码,我创建了一个网络服务,每当用户使用要转换为 .wave 音频文件的文本点击网络服务时,然后使用 HTML5 @987654324 在客户端浏览器中播放该音频文件@标签。
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);
}
上面代码的问题是
- 如果 Web 服务有很多点击,显然它会创建 大量 .wave 音频文件,在某些情况下可能会导致存储问题 时间。我已经通过执行标识号的批处理解决了这个问题。的音频文件已排除特定编号。然后我正在删除旧文件,但是这个解决方案不是很好
- 此外,此过程非常耗时,在使用长文本访问 Web 服务后,需要一些时间才能在客户端浏览器上播放语音。
- 在 freeTTS 中我没有找到任何女性声音,我使用男性声音作为 kevin16,这听起来不友好,听起来像机器。
方法二---
Text to Speech 的另一种方法是使用外部 api。我使用了 google 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>
它可以产生具有不同国家特定口音的好声音,但仍然存在诸如
之类的问题- 它不是免费的
- 需要有效的互联网连接
- 它是特定于浏览器的,它在 Internet Explorer 中不起作用
【问题讨论】:
-
为了便于使用,我推荐responsivevoice.org - 但它又不是免费的(令人惊讶的是有多少公司想赚钱,对吧?)。
-
我已经检查了 responsivevoice.org 它不是免费的,但它需要互联网连接,这在我的项目中是不允许的
-
虽然它不能在 IE 中工作,但 W3C Web Speech API 是免费的并且可以离线工作。 dvcs.w3.org/hg/speech-api/raw-file/tip/…
标签: javascript java html text-to-speech