Add VAD+ASR demo for HarmonyOS (#1573)
@@ -0,0 +1,43 @@
|
||||
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
|
||||
import hilog from '@ohos.hilog';
|
||||
import UIAbility from '@ohos.app.ability.UIAbility';
|
||||
import Want from '@ohos.app.ability.Want';
|
||||
import window from '@ohos.window';
|
||||
|
||||
export default class EntryAbility extends UIAbility {
|
||||
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage: window.WindowStage): void {
|
||||
// Main window is created, set main page for this ability
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||
|
||||
windowStage.loadContent('pages/Index', (err) => {
|
||||
if (err.code) {
|
||||
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||
return;
|
||||
}
|
||||
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageDestroy(): void {
|
||||
// Main window is destroyed, release UI related resources
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
|
||||
}
|
||||
|
||||
onForeground(): void {
|
||||
// Ability has brought to foreground
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
|
||||
}
|
||||
|
||||
onBackground(): void {
|
||||
// Ability has back to background
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
import BackupExtensionAbility, { BundleVersion } from '@ohos.application.BackupExtensionAbility';
|
||||
|
||||
export default class EntryBackupAbility extends BackupExtensionAbility {
|
||||
async onBackup() {
|
||||
hilog.info(0x0000, 'testTag', 'onBackup ok');
|
||||
}
|
||||
|
||||
async onRestore(bundleVersion: BundleVersion) {
|
||||
hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion));
|
||||
}
|
||||
}
|
||||
173
harmony-os/SherpaOnnxVadAsr/entry/src/main/ets/pages/Index.ets
Normal file
@@ -0,0 +1,173 @@
|
||||
import { LengthUnit } from '@kit.ArkUI';
|
||||
import worker, { MessageEvents } from '@ohos.worker';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { picker } from '@kit.CoreFileKit';
|
||||
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
@State currentIndex: number = 0;
|
||||
@State resultFromFile: string = '';
|
||||
@State progressForFile: number = 0;
|
||||
@State selectFileBtnEnabled: boolean = false;
|
||||
@State message: string = 'To be implemented';
|
||||
@State lang: string = 'English';
|
||||
private controller: TabsController = new TabsController();
|
||||
private workerInstance?: worker.ThreadWorker
|
||||
private readonly scriptURL: string = 'entry/ets/workers/NonStreamingAsrWithVadWorker.ets'
|
||||
|
||||
aboutToAppear(): void {
|
||||
this.workerInstance = new worker.ThreadWorker(this.scriptURL, {
|
||||
name: 'NonStreaming ASR worker'
|
||||
});
|
||||
|
||||
this.workerInstance.onmessage = (e: MessageEvents) => {
|
||||
const msgType = e.data['msgType'] as string;
|
||||
console.log(`received data ${msgType}`);
|
||||
|
||||
if (msgType == 'init-non-streaming-asr-done') {
|
||||
this.selectFileBtnEnabled = true;
|
||||
}
|
||||
|
||||
if (msgType == 'non-streaming-asr-vad-decode-done') {
|
||||
this.resultFromFile = e.data['text'] as string + '\n';
|
||||
}
|
||||
|
||||
if (msgType == 'non-streaming-asr-vad-decode-partial') {
|
||||
if (this.resultFromFile == '') {
|
||||
this.resultFromFile = e.data['text'] as string;
|
||||
} else {
|
||||
this.resultFromFile += '\n\n' + e.data['text'] as string;
|
||||
}
|
||||
}
|
||||
|
||||
if (msgType == 'non-streaming-asr-vad-decode-error') {
|
||||
this.resultFromFile = e.data['text'] as string;
|
||||
}
|
||||
|
||||
if (msgType == 'non-streaming-asr-vad-decode-progress') {
|
||||
this.progressForFile = e.data['progress'] as number;
|
||||
|
||||
this.selectFileBtnEnabled = this.progressForFile >= 100;
|
||||
}
|
||||
}
|
||||
|
||||
const context = getContext();
|
||||
this.workerInstance.postMessage({ msgType: 'init-vad', context });
|
||||
this.workerInstance.postMessage({ msgType: 'init-non-streaming-asr', context });
|
||||
}
|
||||
|
||||
@Builder
|
||||
TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
|
||||
Column() {
|
||||
Image(this.currentIndex == targetIndex ? selectedImg : normalImg)
|
||||
.size({ width: 25, height: 25 })
|
||||
Text(title)
|
||||
.fontColor(this.currentIndex == targetIndex ? '#28bff1' : '#8a8a8a')
|
||||
}
|
||||
.width('100%')
|
||||
.height(50)
|
||||
.justifyContent(FlexAlign.Center)
|
||||
.onClick(() => {
|
||||
this.currentIndex = targetIndex;
|
||||
this.controller.changeIndex(this.currentIndex);
|
||||
})
|
||||
}
|
||||
|
||||
build() {
|
||||
Column() {
|
||||
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
|
||||
TabContent() {
|
||||
Column({ space: 10 }) {
|
||||
Text('Next-gen Kaldi: VAD + ASR')
|
||||
.fontColor('#182431')
|
||||
.fontSize(25)
|
||||
.lineHeight(41)
|
||||
.fontWeight(500)
|
||||
|
||||
Button('Select .wav file ')
|
||||
.enabled(this.selectFileBtnEnabled)
|
||||
.fontSize(13)
|
||||
.width(296)
|
||||
.height(60)
|
||||
.onClick(() => {
|
||||
this.resultFromFile = '';
|
||||
this.progressForFile = 0;
|
||||
|
||||
const documentSelectOptions = new picker.DocumentSelectOptions();
|
||||
documentSelectOptions.maxSelectNumber = 1;
|
||||
documentSelectOptions.fileSuffixFilters = ['.wav'];
|
||||
const documentViewPicker = new picker.DocumentViewPicker();
|
||||
documentViewPicker.select(documentSelectOptions).then((result: Array<string>) => {
|
||||
console.log(`Result: ${result}`);
|
||||
|
||||
if (!result[0]) {
|
||||
this.resultFromFile = 'Please select a file to decode';
|
||||
this.selectFileBtnEnabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.workerInstance) {
|
||||
this.workerInstance.postMessage({
|
||||
msgType: 'non-streaming-asr-vad-decode',
|
||||
filename: result[0],
|
||||
});
|
||||
} else {
|
||||
console.log(`this worker instance is undefined ${this.workerInstance}`);
|
||||
}
|
||||
}).catch((err: BusinessError) => {
|
||||
console.error(`Failed to select file, code is ${err.code}, message is ${err.message}`);
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Text(`Supported languages: ${this.lang}`)
|
||||
|
||||
if (this.progressForFile > 0) {
|
||||
Row() {
|
||||
Progress({ value: 0, total: 100, type: ProgressType.Capsule })
|
||||
.width('80%')
|
||||
.height(20)
|
||||
.value(this.progressForFile);
|
||||
|
||||
Text(`${this.progressForFile.toFixed(2)}%`).width('15%')
|
||||
}.width('100%').justifyContent(FlexAlign.Center)
|
||||
}
|
||||
|
||||
TextArea({ text: this.resultFromFile }).width('100%').lineSpacing({ value: 10, unit: LengthUnit.VP });
|
||||
|
||||
}
|
||||
.alignItems(HorizontalAlign.Center)
|
||||
.justifyContent(FlexAlign.Start)
|
||||
}.tabBar(this.TabBuilder('From file', 0, $r('app.media.icon_doc'), $r('app.media.icon_doc_default')))
|
||||
|
||||
TabContent() {
|
||||
Column() {
|
||||
Text(this.message)
|
||||
.fontSize(50)
|
||||
.fontWeight(FontWeight.Bold);
|
||||
}
|
||||
}
|
||||
.tabBar(this.TabBuilder('From mic', 1, $r('app.media.ic_public_input_voice'),
|
||||
$r('app.media.ic_public_input_voice_default')))
|
||||
|
||||
TabContent() {
|
||||
Column() {
|
||||
Text("Everything is open-sourced");
|
||||
Divider();
|
||||
Text("It runs locally, without accessing the network");
|
||||
Divider();
|
||||
Text("See also https://github.com/k2-fsa/sherpa-onnx");
|
||||
Divider();
|
||||
Text("and https://k2-fsa.github.io/sherpa/social-groups.html");
|
||||
}.justifyContent(FlexAlign.Start)
|
||||
}.tabBar(this.TabBuilder('Help', 2, $r('app.media.info_circle'),
|
||||
$r('app.media.info_circle_default')))
|
||||
|
||||
}.scrollable(false)
|
||||
}
|
||||
.width('100%')
|
||||
.justifyContent(FlexAlign.Start)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
// Please keep in sync with
|
||||
// https://github.com/k2-fsa/sherpa-onnx/blob/master/sherpa-onnx/kotlin-api/OfflineRecognizer.kt#L184
|
||||
|
||||
import { OfflineModelConfig } from 'sherpa_onnx';
|
||||
|
||||
export function getOfflineModelConfig(type: number): OfflineModelConfig {
|
||||
const c = new OfflineModelConfig();
|
||||
switch (type) {
|
||||
case 0: {
|
||||
const modelDir = 'sherpa-onnx-paraformer-zh-2023-09-14'
|
||||
c.paraformer.model = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "paraformer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 1: {
|
||||
const modelDir = 'icefall-asr-multidataset-pruned_transducer_stateless7-2023-05-04'
|
||||
c.transducer.encoder = `$modelDir}/encoder-epoch-30-avg-4.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-30-avg-4.onnx`;
|
||||
c.transducer.encoder = `${modelDir}/joiner-epoch-30-avg-4.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: {
|
||||
const modelDir = 'sherpa-onnx-whisper-tiny.en';
|
||||
c.whisper.encoder = `${modelDir}/tiny.en-encoder.int8.onnx`;
|
||||
c.whisper.decoder = `${modelDir}/tiny.en-decoder.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tiny.en-tokens.txt`;
|
||||
c.modelType = "whisper";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: {
|
||||
const modelDir = 'sherpa-onnx-whisper-base.en';
|
||||
c.whisper.encoder = `${modelDir}/base.en-encoder.int8.onnx`;
|
||||
c.whisper.decoder = `${modelDir}/base.en-decoder.int8.onnx`;
|
||||
c.tokens = `${modelDir}/base.en-tokens.txt`;
|
||||
c.modelType = "whisper";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 4: {
|
||||
const modelDir = "icefall-asr-zipformer-wenetspeech-20230615";
|
||||
c.transducer.encoder = `${modelDir}/encoder-epoch-12-avg-4.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-12-avg-4.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner-epoch-12-avg-4.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 5: {
|
||||
const modelDir = "sherpa-onnx-zipformer-multi-zh-hans-2023-9-2";
|
||||
c.transducer.encoder = `${modelDir}/encoder-epoch-20-avg-1.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-20-avg-1.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner-epoch-20-avg-1.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 6: {
|
||||
const modelDir = "sherpa-onnx-nemo-ctc-en-citrinet-512";
|
||||
c.nemoCtc.model = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 7: {
|
||||
const modelDir = "sherpa-onnx-nemo-fast-conformer-ctc-be-de-en-es-fr-hr-it-pl-ru-uk-20k"
|
||||
c.nemoCtc.model = `${modelDir}/model.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 8: {
|
||||
const modelDir = "sherpa-onnx-nemo-fast-conformer-ctc-en-24500"
|
||||
c.nemoCtc.model = `${modelDir}/model.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 9: {
|
||||
const modelDir = "sherpa-onnx-nemo-fast-conformer-ctc-en-de-es-fr-14288"
|
||||
c.nemoCtc.model = `${modelDir}/model.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 10: {
|
||||
const modelDir = "sherpa-onnx-nemo-fast-conformer-ctc-es-1424"
|
||||
c.nemoCtc.model = `${modelDir}/model.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 11: {
|
||||
const modelDir = "sherpa-onnx-telespeech-ctc-int8-zh-2024-06-04"
|
||||
c.telespeechCtc = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "telespeech_ctc";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 12: {
|
||||
const modelDir = "sherpa-onnx-zipformer-thai-2024-06-20"
|
||||
c.transducer.encoder = `${modelDir}/encoder-epoch-12-avg-5.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-12-avg-5.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner-epoch-12-avg-5.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 13: {
|
||||
const modelDir = "sherpa-onnx-zipformer-korean-2024-06-24";
|
||||
c.transducer.encoder = `${modelDir}/encoder-epoch-99-avg-1.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-99-avg-1.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner-epoch-99-avg-1.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 14: {
|
||||
const modelDir = "sherpa-onnx-paraformer-zh-small-2024-03-09";
|
||||
c.paraformer.model = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "paraformer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 15: {
|
||||
const modelDir = "sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17";
|
||||
c.senseVoice.model = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 16: {
|
||||
const modelDir = "sherpa-onnx-zipformer-ja-reazonspeech-2024-08-01";
|
||||
c.transducer.encoder = `${modelDir}/encoder-epoch-99-avg-1.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder-epoch-99-avg-1.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner-epoch-99-avg-1.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 17: {
|
||||
const modelDir = "sherpa-onnx-zipformer-ru-2024-09-18";
|
||||
c.transducer.encoder = `${modelDir}/encoder.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 18: {
|
||||
const modelDir = "sherpa-onnx-small-zipformer-ru-2024-09-18";
|
||||
c.transducer.encoder = `${modelDir}/encoder.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 19: {
|
||||
const modelDir = "sherpa-onnx-nemo-ctc-giga-am-russian-2024-10-24";
|
||||
c.nemoCtc.model = `${modelDir}/model.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 20: {
|
||||
const modelDir = "sherpa-onnx-nemo-transducer-giga-am-russian-2024-10-24";
|
||||
c.transducer.encoder = `${modelDir}/encoder.int8.onnx`;
|
||||
c.transducer.decoder = `${modelDir}/decoder.onnx`;
|
||||
c.transducer.joiner = `${modelDir}/joiner.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
c.modelType = "nemo_transducer";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 21: {
|
||||
const modelDir = "sherpa-onnx-moonshine-tiny-en-int8";
|
||||
c.moonshine.preprocessor = `${modelDir}/preprocess.onnx`;
|
||||
c.moonshine.encoder = `${modelDir}/encode.int8.onnx`;
|
||||
c.moonshine.uncachedDecoder = `${modelDir}/uncached_decode.int8.onnx`;
|
||||
c.moonshine.cachedDecoder = `${modelDir}/cached_decode.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 22: {
|
||||
const modelDir = "sherpa-onnx-moonshine-base-en-int8";
|
||||
c.moonshine.preprocessor = `${modelDir}/preprocess.onnx`;
|
||||
c.moonshine.encoder = `${modelDir}/encode.int8.onnx`;
|
||||
c.moonshine.uncachedDecoder = `${modelDir}/uncached_decode.int8.onnx`;
|
||||
c.moonshine.cachedDecoder = `${modelDir}/cached_decode.int8.onnx`;
|
||||
c.tokens = `${modelDir}/tokens.txt`;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Please specify a supported type. Given type ${type}`);
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
|
||||
import {
|
||||
OfflineRecognizer,
|
||||
OfflineRecognizerConfig,
|
||||
readWaveFromBinary,
|
||||
SileroVadConfig,
|
||||
Vad,
|
||||
VadConfig,
|
||||
} from 'sherpa_onnx';
|
||||
import { Context } from '@kit.AbilityKit';
|
||||
import { fileIo } from '@kit.CoreFileKit';
|
||||
import { getOfflineModelConfig } from '../pages/NonStreamingAsrModels';
|
||||
|
||||
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
|
||||
|
||||
let recognizer: OfflineRecognizer;
|
||||
let vad: Vad; // vad for decoding files
|
||||
|
||||
function initVad(context: Context): Vad {
|
||||
let mgr = context.resourceManager;
|
||||
const config = new VadConfig(
|
||||
new SileroVadConfig(
|
||||
'silero_vad.onnx',
|
||||
0.5,
|
||||
0.25,
|
||||
0.5,
|
||||
512,
|
||||
),
|
||||
16000,
|
||||
true,
|
||||
1,
|
||||
);
|
||||
|
||||
const bufferSizeInSeconds = 60;
|
||||
return new Vad(config, bufferSizeInSeconds, mgr);
|
||||
}
|
||||
|
||||
function initNonStreamingAsr(context: Context): OfflineRecognizer {
|
||||
let mgr = context.resourceManager;
|
||||
const config = new OfflineRecognizerConfig();
|
||||
|
||||
// Note that you can switch to a new model by changing type
|
||||
//
|
||||
// If you use type = 2, which means you will use
|
||||
// sherpa-onnx-whisper-tiny.en
|
||||
// we assume you have the following folder structure in you resources/rawfile
|
||||
/*
|
||||
(py38) fangjuns-MacBook-Pro:main fangjun$ pwd
|
||||
/Users/fangjun/open-source/sherpa-onnx/harmony-os/SherpaOnnxVadAsr/entry/src/main
|
||||
(py38) fangjuns-MacBook-Pro:main fangjun$ tree resources/rawfile/
|
||||
resources/rawfile/
|
||||
├── sherpa-onnx-whisper-tiny.en
|
||||
│ ├── README.md
|
||||
│ ├── tiny.en-decoder.int8.onnx
|
||||
│ ├── tiny.en-encoder.int8.onnx
|
||||
│ └── tiny.en-tokens.txt
|
||||
└── silero_vad.onnx
|
||||
|
||||
1 directory, 5 files
|
||||
*/
|
||||
const type = 2;
|
||||
config.modelConfig = getOfflineModelConfig(type);
|
||||
config.modelConfig.debug = true;
|
||||
return new OfflineRecognizer(config, mgr)
|
||||
}
|
||||
|
||||
function decode(filename: string): string {
|
||||
vad.reset();
|
||||
|
||||
const fp = fileIo.openSync(filename);
|
||||
const stat = fileIo.statSync(fp.fd);
|
||||
const arrayBuffer = new ArrayBuffer(stat.size);
|
||||
fileIo.readSync(fp.fd, arrayBuffer);
|
||||
const data = new Uint8Array(arrayBuffer);
|
||||
|
||||
const wave = readWaveFromBinary(data);
|
||||
|
||||
console.log(`sample rate ${wave.sampleRate}`);
|
||||
console.log(`samples length ${wave.samples.length}`);
|
||||
const resultList: string[] = [];
|
||||
|
||||
const windowSize = vad.config.sileroVad.windowSize;
|
||||
for (let i = 0; i < wave.samples.length; i += windowSize) {
|
||||
const thisWindow = wave.samples.subarray(i, i + windowSize)
|
||||
vad.acceptWaveform(thisWindow);
|
||||
if (i + windowSize >= wave.samples.length) {
|
||||
vad.flush();
|
||||
}
|
||||
while (!vad.isEmpty()) {
|
||||
const segment = vad.front();
|
||||
const _startTime = (segment.start / wave.sampleRate);
|
||||
const _endTime = _startTime + segment.samples.length / wave.sampleRate;
|
||||
|
||||
if (_endTime - _startTime < 0.2) {
|
||||
vad.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
const startTime = _startTime.toFixed(2);
|
||||
const endTime = _endTime.toFixed(2);
|
||||
|
||||
const progress = (segment.start + segment.samples.length) / wave.samples.length * 100;
|
||||
|
||||
workerPort.postMessage({ 'msgType': 'non-streaming-asr-vad-decode-progress', progress });
|
||||
|
||||
const stream = recognizer.createStream();
|
||||
stream.acceptWaveform({ samples: segment.samples, sampleRate: wave.sampleRate });
|
||||
recognizer.decode(stream);
|
||||
const result = recognizer.getResult(stream);
|
||||
|
||||
const text = `${startTime} -- ${endTime} ${result.text}`
|
||||
resultList.push(text);
|
||||
console.log(`partial result ${text}`);
|
||||
|
||||
workerPort.postMessage({ 'msgType': 'non-streaming-asr-vad-decode-partial', text });
|
||||
|
||||
vad.pop();
|
||||
}
|
||||
}
|
||||
|
||||
return resultList.join('\n\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e message data
|
||||
*/
|
||||
workerPort.onmessage = (e: MessageEvents) => {
|
||||
const msgType = e.data['msgType'] as string;
|
||||
console.log(`msg-type: ${msgType}`)
|
||||
if (msgType == 'init-vad' && !vad) {
|
||||
const context = e.data['context'] as Context;
|
||||
vad = initVad(context);
|
||||
console.log('init vad done');
|
||||
workerPort.postMessage({ 'msgType': 'init-vad-done' });
|
||||
}
|
||||
|
||||
if (msgType == 'init-non-streaming-asr' && !recognizer) {
|
||||
const context = e.data['context'] as Context;
|
||||
recognizer = initNonStreamingAsr(context);
|
||||
console.log('init non streaming ASR done');
|
||||
workerPort.postMessage({ 'msgType': 'init-non-streaming-asr-done' });
|
||||
}
|
||||
|
||||
if (msgType == 'non-streaming-asr-vad-decode') {
|
||||
const filename = e.data['filename'] as string;
|
||||
console.log(`decoding ${filename}`);
|
||||
try {
|
||||
const text = decode(filename);
|
||||
workerPort.postMessage({ msgType: 'non-streaming-asr-vad-decode-done', text });
|
||||
} catch (e) {
|
||||
workerPort.postMessage({ msgType: 'non-streaming-asr-vad-decode-error', text: `Failed to decode ${filename}` });
|
||||
}
|
||||
|
||||
workerPort.postMessage({ 'msgType': 'non-streaming-asr-vad-decode-progress', progress: 100 });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e message data
|
||||
*/
|
||||
workerPort.onmessageerror = (e: MessageEvents) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the event handler to be called when an exception occurs during worker execution.
|
||||
* The event handler is executed in the worker thread.
|
||||
*
|
||||
* @param e error message
|
||||
*/
|
||||
workerPort.onerror = (e: ErrorEvent) => {
|
||||
}
|
||||
52
harmony-os/SherpaOnnxVadAsr/entry/src/main/module.json5
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "entry",
|
||||
"type": "entry",
|
||||
"description": "$string:module_desc",
|
||||
"mainElement": "EntryAbility",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet",
|
||||
"2in1"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false,
|
||||
"pages": "$profile:main_pages",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "EntryAbility",
|
||||
"srcEntry": "./ets/entryability/EntryAbility.ets",
|
||||
"description": "$string:EntryAbility_desc",
|
||||
"icon": "$media:layered_image",
|
||||
"label": "$string:EntryAbility_label",
|
||||
"startWindowIcon": "$media:startIcon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"exported": true,
|
||||
"skills": [
|
||||
{
|
||||
"entities": [
|
||||
"entity.system.home"
|
||||
],
|
||||
"actions": [
|
||||
"action.system.home"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"extensionAbilities": [
|
||||
{
|
||||
"name": "EntryBackupAbility",
|
||||
"srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
|
||||
"type": "backup",
|
||||
"exported": false,
|
||||
"metadata": [
|
||||
{
|
||||
"name": "ohos.extension.backup",
|
||||
"resource": "$profile:backup_config"
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "VAD+ASR with Next-gen Kaldi"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "VAD+ASR"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "VAD_ASR"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 474 B |
|
After Width: | Height: | Size: 438 B |
|
After Width: | Height: | Size: 295 B |
|
After Width: | Height: | Size: 295 B |
|
After Width: | Height: | Size: 636 B |
|
After Width: | Height: | Size: 636 B |
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"layered-image":
|
||||
{
|
||||
"background" : "$media:background",
|
||||
"foreground" : "$media:foreground"
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 20 KiB |
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"allowToBackupRestore": true
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"src": [
|
||||
"pages/Index"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "module description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "module_desc",
|
||||
"value": "模块描述"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_desc",
|
||||
"value": "description"
|
||||
},
|
||||
{
|
||||
"name": "EntryAbility_label",
|
||||
"value": "label"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import hilog from '@ohos.hilog';
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
|
||||
export default function abilityTest() {
|
||||
describe('ActsAbilityTest', () => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
})
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
})
|
||||
it('assertContain', 0, () => {
|
||||
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
|
||||
let a = 'abc';
|
||||
let b = 'b';
|
||||
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
expect(a).assertContain(b);
|
||||
expect(a).assertEqual(a);
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import abilityTest from './Ability.test';
|
||||
|
||||
export default function testsuite() {
|
||||
abilityTest();
|
||||
}
|
||||
13
harmony-os/SherpaOnnxVadAsr/entry/src/ohosTest/module.json5
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "entry_test",
|
||||
"type": "feature",
|
||||
"deviceTypes": [
|
||||
"phone",
|
||||
"tablet",
|
||||
"2in1"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false
|
||||
}
|
||||
}
|
||||
5
harmony-os/SherpaOnnxVadAsr/entry/src/test/List.test.ets
Normal file
@@ -0,0 +1,5 @@
|
||||
import localUnitTest from './LocalUnit.test';
|
||||
|
||||
export default function testsuite() {
|
||||
localUnitTest();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
|
||||
export default function localUnitTest() {
|
||||
describe('localUnitTest', () => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
it('assertContain', 0, () => {
|
||||
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
let a = 'abc';
|
||||
let b = 'b';
|
||||
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
expect(a).assertContain(b);
|
||||
expect(a).assertEqual(a);
|
||||
});
|
||||
});
|
||||
}
|
||||