????????GrammarID???????????У?????????????????????????????????????????????????ubuntu??????????????Σ????????????????bbs????????????????????????????????????£???????16K??8KHz??????λ??16λ???????????????PCM??WAV????????????????????32λ???????????ffmpeg?????д????????ffmpeg???????£?
????ffmpeg -f alsa -i hw:0 -ar 16000 -ac 1 lib.wav
????????????????2???“????”??????????????????

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <qisr.h>
#define TRUE 1
#define FALSE 0
int run_asr(const char* asrfile);
const int BUFFER_NUM = 4096;
const int MAX_KEYWORD_LEN = 4096;
int main(int argc?? char* argv[])
{
int ret = MSP_SUCCESS;
const char* asrfile ="lib.wav";
ret = QISRInit("appid=xxxxxx");
if(ret != MSP_SUCCESS)
{
printf("QISRInit with errorCode: %d "?? ret);
return 0;
}
ret = run_asr(asrfile);
QISRFini();
char key = getchar();
return 0;
}
int run_asr(const char* asrfile)
{
int ret = MSP_SUCCESS;
int i = 0;
FILE* fp = NULL;
char buff[BUFFER_NUM];
unsigned int len;
int status = MSP_AUDIO_SAMPLE_CONTINUE?? ep_status = -1?? rec_status = -1?? rslt_status = -1;
//const char* GrammarID="e7eb1a443ee143d5e7ac52cb794810fe";
const char *GrammarID="c66d4eecd37d4fe1c8274a2224b832d5";
const char* param = "rst=json??sub=asr??ssm=1??aue=speex??auf=audio/L16;rate=16000";//???sub=asr
const char* sess_id = QISRSessionBegin(GrammarID?? param?? &ret);
if ( MSP_SUCCESS != ret )
{
printf("QISRSessionBegin err %d "?? ret);
return ret;
}
fp = fopen( asrfile ?? "rb");
if ( NULL == fp )
{
printf("failed to open file??please check the file. ");
QISRSessionEnd(sess_id?? "normal");
return -1;
}
printf("writing audio... ");
//
int count=0;
//
while ( !feof(fp) )
{
len = (unsigned int)fread(buff?? 1?? BUFFER_NUM?? fp);
feof(fp) ? status = MSP_AUDIO_SAMPLE_LAST : status = MSP_AUDIO_SAMPLE_CONTINUE;
if(status==MSP_AUDIO_SAMPLE_LAST)
printf("MSP_AUDIO_SAMPLE_LAST ");
if(status==MSP_AUDIO_SAMPLE_CONTINUE)
printf("MSP_AUDIO_SAMPLE_CONTINUE ");
//
ret = QISRAudioWrite(sess_id?? buff?? len?? status?? &ep_status?? &rec_status);
if ( ret != MSP_SUCCESS )
{
printf(" QISRAudioWrite err %d "?? ret);
break;
}
//
printf("%d "??count++);
//
if ( rec_status == MSP_REC_STATUS_SUCCESS )
{
const char* result = QISRGetResult(sess_id?? &rslt_status?? 0?? &ret);
if (ret != MSP_SUCCESS )
{
printf("error code: %d "?? ret);
break;
}
else if( rslt_status == MSP_REC_STATUS_NO_MATCH )
printf("get result nomatch ");
else
{
if ( result != NULL )
printf("get result[%d/%d]:len:%d %s "?? ret?? rslt_status??strlen(result)?? result);
}
}
printf(".");
}
printf(" ");
if (ret == MSP_SUCCESS)
{
printf("get reuslt~~~~~~~ ");
char asr_result[1024] = "";
unsigned int pos_of_result = 0;
int loop_count = 0;
do
{
const char* result = QISRGetResult(sess_id?? &rslt_status?? 0?? &ret);
if ( ret != 0 )
{
printf("QISRGetResult err %d "?? ret);
break;
}
if( rslt_status == MSP_REC_STATUS_NO_MATCH )
{
printf("get result nomatch ");
}
else if ( result != NULL )
{
//
FILE*f=fopen("data.txt"??"wb");
printf("~~~%d "??strlen(result));
fwrite(result??1??strlen(result)??f);
fclose(f);
//
printf("[%d]:get result[%d/%d]: %s "?? (loop_count)?? ret?? rslt_status?? result);
strcpy(asr_result+pos_of_result??result);
pos_of_result += (unsigned int)strlen(result);
}
else
{
printf("[%d]:get result[%d/%d] "??(loop_count)?? ret?? rslt_status);
}
usleep(500000);
} while (rslt_status != MSP_REC_STATUS_COMPLETE && loop_count++ < 30);
if (strcmp(asr_result??"")==0)
{
printf("no result ");
}
}
QISRSessionEnd(sess_id?? NULL);
printf("QISRSessionEnd. ");
fclose(fp);
return 0;
}