C#调用科大讯飞离线语音合成TTS

上传人:m**** 文档编号:122141500 上传时间:2022-07-20 格式:DOC 页数:12 大小:125.50KB
收藏 版权申诉 举报 下载
C#调用科大讯飞离线语音合成TTS_第1页
第1页 / 共12页
C#调用科大讯飞离线语音合成TTS_第2页
第2页 / 共12页
C#调用科大讯飞离线语音合成TTS_第3页
第3页 / 共12页
资源描述:

《C#调用科大讯飞离线语音合成TTS》由会员分享,可在线阅读,更多相关《C#调用科大讯飞离线语音合成TTS(12页珍藏版)》请在装配图网上搜索。

1、c#调用科大讯飞的离线语音合成,需要在科大讯飞开放平台:注册、创建应用(获取APPID)、开通离线语音服务、下载 SDK。SDK 中目录如下:bind&ce-xamples.include国lib闊 e-xa m p I es_vs2010_VE.2-012. s I n扁 e-xa m p I e5_v&2010_v5.2-012.5 u oJ Re-adme.txt关于目录结构,讯飞平台的资料库中有介绍。bin目录下有msc.dll动态库。C#调用科大讯飞的语音合成,需要引用 该文件。该文件是C语言生成的dll,C#没办法直接引用。所有需要用 C#重新封装一个TTS.dll,然后再在C#项

2、目中引用封装的TTS.dll。步骤:1在VS中新建一个类库项目(TTS),新建一个类文件(TTS.cs)中的内容如下:Eusing System;using System.Collections.Generic;using System.Linq; using System.Text;using System.Runtime.InteropServices; namespace TTS public enum ErrorCode MSP_SUCCESS = 0, MSP_ERROR_FAIL = -1, MSP_ERROR_EXCEPTION = -2, /* General errors 1

3、0100(0x2774) */ MSP_ERROR_GENERAL = 10100, /* 0x2774 */ MSP_ERROR_OUT_OF_MEMORY = 10101, /* 0x2775 */ MSP_ERROR_FILE_NOT_FOUND = 10102, /* 0x2776 */ MSP_ERROR_NOT_SUPPORT = 10103, /* 0x2777 */ MSP_ERROR_NOT_IMPLEMENT = 10104, /* 0x2778 */ MSP_ERROR_ACCESS = 10105, /* 0x2779 */ MSP_ERROR_INVALID_PARA

4、 = 10106, /* 0x277A */ MSP_ERROR_INVALID_PARA_VALUE = 10107, /* 0x277B */ MSP_ERROR_INVALID_HANDLE = 10108, /* 0x277C */ MSP_ERROR_INVALID_DATA = 10109, /* 0x277D */ MSP_ERROR_NO_LICENSE = 10110, /* 0x277E */ MSP_ERROR_NOT_INIT = 10111, /* 0x277F */ MSP_ERROR_NULL_HANDLE = 10112, /* 0x2780 */ MSP_ER

5、ROR_OVERFLOW = 10113, /* 0x2781 */ MSP_ERROR_TIME_OUT = 10114, /* 0x2782 */ MSP_ERROR_OPEN_FILE = 10115, /* 0x2783 */ MSP_ERROR_NOT_FOUND = 10116, /* 0x2784 */ MSP_ERROR_NO_ENOUGH_BUFFER = 10117, /* 0x2785 */ MSP_ERROR_NO_DATA = 10118, /* 0x2786 */ MSP_ERROR_NO_MORE_DATA = 10119, /* 0x2787 */ MSP_ER

6、ROR_SKIPPED = 10120, /* 0x2788 */ MSP_ERROR_ALREADY_EXIST = 10121, /* 0x2789 */ MSP_ERROR_LOAD_MODULE = 10122, /* 0x278A */ MSP_ERROR_BUSY = 10123, /* 0x278B */MSP_ERROR_INVALID_CONFIG = 10124, /* 0x278C */ MSP_ERROR_VERSION_CHECK = 10125, /* 0x278D */ MSP_ERROR_CANCELED = 10126, /* 0x278E */ MSP_ER

7、ROR_INVALID_MEDIA_TYPE = 10127, /* 0x278F */ MSP_ERROR_CONFIG_INITIALIZE = 10128, /* 0x2790 */ MSP_ERROR_CREATE_HANDLE = 10129, /* 0x2791 */ MSP_ERROR_CODING_LIB_NOT_LOAD = 10130, /* 0x2792 */ /* Error codes of network 10200(0x27D8)*/ MSP_ERROR_NET_GENERAL = 10200, /* 0x27D8 */ MSP_ERROR_NET_OPENSOC

