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

2020-04-16 12:58

x_soft, soft, medium, loud, x_loud }

///

/// speed语速参数的枚举常量 ///

publicenum enuSpeed {

x_slow, slow, medium, fast, x_fast }

///

/// speeker朗读者枚举常量 ///

publicenum enuSpeeker {

小燕_青年女声_中英文_普通话 = 0, 小宇_青年男声_中英文_普通话, 凯瑟琳_青年女声_英语, 亨利_青年男声_英语, 玛丽_青年女声_英语,

小研_青年女声_中英文_普通话, 小琪_青年女声_中英文_普通话, 小峰_青年男声_中英文_普通话, 小梅_青年女声_中英文_粤语, 小莉_青年女声_中英文_台普, 小蓉_青年女声_汉语_四川话, 小芸_青年女声_汉语_东北话, 小坤_青年男声_汉语_河南话, 小强_青年男声_汉语_湖南话, 小莹_青年女声_汉语_陕西话, 小新_童年男声_汉语_普通话, 楠楠_童年女声_汉语_普通话, 老孙_老年男声_汉语_普通话 }

publicenum SynthStatus {

MSP_TTS_FLAG_STILL_HAVE_DATA = 1,

MSP_TTS_FLAG_DATA_END = 2, MSP_TTS_FLAG_CMD_CANCELED = 0 }

#endregion

publicclass TTSDll {

#region TTS dll import

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticexternint MSPLogin(string user, string password, string configs);

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticexternint MSPLogout();

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticextern IntPtr QTTSSessionBegin(string _params, refint errorCode);

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticexternint QTTSTextPut(string sessionID, string textString, uint textLen, string _params);

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticextern IntPtr QTTSAudioGet(string sessionID, refuint audioLen, ref SynthStatus synthStatus, refint errorCode);

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticextern IntPtr QTTSAudioInfo(string sessionID);

[DllImport(\, CallingConvention = CallingConvention.Winapi)] publicstaticexternint QTTSSessionEnd(string sessionID, string hints); #endregion } }

View Code

3.把该类库生成一个TTS.dll 4.在C#项目中引用该类库TTS.dll

5.另外需要把下载的SDK中的msc.dll放到语音合成项目中的Debug目录下面(可以百度一下C#调用C/C++的DLL) 6.语音合成项目的代码如下:

using System;

using System.Collections.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 OfflineSpeech {

publicpartialclass Form1 : Form {

public Form1() {

InitializeComponent(); }

int ret = 0;

IntPtr session_ID;

privatevoid button1_Click(object sender, EventArgs e) { try {

///APPID请勿随意改动 string login_configs = \=***** \;//登录参数,自己注册后获取的appid string text = richTextBox1.Text.Trim();//待合成的文本 if (string.IsNullOrEmpty(richTextBox1.Text.Trim())) {

text = \请输入合成语音的内容\; }

string filename = \; //合成的语音文件 uint audio_len = 0;

SynthStatus synth_status = SynthStatus.MSP_TTS_FLAG_STILL_HAVE_DATA; ret = TTSDll.MSPLogin(string.Empty, string.Empty, login_configs);//第一个参数为用户名,第二个参数为密码,第三个参数是登录参数,用户名和密码需要在http://open.voicecloud.cn //MSPLogin方法返回失败

if (ret !=(int) ErrorCode.MSP_SUCCESS) {

return;

}

//string parameter = \tts_res_path =fo|res\\\\tts\\\\xiaoyan.jet;fo|res\\\\tts\\\\common.jet, sample_rate = 16000\ string _params =

\f=audio/L16;rate=16000\;

//string @params = \

local,voice_name=xiaoyan,speed=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(text), string.Empty); //QTTSTextPut方法返回失败

if (ret != (int)ErrorCode.MSP_SUCCESS) {

return; }

MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(newbyte[44], 0, 44); while (true) {

IntPtr source = TTSDll.QTTSAudioGet(Ptr2Str(session_ID), ref audio_len, ref synth_status, ref ret);

byte[] array = newbyte[(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 = this.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) { }

finally {

ret = TTSDll.QTTSSessionEnd(Ptr2Str(session_ID), \); ret = TTSDll.MSPLogout();//退出登录 } }

///

/// 结构体转字符串 ///

///

privatebyte[] StructToBytes(object structure) {

int num = Marshal.SizeOf(structure);

IntPtr intPtr = Marshal.AllocHGlobal(num); byte[] result; try {

Marshal.StructureToPtr(structure, intPtr, false); byte[] array = newbyte[num];

Marshal.Copy(intPtr, array, 0, num); result = array; }

finally {

Marshal.FreeHGlobal(intPtr); }

return result;


C#调用科大讯飞离线语音合成TTS(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新闻标题和评论标题制作方法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: