做网站的主要内容,00908网络营销与策划,建设网站女装名字大全,产品市场营销策划书HarmonyOS 原生智能之语音识别实战
背景
公司很多业务场景使用到了语音识别功能#xff0c;当时我们的语音团队自研了语音识别模型#xff0c;方案是云端模型加端侧SDK交互#xff0c;端侧负责做语音采集、VAD、opus编码#xff0c;实时传输给云端#xff0c;云端识别后…HarmonyOS 原生智能之语音识别实战
背景
公司很多业务场景使用到了语音识别功能当时我们的语音团队自研了语音识别模型方案是云端模型加端侧SDK交互端侧负责做语音采集、VAD、opus编码实时传输给云端云端识别后返回识别结果。这些业务场景在适配鸿蒙的过程发现HarmonyOS 原生智能中提供了本地语音识别SDK动手封装一波。
场景介绍
原生语音识别能力支持两种模式
短语音模式不超过60s长语音模式不超过8h
API接口介绍
1. 引擎初始化
speechRecognizer.createEngine
let asrEngine: speechRecognizer.SpeechRecognitionEngine;
// 创建引擎通过callback形式返回
// 设置创建引擎参数
let extraParam: Recordstring, Object {locate: CN, recognizerMode: short};
let initParamsInfo: speechRecognizer.CreateEngineParams {language: zh-CN,online: 1,extraParams: extraParam
};
// 调用createEngine方法
speechRecognizer.createEngine(initParamsInfo, (err: BusinessError, speechRecognitionEngine: speechRecognizer.SpeechRecognitionEngine) {if (!err) {console.info(Succeeded in creating engine.);// 接收创建引擎的实例asrEngine speechRecognitionEngine;} else {// 无法创建引擎时返回错误码1002200008原因引擎正在销毁中console.error(Failed to create engine. Code: ${err.code}, message: ${err.message}.);}
});主要是需要构建引擎参数speechRecognizer.CreateEngineParams
language语言online模式1为离线目前只支持离线引擎extraParams区域信息等 locate区域信息可选不设置时默认为“CN”当前仅支持“CN”recognizerMode识别模式包含短语音short与场语音long 回调中可以查看错误信息
无法创建引擎时返回错误码1002200001原因语种不支持、模式不支持、初始化超时、资源不存在等导致创建引擎失败无法创建引擎时返回错误码1002200006原因引擎正在忙碌中一般多个应用同时调用语音识别引擎时触发无法创建引擎时返回错误码1002200008原因引擎正在销毁中
2、设置RecognitionListener回调
回调主要处理识别过程中的事件最主要的就是onResult处理识别内容不同的对话对应不同的sessionId
// 创建回调对象
let setListener: speechRecognizer.RecognitionListener {// 开始识别成功回调onStart(sessionId: string, eventMessage: string) {},// 事件回调onEvent(sessionId: string, eventCode: number, eventMessage: string) {},// 识别结果回调包括中间结果和最终结果onResult(sessionId: string, result: speechRecognizer.SpeechRecognitionResult) {},// 识别完成回调onComplete(sessionId: string, eventMessage: string) {},// 错误回调错误码通过本方法返回,如返回错误码1002200006识别引擎正忙引擎正在识别中onError(sessionId: string, errorCode: number, errorMessage: string) {}
}
// 设置回调
asrEngine.setListener(setListener);3、开始识别
let audioParam: speechRecognizer.AudioInfo {audioType: pcm, sampleRate: 16000, soundChannel: 1, sampleBit: 16};
let extraParam: Recordstring, Object {vadBegin: 2000, vadEnd: 3000, maxAudioDuration: 40000};
let recognizerParams: speechRecognizer.StartParams {sessionId: sessionId,audioInfo: audioParam,extraParams: extraParam
};
// 调用开始识别方法
asrEngine.startListening(recognizerParams);主要是设置开始识别的相关参数
sessionId会话id与onResult回调中的sessionId要对应audioInfo音频配置信息可选 audioType目前只支持PCM如果要识别MP3文件等需要解码后再传给引擎sampleRate音频的采样率当前仅支持16000采样率sampleBit音频返回的采样位数当前仅支持16位soundChannel音频返回的通道数信息当前仅支持通道1extraParams音频的压缩率pcm格式音频默认为0 extraParams额外配置信息主要包含 recognitionMode实时语音识别模式不传时默认为1 0实时录音识别需应用开启录音权限ohos.permission.MICROPHONE若需结束录音则调用finish方法1实时音频转文字识别开启此模式时需要额外调用writeAudio方法传入待识别音频流 vadBeginVoice Activity Detection(VAD)前端点设置参数范围是[500,10000]不传参时默认为10000msvadEndVoice Activity Detection(VAD)后端点设置。参数范围是[500,10000]不传参时默认为800ms。maxAudioDuration最大支持音频时长 短语音模式支持范围[20000-60000]ms不传参时默认20000ms。长语音模式支持范围[20000 - 8 * 60 * 60 * 1000]ms。 VAD作用主要是语音活动检测对静音数据不进行识别
4、传入音频流
asrEngine.writeAudio(sessionId, uint8Array);向引擎写入音频数据可以从麦克风或者音频文件中读取音频流。 注意音频流长度仅支持640或1280。
5、其他接口
listLanguages查询语音识别服务支持的语种信息finish结束识别取消识别cancelshutdown释放识别引起资源
最佳实践
实时识别的场景需要从麦克风实时读取音频写入到asrEngine在onResult回调中获取识别结果。 配置音频采集参数并创建AudioCapturer实例 import { audio } from kit.AudioKit;let audioStreamInfo: audio.AudioStreamInfo {samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, // 采样率channels: audio.AudioChannel.CHANNEL_1, // 通道sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式};let audioCapturerInfo: audio.AudioCapturerInfo {source: audio.SourceType.SOURCE_TYPE_MIC,capturerFlags: 0};let audioCapturerOptions: audio.AudioCapturerOptions {streamInfo: audioStreamInfo,capturerInfo: audioCapturerInfo};audio.createAudioCapturer(audioCapturerOptions, (err, data) {if (err) {console.error(Invoke createAudioCapturer failed, code is ${err.code}, message is ${err.message});} else {console.info(Invoke createAudioCapturer succeeded.);let audioCapturer data;}});这里注意采样率和声道以及采样位数要符合ASR引擎要求16k采样、单声道、16位采样位数。 接着调用on(‘readData’)方法订阅监听音频数据读入回调 import { BusinessError } from kit.BasicServicesKit;import { fileIo } from kit.CoreFileKit;let bufferSize: number 0;class Options {offset?: number;length?: number;}let readDataCallback (buffer: ArrayBuffer) {//将buffer写入asr引擎asrEngine.writeAudio(sessionId, new Uint8Array(buffer));}audioCapturer.on(readData, readDataCallback);这里注意写入buffer的大小显示ASR只支持640或1280。
总结
本文介绍了 HarmonyOS 官方提供的语音识别能力详解介绍了ASR引擎接口最后基于麦克风采集数据实现了实时麦克风语音识别功能。