.NET 语音合成
语音合成技术是一种将文本转化为自然语音的技术。在.NET平台上进行语音合成,通常可以使用以下几种方法,System.Speech,Azure Cognitive Services(这个要使用需要Azure账号和API密钥),第三方库。
微软MS的文本转语音
1. 引用System.Speech
2. 通过SpeechSynthesizer类朗读文本
new SpeechSynthesizer().SpeakAsync("我们都是好孩子We're good kids.")
3. Speck vs SpeckAsync函数
- PlayAsync--异步播放,可以将需要朗读的文本进行排队。如果不需要,可以按如下取消当前的播放操作。
- Speak--同步播放,会卡UI线程。如果在朗读时,界面没有其它操作,则可以使用此函数
1 private SpeechSynthesizer speechSyn=new SpeechSynthesizer(); 2 /// <summary> 3 /// 异步播放 4 /// </summary> 5 private void PlayAsync() 6 { 7 var currentSpokenPrompt = speechSyn.GetCurrentlySpokenPrompt(); 8 if (currentSpokenPrompt != null) 9 { 10 speechSyn.SpeakAsyncCancel(currentSpokenPrompt); 11 } 12 speechSyn.SpeakAsync(richTextBox1.Text); 13 } 14 /// <summary> 15 /// 同步播放 16 /// 注:卡UI 17 /// </summary> 18 private void Play() 19 { 20 using (SpeechSynthesizer speechSyn = new SpeechSynthesizer()) 21 { 22 speechSyn.Speak(richTextBox1.Text); 23 } 24 }
4. 设置朗读角色
1 var speechSynthesizer = new SpeechSynthesizer(); 2 var voices= speechSynthesizer.GetInstalledVoices(CultureInfo.CurrentCulture).Select(x => x.VoiceInfo.Name).ToList(); 3 speechSynthesizer.SelectVoice(voices[0]); 4 speechSynthesizer.SpeakAsync("我们都是好孩子We're good kids.");
5. 其它
- Rate -- 语速设置,默认为0
- Volume -- 音量设置
6. 导出音频文件
可以将文本语音合成后,导出成一个wav、mp3等音频文件。
1 private void ExportAudioFile() 2 { 3 using (SpeechSynthesizer speechSyn = new SpeechSynthesizer()) 4 { 5 speechSyn.Volume = 50; 6 speechSyn.Rate = 0; 7 8 var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + $"\\{richTextBox1.Text}.mp3"; 9 if (File.Exists(filePath)) 10 { 11 File.Delete(filePath); 12 } 13 14 speechSyn.SetOutputToWaveFile(filePath); 15 speechSyn.Speak(richTextBox1.Text); 16 speechSyn.SetOutputToDefaultAudioDevice(); 17 18 MessageBox.Show($"保存录音文件成功,保存路径:{filePath}"); 19 } 20 }
Demo下载
第三方的语音合成接口
- 百度 http://ai.baidu.com/tech/speech/tts
- Demo效果还可以 BaiduTTSDemo
- 有道 http://ai.youdao.com/product-tts.s?keyfrom=Bsearch-yyhc1
- Demo YouDaoTTSDemo
- 科大讯飞 https://www.xfyun.cn/services/online_tts
- 科大讯飞的语音合成效果很垃圾 XunFeiTTSDemo
如果是英文朗读的话,有道的效果最好。可以下载Demo体验下
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验