8、K = 10201, /* 0x27D9 */ /* Open socket */ MSP_ERROR_NET_CONNECTSOCK = 10202, /* 0x27DA */ /* Connect socket */ MSP_ERROR_NET_ACCEPTSOCK = 10203, /* 0x27DB */ /* Accept socket */ MSP_ERROR_NET_SENDSOCK = 10204, /* 0x27DC */ /* Send socket data */ MSP_ERROR_NET_RECVSOCK = 10205, /* 0x27DD */ /* Recv s

9、ocket data */ MSP_ERROR_NET_INVALIDSOCK = 10206, /* 0x27DE */ /* Invalid socket handle */MSP_ERROR_NET_BADADDRESS = 10207, /* 0x27EF */ /* Bad network address */MSP_ERROR_NET_BINDSEQUENCE = 10208, /* 0x27E0 */ /* Bind after listen/connect */MSP_ERROR_NET_NOTOPENSOCK = 10209, /* 0x27E1 */ /* Socket i

10、s not opened */MSP_ERROR_NET_NOTBIND = 10210, /* 0x27E2 */ /* Socket is not bind to an address */MSP_ERROR_NET_NOTLISTEN = 10211, /* 0x27E3 */ /* Socket is not listenning */MSP_ERROR_NET_CONNECTCLOSE = 10212, /* 0x27E4 */ /* The other side of connection is closed */MSP_ERROR_NET_NOTDGRAMSOCK = 10213

11、, /* 0x27E5 */ /* The socket is not datagram type */* Error codes of mssp message 10300(0x283C) */ MSP_ERROR_MSG_GENERAL = 10300, /* 0x283C */ MSP_ERROR_MSG_PARSE_ERROR = 10301, /* 0x283D */ MSP_ERROR_MSG_BUILD_ERROR = 10302, /* 0x283E */ MSP_ERROR_MSG_PARAM_ERROR = 10303, /* 0x283F */ MSP_ERROR_MSG

12、_CONTENT_EMPTY = 10304, /* 0x2840 */ MSP_ERROR_MSG_INVALID_CONTENT_TYPE = 10305, /* 0x2841 */ MSP_ERROR_MSG_INVALID_CONTENT_LENGTH = 10306, /* 0x2842 */ MSP_ERROR_MSG_INVALID_CONTENT_ENCODE = 10307, /* 0x2843 */ MSP_ERROR_MSG_INVALID_KEY = 10308, /* 0x2844 */ MSP_ERROR_MSG_KEY_EMPTY = 10309, /* 0x28

13、45 */ MSP_ERROR_MSG_SESSION_ID_EMPTY = 10310, /* 0x2846 */ MSP_ERROR_MSG_LOGIN_ID_EMPTY = 10311, /* 0x2847 */ MSP_ERROR_MSG_SYNC_ID_EMPTY = 10312, /* 0x2848 */MSP_ERROR_MSG_APP_ID_EMPTY = 10313, /* 0x2849 */ MSP_ERROR_MSG_EXTERN_ID_EMPTY = 10314, /* 0x284A */ MSP_ERROR_MSG_INVALID_CMD = 10315, /* 0x

14、284B */ MSP_ERROR_MSG_INVALID_SUBJECT = 10316, /* 0x284C */ MSP_ERROR_MSG_INVALID_VERSION = 10317, /* 0x284D */ MSP_ERROR_MSG_NO_CMD = 10318, /* 0x284E */ MSP_ERROR_MSG_NO_SUBJECT = 10319, /* 0x284F */ MSP_ERROR_MSG_NO_VERSION = 10320, /* 0x2850 */ MSP_ERROR_MSG_MSSP_EMPTY = 10321, /* 0x2851 */ MSP_

15、ERROR_MSG_NEW_RESPONSE = 10322, /* 0x2852 */ MSP_ERROR_MSG_NEW_CONTENT = 10323, /* 0x2853 */ MSP_ERROR_MSG_INVALID_SESSION_ID = 10324, /* 0x2854 */ /* Error codes of DataBase 10400(0x28A0)*/ MSP_ERROR_DB_GENERAL = 10400, /* 0x28A0 */ MSP_ERROR_DB_EXCEPTION = 10401, /* 0x28A1 */ MSP_ERROR_DB_NO_RESUL

