-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add non-streaming ASR APIs for node-addon-api (#868)
- Loading branch information
1 parent
384f96c
commit 697b960
Showing
18 changed files
with
1,588 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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/asr-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'nemoCtc': { | ||
'model': | ||
'./sherpa-onnx-nemo-fast-conformer-ctc-be-de-en-es-fr-hr-it-pl-ru-uk-20k/model.onnx', | ||
}, | ||
'tokens': | ||
'./sherpa-onnx-nemo-fast-conformer-ctc-be-de-en-es-fr-hr-it-pl-ru-uk-20k/tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
} | ||
}; | ||
|
||
const waveFilename = | ||
'./sherpa-onnx-nemo-fast-conformer-ctc-be-de-en-es-fr-hr-it-pl-ru-uk-20k/test_wavs/de-german.wav'; | ||
|
||
const recognizer = new sherpa_onnx.OfflineRecognizer(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = recognizer.createStream(); | ||
const wave = sherpa_onnx.readWave(waveFilename); | ||
stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples}); | ||
|
||
recognizer.decode(stream); | ||
result = recognizer.getResult(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', result) |
48 changes: 48 additions & 0 deletions
48
nodejs-addon-examples/test_asr_non_streaming_paraformer.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,48 @@ | ||
// 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/asr-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'paraformer': { | ||
'model': './sherpa-onnx-paraformer-zh-2023-03-28/model.int8.onnx', | ||
}, | ||
'tokens': './sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
} | ||
}; | ||
|
||
const waveFilename = | ||
'./sherpa-onnx-paraformer-zh-2023-03-28/test_wavs/5-henan.wav'; | ||
|
||
const recognizer = new sherpa_onnx.OfflineRecognizer(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = recognizer.createStream(); | ||
const wave = sherpa_onnx.readWave(waveFilename); | ||
stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples}); | ||
|
||
recognizer.decode(stream); | ||
result = recognizer.getResult(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', result) |
52 changes: 52 additions & 0 deletions
52
nodejs-addon-examples/test_asr_non_streaming_transducer.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,52 @@ | ||
// 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/asr-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'transducer': { | ||
'encoder': | ||
'./sherpa-onnx-zipformer-en-2023-04-01/encoder-epoch-99-avg-1.int8.onnx', | ||
'decoder': | ||
'./sherpa-onnx-zipformer-en-2023-04-01/decoder-epoch-99-avg-1.onnx', | ||
'joiner': | ||
'./sherpa-onnx-zipformer-en-2023-04-01/joiner-epoch-99-avg-1.int8.onnx', | ||
}, | ||
'tokens': './sherpa-onnx-zipformer-en-2023-04-01/tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
} | ||
}; | ||
|
||
const waveFilename = './sherpa-onnx-zipformer-en-2023-04-01/test_wavs/1.wav'; | ||
|
||
const recognizer = new sherpa_onnx.OfflineRecognizer(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = recognizer.createStream(); | ||
const wave = sherpa_onnx.readWave(waveFilename); | ||
stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples}); | ||
|
||
recognizer.decode(stream); | ||
result = recognizer.getResult(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', result) |
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,48 @@ | ||
// 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/asr-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'whisper': { | ||
'encoder': './sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx', | ||
'decoder': './sherpa-onnx-whisper-tiny.en/tiny.en-decoder.int8.onnx', | ||
}, | ||
'tokens': './sherpa-onnx-whisper-tiny.en/tiny.en-tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
} | ||
}; | ||
|
||
const waveFilename = './sherpa-onnx-whisper-tiny.en/test_wavs/0.wav'; | ||
|
||
const recognizer = new sherpa_onnx.OfflineRecognizer(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = recognizer.createStream(); | ||
const wave = sherpa_onnx.readWave(waveFilename); | ||
stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples}); | ||
|
||
recognizer.decode(stream); | ||
result = recognizer.getResult(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', result) |
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,56 @@ | ||
// 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/asr-models | ||
const config = { | ||
'featConfig': { | ||
'sampleRate': 16000, | ||
'featureDim': 80, | ||
}, | ||
'modelConfig': { | ||
'paraformer': { | ||
'encoder': | ||
'./sherpa-onnx-streaming-paraformer-bilingual-zh-en/encoder.int8.onnx', | ||
'decoder': | ||
'./sherpa-onnx-streaming-paraformer-bilingual-zh-en/decoder.int8.onnx', | ||
}, | ||
'tokens': './sherpa-onnx-streaming-paraformer-bilingual-zh-en/tokens.txt', | ||
'numThreads': 2, | ||
'provider': 'cpu', | ||
'debug': 1, | ||
} | ||
}; | ||
|
||
const waveFilename = | ||
'./sherpa-onnx-streaming-paraformer-bilingual-zh-en/test_wavs/0.wav'; | ||
|
||
const recognizer = new sherpa_onnx.OnlineRecognizer(config); | ||
console.log('Started') | ||
let start = performance.now(); | ||
const stream = recognizer.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}); | ||
|
||
while (recognizer.isReady(stream)) { | ||
recognizer.decode(stream); | ||
} | ||
result = recognizer.getResult(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', result) |
Oops, something went wrong.