PV1_MakeProject/Modules/voice/demo.c

94 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "SrcApi.h"
#define DEBUG_CODE_LINE() (printf("%s(%d)\n", __FUNCTION__, __LINE__))
#define POSTURL "api-test.vop.netease.com/echo_api?rate=16000&lan=zh"
int main()
{
#if 1
const int kSamples = 1024; //40ms
int rcv_code = 0;
int error_code = 0;
short data[kSamples];
SRCParam srcParam;
srcParam.URL = POSTURL;
srcParam.appKey = "onlinedemo";
srcParam.appSecret = "4ea70c117a5b858cd9e6e384009bbad7";
srcParam.severVadThresh1 = "2.0";
srcParam.severVadThresh2 = "2.0";
srcParam.cuid = "1215468784532";
SRCSetUPParam("uuid","0-A024067C55B0163C");
SRCSetUPParam("aes", "0");
//SRCSetUPParam("state", "{\"filed\":1}");
//SRCSetSPParam("name1", "xxxxxx");
//SRCSetSPParam("name2", "xxxxxx");
const char* session_id;
DEBUG_CODE_LINE();
int status = 0;
FILE *asrFile = fopen("data.pcm", "rb");
session_id = SRCSessionBegin(srcParam, &status);
if (status != SRC_SUCCESS)
{
printf("SRCStartNewSession error:%d\n", status);
}
printf("ssid = %s\n", session_id);
int getResCnt = 0;
rcv_code = 0;
const char *asr_out = NULL;
DEBUG_CODE_LINE();
while (1)
{
if (fread(data, 2, kSamples, asrFile) == 0)
{
fseek(asrFile, 0, 0);
}
int vadFlag = 0;
int recFlag = 0;
DEBUG_CODE_LINE();
status = SRCAudioWrite(session_id, data, kSamples, &vadFlag, &recFlag);
DEBUG_CODE_LINE();
if (status != SRC_SUCCESS)
{
printf("SRCAudioWrite error:%d\n", status);
}
printf("vadFlag = %d, recFlag = %d\n", vadFlag, recFlag);
if (vadFlag == SRC_VAD_END_SPEECH || recFlag == 1002)
break;
usleep(40 * 1000);
}
DEBUG_CODE_LINE();
while (rcv_code != 3100)
{
usleep(100 * 1000);
asr_out = SRCGetResult(session_id, &rcv_code);
getResCnt++;
if (error_code != 0)
{
break;
}
if (getResCnt > 10)
{
break;
}
printf("rcv_code = %d, retry = %d\n", rcv_code, getResCnt);
}
printf("=============%s=============\n", (asr_out));
fclose(asrFile);
SRCSessionEnd(session_id);
#endif
printf("Demo++++++++++++++++++++++\n");
return 0;
}