美文网首页
科大讯飞tts

科大讯飞tts

作者: zjh3029 | 来源:发表于2017-11-20 20:53 被阅读0次
    #include <windows.h>
    #include<iostream>
    #include "XAudio2.h"
    #include "qtts.h"
    #include "msp_cmn.h"
    #include "msp_errors.h"
    #include <string>
    using namespace std;
    #pragma comment(lib,"xaudio2.lib")
    
    int text_to_speech(const char* src_text)
    {
        const char* params = "voice_name = xiaoyan, text_encoding = gb2312, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
        MSPLogin(NULL, NULL, "appid = 59c066c4, work_dir = .");
    
        int ret = -1;
        int  synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
    
        unsigned int audio_len = 0;
        unsigned int speech_len = 0;
    
        const char*sessionID = QTTSSessionBegin(params, &ret);
        const void* audio_data = NULL;
        char* synth_speech = new char[2 * 1024 * 1024];
    
        ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
    
        IXAudio2 *pEngine = NULL;
        IXAudio2MasteringVoice *pMasterVoice = NULL;
        IXAudio2SourceVoice *pSourceVoice = NULL;
    
        WAVEFORMATEX waveFormat = { 1,1,16000,32000,2,16,0 };//获取文件格式
    
        if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))exit(0);
        if (FAILED(XAudio2Create(&pEngine)))exit(0);
        if (FAILED(pEngine->CreateMasteringVoice(&pMasterVoice)))exit(0);
        if (FAILED(pEngine->CreateSourceVoice(&pSourceVoice, &waveFormat)))exit(0);
    
        while (MSP_TTS_FLAG_DATA_END != synth_status)
        {
            audio_data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
            if (ret) break;
            if (NULL != audio_data && 0 != audio_len)
            {
                memcpy(synth_speech + speech_len, audio_data, audio_len);
                speech_len += audio_len;
            }
        }
        BYTE *pData = new BYTE[speech_len];//申请内存空间,用于保存数据
        pData = (BYTE*)synth_speech;
    
        XAUDIO2_BUFFER buffer = { 0 };//将读取的文件数据,赋值XAUDIO2_BUFFER
        buffer.AudioBytes = speech_len;
        buffer.pAudioData = pData;
        buffer.Flags = XAUDIO2_END_OF_STREAM;
        pSourceVoice->SubmitSourceBuffer(&buffer);//提交内存数据
        pSourceVoice->Start(0);//启动源声音
        XAUDIO2_VOICE_STATE state;
        pSourceVoice->GetState(&state, XAUDIO2_VOICE_NOSAMPLESPLAYED);//获取状态
        while (state.BuffersQueued)
        {
            pSourceVoice->GetState(&state);
        }
        pMasterVoice->DestroyVoice();//释放资源
        pSourceVoice->DestroyVoice();//释放资源
        pEngine->Release();//释放资源
        CoUninitialize();//释放资源
        cout << pData;
        delete[] pData;//释放资源
        pData = NULL;
        MSPLogout(); //退出登录
        return 0;
    }
    
    void tts(std::string stringall)
    {
        while (1)
        {
            int x = stringall.find_first_of("/");
            if (x <= 0) break;
            std::string str_temp = stringall.substr(0, x);
            cout << "o_O:" << str_temp << endl;
            text_to_speech(str_temp.c_str());
            std::string str_others = stringall.substr(str_temp.length() + 1);
            stringall = str_others;
        }
    }
    
    int main()
    {
        std::string text = "据银陀诊断您属于乳腺炎,阴虚血瘀症。/乳腺疾病是女性常见病,乳房肿块是乳腺疾病的首发或主要临床表现。/乳房疾病是由人体气血、津液、经络腑脏功能失调而引起。/日常生活中应该摄入均衡的营养,不要暴饮暴食和偏食,要注意适当地运动。养成良好习惯和有规律的生活节奏,/保持稳定情绪;有助于调节内分泌。促进乳房健康!/"; //合成文本
        tts(text);
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:科大讯飞tts

          本文链接:https://www.haomeiwen.com/subject/hasbvxtx.html