Fangjun Kuang
Committed by GitHub

Add ArkTS API for Keyword spotting. (#1775)

1 /** 1 /**
2 * Use these variables when you tailor your ArkTS code. They must be of the const type. 2 * Use these variables when you tailor your ArkTS code. They must be of the const type.
3 */ 3 */
4 -export const HAR_VERSION = '1.10.40'; 4 +export const HAR_VERSION = '1.10.41';
5 export const BUILD_MODE_NAME = 'debug'; 5 export const BUILD_MODE_NAME = 'debug';
6 export const DEBUG = true; 6 export const DEBUG = true;
7 export const TARGET_NAME = 'default'; 7 export const TARGET_NAME = 'default';
@@ -53,3 +53,8 @@ export { OfflineSpeakerSegmentationPyannoteModelConfig, @@ -53,3 +53,8 @@ export { OfflineSpeakerSegmentationPyannoteModelConfig,
53 OfflineSpeakerDiarization, 53 OfflineSpeakerDiarization,
54 FastClusteringConfig, 54 FastClusteringConfig,
55 } from './src/main/ets/components/NonStreamingSpeakerDiarization'; 55 } from './src/main/ets/components/NonStreamingSpeakerDiarization';
  56 +
  57 +export { KeywordSpotterConfig,
  58 + KeywordSpotterResult,
  59 + KeywordSpotter,
  60 +} from './src/main/ets/components/KeywordSpotting';
@@ -68,3 +68,10 @@ export const getOfflineSpeakerDiarizationSampleRate: (handle: object) => number; @@ -68,3 +68,10 @@ export const getOfflineSpeakerDiarizationSampleRate: (handle: object) => number;
68 export const offlineSpeakerDiarizationProcess: (handle: object, input: object) => object; 68 export const offlineSpeakerDiarizationProcess: (handle: object, input: object) => object;
69 export const offlineSpeakerDiarizationProcessAsync: (handle: object, input: object, callback: object) => object; 69 export const offlineSpeakerDiarizationProcessAsync: (handle: object, input: object, callback: object) => object;
70 export const offlineSpeakerDiarizationSetConfig: (handle: object, config: object) => void; 70 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;
  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 +}