|
| 1 | +// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang) |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const {Readable} = require('stream'); |
| 5 | +const wav = require('wav'); |
| 6 | + |
| 7 | +const sherpa_onnx = require('sherpa-onnx'); |
| 8 | + |
| 9 | +function createOfflineRecognizer() { |
| 10 | + let featConfig = { |
| 11 | + sampleRate: 16000, |
| 12 | + featureDim: 80, |
| 13 | + }; |
| 14 | + |
| 15 | + let modelConfig = { |
| 16 | + senseVoice: { |
| 17 | + model: |
| 18 | + './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx', |
| 19 | + language: '', |
| 20 | + useInverseTextNormalization: 1, |
| 21 | + }, |
| 22 | + tokens: './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt', |
| 23 | + numThreads: 1, |
| 24 | + debug: 0, |
| 25 | + provider: 'cpu', |
| 26 | + }; |
| 27 | + |
| 28 | + let config = { |
| 29 | + featConfig: featConfig, |
| 30 | + modelConfig: modelConfig, |
| 31 | + decodingMethod: 'greedy_search', |
| 32 | + }; |
| 33 | + |
| 34 | + return sherpa_onnx.createOfflineRecognizer(config); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +const recognizer = createOfflineRecognizer(); |
| 39 | +const stream = recognizer.createStream(); |
| 40 | + |
| 41 | +const waveFilename = |
| 42 | + './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/test_wavs/zh.wav'; |
| 43 | + |
| 44 | +const reader = new wav.Reader(); |
| 45 | +const readable = new Readable().wrap(reader); |
| 46 | +const buf = []; |
| 47 | + |
| 48 | +reader.on('format', ({audioFormat, bitDepth, channels, sampleRate}) => { |
| 49 | + if (sampleRate != recognizer.config.featConfig.sampleRate) { |
| 50 | + throw new Error(`Only support sampleRate ${ |
| 51 | + recognizer.config.featConfig.sampleRate}. Given ${sampleRate}`); |
| 52 | + } |
| 53 | + |
| 54 | + if (audioFormat != 1) { |
| 55 | + throw new Error(`Only support PCM format. Given ${audioFormat}`); |
| 56 | + } |
| 57 | + |
| 58 | + if (channels != 1) { |
| 59 | + throw new Error(`Only a single channel. Given ${channel}`); |
| 60 | + } |
| 61 | + |
| 62 | + if (bitDepth != 16) { |
| 63 | + throw new Error(`Only support 16-bit samples. Given ${bitDepth}`); |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +fs.createReadStream(waveFilename, {'highWaterMark': 4096}) |
| 68 | + .pipe(reader) |
| 69 | + .on('finish', function(err) { |
| 70 | + // tail padding |
| 71 | + const floatSamples = |
| 72 | + new Float32Array(recognizer.config.featConfig.sampleRate * 0.5); |
| 73 | + |
| 74 | + buf.push(floatSamples); |
| 75 | + const flattened = |
| 76 | + Float32Array.from(buf.reduce((a, b) => [...a, ...b], [])); |
| 77 | + |
| 78 | + stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); |
| 79 | + recognizer.decode(stream); |
| 80 | + const text = recognizer.getResult(stream).text; |
| 81 | + console.log(text); |
| 82 | + |
| 83 | + stream.free(); |
| 84 | + recognizer.free(); |
| 85 | + }); |
| 86 | + |
| 87 | +readable.on('readable', function() { |
| 88 | + let chunk; |
| 89 | + while ((chunk = readable.read()) != null) { |
| 90 | + const int16Samples = new Int16Array( |
| 91 | + chunk.buffer, chunk.byteOffset, |
| 92 | + chunk.length / Int16Array.BYTES_PER_ELEMENT); |
| 93 | + |
| 94 | + const floatSamples = new Float32Array(int16Samples.length); |
| 95 | + for (let i = 0; i < floatSamples.length; i++) { |
| 96 | + floatSamples[i] = int16Samples[i] / 32768.0; |
| 97 | + } |
| 98 | + |
| 99 | + buf.push(floatSamples); |
| 100 | + } |
| 101 | +}); |
0 commit comments