24 std::cout <<
"--------- xunfei asr demo ------------" << std::endl;
25 std::cout <<
"这是科大讯飞在线语音识别功能/离线语法识别的示例demo." << std::endl;
26 std::cout <<
"你可以选择在线语音识别功能或者离线语法识别功能." << std::endl;
27 std::cout <<
"0:在线语音识别功能;" << std::endl;
28 std::cout <<
"1:离线语法识别功能." << std::endl;
29 std::cout <<
"请输入0或1选择不同功能:" << std::endl;
30 std::cout <<
"asr_module :_\b";
32 std::cout <<
"------------------------------" << std::endl;
33 std::cout <<
"你可以选择通过已录好的pcm文件或者实时录音进行语音识别测试." << std::endl;
34 std::cout <<
"0:选择已录好的pcm音频文件进行唤醒测试;" << std::endl;
35 std::cout <<
"1:通过实时录音进行测试." << std::endl;
36 std::cout <<
"请输入0或1选择不同方式:" << std::endl;
37 std::cout <<
"asr_mode :_\b";
42 void fileToASR(
const std::string asr_param,
const std::string pcm_file)
46 std::string xfei_online_result =
"none_result";
54 std::cout <<
"在线语音识别结果:" << xfei_online_result << std::endl;
60 void recordToASR(
const std::string asr_param,
const std::string pcm_file)
63 struct DataBuff pcm_buff = { NULL, 0 };
66 char* xfei_online_result = NULL;
71 float record_time = 3.5;
73 bool enable_record_save =
true;
78 std::cout <<
"在线语音识别结果:" << xfei_online_result << std::endl;
88 char* recog_result_json = NULL;
97 std::vector<std::string> offline_result_vector(2);
98 if (recog_result_json == NULL)
100 offline_result_vector[0] =
"none_result";
101 offline_result_vector[1] =
"0";
108 std::cout <<
"离线语法识别结果:" << offline_result_vector[0] <<
" 置信度:" << offline_result_vector[1] << std::endl;
117 struct DataBuff pcm_buff = { NULL, 0 };
120 char* recog_result_json = NULL;
125 float record_time = 3.5;
127 bool enable_record_save =
true;
129 pcm_buff = xfei_sr_offline.
recordThroughMIC(record_time, enable_record_save);
134 std::vector<std::string> offline_result_vector(2);
135 if (recog_result_json == NULL)
137 offline_result_vector[0] =
"none_result";
138 offline_result_vector[1] =
"0";
145 std::cout <<
"离线语法识别结果:" << offline_result_vector[0] <<
" 置信度:" << offline_result_vector[1] << std::endl;
154 std::string grammar_file =
base_path +
"/cache/grammar_config";
156 std::string grammar_build_path = grammar_file +
"/grammar";
157 grammar_file = grammar_file +
"/grammar.bnf";
162 string asr_res_path =
"fo|res/asr/common.jet";
167 int main(
int argc,
char** argv)
171 usage(asr_module, asr_mode);
173 const std::string default_pcm_file =
base_path +
"/defaultconfig/audio/nihao_test.pcm";
182 std::string xfei_online_asr_params_ =
"sub = iat, domain = iat, language = zh_cn, " 183 "accent = mandarin, sample_rate = 16000, " 184 "result_type = plain, result_encoding = utf8";
189 std::string pcm_file;
190 std::cout <<
"------------------------------" << std::endl;
191 std::cout <<
"请输入用于测试的pcm文件,若输入为d,则用默认的文件进行测试:";
192 std::cin >> pcm_file;
195 pcm_file = default_pcm_file;
197 fileToASR(xfei_online_asr_params_, pcm_file);
201 recordToASR(xfei_online_asr_params_, default_pcm_file);
210 std::string grammar_path =
base_path +
"/cache/grammar_config";
216 std::string pcm_file;
217 std::cout <<
"------------------------------" << std::endl;
218 std::cout <<
"请输入用于测试的pcm文件,若输入为d,则用默认的文件进行测试:";
219 std::cin >> pcm_file;
222 pcm_file =
base_path +
"/defaultconfig/audio/nihao_test.pcm";
234 std::cout <<
"无效的功能模式的选择!!!" << std::endl;
void fileToASROffline(const std::string asr_param, const std::string pcm_file)
void usage(int &asr_module, int &asr_mode)
void recordToASROffline(const std::string asr_param, const std::string pcm_file)
void initAsr()
科大讯飞识别模块的初始化.
static CommonConfig & get_instance()
void uninitAsr()
一次识别结束后释放资源.
void fileToASR(const std::string asr_param, const std::string pcm_file)
void loginToXunfei(const std::string base_path)
科大讯飞语音识别模块接口头文件. TODO: 还需要添加版权、版本等信息
int main(int argc, char **argv)
void recordToASR(const std::string asr_param, const std::string pcm_file)
char * dataLoopRecog()
将全部音频数据循环写入科大讯飞接口进行识别并获取完整的json识别结果.
XfeiSpeechRecog asr_module
struct DataBuff recordThroughMIC(const float record_time, bool enable_audio_save)
录音接口.
void setAsrParams(const std::string base_path, const std::string pcm_file, const std::string params, const int channel)
设置语音模块需外部传入的路径等参数.
std::string configGramParas(const std::string base_path, const std::string grammar_file)
const std::string base_path
void buildGrammar(UserData *udata, const string base_path, const string grammar_build_path, const string asr_res_path, const string grammar_file)
std::vector< std::string > resultFromJson()
从完整的json语音识别结果中解析出需要的字符串结果和置信度值.
void getPcmFileData()
读取pcm文件里的音频数据,并将数据的内容和数据大小存入pcm_data_.
void getPCMData(struct DataBuff pcm_buff)
获取pcm数据接口.