qml采用科大讯飞语音识别SDK

1、下载科大讯飞SDK语音demo

 

2、语音识别头文件

#ifndef BLL_SPEECHRECOGNITE_H
#define BLL_SPEECHRECOGNITE_H

#include <QObject>
#include <QTimer>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "qisr.h"
#include "msp_cmn.h"
#include "msp_errors.h"
#include "speech_recognizer.h"
#include <QDebug>

class BLL_SpeechRecognite : public QObject
{
    Q_OBJECT
public:
    enum SpeechType {
        SpeechStart,
        SpeechEnd,
        SpeechTimeOut
    };

    explicit BLL_SpeechRecognite(QObject *parent = nullptr);

    virtual ~BLL_SpeechRecognite();

    // 开始语音识别
    void speechRecognite();


public slots:


signals:
    void isBeginChanged();

private:
    void demo_mic(const char* session_begin_params);

    static void show_result(char *string, char is_over);

    static void on_speech_end(int reason);

    static void on_speech_begin();

    static void on_result(const char *result, char is_last);

private:
    QTimer m_timer;
    static struct speech_rec iat;
    struct speech_rec_notifier recnotifier;
};

#endif // BLL_SPEECHRECOGNITE_H

3、语音识别cpp文件

#include "bll_speechrecognite.h"
#include "bll_globalsettings.h"

#define FRAME_LEN	640
#define	BUFFER_SIZE	4096

static char *g_result = NULL;
static unsigned int g_buffersize = BUFFER_SIZE;

struct speech_rec BLL_SpeechRecognite::iat;
BLL_SpeechRecognite::BLL_SpeechRecognite(QObject *parent) : QObject(parent)
{
    system("amixer -c 0 cset numid=2 1");

    recnotifier = {
        on_result,
        on_speech_begin,
        on_speech_end
    };

    m_timer.setSingleShot(true);
    connect(&m_timer, &QTimer::timeout, this, [&](){
        int errcode = sr_stop_listening(&iat);
        if (errcode) {
           qDebug()<<__FUNCTION__<<__LINE__<<"stop listening failed "<<errcode;
        }

        qDebug()<<__LINE__<<"speech recognite timeout";
        sr_uninit(&iat);
        MSPLogout();
        BLL_GlobalSettings::getInstance()->getSpeechStatus(SpeechType::SpeechTimeOut);
    });
}

BLL_SpeechRecognite::~BLL_SpeechRecognite()
{

}

void BLL_SpeechRecognite::show_result(char *string, char is_over)
{
    QString str = QString("Result: [ %1 ]").arg(string);
    qDebug()<<__FUNCTION__<<__LINE__<<str;
    if(is_over) {
        qDebug()<<__FUNCTION__<<__LINE__<<"speech recognite normal end!";
    }

    QString result = QString(string).remove(QRegExp("[,。 ]"));
    BLL_GlobalSettings::getInstance()->getSpeechResult(result);
}

void BLL_SpeechRecognite::on_result(const char *result, char is_last)
{
    if (result) {
        size_t left = g_buffersize - 1 - strlen(g_result);
        size_t size = strlen(result);
        if (left < size) {
            g_result = (char*)realloc(g_result, g_buffersize + BUFFER_SIZE);
            if (g_result)
                g_buffersize += BUFFER_SIZE;
            else {
                qDebug()<<__FUNCTION__<<__LINE__<<"mem alloc failed";
                return;
            }
        }

        strncat(g_result, result, size);
        show_result(g_result, is_last);
    }
}

void BLL_SpeechRecognite::on_speech_begin()
{
    if (g_result)
    {
        free(g_result);
    }
    g_result = (char*)malloc(BUFFER_SIZE);
    g_buffersize = BUFFER_SIZE;
    memset(g_result, 0, g_buffersize);

    qDebug()<<__FUNCTION__<<__LINE__<<"Start Listening...";

    BLL_GlobalSettings::getInstance()->getSpeechStatus(SpeechType::SpeechStart);
}

void BLL_SpeechRecognite::on_speech_end(int reason)
{
    if (reason == END_REASON_VAD_DETECT) {
        qDebug()<<__FUNCTION__<<__LINE__<<"Speaking done";
    } else {
        qDebug()<<__FUNCTION__<<__LINE__<<"Recognizer error: "<<reason;
    }

    BLL_GlobalSettings::getInstance()->getSpeechStatus(SpeechType::SpeechEnd);
}

void BLL_SpeechRecognite::demo_mic(const char* session_begin_params)
{
    int errcode;

    errcode = sr_init(&iat, session_begin_params, SR_MIC, &recnotifier);
    if (errcode) {
        qDebug()<<__FUNCTION__<<__LINE__<<"speech recognizer init failed";
        return;
    }

    errcode = sr_start_listening(&iat);
    if (errcode) {
        qDebug()<<__FUNCTION__<<__LINE__<<"start listen failed "<<errcode;
        return;
    }

    m_timer.start(15*1000);
}

void BLL_SpeechRecognite::speechRecognite()
{
    // 语音识别前停止录音
    int errcode = sr_stop_listening(&iat);
    if (errcode) {
        qDebug()<<__FUNCTION__<<__LINE__<<"stop listening failed"<<errcode;
    }

    sr_uninit(&iat);
    MSPLogout();

    int ret = MSP_SUCCESS;
    /* login params, please do keep the appid correct */
#ifdef PLATFORM_ARM
    QString login_params = "appid = f22fe9f8, work_dir = .";
#else
    QString login_params = "appid = 42ffef45, work_dir = .";
#endif
//    const char* login_params = "appid = f22fe9f8, work_dir = .";
    /*
    * 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.toUtf8().data());
    if (MSP_SUCCESS != ret)	{
        qDebug()<<__FUNCTION__<<__LINE__<<"MSPLogin failed , Error code "<<ret;
        MSPLogout(); // login fail, exit the program
    }

    demo_mic(session_begin_params);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者博客:blog.csdn.net/wwplh5520370 本程序使用的在线语音听写websocket API实现语音识别,在网络条件良好的前提下,识别速度是很快的。 使用前提:要求python3运行环境!因此请确保您的机器已经安装好python3。 建议python3.7以上的64位版本,一定要64位。不支持python2。 如需32位版本的应用程序或源码,请联系作者微信784765727。 使用步骤: 1、用文本编辑器打开create_url.py文件,修改APISecret和APIKey; 2、打开appid.txt,写入自己的appid; 3、默认ws非安全连接,可切换至wss安全连接,修改create_url.py文件中的url参数,将开头的ws改为wss即可。如果不懂就不要修改了,wss连接比ws要耗时多一点。 4、打开exe即可享受在线语音识别。若提示不支持音频设备,请尝试在左下角音频设备列表重新选择,不报错说明可以使用。 特性: 1、自动更新url:api要求生成url的时间与请求的时间相差不超过300秒,因此需要自动更新url。 2、断线自动重连: 情况1:每次接收到返回的识别结果后,客户端会主动断开连接并再次重新连接,否则再次发送语音识别请求无效。 情况2:客户端与服务器超过10秒无数据来往,服务器会主动断开连接。 情况3:建立连接后最多可维持60秒,即使与服务器有数据来往,到时间了服务器也会自动断开连接。 3、按住“录音”按钮录制音频,释放后自动发送至服务器进行识别。 4、“send”按钮发送上一次的录音。 5、“clear”按钮清空界面内容。 6、自动解析服务器返回的结果数据,显示在界面上。 特别说明:识别速度基本上取决于网络环境,网络延迟高,识别速度就慢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值