由于最近想要做一个简单的语音合成模块,初步决定使用STM8作为主控制器,外扩flash用于存放语音库,PWM输出语音。语音库的来源问题成了一大问题。
这里就是为了解决这个问题的,使用speech sdk生成二级汉字的语音文件(WAV),将其重新编排,烧入FLASH。
下面简单记录下SPEECH SDK的使用方式。
//这是 SPEECH SDK的命名空间 using SpeechLib; //SPEECH SDK属于COM组件 using System.ComponentModel; //对于语音的输出部分可控参数 //语音速率 -10 --- 10 有效 Voice.Rate = 0; //语音输出格式(貌似不可调整,其他模式语音不正常) Voice.AudioOutputStream.Format.Type = SpeechLib.SpeechAudioFormatType.SAFT16kHz16BitMono;
下面是文字转语音的关键代码:
private void btn1_Click(object sender, EventArgs e) { SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); Voice.Rate = 0; Voice.AudioOutputStream.Format.Type = SpeechLib.SpeechAudioFormatType.SAFT16kHz16BitMono; Voice.Voice = Voice.GetVoices().Item(0) ; private void btn1_Click(object sender, EventArgs e) { SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); Voice.Rate = 0; Voice.AudioOutputStream.Format.Type = SpeechLib.SpeechAudioFormatType.SAFT16kHz16BitMono; Voice.Voice = Voice.GetVoices().Item(0) ; Voice.Speak(this.textBoxText.Text, SpFlags); } //此处获取需要转换为语音的文本 Voice.Speak(this.textBoxText.Text, SpFlags); }
下面贴出将转换的语音直接输出为wav的关键代码:
private void bt4_Click(object sender, EventArgs e) { int Timeout = 1000; SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "wav files (*.wav)|*.wav"; sfd.Title = "Save to a wave file"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(sfd.FileName, SpFileMode, false); Voice.AudioOutputStream.Format.Type = SpeechLib.SpeechAudioFormatType.SAFT16kHz16BitMono; Voice.AudioOutputStream = SpFileStream; Voice.Speak(textBoxText.Text, SpFlags); Voice.WaitUntilDone(Timeout); SpFileStream.Close(); MessageBox.Show("生成成功"); } }
转载于:https://blog.51cto.com/richards/1336134