16、T = 10402, /* 0x28A2 */ MSP_ERROR_DB_INVALID_USER = 10403, /* 0x28A3 */ MSP_ERROR_DB_INVALID_PWD = 10404, /* 0x28A4 */ MSP_ERROR_DB_CONNECT = 10405, /* 0x28A5 */ MSP_ERROR_DB_INVALID_SQL = 10406, /* 0x28A6 */ MSP_ERROR_DB_INVALID_APPID = 10407, /* 0x28A7 */ /* Error codes of Resource 10500(0x2904)*/

17、 MSP_ERROR_RES_GENERAL = 10500, /* 0x2904 */ MSP_ERROR_RES_LOAD = 10501, /* 0x2905 */ /* Load resource */ MSP_ERROR_RES_FREE = 10502, /* 0x2906 */ /* Free resource */ MSP_ERROR_RES_MISSING = 10503, /* 0x2907 */ /* Resource File Missing */MSP_ERROR_RES_INVALID_NAME = 10504, /* 0x2908 */ /* Invalid re

18、source file name */MSP_ERROR_RES_INVALID_ID = 10505, /* 0x2909 */ /* Invalid resource ID */MSP_ERROR_RES_INVALID_IMG = 10506, /* 0x290A */ /* Invalid resource image pointer */MSP_ERROR_RES_WRITE = 10507, /* 0x290B */ /* Write read-only resource */MSP_ERROR_RES_LEAK = MSP_ERROR_RES_HEAD = MSP_ERROR_R

19、ES_DATA = MSP_ERROR_RES_SKIP =10508, /* 0x290C */ /*10509, /* 0x290D */ /*10510, /* 0x290E */ /*10511, /* 0x290F */ /*Resource leak out */Resource head currupt */Resource data currupt */Resource file skipped */* Error codes of TTS 10600(0x2968)*/MSP_ERROR_TTS_GENERAL = 10600, /* 0x2968 */MSP_ERROR_T

20、TS_TEXTEND = 10601, /* 0x2969 */ /* Meet text end */MSP_ERROR_TTS_TEXT_EMPTY = 10602, /* 0x296A */ /* no synth text */ /* Error codes of Recognizer 10700(0x29CC) */MSP_ERROR_REC_GENERAL = 10700, /* 0x29CC */ MSP_ERROR_REC_INACTIVE = 10701, /* 0x29CD */ MSP_ERROR_REC_GRAMMAR_ERROR = 10702, /* 0x29CE

21、*/ MSP_ERROR_REC_NO_ACTIVE_GRAMMARS = 10703, /* 0x29CF */ MSP_ERROR_REC_DUPLICATE_GRAMMAR = 10704, /* 0x29D0 */ MSP_ERROR_REC_INVALID_MEDIA_TYPE = 10705, /* 0x29D1 */ MSP_ERROR_REC_INVALID_LANGUAGE = 10706, /* 0x29D2 */ MSP_ERROR_REC_URI_NOT_FOUND = 10707, /* 0x29D3 */ MSP_ERROR_REC_URI_TIMEOUT = 10

22、708, /* 0x29D4 */ MSP_ERROR_REC_URI_FETCH_ERROR = 10709, /* 0x29D5 */ /* Error codes of Speech Detector 10800(0x2A30) */ MSP_ERROR_EP_GENERAL = 10800, /* 0x2A30 */ MSP_ERROR_EP_NO_SESSION_NAME = 10801, /* 0x2A31 */ MSP_ERROR_EP_INACTIVE = 10802, /* 0x2A32 */ MSP_ERROR_EP_INITIALIZED = 10803, /* 0x2A

23、33 */ /* Error codes of TUV */MSP_ERROR_TUV_GENERAL = 10900, /* 0x2A94 */ MSP_ERROR_TUV_GETHIDPARAM = 10901, /* 0x2A95 */ /* Get Busin Param huanid*/MSP_ERROR_TUV_TOKEN = 10902, /* 0x2A96 */ /* Get Token */ MSP_ERROR_TUV_CFGFILE = 10903, /* 0x2A97 */ /* Open cfg file */ MSP_ERROR_TUV_RECV_CONTENT =

24、10904, /* 0x2A98 */* received content is error */MSP_ERROR_TUV_VERFAIL = 10905, /* 0x2A99 */ /* Verify failure */ /* Error codes of IMTV */MSP_ERROR_IMTV_SUCCESS = 11000, /* 0x2AF8 */* 成功 */ MSP_ERROR_IMTV_NO_LICENSE = 11001, /* 0x2AF9 */ /*试用次数结束,用户 需要付费 */MSP_ERROR_IMTV_SESSIONID_INVALID = 11002,

