本文主要讨论C#开发使用百度语音合成API在线将文本内容合成语音,保存为 MP3 文件,本文最后会提供本安全源代码以及运行软件包,如有需要可自行下载
开发流程如下:
1. 创建百度应用:
创建应用之后就能看到创建完的应用和 API KEY 以及 Secret KEY了,在调用API时需传入这些配置值
详见:https://console.bce.baidu.com/ai/#/ai/ocr/overview/index
2. 引用 Baidu.Aip (SDK)
Baidu.Aip 封闭了文字识别Ocr API 的Http请求,简化代码,提高开发效率。如果不想引用该DLL,可以自己封装Http请求方法。
C# SDK 现已开源,快速接入文档参考:https://ai.baidu.com/ai-doc/SPEECH/Jk4nlz6gp
3. 调用API,
这里仅展示通用及高精度识别示例,特殊场景识别,详见官方文档:https://ai.baidu.com/ai-doc/SPEECH/Jk4nlz6gp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /// <summary> /// 语音合成 /// </summary> /// <param name="reqItem">请求相关参数</param> /// <param name="destPath">mp3文件保存路径</param> /// <param name="synthesisEvent">状态事件</param> /// <returns></returns> public static bool Synthesis(ReqSynthesis reqItem, string destPath, SynthesisEvent synthesisEvent) { bool callResult = false ; try { synthesisEvent( "语音合成初始化..." ); // 百度应用配置 var apiKey = ConfigUtil.GetConfigStr(EnumConfigKey.SpeechAPI_Key); var secretKey = ConfigUtil.GetConfigStr(EnumConfigKey.SpeechSECRET_KEY); if (!RiverAuth.Instant.CheckSynthesis(apiKey, secretKey)) { return false ; } var client = new Baidu.Aip.Speech.Tts(apiKey, secretKey); client.Timeout = 60000; // 修改超时时间 var option = new Dictionary< string , object >() { { "vol" , reqItem.vol}, // { "pit" , reqItem.pit}, // { "spd" , reqItem.spd}, // 语速 { "per" , reqItem.per} // 发音人,4:情感度丫丫童声 }; synthesisEvent( "开始语音合成..." ); var result = client.Synthesis(reqItem.tex, option); // 保存文件 if (result.Success) // 或 result.Success { File.WriteAllBytes(destPath, result.Data); synthesisEvent( "语音合成返回成功,文件:" + destPath); callResult = true ; } else { synthesisEvent( string .Format( "语音合成失败, 错误码:[{0}], 原因:{1}" , result.ErrorCode, result.ErrorMsg)); } RiverAuth.Instant.EndCallEvent(); } catch (Exception ex) { synthesisEvent( "语音合成失败,原因:" + ex.Message); LogHelper.LogError(ex, "语音合成" ); } return callResult; } } /// <summary> /// 语音合成请求 /// </summary> [Serializable] public class ReqSynthesis { public string tex { get ; set ; } //合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节 必填项 public string cuid { get ; set ; } // 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内 否 public int spd { get ; set ; } //语速,取值0-9,默认为5中语速 否 public int pit { get ; set ; } //音调,取值0-9,默认为5中语调 否 public int vol { get ; set ; } //音量,取值0-15,默认为5中音量 否 public int per { get ; set ; } //发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 } /// <summary> /// 语音转文字返回结果 /// </summary> public class RetSpeech { public string corpus_no { get ; set ; } public string err_msg { get ; set ; } public int err_no { get ; set ; } public string [] result { get ; set ; } public string sn { get ; set ; } } |
4. 语音合成最后保存为 MP3 文件
5. 源代码及软件包下载
软件开源代码地址(项目集成文字识别、语音合成、语音识别):https://gitee.com/bidev_admin/river-int-ai.git
运行软件包下载地址(.Net Framework 4.5): https://pan.baidu.com/s/1z-WFDGHhEP8tqLqwp9I0Mw 提取码: r3sk
作者:沐汐 Vicky
出处:http://www.cnblogs.com/EasyInvoice
以上就是c# 实现语音合成的详细内容,更多关于c# 语音合成的资料请关注自学编程网其它相关文章!
- 本文固定链接: https://zxbcw.cn/post/202084/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)