讯飞C/C++语音合成基础篇
首先到訊飛上注冊(cè)一個(gè)帳號(hào)(感覺訊飛官網(wǎng)和阿里云官網(wǎng)的差不多)
隨后創(chuàng)建一個(gè)新應(yīng)用(Windows平臺(tái)下)
最后我們要的是Appid如下圖所示
下面是訊飛SDK(含說明文檔)下載地址:
鏈接:http://pan.baidu.com/s/1i45xEo1 密碼:m8il
因?yàn)榇顺绦蛏婕癢AV語音知識(shí),未了解WAV的朋友請(qǐng)百度補(bǔ)充,或者在下面這個(gè)鏈接初略的學(xué)習(xí)下
http://blog.csdn.net/qq78442761/article/details/53385561
下面先來認(rèn)識(shí)下幾個(gè)API(從文檔提取出來)
1.int MSPAPI MSPLogin (const char *usr, const char *pwd, const char *params) //初始化msc,用戶登錄。
usr[in] 此參數(shù)保留,傳入NULL即可。
pwd[in] 此參數(shù)保留,傳入NULL即可。
官方例子
const char* usr = NULL; const char* pwd = NULL; const char* lgi_param = "appid = ********"; int ret = MSPLogin(usr, pwd, lgi_param); if( MSP_SUCCESS != ret ) {printf( "MSPLogin failed, error code is: %d", ret ); }2.const char *MSPAPI QTTSSessionBegin (const char *params, int *errorCode) //開始一次語音合成,分配語音合成資源。
(信息有點(diǎn)多,參數(shù)具體見文檔qtts.h 文件參考)
下面是官方例子
const char * ssb_param = "voice_name = xiaoyan, aue = speex-wb;7, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2"; int ret = -1; const char * sessionID = QTTSSessionBegin( ssb_param, &ret ); if( MSP_SUCCESS != ret ) {printf( “QTTSSessionBegin failed, error code is: %d”, ret ); }
(信息有點(diǎn)多,參數(shù)具體見文檔qtts.h 文件參考)
例子
FILE* fp = fopen("tts.pcm", "wb"); while (1) {const void * data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);if (NULL != data){fwrite(data, audio_len, 1, fp);}if (MSP_TTS_FLAG_DATA_END == synth_status || MSP_SUCCESS != ret){break;} } fclose(fp);
下面是完整項(xiàng)目的代碼:
幾個(gè)個(gè)文件1.stdafx.h。2.targetver.h。3.QTTSDemo.cpp。4.stdafx.cpp。
除了QTTSDemo.cpp外,其他文件都是vs2013創(chuàng)建控制臺(tái)程序自帶的,在此只給出QTTSDemo.cpp代碼
但注意在stdafx.h中加入#define _CRT_SECURE_NO_WARNINGS
QTTSDemo.cpp
// QTTSDemo.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 //#include "stdafx.h" #include <msp_cmn.h> #include <msp_errors.h> #include <qtts.h> #include <string.h> #include <windows.h>#pragma comment(lib,"WinMM.lib")#ifdef _WIN64 #pragma comment(lib,"msc_x64.lib") #else #pragma comment(lib,"msc.lib") #endif//wav音頻頭部格式 typedef struct _wave_pcm_hdr {char riff[4]; //="RIFF"int size_8; //=FileSize=8char wave[4]; //="WAVE"char fmt[4]; //="fmt"int fmt_size; //=下一個(gè)結(jié)構(gòu)體的大小:16short int format_tag; //=PCM:1short int channels; //=通道數(shù):1int samples_per_sec; //=采樣率:8000|6000|16000int avg_bytes_per_sec; //=每秒字節(jié)數(shù):samples_per_sec*bit_per_sampleshort int block_align; //=每采樣點(diǎn)字節(jié)數(shù):wBitsPerSample/8short int bits_per_sample; //=量化比特?cái)?shù):8|16char data[4]; //="data";int data_size; //=純數(shù)據(jù)長(zhǎng)度:FileSize-44 }wave_pcm_hdr;/*默認(rèn)wav音頻頭部數(shù)據(jù)*/ wave_pcm_hdr default_wav_hdr = {{ 'R', 'I', 'F', 'F' },0,{ 'W', 'A', 'V', 'E' },{ 'f', 'm', 't', ' ' },16,1,1,16000,32000,2,16,{ 'd', 'a', 't', 'a' },0 };int _tmain(int argc, _TCHAR* argv[]) {//登錄const char* usr = NULL;const char* pwd = NULL;const char* lgi_param = "appid = 583aea17";int ret = MSPLogin(usr, pwd, lgi_param);if (MSP_SUCCESS != ret){printf("MSPLogin failed, error code is: %d", ret);}//開始合成const char * ssb_param = "voice_name = xiaorong, aue = speex-wb;7, sample_rate = 16000, speed = 50, volume = 80, pitch = 50, rdn = 2";ret = -1;const char * sessionID = QTTSSessionBegin(ssb_param, &ret);if (MSP_SUCCESS != ret){printf("QTTSSessionBegin failed, error code is : %d", ret);}//設(shè)置待合成文本char src_text[1024];printf("請(qǐng)輸入一段文字(中文注意逗號(hào),句話):\n");gets(src_text);unsigned int text_len = strlen(src_text); //textLen參數(shù)為合成文本所占字節(jié)數(shù)ret = QTTSTextPut(sessionID, src_text, text_len, NULL);if (MSP_SUCCESS != ret){printf("QTTSTextPut failed, error code is : %d", ret);}//獲取合成的音頻wavFILE* fp = fopen("Demo.wav", "wb"); //一定是二進(jìn)制模式fwrite(&default_wav_hdr, sizeof(default_wav_hdr), 1, fp);unsigned int audio_len = 0;int synth_status = 0;while (1){const void * data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);if (NULL != data){fwrite(data, audio_len, 1, fp);default_wav_hdr.data_size += audio_len;}if (MSP_TTS_FLAG_DATA_END == synth_status || MSP_SUCCESS != ret){break;}}default_wav_hdr.size_8 += default_wav_hdr.data_size + (sizeof(default_wav_hdr)-8);fseek(fp, 4, 0);fwrite(&default_wav_hdr.size_8, sizeof(default_wav_hdr.size_8), 1, fp); //寫入size_8的值fseek(fp, 40, 0); //將文件指針偏移到存儲(chǔ)data_size值的位置fwrite(&default_wav_hdr.data_size, sizeof(default_wav_hdr.data_size), 1, fp);//寫入data_size的值fclose(fp);PlaySoundA("Demo.wav", NULL, SND_ASYNC);ret = QTTSSessionEnd(sessionID, "normal end");if (MSP_SUCCESS != ret){printf("QTTSSessionEnd failed, error code is : %d", ret);}//退出ret = MSPLogout();if (MSP_SUCCESS != ret){printf("MSPLogout failed, error code is: %d", ret);}system("pause");return 0; }
總結(jié)
以上是生活随笔為你收集整理的讯飞C/C++语音合成基础篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java爬取网页并保存_第九讲:Pyth
- 下一篇: python的坐标代码_基于Python