25、/* 0x2AFA */* SessionId 失效,需要重新登录通行证 */MSP_ERROR_IMTV_SESSIONID_ERROR = 11003, /* 0x2AFB */ /* Sessionld 为 空,或者非法 */MSP_ERROR_IMTV_UNLOGIN =11004, /* 0x2AFC */* 未登录通行证 */ MSP_ERROR_IMTV_SYSTEM_ERROR = 11005,/* 0x2AFD */ /*系统错误 */ /* Error codes of HCR */MSP_ERROR_HCR_GENERAL = 11100, MSP_ERROR_HCR_R

26、ESOURCE_NOT_EXIST =11101, /* Error codes of http 12000(0x2EE0) */ MSP_ERROR_HTTP_BASE = 12000,/* 0x2EE0 */ /*Error codes of ISV */MSP_ERROR_ISV_NO_USER = 13000,/* 32C8 */ /* the user doesnt exist */#region TTS枚举常量/ / vol参数的枚举常量/ pub l ic enum enuVolx_soft, soft, medium, loud, x_loud/ / speed语速参数的枚举常

27、量/ publicenum enuSpeedx_slow,slow,medium, fast, x_fast/ / speeker朗读者枚举常量/ publicenum enuSpeeker 小燕_青年女声_中英文_普通话 = 0, 小宇_青年男声_中英文_普通话, 凯瑟琳_青年女声_英语, 亨利_青年男声_英语, 玛丽_青年女声_英语, 小研_青年女声_中英文_普通话, 小琪_青年女声_中英文_普通话, 小峰_青年男声_中英文_普通话, 小梅_青年女声_中英文_粤语, 小莉_青年女声_中英文_台普, 小蓉_青年女声_汉语_四川话, 小芸_青年女声_汉语_东北话, 小坤_青年男声_汉语_河南话

28、, 小强_青年男声_汉语_湖南话, 小莹_青年女声_汉语_陕西话, 小新_童年男声_汉语_普通话, 楠楠_童年女声_汉语_普通话,老孙_老年男声_汉语_普通话public enum SynthStatusMSP_TTS_FLAG_STILL_HAVE_DATA =1, MSP_TTS_FLAG_DATA_END = 2,MSP_TTS_FLAG_CMD_CANCELED = 0#endregion public class TTSDll#region TTS dll importDllImport(msc.dll, CallingConvention = CallingConvention.W

29、inapi) public static extern int MSPLogin(string user, string password, string configs);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static extern int MSPLogout();DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static extern IntPtr QTTSSessionBegin(s

30、tring _params, ref int errorCode);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static extern int QTTSTextPut(string sessionID, string textString, uint textLen, string _params);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static extern IntPtr QTTS

31、AudioGet(string sessionID, ref uint audioLen, ref SynthStatus synthStatus, ref int errorCode);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static extern IntPtr QTTSAudioInfo(string sessionID);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi) public static ex

32、tern int QTTSSessionEnd(string sessionID, string hints);#endregionView Code3把该类库生成一个TTS.dll4在C#项目中引用该类库TTS.dll5另外需要把下载的SDK中的msc.dll放到语音合成项目中的Debug 目录下面(可以百度一下C#调用C/C+的DLL)6语音合成项目的代码如下:Eusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;us

33、ing System.Linq;using System.Text;using System.Windows.Forms;using TTS;using System.Runtime.InteropServices;using System.IO;using System.Media;using System.Threading;namespace OfflineSpeechpublic partial class Form1 : Formpublic Form1()InitializeComponent();int ret = 0;IntPtr session_ID;private void

