Non-Streaming ASR
Recognize speech in a WAV file using a non-streaming (offline) Zipformer transducer model. The recognizer processes the entire audio file at once.
Source file
Code
1// Copyright (c) 2024 Xiaomi Corporation
2//
3// Non-streaming (offline) automatic speech recognition with a Zipformer
4// transducer model.
5//
6// Usage:
7// node non_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-zipformer-en-2023-04-01/encoder-epoch-99-avg-1.int8.onnx',
20 'decoder':
21 './sherpa-onnx-zipformer-en-2023-04-01/decoder-epoch-99-avg-1.onnx',
22 'joiner':
23 './sherpa-onnx-zipformer-en-2023-04-01/joiner-epoch-99-avg-1.int8.onnx',
24 },
25 'tokens': './sherpa-onnx-zipformer-en-2023-04-01/tokens.txt',
26 'numThreads': 2,
27 'provider': 'cpu',
28 'debug': 1,
29 }
30};
31
32const waveFilename = './sherpa-onnx-zipformer-en-2023-04-01/test_wavs/1.wav';
33
34// Create the recognizer and a stream.
35const recognizer = new sherpa_onnx.OfflineRecognizer(config);
36const stream = recognizer.createStream();
37
38// Read the wave file and feed it to the stream.
39const wave = sherpa_onnx.readWave(waveFilename);
40stream.acceptWaveform({sampleRate: wave.sampleRate, samples: wave.samples});
41
42// Decode and get the result.
43let start = Date.now();
44recognizer.decode(stream);
45const result = recognizer.getResult(stream);
46let stop = Date.now();
47
48const elapsed_seconds = (stop - start) / 1000;
49const duration = wave.samples.length / wave.sampleRate;
50const real_time_factor = elapsed_seconds / duration;
51console.log('Wave duration', duration.toFixed(3), 'seconds');
52console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
53console.log(
54 `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
55 real_time_factor.toFixed(3));
56console.log(waveFilename);
57console.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-zipformer-en-2023-04-01.tar.bz2 tar xvf sherpa-onnx-zipformer-en-2023-04-01.tar.bz2 rm sherpa-onnx-zipformer-en-2023-04-01.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 non_streaming_asr.js
Expected output
Wave duration 5.280 seconds
Elapsed 0.156 seconds
RTF = 0.156/5.280 = 0.030
./sherpa-onnx-zipformer-en-2023-04-01/test_wavs/1.wav
result
{ text: ' ...', tokens: [...], timestamps: [...] }
Notes
OfflineRecognizeris the non-streaming recognizer. Unlike the streaming version, it processes the entire audio in one call.The API is simpler:
createStream()->acceptWaveform()->decode()->getResult(). No loop needed.Non-streaming models generally produce more accurate results than streaming models, but cannot be used for real-time transcription.