//引擎关闭 int Msc_QTTS_Tools::fini() { ret = QTTSFini(); if ( ret != MSP_SUCCESS ) { printf("QTTSFini: failed , Error code %d.\n",ret); } return ret; }
int _tmain(int argc, _TCHAR* argv[]) { ///APPID请勿随意改动 const char* m_configs = "appid=52d8f781"; const char* text1 = "讯飞语音,沟通无限。"; const char* filename1 = "text_to_speech_test_1.wav"; const char* param1 = "ssm=1,auf=audio/L16;rate=16000,vcn=xiaoyan"; const char* text2 = "欢迎使用,安徽科大讯飞信息科技股份有限公司,云语音合成。"; const char* filename2 = "text_to_speech_test_2.wav"; const char* param2 = "ssm=1,auf=audio/L16;rate=16000,vcn=xiaoyu"; int ret = 0; char key = 0; //引擎初始化 ret = QTTSInit( m_configs); if ( ret != MSP_SUCCESS ) { printf("QTTSInit: failed, Error code %d.\n", ret); key = _getch(); return ret; } //合成文本 ret = text_to_speech(text1,filename1,param1); if ( ret != MSP_SUCCESS ) { printf("text_to_speech: failed , Error code %d.\n",ret); } ret = text_to_speech(text2,filename2,param2); if ( ret != MSP_SUCCESS ) { printf("text_to_speech: failed , Error code %d.\n",ret); } //引擎关闭 ret = QTTSFini(); if ( ret != MSP_SUCCESS ) { printf("QTTSFini: failed , Error code %d.\n",ret); } else { printf("Complete!\nPress any key to exit.\n"); } key = _getch(); return 0; }
int TTSHelper::CreateWaveAudioFile(const char *szText,const char *audioFileName) { const char* m_configs = ttsconf.m_configs; const char* szParam = ttsconf.szParam; printf("begin to synth...\n"); int ret = QTTSInit(m_configs); if ( ret != MSP_SUCCESS ) { LastErrorInfo="QTTSInit: qtts init failed, ret = "+ret; return ret; } //每次合成时,需要将这两个数据清零 pcmwavhdr.size_8=0; pcmwavhdr.data_size=0; const char* sess_id = QTTSSessionBegin(szParam, &ret); if ( ret != MSP_SUCCESS ) { //printf("QTTSSessionBegin: qtts begin session failed Error code %d.\n",ret); LastErrorInfo="QTTSSessionBegin: qtts begin session failed Error code: "+ret; QTTSFini(); return ret; } int len = strlen(szText); ret = QTTSTextPut(sess_id, szText, len, NULL ); if ( ret != MSP_SUCCESS ) { //printf("QTTSTextPut: qtts put text failed Error code %d.\n",ret); LastErrorInfo="QTTSTextPut: qtts put text failed Error code: "+ret; QTTSSessionEnd(sess_id, ""); QTTSFini(); return ret; } char* audio_data; unsigned int audio_len = 0; int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA; const char* tmpname = audioFileName; FILE* fp = fopen(tmpname, "wb"); if ( fp == NULL ) { LastErrorInfo="Failed to open file: "+string(tmpname); QTTSSessionEnd(sess_id, ""); QTTSFini(); return ret; } //写入音频头部数据 fwrite(&pcmwavhdr, 1, sizeof(pcmwavhdr), fp); int pos = 0; //用于标记上一次已经合成到的位置 int loop_count = 0; //用于标记,取了几次结果 int upload_flow = 0,download_flow = 0;//上传流量和下载流量 char param_value[32] = "";//参数值的字符串形式 size_t value_len = 32; //字符串长度或buffer长度 while ( true ) { audio_data = (char*)QTTSAudioGet( sess_id ,&audio_len , &synth_status , &ret ); if ( ret != MSP_SUCCESS ) { LastErrorInfo="QTTSAudioGet: qtts get audio failed Error code: "+ret; char key = _getch(); break; } fwrite(audio_data, 1, audio_len, fp); pcmwavhdr.data_size += audio_len;//修正pcm数据的大小 if ( MSP_TTS_FLAG_DATA_END == synth_status ) { printf("QTTSAudioGet: get end of data.\n"); break; } } //修正pcm文件头数据的大小 pcmwavhdr.size_8 += pcmwavhdr.data_size + 36; //将修正过的数据写回文件头部 fseek(fp, 4, 0); fwrite(&(pcmwavhdr.size_8),sizeof(pcmwavhdr.size_8), 1, fp); fseek(fp, 40, 0); fwrite(&(pcmwavhdr.data_size),sizeof(pcmwavhdr.data_size), 1, fp); fclose(fp); ret = QTTSSessionEnd(sess_id, ""); if ( ret != MSP_SUCCESS ) { //printf("QTTSSessionEnd: qtts end failed Error code %d.\n",ret); LastErrorInfo="QTTSSessionEnd: qtts end failed Error code: "+ret; char key = _getch(); QTTSFini(); return ret; } ret = QTTSFini(); if ( ret != MSP_SUCCESS ) { //printf("CMSPAudioCtrl::PlayText: qtts fini failed Error code %d.\n",ret); LastErrorInfo="CMSPAudioCtrl::PlayText: qtts fini failed Error code: "+ret; char key = _getch(); return ret; } filebuf *pbuf; ifstream filestr; long size; // 要读入整个文件,必须采用二进制打开 filestr.open (audioFileName, ios::binary); // 获取filestr对应buffer对象的指针 pbuf=filestr.rdbuf(); // 调用buffer对象方法获取文件大小 size=pbuf->pubseekoff (0,ios::end,ios::in); pbuf->pubseekpos (0,ios::in); // 分配内存空间 AudioBuffer=new char[size]; AudioSize=size; // 获取文件内容 pbuf->sgetn (AudioBuffer,size); filestr.close(); return 0; }