版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、窗體頂端窗體底端C#調用科大訊飛離線語音合成TTSc#調用科大訊飛的離線語音合成,需要在科大訊飛開放平臺: 注冊、創(chuàng)建應用(獲取APPID)、開通離線語音服務、下載SDK。SDK中目錄如下:關于目錄結構,訊飛平臺的資料庫中有介紹。bin目錄下有msc.dll動態(tài)庫。c#調用科大訊飛的語音合成,需要引用該文件。該文件是C語言生成的dll,C#沒辦法直接引用。所有需要用C#重新封裝一個TTS.dll,然后再在C#項目中引用封裝的TTS.dll。步驟:1.在VS中新建一個類庫項目(TTS),新建一個類文件(TTS.cs)2.TTS.cs中的內容如下:using System;using System
2、.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace TTSpublic enum ErrorCodeMSP_SUCCESS = 0,MSP_ERROR_FAIL = -1,MSP_ERROR_EXCEPTION = -2,/* General errors 10100(0x2774) */MSP_ERROR_GENERAL = 10100, /* 0x2774 */MSP_ERROR_OUT_OF_MEMORY = 10101, /* 0x
3、2775 */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 = 10106, /* 0x277A */MSP_ERROR_INVALID_PARA_VALUE = 10107, /* 0x277B */MSP_ERROR_INVALID_HANDLE =
4、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_ERROR_OVERFLOW = 10113, /* 0x2781 */MSP_ERROR_TIME_OUT = 10114, /* 0x2782 */MSP_ERROR_OPEN_FILE = 10115, /*
5、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_ERROR_SKIPPED = 10120, /* 0x2788 */MSP_ERROR_ALREADY_EXIST = 10121, /* 0x2789 */MSP_ERROR_LOAD_MODULE = 10122, /* 0
6、x278A */MSP_ERROR_BUSY = 10123, /* 0x278B */MSP_ERROR_INVALID_CONFIG = 10124, /* 0x278C */MSP_ERROR_VERSION_CHECK = 10125, /* 0x278D */MSP_ERROR_CANCELED = 10126, /* 0x278E */MSP_ERROR_INVALID_MEDIA_TYPE = 10127, /* 0x278F */MSP_ERROR_CONFIG_INITIALIZE = 10128, /* 0x2790 */MSP_ERROR_CREATE_HANDLE =
7、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_OPENSOCK = 10201, /* 0x27D9 */ /* Open socket */MSP_ERROR_NET_CONNECTSOCK = 10202, /* 0x27DA */ /* Connect socket */MSP_ERROR_NET_ACC
8、EPTSOCK = 10203, /* 0x27DB */ /* Accept socket */MSP_ERROR_NET_SENDSOCK = 10204, /* 0x27DC */ /* Send socket data */MSP_ERROR_NET_RECVSOCK = 10205, /* 0x27DD */ /* Recv socket data */MSP_ERROR_NET_INVALIDSOCK = 10206, /* 0x27DE */ /* Invalid socket handle */MSP_ERROR_NET_BADADDRESS = 10207, /* 0x27E
9、F */ /* Bad network address */MSP_ERROR_NET_BINDSEQUENCE = 10208, /* 0x27E0 */ /* Bind after listen/connect */MSP_ERROR_NET_NOTOPENSOCK = 10209, /* 0x27E1 */ /* Socket is not opened */MSP_ERROR_NET_NOTBIND = 10210, /* 0x27E2 */ /* Socket is not bind to an address */MSP_ERROR_NET_NOTLISTEN = 10211, /
10、* 0x27E3 */ /* Socket is not listenning */MSP_ERROR_NET_CONNECTCLOSE = 10212, /* 0x27E4 */ /* The other side of connection is closed */MSP_ERROR_NET_NOTDGRAMSOCK = 10213, /* 0x27E5 */ /* The socket is not datagram type */* Error codes of mssp message 10300(0x283C) */MSP_ERROR_MSG_GENERAL = 10300, /*
11、 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_CONTENT_EMPTY = 10304, /* 0x2840 */MSP_ERROR_MSG_INVALID_CONTENT_TYPE = 10305, /* 0x2841 */MSP_ERROR_MSG_INVALID_CONTENT_LENGTH = 10306
12、, /* 0x2842 */MSP_ERROR_MSG_INVALID_CONTENT_ENCODE = 10307, /* 0x2843 */MSP_ERROR_MSG_INVALID_KEY = 10308, /* 0x2844 */MSP_ERROR_MSG_KEY_EMPTY = 10309, /* 0x2845 */MSP_ERROR_MSG_SESSION_ID_EMPTY = 10310, /* 0x2846 */MSP_ERROR_MSG_LOGIN_ID_EMPTY = 10311, /* 0x2847 */MSP_ERROR_MSG_SYNC_ID_EMPTY = 1031
13、2, /* 0x2848 */MSP_ERROR_MSG_APP_ID_EMPTY = 10313, /* 0x2849 */MSP_ERROR_MSG_EXTERN_ID_EMPTY = 10314, /* 0x284A */MSP_ERROR_MSG_INVALID_CMD = 10315, /* 0x284B */MSP_ERROR_MSG_INVALID_SUBJECT = 10316, /* 0x284C */MSP_ERROR_MSG_INVALID_VERSION = 10317, /* 0x284D */MSP_ERROR_MSG_NO_CMD = 10318, /* 0x28
14、4E */MSP_ERROR_MSG_NO_SUBJECT = 10319, /* 0x284F */MSP_ERROR_MSG_NO_VERSION = 10320, /* 0x2850 */MSP_ERROR_MSG_MSSP_EMPTY = 10321, /* 0x2851 */MSP_ERROR_MSG_NEW_RESPONSE = 10322, /* 0x2852 */MSP_ERROR_MSG_NEW_CONTENT = 10323, /* 0x2853 */MSP_ERROR_MSG_INVALID_SESSION_ID = 10324, /* 0x2854 */* Error
15、codes of DataBase 10400(0x28A0)*/MSP_ERROR_DB_GENERAL = 10400, /* 0x28A0 */MSP_ERROR_DB_EXCEPTION = 10401, /* 0x28A1 */MSP_ERROR_DB_NO_RESULT = 10402, /* 0x28A2 */MSP_ERROR_DB_INVALID_USER = 10403, /* 0x28A3 */MSP_ERROR_DB_INVALID_PWD = 10404, /* 0x28A4 */MSP_ERROR_DB_CONNECT = 10405, /* 0x28A5 */MS
16、P_ERROR_DB_INVALID_SQL = 10406, /* 0x28A6 */MSP_ERROR_DB_INVALID_APPID = 10407, /* 0x28A7 */* Error codes of Resource 10500(0x2904)*/MSP_ERROR_RES_GENERAL = 10500, /* 0x2904 */MSP_ERROR_RES_LOAD = 10501, /* 0x2905 */ /* Load resource */MSP_ERROR_RES_FREE = 10502, /* 0x2906 */ /* Free resource */MSP_
17、ERROR_RES_MISSING = 10503, /* 0x2907 */ /* Resource File Missing */MSP_ERROR_RES_INVALID_NAME = 10504, /* 0x2908 */ /* Invalid resource file name */MSP_ERROR_RES_INVALID_ID = 10505, /* 0x2909 */ /* Invalid resource ID */MSP_ERROR_RES_INVALID_IMG = 10506, /* 0x290A */ /* Invalid resource image pointe
18、r */MSP_ERROR_RES_WRITE = 10507, /* 0x290B */ /* Write read-only resource */MSP_ERROR_RES_LEAK = 10508, /* 0x290C */ /* Resource leak out */MSP_ERROR_RES_HEAD = 10509, /* 0x290D */ /* Resource head currupt */MSP_ERROR_RES_DATA = 10510, /* 0x290E */ /* Resource data currupt */MSP_ERROR_RES_SKIP = 105
19、11, /* 0x290F */ /* Resource file skipped */* Error codes of TTS 10600(0x2968)*/MSP_ERROR_TTS_GENERAL = 10600, /* 0x2968 */MSP_ERROR_TTS_TEXTEND = 10601, /* 0x2969 */ /* Meet text end */MSP_ERROR_TTS_TEXT_EMPTY = 10602, /* 0x296A */ /* no synth text */* Error codes of Recognizer 10700(0x29CC) */MSP_
20、ERROR_REC_GENERAL = 10700, /* 0x29CC */MSP_ERROR_REC_INACTIVE = 10701, /* 0x29CD */MSP_ERROR_REC_GRAMMAR_ERROR = 10702, /* 0x29CE */MSP_ERROR_REC_NO_ACTIVE_GRAMMARS = 10703, /* 0x29CF */MSP_ERROR_REC_DUPLICATE_GRAMMAR = 10704, /* 0x29D0 */MSP_ERROR_REC_INVALID_MEDIA_TYPE = 10705, /* 0x29D1 */MSP_ERR
21、OR_REC_INVALID_LANGUAGE = 10706, /* 0x29D2 */MSP_ERROR_REC_URI_NOT_FOUND = 10707, /* 0x29D3 */MSP_ERROR_REC_URI_TIMEOUT = 10708, /* 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_S
22、ESSION_NAME = 10801, /* 0x2A31 */MSP_ERROR_EP_INACTIVE = 10802, /* 0x2A32 */MSP_ERROR_EP_INITIALIZED = 10803, /* 0x2A33 */* 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, /* 0x2A9
23、6 */ /* Get Token */MSP_ERROR_TUV_CFGFILE = 10903, /* 0x2A97 */ /* Open cfg file */MSP_ERROR_TUV_RECV_CONTENT = 10904, /* 0x2A98 */ /* received content is error */MSP_ERROR_TUV_VERFAIL = 10905, /* 0x2A99 */ /* Verify failure */* Error codes of IMTV */MSP_ERROR_IMTV_SUCCESS = 11000, /* 0x2AF8 */ /* 成
24、功 */MSP_ERROR_IMTV_NO_LICENSE = 11001, /* 0x2AF9 */ /* 試用次數結束,用戶需要付費 */MSP_ERROR_IMTV_SESSIONID_INVALID = 11002, /* 0x2AFA */ /* SessionId失效,需要重新登錄通行證 */MSP_ERROR_IMTV_SESSIONID_ERROR = 11003, /* 0x2AFB */ /* SessionId為空,或者非法 */MSP_ERROR_IMTV_UNLOGIN = 11004, /* 0x2AFC */ /* 未登錄通行證 */MSP_ERROR_IMTV_
25、SYSTEM_ERROR = 11005, /* 0x2AFD */ /* 系統(tǒng)錯誤 */* Error codes of HCR */MSP_ERROR_HCR_GENERAL = 11100,MSP_ERROR_HCR_RESOURCE_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 doesn
26、t exist */#region TTS枚舉常量/ / vol參數的枚舉常量/ public enum enuVolx_soft,soft,medium,loud,x_loud/ / speed語速參數的枚舉常量/ public enum enuSpeedx_slow,slow,medium,fast,x_fast/ / speeker朗讀者枚舉常量/ public enum enuSpeeker小燕_青年女聲_中英文_普通話 = 0,小宇_青年男聲_中英文_普通話,凱瑟琳_青年女聲_英語,亨利_青年男聲_英語,瑪麗_青年女聲_英語,小研_青年女聲_中英文_普通話,小琪_青年女聲_中英文_普
27、通話,小峰_青年男聲_中英文_普通話,小梅_青年女聲_中英文_粵語,小莉_青年女聲_中英文_臺普,小蓉_青年女聲_漢語_四川話,小蕓_青年女聲_漢語_東北話,小坤_青年男聲_漢語_河南話,小強_青年男聲_漢語_湖南話,小瑩_青年女聲_漢語_陜西話,小新_童年男聲_漢語_普通話,楠楠_童年女聲_漢語_普通話,老孫_老年男聲_漢語_普通話public enum SynthStatusMSP_TTS_FLAG_STILL_HAVE_DATA = 1,MSP_TTS_FLAG_DATA_END = 2,MSP_TTS_FLAG_CMD_CANCELED = 0#endregionpublic clas
28、s TTSDll#region TTS dll importDllImport(msc.dll, CallingConvention = CallingConvention.Winapi)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, Callin
29、gConvention = CallingConvention.Winapi)public static extern IntPtr QTTSSessionBegin(string _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
30、.dll, CallingConvention = CallingConvention.Winapi)public static extern IntPtr QTTSAudioGet(string sessionID, ref uint audioLen, ref SynthStatus synthStatus, ref int errorCode);DllImport(msc.dll, CallingConvention = CallingConvention.Winapi)public static extern IntPtr QTTSAudioInfo(string sessionID)
31、;DllImport(msc.dll, CallingConvention = CallingConvention.Winapi)public static extern int QTTSSessionEnd(string sessionID, string hints);#endregionView Code3.把該類庫生成一個TTS.dll4.在C#項目中引用該類庫TTS.dll5.另外需要把下載的SDK中的msc.dll放到語音合成項目中的Debug目錄下面(可以百度一下C#調用C/C+的DLL)6.語音合成項目的代碼如下:using System;using System.Collec
32、tions.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using 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 :
33、 Formpublic Form1()InitializeComponent();int ret = 0;IntPtr session_ID;private void button1_Click(object sender, EventArgs e)try/APPID請勿隨意改動string login_configs = appid =* ;/登錄參數,自己注冊后獲取的appidstring text = richTextBox1.Text.Trim();/待合成的文本if (string.IsNullOrEmpty(richTextBox1.Text.Trim()text = 請輸入合成語
34、音的內容;string filename = Call.wav; /合成的語音文件uint audio_len = 0;SynthStatus synth_status = 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
35、 parameter = engine_type = local, voice_name=xiaoyan, tts_res_path =fo|resttsxiaoyan.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=
36、50,volume=50,pitch=50,rcn=1, text_encoding = UTF8, background_sound=1,sample_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
37、(text), string.Empty);/QTTSTextPut方法返回失敗if (ret != (int)ErrorCode.MSP_SUCCESS)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 by
38、te(int)audio_len;if (audio_len 0)Marshal.Copy(source, array, 0, (int)audio_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 = thi
39、s.StructToBytes(wave_Header);memoryStream.Position = 0L;memoryStream.Write(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);memoryStream.Close();fileStream.Close();catch (Exception)finallyret = TTSDll.QTTSSessionEnd(Ptr2Str(session_ID), );ret = TTSDll.MSPLogout();/退出登錄 / / 結構體轉字符串/ / / private byte StructToBytes(object structure)int num = Ma
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 設備租賃合同:考古挖掘
- 財務管理工具與技能培訓管理辦法
- 2024年重組家庭共有財產處理離婚協(xié)議3篇
- 自動離職員工交接指南
- 橋梁弱電工程承包合同樣本
- 生產能力評估與改進
- 2025年度企業(yè)人才引進合同主體變更三方協(xié)議3篇
- 游戲設備租賃合同自行操作手冊
- 長期倉儲租賃合同樣本
- 合同負債在施工企業(yè)中的應對策略
- 2024年度寵物用品銷售代理合同范本3篇
- 湖南2025年湖南生物機電職業(yè)技術學院招聘35人歷年參考題庫(頻考版)含答案解析
- 部隊物業(yè)服務投標方案
- 銷售單 代合同范例
- 2024年3月天津第一次高考英語試卷真題答案解析(精校打?。?/a>
- 2024譯林版七年級英語上冊單詞(帶音標)
- 品管圈PDCA案例-普外科提高甲狀腺手術患者功能鍛煉合格率
- 2024-2025學年語文二年級上冊 部編版期末測試卷(含答案)
- 期末模擬卷 2024-2025學年人教版數學六年級上冊(含答案)
- 2025年消防救援設施操作員職業(yè)技能資格知識考試題庫與答案
- 電玩城租賃經營合同
評論
0/150
提交評論