如何使用Microsoft SAPI将文本到语音保存为wav?

我需要将文本转换为语音,然后将其保存为wav文件。

以下C#代码使用.Net框架中的System.Speech命名空间。 在使用它之前必须引用命名空间,因为它不会被Visual Studio自动引用。

SpeechSynthesizer ss = new SpeechSynthesizer(); ss.Volume = 100; ss.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult); ss.SetOutputToWaveFile(@"C:\MyAudioFile.wav"); ss.Speak("Hello World"); 

我希望这是相关和有帮助的。

正如我已经找到了如何更改输出格式,我们编写如下代码:

 SpeechAudioFormatInfo info = new SpeechAudioFormatInfo(6, AudioBitsPerSample.Sixteen, AudioChannel.Mono); //Same code comes here ss.SetOutputToWaveFile(@"C:\MyAudioFile.wav",info); 

这很容易理解。

酷.net

这是一段时间的游戏,所以需要注意。 为我工作得很好。 我注意到SpFileStream(它没有实现IDisposable,因此try / finally)更喜欢相对的绝对路径。 C#。

  SpFileStream fs = null; try { SpVoice voice = new SpVoice(); fs = new SpFileStream(); fs.Open(@"c:\hello.wav", SpeechStreamFileMode.SSFMCreateForWrite, false); voice.AudioOutputStream = fs; voice.Speak("Hello world.", SpeechVoiceSpeakFlags.SVSFDefault); } finally { if (fs != null) { fs.Close(); } }