|
| 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