-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keyword spotting API for node-addon-api (#877)
- Loading branch information
1 parent
75630b9
commit 03c956a
Showing
18 changed files
with
492 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
const sherpa_onnx = require('sherpa-onnx-node'); | ||
const performance = require('perf_hooks').performance; | ||
|
||
|
||
// Please download test files from | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/kws-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'transducer': { | ||
'encoder': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
'decoder': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
'joiner': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
}, | ||
'tokens': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt', | ||
'numThreads': 1, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
}, | ||
'keywordsFile': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/test_keywords.txt', | ||
}; | ||
|
||
const waveFilename = | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav'; | ||
|
||
const kws = new sherpa_onnx.KeywordSpotter(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = kws.createStream(); | ||
const wave = sherpa_onnx.readWave(waveFilename); | ||
stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples}); | ||
|
||
const tailPadding = new Float32Array(wave.sampleRate * 0.4); | ||
stream.acceptWaveform({samples: tailPadding, sampleRate: wave.sampleRate}); | ||
|
||
const detectedKeywords = []; | ||
while (kws.isReady(stream)) { | ||
const keyword = kws.getResult(stream).keyword; | ||
if (keyword != '') { | ||
detectedKeywords.push(keyword); | ||
} | ||
kws.decode(stream); | ||
} | ||
let stop = performance.now(); | ||
|
||
console.log('Done') | ||
|
||
const elapsed_seconds = (stop - start) / 1000; | ||
const duration = wave.samples.length / wave.sampleRate; | ||
const real_time_factor = elapsed_seconds / duration; | ||
console.log('Wave duration', duration.toFixed(3), 'secodns') | ||
console.log('Elapsed', elapsed_seconds.toFixed(3), 'secodns') | ||
console.log( | ||
`RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`, | ||
real_time_factor.toFixed(3)) | ||
console.log(waveFilename) | ||
console.log('result\n', detectedKeywords) |
74 changes: 74 additions & 0 deletions
74
nodejs-addon-examples/test_keyword_spotter_transducer_microphone.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2023-2024 Xiaomi Corporation (authors: Fangjun Kuang) | ||
// | ||
const portAudio = require('naudiodon2'); | ||
// console.log(portAudio.getDevices()); | ||
|
||
const sherpa_onnx = require('sherpa-onnx-node'); | ||
|
||
function createKeywordSpotter() { | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'transducer': { | ||
'encoder': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
'decoder': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
'joiner': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx', | ||
}, | ||
'tokens': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
}, | ||
'keywordsFile': | ||
'./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/keywords.txt', | ||
}; | ||
|
||
return new sherpa_onnx.KeywordSpotter(config); | ||
} | ||
|
||
const kws = createKeywordSpotter(); | ||
const stream = kws.createStream(); | ||
|
||
let lastText = ''; | ||
let segmentIndex = 0; | ||
|
||
const ai = new portAudio.AudioIO({ | ||
inOptions: { | ||
channelCount: 1, | ||
closeOnError: true, // Close the stream if an audio error is detected, if | ||
// set false then just log the error | ||
deviceId: -1, // Use -1 or omit the deviceId to select the default device | ||
sampleFormat: portAudio.SampleFormatFloat32, | ||
sampleRate: kws.config.featConfig.sampleRate | ||
} | ||
}); | ||
|
||
const display = new sherpa_onnx.Display(50); | ||
|
||
ai.on('data', data => { | ||
const samples = new Float32Array(data.buffer); | ||
|
||
stream.acceptWaveform( | ||
{sampleRate: kws.config.featConfig.sampleRate, samples: samples}); | ||
|
||
while (kws.isReady(stream)) { | ||
kws.decode(stream); | ||
} | ||
|
||
const keyword = kws.getResult(stream).keyword | ||
if (keyword != '') { | ||
display.print(segmentIndex, keyword); | ||
segmentIndex += 1; | ||
} | ||
}); | ||
|
||
ai.start(); | ||
console.log('Started! Please speak.') | ||
console.log(`Only words from ${kws.config.keywordsFile} can be recognized`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const addon = require('./addon.js'); | ||
const streaming_asr = require('./streaming-asr.js'); | ||
|
||
class KeywordSpotter { | ||
constructor(config) { | ||
this.handle = addon.createKeywordSpotter(config); | ||
this.config = config | ||
} | ||
|
||
createStream() { | ||
const handle = addon.createKeywordStream(this.handle); | ||
return new streaming_asr.OnlineStream(handle); | ||
} | ||
|
||
isReady(stream) { | ||
return addon.isKeywordStreamReady(this.handle, stream.handle); | ||
} | ||
|
||
decode(stream) { | ||
addon.decodeKeywordStream(this.handle, stream.handle); | ||
} | ||
|
||
getResult(stream) { | ||
const jsonStr = addon.getKeywordResultAsJson(this.handle, stream.handle); | ||
|
||
return JSON.parse(jsonStr); | ||
} | ||
} | ||
|
||
module.exports = { | ||
KeywordSpotter, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.