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);
}