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

相关文章: