< >
Home » ROS与语音交互教程 » ROS与语音交互教程-ROS下科大讯飞语音听写

ROS与语音交互教程-ROS下科大讯飞语音听写

ROS与语音交互教程-ROS下科大讯飞语音听写

说明:

  • 让机器人具备说的功能,把要告诉用户的内容使用中文说出来

  • 这个功能节点基于科大讯飞SDK中的“iat_record_sample”例程

步骤:

  • 将例程中的代码拷贝到自己的功能包中

  • 修改iat_record_sample.c

int main(int argc, char* argv[])
{
    // 初始化ROS
    ros::init(argc, argv, "voiceRecognition");
    ros::NodeHandle n;
    ros::Rate loop_rate(10);

    // 声明Publisher和Subscriber
    // 订阅唤醒语音识别的信号
    ros::Subscriber wakeUpSub =         n.subscribe("voiceWakeup", 1000, WakeUp);   
    // 订阅唤醒语音识别的信号    
    ros::Publisher voiceWordsPub = n.advertise<std_msgs::String>("voiceWords", 1000);  

    ROS_INFO("Sleeping...");
    int count=0;
    while(ros::ok())
    {
        // 语音识别唤醒
        if (wakeupFlag){
            ROS_INFO("Wakeup...");
            int ret = MSP_SUCCESS;
            const char* login_params = "appid = 5cda6c63, work_dir = .";

            const char* session_begin_params =
                "sub = iat, domain = iat, language = zh_cn, "
                "accent = mandarin, sample_rate = 16000, "
                "result_type = plain, result_encoding = utf8";

            ret = MSPLogin(NULL, NULL, login_params);
            if(MSP_SUCCESS != ret){
                MSPLogout();
                printf("MSPLogin failed , Error code %d.\n",ret);
            }

            printf("Demo recognizing the speech from microphone\n");
            printf("Speak in 10 seconds\n");

            demo_mic(session_begin_params);

            printf("10 sec passed\n");
    
            wakeupFlag=0;
            MSPLogout();
        }

        // 语音识别完成
        if(resultFlag){
            resultFlag=0;
            std_msgs::String msg;
            msg.data = g_result;
            voiceWordsPub.publish(msg);
        }

        ros::spinOnce();
        loop_rate.sleep();
        count++;
    }

exit:
   MSPLogout(); // Logout...

    return 0;
}

代码解释:
加入了一个Publisher和一个Subscriber。Subscriber用来接收语音唤醒信号,接收到唤醒信号后,会将wakeupFlag变量置位,然后在主循环中调用SDK的语音听写功能,识别成功后职位resultFlag变量,通过Publisher将识别出来的字符串进行发布

  • 修改CMakeList.txt
add_executable(iat_publish 
    src/iat_publish.cpp 
    src/speech_recognizer.c 
    src/linuxrec.c)
target_link_libraries(
    iat_publish
    ${catkin_LIBRARIES} 
    libmsc.so -ldl -lpthread -lm -lrt -lasound
)
  • 编译
catkin_make
  • 执行,发布唤醒信号(任意字符串)
rosrun robot_voice iat_publish
rostopic echo /voiceWords
rostopic pub /voiceWakeup std_msgs/String "data: 'any string'"

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros与语音交互教程, ros 语音, 科大讯飞