34、 button1_Click(object sender, EventArgs e)try/APPID请勿随意改动string login_configs = appid =* ;/登录参数,自己注册后获取的 appidst ring text 二 richTex tBoxl.Tex t.Trim();/待合成的文本if (string.IsNullOrEmpty(richTextBox1.Text.Trim()text =请输入合成语音的内容;string filename = Call.wav; /合成的语音文件uint audio_len = 0;SynthStatus synth_st

35、atus = SynthStatus.MSP_TTS_FLAG_STILL_HAVE_DATA;ret 二 TTSDll.MSPLogin(string.Empty, string.Empty, login_configs);/第 一个参数为用户名,第二个参数为密码,第三个参数是登录参数,用户名和密 码需要在 /MSPLogin方法返回失败if (ret !=(int) ErrorCode.MSP_SUCCESS)return;/string parameter = engine_type = local, voice_name=xiaoyan, tts_res_path =fo|restts

36、xiaoyan.jet;fo|resttscommon.jet, sample_rate = 16000;string _params = ssm=1,ent=sms16k,vcn=xiaoyan,spd=medium,aue=speex- wb;7,vol=x-loud,auf=audio/L16;rate=16000;/string params = engine_type = local,voice_name=xiaoyan,speed=50,volume=50,pitch=50,rcn=1, text_encoding = UTF8, background_sound=1,sample

37、_rate = 16000; session_ID = TTSDll.QTTSSessionBegin(_params, ref ret);/QTTSSessionBegin方法返回失败if (ret != (int)ErrorCode.MSP_SUCCESS)return;ret = TTSDll.QTTSTextPut(Ptr2Str(session_ID), text,(uint)Encoding.Default.GetByteCount(text), string.Empty);/QTTSTextPut方法返回失败if (ret != (int)ErrorCode.MSP_SUCCES

38、S)return;MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(new byte44, 0, 44);while (true)IntPtr source = TTSDll.QTTSAudioGet(Ptr2Str(session_ID), ref audio_len, ref synth_status, ref ret);byte array = new byte(int)audio_len;if (audio_len 0)Marshal.Copy(source, array, 0, (int)audio_

39、len);memoryStream.Write(array, 0, array.Length);Thread.Sleep(1000);if (synth_status = SynthStatus.MSP_TTS_FLAG_DATA_END | ret != 0) break;WAVE_Header wave_Header = getWave_Header(int)memoryStream.Length - 44);byte array2 = this.StructToBytes(wave_Header); memoryStream.Position = 0L;memoryStream.Writ

40、e(array2, 0, array2.Length); memoryStream.Position = 0L;SoundPlayer soundPlayer = new SoundPlayer(memoryStream); soundPlayer.Stop();soundPlayer.Play();if (filename != null)FileStream fileStream = new FileStream(filename, FileMode.Create,FileAccess.Write); memoryStream.WriteTo(fileStream);memoryStrea

41、m.Close(); fileStream.Close();catch (Exception)finallyret = TTSDll.QTTSSessionEnd(Ptr2Str(session_ID), );ret = TTSDll.MSPLogou t();/退出登录/ / 结构体转字符串/ / / private byte StructToBytes(object structure)int num = Marshal.SizeOf(structure);IntPtr intPtr = Marshal.AllocHGlobal(num); byte result;tryMarshal.S

42、tructureToPtr(structure, intPtr, false);byte array = new bytenum; Marshal.Copy(intPtr, array, 0, num);result = array;finally Marshal.FreeHGlobal(intPtr);return result;/ / 结构体初始化赋值/ / / private WAVE_Header getWave_Header(int data_len) return new WAVE_HeaderRIFF_ID = 1179011410, File_Size = data_len +

43、 36,RIFF_Type = 1163280727,FMT_ID = 544501094,FMT_Size = 16,FMT_Tag = 1,FMT_Channel = 1, FMT_SamplesPerSec = 16000, AvgBytesPerSec = 32000, BlockAlign = 2,BitsPerSample = 16,DATA_ID = 1635017060, DATA_Size = data_len ;/ / 语音音频头/ private struct WAVE_Header public int RIFF_ID;public int File_Size;publ

44、ic int RIFF_Type;public int FMT_ID;public int FMT_Size;public short FMT_Tag;public ushort FMT_Channel;public int FMT_SamplesPerSec;public int AvgBytesPerSec;public ushort BlockAlign;public ushort BitsPerSample;public int DATA_ID;public int DATA_Size;/ 指针转字符串/ /param name二p指向非托管代码字符串的指针/ returns返回指针指

45、向的字符串 public static string Ptr2Str(IntPtr p)List lb = new List();while (Marshal.ReadByte(p) != 0)lb.Add(Marshal.ReadByte(p);p = p + 1;byte bs = lb.ToArray();return Encoding.Default.GetString(lb.ToArray();View Code 7.语音合成项目界面如下:推荐科大讯飞平台论坛帖子: . &highlight二c%23posted 2015-05-15 11:52 feitiana03120 阅读(.)评论(.)编辑收藏刷新评论刷新页面返回顶部博客园首页博问新闻闪存程序员招聘知识库公告Copyright 2015 feitiana03120

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!