百度语音开发介绍文档:
http://yuyin.baidu.com/docs/asr#
使用语音识别,需要在百度申请一个应用,然后拿到API Key和Secret Key,然后才可以使用语音识别
ps:我的示例里面有放了一组可用的Key,但是仅供各位测试使用,有需要开发App的请自行申请,放在示例中的Key我可能随时会撤销。
ps:编译需要开启C++11的支持
ps:我写示例在OS X下是没问题的。但是当我移植到Windows下的时候,在刷新token那一步有问题,貌似和https有关,等到我想到解决方法的时候回来更新。
ps:示例里,在刷新token的地方我直接写了apikey的明文字符串,应该替换成m_apiKey,请手动替换。本页面中我已经更改了。
直接上代码
.h头文件部分
- class BaiduVop: public QObject
- {
- Q_OBJECT
- private:
- QAudioDeviceInfo m_currentDevice;
- QString m_apiKey;
- QString m_secretKey;
- QString m_token;
- QAudioInput *m_audioInput = NULL;
- QByteArray m_buf;
- QBuffer *m_buffer = NULL;
- JasonQt_Net::HTTP m_http;
- public:
- BaiduVop(const QString &apiKey, const QString &secretKey);
- void setDevice(const QAudioDeviceInfo &device);
- public slots:
- bool refreshToken(void);
- bool start(void);
- std::pair<bool, QString> finish(void);
- };
.cpp实现文件
- BaiduVop::BaiduVop(const QString &apiKey, const QString &secretKey):
- m_apiKey(apiKey),
- m_secretKey(secretKey)
- {
- const auto &&availableDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
- if(!availableDevices.isEmpty())
- {
- m_currentDevice = availableDevices.first();
- QAudioFormat format;
- format.setSampleRate(8000);
- format.setChannelCount(1);
- format.setSampleSize(16);
- format.setSampleType(QAudioFormat::SignedInt);
- format.setByteOrder(QAudioFormat::LittleEndian);
- format.setCodec("audio/pcm");
- m_audioInput = new QAudioInput(m_currentDevice, format, this);
- }
- }
- void BaiduVop::setDevice(const QAudioDeviceInfo &device)
- {
- m_currentDevice = device;
- }
- bool BaiduVop::refreshToken(void)
- {
- QNetworkRequest request(QUrl(QString("https://openapi.baidu.com/oauth/2.0/token?")));
- QByteArray append = QString("grant_type=client_credentials&client_id=%1&client_secret=%2&").arg(m_apiKey, m_secretKey).toLatin1();
- QByteArray buf;
- request.setRawHeader("Content-Type", "application/json");
- const auto &&flag = m_http.post(request, append, buf, 15000);
- if(!flag) { return false; }
- const auto &&data = QJsonDocument::fromJson(buf).object();
- if(data.isEmpty() || !data.contains("access_token")) { return false; }
- m_token = data["access_token"].toString();
- return true;
- }
- bool BaiduVop::start(void)
- {
- if(m_token.isEmpty())
- {
- qDebug("BaiduVop::start fail, Need refresh token befor start.");
- return false;
- }
- m_buffer = new QBuffer;
- m_buffer->open(QIODevice::ReadWrite);
- m_audioInput->start(m_buffer);
- return true;
- }
- std::pair<bool, QString> BaiduVop::finish(void)
- {
- m_audioInput->stop();
- const auto &sendData = m_buffer->data();
- m_buffer->deleteLater();
- QNetworkRequest request(QUrl("http://vop.baidu.com/server_api"));
- QJsonObject append;
- request.setRawHeader("Content-Type", "application/json");
- append["format"] = "pcm";
- append["rate"] = 8000;
- append["channel"] = 1;
- append["token"] = m_token;
- append["lan"] = "zh";
- append["cuid"] = "JasonQt";
- append["speech"] = QString(sendData.toBase64());
- append["len"] = sendData.size();
- QByteArray buf;
- m_http.post(request, QJsonDocument(append).toJson(), buf, 15000);
- QJsonObject acceptedData(QJsonDocument::fromJson(buf).object());
- if(buf.isEmpty() || acceptedData.isEmpty() || !acceptedData.contains("result")) { return { false, buf }; }
- const auto &&message = acceptedData["result"].toArray()[0].toString();
- return { true, message.mid(0, message.size() - 1) };
- }
其中有几个http的接口已经被我封装了,需要自行开发的请更改代码或者直接下载我的示例,里面有完整的工程。
可以到下方链接中下载
http://download.csdn.net/detail/wsj18808050/8659091
http://blog.csdn.net/wsj18808050/article/details/45478983