微软的SAPI(Speech Application Programming Interface)提供了强大的Text To Speech接口,而且支持自动化。这样可以在.Net的程序中引入该COM组件:
先添加SpeechLib的引用,在系统盘Program Files\Common Files\Microsoft Shared\Speech目录中找到sapi.dll。
在代码中调用SpVoiceClass类的Speak方法可以实现简单的句子发言:
ISpeechVoice v=new SpVoiceClass();
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
还可以用SpFileStream创建音频流,如下面程序可以把文本“读到”wav文件中:
//Declare the SpVoice object. 
ISpeechVoice v=new SpVoiceClass();
//Create a wave stream
SpFileStream s = new SpFileStream();
//Create a new .wav file for writing.
s.Open("out.wav", SpeechStreamFileMode.SSFMCreateForWrite, false);
//Set the .wav file stream as the output for the Voice object
v.AudioOutputStream = s;
//Call the Speak method now will send the output to the .wav file.
v.Speak(txtSpeak, SpeechVoiceSpeakFlags.SVSFDefault);
//Close the file
s.Close();
这只是很浅显的一些应用。Windows(我的是英文版win2003)自带的SAPI不支持中文,MS称5.1版本的SAPI可以支持中日英三国语言,可以到MS网站上免费下载。继续研究中……
posted on 2005-07-27 23:34  小生杂谈  阅读(3164)  评论(1编辑  收藏  举报