赞
踩
公司很多业务场景使用到了语音识别功能,当时我们的语音团队自研了语音识别模型,方案是云端模型加端侧SDK交互,端侧负责做语音采集、VAD、opus编码,实时传输给云端,云端识别后返回识别结果。这些业务场景在适配鸿蒙的过程发现HarmonyOS 原生智能中提供了本地语音识别SDK,动手封装一波。
原生语音识别能力支持两种模式:
speechRecognizer.createEngine
let asrEngine: speechRecognizer.SpeechRecognitionEngine; // 创建引擎,通过callback形式返回 // 设置创建引擎参数 let extraParam: Record<string, 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:
回调主要处理识别过程中的事件,最主要的就是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);
let audioParam: speechRecognizer.AudioInfo = {audioType: 'pcm', sampleRate: 16000, soundChannel: 1, sampleBit: 16};
let extraParam: Record<string, Object> = {"vadBegin": 2000, "vadEnd": 3000, "maxAudioDuration": 40000};
let recognizerParams: speechRecognizer.StartParams = {
sessionId: sessionId,
audioInfo: audioParam,
extraParams: extraParam
};
// 调用开始识别方法
asrEngine.startListening(recognizerParams);
主要是设置开始识别的相关参数:
[500,10000]
,不传参时默认为10000ms[500,10000]
,不传参时默认为800ms。[20000-60000]
ms,不传参时默认20000ms。[20000 - 8 * 60 * 60 * 1000]
ms。asrEngine.writeAudio(sessionId, uint8Array);
向引擎写入音频数据,可以从麦克风或者音频文件中读取音频流。
注意:音频流长度仅支持640或1280。
实时识别的场景需要从麦克风实时读取音频,写入到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引擎接口,最后基于麦克风采集数据实现了实时麦克风语音识别功能。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。