Skip to content

Commit 8847151

Browse files
authored
Add ArkTS API for Keyword spotting. (#1775)
1 parent f178e96 commit 8847151

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

harmony-os/SherpaOnnxHar/sherpa_onnx/BuildProfile.ets

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Use these variables when you tailor your ArkTS code. They must be of the const type.
33
*/
4-
export const HAR_VERSION = '1.10.40';
4+
export const HAR_VERSION = '1.10.41';
55
export const BUILD_MODE_NAME = 'debug';
66
export const DEBUG = true;
77
export const TARGET_NAME = 'default';

harmony-os/SherpaOnnxHar/sherpa_onnx/Index.ets

+5
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ export { OfflineSpeakerSegmentationPyannoteModelConfig,
5353
OfflineSpeakerDiarization,
5454
FastClusteringConfig,
5555
} from './src/main/ets/components/NonStreamingSpeakerDiarization';
56+
57+
export { KeywordSpotterConfig,
58+
KeywordSpotterResult,
59+
KeywordSpotter,
60+
} from './src/main/ets/components/KeywordSpotting';

harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/types/libsherpa_onnx/Index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ export const getOfflineSpeakerDiarizationSampleRate: (handle: object) => number;
6868
export const offlineSpeakerDiarizationProcess: (handle: object, input: object) => object;
6969
export const offlineSpeakerDiarizationProcessAsync: (handle: object, input: object, callback: object) => object;
7070
export const offlineSpeakerDiarizationSetConfig: (handle: object, config: object) => void;
71+
72+
export const createKeywordSpotter: (config: object, mgr?: object) => object;
73+
export const createKeywordStream: (handle: object, keywords?: string) => object;
74+
export const isKeywordStreamReady: (handle: object, stream: object) => boolean;
75+
export const decodeKeywordStream: (handle: object, stream: object) => void;
76+
export const resetKeywordStream: (handle: object, stream: object) => void;
77+
export const getKeywordResultAsJson: (handle: object, stream: object) => string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
createKeywordSpotter,
3+
createKeywordStream,
4+
isKeywordStreamReady,
5+
decodeKeywordStream,
6+
resetKeywordStream,
7+
getKeywordResultAsJson,
8+
} from 'libsherpa_onnx.so';
9+
10+
import { FeatureConfig } from './NonStreamingAsr';
11+
import { OnlineModelConfig, OnlineStream } from './StreamingAsr';
12+
13+
export class KeywordSpotterConfig {
14+
public featConfig: FeatureConfig = new FeatureConfig();
15+
public modelConfig: OnlineModelConfig = new OnlineModelConfig();
16+
public maxActivePaths: number = 4;
17+
public numTrailingBlanks: number = 1;
18+
public keywordsScore: number = 1;
19+
public keywordsThreshold: number = 0.25;
20+
public keywordsFile: string = '';
21+
}
22+
23+
interface KeywordSpotterResultJson {
24+
keyword: string;
25+
timestamps: number[];
26+
tokens: string[];
27+
}
28+
29+
export class KeywordSpotterResult {
30+
public keyword: string = '';
31+
public tokens: string[] = [];
32+
public timestamps: number[] = [];
33+
public json: string = '';
34+
}
35+
36+
export class KeywordSpotter {
37+
public handle: object;
38+
public config: KeywordSpotterConfig;
39+
40+
constructor(config: KeywordSpotterConfig, mgr?: object) {
41+
this.handle = createKeywordSpotter(config, mgr);
42+
this.config = config
43+
}
44+
45+
createStream(keywords?: string): OnlineStream {
46+
if (typeof keywords !== "undefined") {
47+
return new OnlineStream(createKeywordStream(this.handle, keywords));
48+
} else {
49+
return new OnlineStream(createKeywordStream(this.handle));
50+
}
51+
}
52+
53+
isReady(stream: OnlineStream): boolean {
54+
return isKeywordStreamReady(this.handle, stream.handle);
55+
}
56+
57+
decode(stream: OnlineStream) {
58+
decodeKeywordStream(this.handle, stream.handle);
59+
}
60+
61+
reset(stream: OnlineStream) {
62+
resetKeywordStream(this.handle, stream.handle);
63+
}
64+
65+
getResult(stream: OnlineStream): KeywordSpotterResult {
66+
const jsonStr: string = getKeywordResultAsJson(this.handle, stream.handle);
67+
68+
let o = JSON.parse(jsonStr) as KeywordSpotterResultJson;
69+
70+
const r = new KeywordSpotterResult()
71+
r.keyword = o.keyword
72+
r.timestamps = o.timestamps;
73+
r.tokens = o.tokens;
74+
r.json = jsonStr;
75+
76+
return r;
77+
}
78+
}

0 commit comments

Comments
 (0)