Streaming ASR
Recognize speech in a WAV file using a streaming (online) Zipformer transducer model. The recognizer processes audio incrementally, producing partial results as more audio arrives.
Source file
Code
1// Copyright (c) 2024 Xiaomi Corporation
2//
3// Streaming (online) automatic speech recognition with a Zipformer
4// transducer model.
5//
6// Usage:
7// node streaming_asr.js
8//
9const sherpa_onnx = require('sherpa-onnx-node');
10
11const config = {
12 'featConfig': {
13 'sampleRate': 16000,
14 'featureDim': 80,
15 },
16 'modelConfig': {
17 'transducer': {
18 'encoder':
19 './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx',
20 'decoder':
21 './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx',
22 'joiner':
23 './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx',
24 },
25 'tokens':
26 './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt',
27 'numThreads': 2,
28 'provider': 'cpu',
29 'debug': 1,
30 }
31};
32
33const waveFilename =
34 './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/0.wav';
35
36// Create the recognizer and a stream.
37const recognizer = new sherpa_onnx.OnlineRecognizer(config);
38const stream = recognizer.createStream();
39
40// Read the wave file and feed it to the stream.
41const wave = sherpa_onnx.readWave(waveFilename);
42stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples});
43
44// Append tail padding so the model can process the last chunk.
45const tailPadding = new Float32Array(wave.sampleRate * 0.4);
46stream.acceptWaveform({samples: tailPadding, sampleRate: wave.sampleRate});
47
48// Decode in a loop until all frames are consumed.
49let start = Date.now();
50while (recognizer.isReady(stream)) {
51 recognizer.decode(stream);
52}
53const result = recognizer.getResult(stream);
54let stop = Date.now();
55
56const elapsed_seconds = (stop - start) / 1000;
57const duration = wave.samples.length / wave.sampleRate;
58const real_time_factor = elapsed_seconds / duration;
59console.log('Wave duration', duration.toFixed(3), 'seconds');
60console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
61console.log(
62 `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
63 real_time_factor.toFixed(3));
64console.log(waveFilename);
65console.log('result\n', result);
How to run
Install the package:
npm install sherpa-onnx-node
Download the model and test files:
curl -LS -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
Set the library path and run:
# macOS export DYLD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$DYLD_LIBRARY_PATH # Linux export LD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$LD_LIBRARY_PATH node streaming_asr.js
Expected output
Wave duration 6.625 seconds
Elapsed 0.234 seconds
RTF = 0.234/6.625 = 0.035
./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/0.wav
result
{ text: ' 吃饭了吗', tokens: [...], timestamps: [...] }
Notes
OnlineRecognizeris the streaming recognizer. CallcreateStream()to create a stream, then feed audio withacceptWaveform().Append 0.4 seconds of tail padding (zeros) after the main audio so the model can process the last chunk.
Call
isReady()anddecode()in a loop until no more frames are available, then callgetResult()for the final transcription.This model supports both Chinese and English.