使用实时语音识别
前提条件
- 确保已按照配置CPP环境(Windows)配置完毕。
- 请参考SDK(websocket)获取最新版本SDK包。
初始化Client
初始化RasrClient,其参数包括AuthInfo。
请求参数
请求类为RasrRequest,详见表 RasrRequest。
|
参数名称 |
是否必选 |
参数类型 |
描述 |
|---|---|---|---|
|
audioFormat |
是 |
String |
音频格式,支持pcm等,如pcm8k16bit,参见《API参考》中开始识别章节。 |
|
property |
是 |
String |
属性字符串,language_sampleRate_domain, 如chinese_8k_common,参见《API参考》中开始识别章节。 |
通过set方法可以设置具体参数,详见表 RasrRequest设置参数
|
方法名称 |
是否必选 |
参数类型 |
描述 |
|---|---|---|---|
|
SetPunc |
否 |
String |
表示是否在识别结果中添加标点,取值为yes 、 no,默认no。 |
|
SetDigitNorm |
否 |
String |
表示是否将语音中的数字识别为阿拉伯数字,取值为yes 、 no,默认为yes。 |
|
SetVadHead |
否 |
Integer |
头部最大静音时间,[0, 60000],默认10000ms。 |
|
SetVadTail |
否 |
Integer |
尾部最大静音时间,[0, 3000],默认500ms。 |
|
SetMaxSeconds |
否 |
Integer |
音频最长持续时间, [1, 60],默认30s。 |
|
SetIntermediateResult |
否 |
String |
是否显示中间结果,yes 或 no,默认no。 |
|
SetVocabularyId |
否 |
String |
热词表id,若没有则不填。 |
|
SetNeedWordInfo |
否 |
String |
表示是否在识别结果中输出分词结果信息,取值为“yes”和“no”,默认为“no”。 |
示例代码
如下示例仅供参考,最新代码请前往SDK(websocket)章节获取并运行。
#include <iostream>
#include "RasrClient.h"
#include "RasrRequest.h"
#include "IoUtil.h"
void OnConnect() {
std::cout << "now rasr Connect success" << std::endl;
}
void OnStart(std::string text) {
std::cout << "now rasr receive start response: " << text << std::endl;
}
void OnResp(std::string text) {
// text encoded by utf-8 contains chinese character, which will cause error code. So we should convert to ansi
// cout << "rasr receive " << text << endl;
std::cout << "now rasr receive " << Utf8ToAnsi(text) << std::endl;
}
void OnEnd(std::string text) {
std::cout << "now rasr receive end response: " << text << std::endl;
}
void OnClose() {
std::cout << "now rasr receive Close" << std::endl;
}
void OnError(string text) {
std::cout << "now rasr receive error: " << text << std::endl;
}
void OnEvent(string text) {
std::cout << "now rasr receive event: " << text << std::endl;
}
void RasrTest() {
// 1. config parameter
// 1.1 init authInfo
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
// 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
std::string ak = GetEnv("HUAWEICLOUD_SDK_AK");
std::string sk = GetEnv("HUAWEICLOUD_SDK_SK");
string region = "";
string projectId = "";
AuthInfo authInfo(ak, sk, region, projectId);
// 1.2 config Connect parameter
HttpConfig httpConfig;
httpConfig.SetReadTimeout(20000);
httpConfig.SetConnectTimeout(20000);
// 1.3 config callback, callback function are optional, if not set, it will use function in RasrListener
WebsocketService::ptr websocketServicePtr = websocketpp::lib::make_shared<WebsocketService>();
websocketServicePtr->SetOnConnectFunc(OnConnect); // Connect success callback
websocketServicePtr->SetOnStartFunc(OnStart); // receive start response callback
websocketServicePtr->SetOnRespFunc(OnResp); // receive transcribe result callback
websocketServicePtr->SetOnEndFunc(OnEnd); // receive end response callback
websocketServicePtr->SetOnCloseFunc(OnClose); // Close callback
websocketServicePtr->SetOnEventFunc(OnEvent); // receive event callback
websocketServicePtr->SetOnErrorFunc(OnError); // receive error callback
// 1.4 config request parameter
RasrRequest request("pcm16k16bit", "chinese_16k_general");
request.SetIntermediateResult("no");
// 2. init client
RasrClient* rasrClient = new RasrClient(authInfo, websocketServicePtr, httpConfig);
// 3. create connection :ContinueStreamConnect/ShortStreamConnect/SentenceStreamConnect
rasrClient->ContinueStreamConnect();
// 4. send start
rasrClient->SendStart(request);
// 5. send binary audio. (filePtr, fileLength, byteLen, SleepTime ). If the audio is generated by recording, then it should set sleep time 0.
int fileLength;
std::string filePath = "../sisCppSdkDemo/123.wav";
unsigned char* buff = ReadBinary(filePath, &fileLength);
if (buff == nullptr) {
cout << filePath << " read file failed";
rasrClient->Close();
delete rasrClient;
return;
}
rasrClient->SendBinary(buff, fileLength, 3200, 50);
// 6. send end
rasrClient->SendEnd();
// 7. close
rasrClient->Close();
delete[] buff;
delete rasrClient;
}
int main() {
RasrTest();
return 0;
}