之前已经初步探索了语音识别的可能性以及单独控制机器狗运动的脚本编写,现在探索使用语音识别初步控制小狗运动,由于树莓派收声部分还有些问题,所以初步探索使用录入的音频文件进行识别。
编写发布器
编写发布器发布语音识别的结果。
源码
只展示主函数的部分,需要注意的地方是,对于音频文件的存放路径
int main(int argc, char* argv[])
{
ros::init(argc, argv, "voiceRecognition");
ros::NodeHandle n;
ros::Rate loop_rate(10);
ros::Publisher voiceWordsPub = n.advertise<std_msgs::String>("voiceWords", 1000);
int ret = MSP_SUCCESS;
int upload_on = 1; /* whether upload the user word */
/* login params, please do keep the appid correct */
const char* login_params = "appid = 5fa8bc98, work_dir = .";
int aud_src = 0; /* from mic or file */
int count =0;
while(ros::ok())
{
/*
* See "iFlytek MSC Reference Manual"
*/
const char* session_begin_params =
"sub = iat, domain = iat, language = zh_cn, "
"accent = mandarin, sample_rate = 16000, "
"result_type = plain, result_encoding = utf8";
/* Login first. the 1st arg is username, the 2nd arg is password
* just set them as NULL. the 3rd arg is login paramertes
* */
ret = MSPLogin(NULL, NULL, login_params);
if (MSP_SUCCESS != ret) {
printf("MSPLogin failed , Error code %d.\n",ret);
goto exit; // login fail, exit the program
}
//是否上传用户词库
printf("Want to upload the user words ? \n0: No.\n1: Yes\n");
scanf("%d", &upload_on);
if (upload_on)
{
printf("Uploading the user words ...\n");
ret = upload_userwords();
if (MSP_SUCCESS != ret)
goto exit;
printf("Uploaded successfully\n");
}
//语音输入的音频来自于什么地方,可以选择音频文件或者麦克风实时输入
printf("Where the audio comes from?\n"
"0: From a audio file.\n1: From microphone.\n");
scanf("%d", &aud_src);
if(aud_src != 0) {
printf("Demo recognizing the speech from microphone\n");
printf("Speak in 15 seconds\n");
demo_mic(session_begin_params);
printf("15 sec passed\n");
} else {
printf("Demo recgonizing the speech from a recorded audio file\n");
demo_file("/home/liuda/spotmicro/src/spotMicro/robot_voice/bin/wav/iflytek02.wav", session_begin_params);
//注意音频文件的存放位置,移植代码的时候注意修改
}
MSPLogout();
//发布消息,当识别到语音时发布识别结果
std_msgs::String msg;
if(resultFlag){