美文网首页
科大讯飞vod实时录音并翻译成文字

科大讯飞vod实时录音并翻译成文字

作者: zjh3029 | 来源:发表于2017-10-15 18:21 被阅读0次
#include <iostream>
#include <windows.h>
#include "qisr.h"
#include "msp_cmn.h"
#include "msp_errors.h"

using namespace std;

#pragma comment(lib,"winmm.lib")

HANDLE wait;
HWAVEIN hWaveIn;//输入设备
WAVEFORMATEX waveform;//采集的音频的结构体
BYTE* pBuffer;//采集音频的数据缓存
WAVEHDR wHder;//采集音频时包含数据缓存的结构体

void recong()
{
    const long      BUFFER_SIZE = 4096;
    char            rec_result[BUFFER_SIZE];
    char            rec_result_1[BUFFER_SIZE];
    char            rec_result_2[BUFFER_SIZE];
    int             aud_stat = MSP_AUDIO_SAMPLE_CONTINUE;       //音频状态
    int             ep_stat = MSP_EP_LOOKING_FOR_SPEECH;        //端点检测
    int             rec_stat = MSP_REC_STATUS_SUCCESS;          //识别状态
    int             errcode = MSP_SUCCESS;

    long            len = 6400;
    unsigned int    total_len = 0;
    char            hints[100] = { NULL };
    const char* session_id = QISRSessionBegin(NULL, "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = gb2312,vad_eos=1000", &errcode);
    
    while (1)
    {
        waveform.wFormatTag = WAVE_FORMAT_PCM;
        waveform.nSamplesPerSec = 16000;//采样率
        waveform.wBitsPerSample = 16;
        waveform.nChannels = 1;
        waveform.nAvgBytesPerSec = 32000;
        waveform.nBlockAlign = 2;
        waveform.cbSize = 0;

        wait = CreateEvent(NULL, 0, 0, NULL);
        waveInOpen(&hWaveIn, WAVE_MAPPER, &waveform, (DWORD_PTR)wait, 0L, CALLBACK_EVENT);

        pBuffer = new BYTE[102400];

        wHder.lpData = (LPSTR)pBuffer;
        wHder.dwBufferLength = 102400;
        wHder.dwBytesRecorded = 0;
        wHder.dwUser = 0;
        wHder.dwFlags = 0;
        wHder.dwLoops = 1;

        waveInPrepareHeader(hWaveIn, &wHder, sizeof(WAVEHDR));
        waveInAddBuffer(hWaveIn, &wHder, sizeof(WAVEHDR));
        waveInStart(hWaveIn);
        Sleep(200);
        
        aud_stat = MSP_AUDIO_SAMPLE_FIRST;

        QISRAudioWrite(session_id, pBuffer, len, aud_stat, &ep_stat, &rec_stat);
        cout << "ep_stat"<< ep_stat << endl;

        if (rec_stat == MSP_REC_STATUS_SUCCESS)
        {
            const char* rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
            if (NULL != rslt)
            {
                unsigned int rslt_len = strlen(rslt);
                total_len += rslt_len;
                strncat(rec_result, rslt, rslt_len);
                strncat(rec_result_1, "a", 8);
            }
        }
        if (MSP_EP_AFTER_SPEECH == ep_stat)
        {
            waveInReset(hWaveIn);
            break;
        }
    }

    std::cout << "OK!!!" << std::endl;
    errcode = QISRAudioWrite(session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST, &ep_stat, &rec_stat);
    std::cout << "OK1!!!" << std::endl;
    while (MSP_REC_STATUS_COMPLETE != rec_stat)
    {
        std::cout << "OK2!!!" << std::endl;
        const char *rslt = QISRGetResult(session_id, &rec_stat, 0, &errcode);
        if (NULL != rslt)
        {
            std::cout << "OK3!!!" << std::endl;
            unsigned int rslt_len = strlen(rslt);
            total_len += rslt_len;
            strncat(rec_result, rslt, rslt_len);
        }
        Sleep(150); //防止频繁占用CPU
    }
    std::cout << "OK4!!!" << std::endl;
    printf("\n语音听写结束\n");
    printf("=============================================================\n");
    printf("%s\n", rec_result);
    printf("=============================================================\n");
    QISRSessionEnd(session_id, hints);
    delete pBuffer;
    return;
}

int main()
{
    MSPLogin(NULL, NULL, "appid = 59c066c4, work_dir = .");

    recong();
    
    waveInClose(hWaveIn);
    MSPLogout();
    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:科大讯飞vod实时录音并翻译成文字

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