-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebAssembly for SenseVoice (#1158)
- Loading branch information
1 parent
c3260ef
commit 70d1435
Showing
21 changed files
with
383 additions
and
351 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,11 @@ jobs: | |
with: | ||
fetch-depth: 0 | ||
|
||
- name: ccache | ||
uses: hendrikmuhs/[email protected] | ||
with: | ||
key: ${{ matrix.os }}-${{ matrix.build_type }}-wasm-nodejs | ||
|
||
- name: Install emsdk | ||
uses: mymindstorm/setup-emsdk@v14 | ||
|
||
|
@@ -77,6 +82,10 @@ jobs: | |
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: | | ||
export CMAKE_CXX_COMPILER_LAUNCHER=ccache | ||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" | ||
cmake --version | ||
./build-wasm-simd-nodejs.sh | ||
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.js ./scripts/nodejs/ | ||
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.wasm ./scripts/nodejs/ | ||
|
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,101 @@ | ||
// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang) | ||
|
||
const fs = require('fs'); | ||
const {Readable} = require('stream'); | ||
const wav = require('wav'); | ||
|
||
const sherpa_onnx = require('sherpa-onnx'); | ||
|
||
function createOfflineRecognizer() { | ||
let featConfig = { | ||
sampleRate: 16000, | ||
featureDim: 80, | ||
}; | ||
|
||
let modelConfig = { | ||
senseVoice: { | ||
model: | ||
'./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx', | ||
language: '', | ||
useInverseTextNormalization: 1, | ||
}, | ||
tokens: './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt', | ||
numThreads: 1, | ||
debug: 0, | ||
provider: 'cpu', | ||
}; | ||
|
||
let config = { | ||
featConfig: featConfig, | ||
modelConfig: modelConfig, | ||
decodingMethod: 'greedy_search', | ||
}; | ||
|
||
return sherpa_onnx.createOfflineRecognizer(config); | ||
} | ||
|
||
|
||
const recognizer = createOfflineRecognizer(); | ||
const stream = recognizer.createStream(); | ||
|
||
const waveFilename = | ||
'./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/test_wavs/zh.wav'; | ||
|
||
const reader = new wav.Reader(); | ||
const readable = new Readable().wrap(reader); | ||
const buf = []; | ||
|
||
reader.on('format', ({audioFormat, bitDepth, channels, sampleRate}) => { | ||
if (sampleRate != recognizer.config.featConfig.sampleRate) { | ||
throw new Error(`Only support sampleRate ${ | ||
recognizer.config.featConfig.sampleRate}. Given ${sampleRate}`); | ||
} | ||
|
||
if (audioFormat != 1) { | ||
throw new Error(`Only support PCM format. Given ${audioFormat}`); | ||
} | ||
|
||
if (channels != 1) { | ||
throw new Error(`Only a single channel. Given ${channel}`); | ||
} | ||
|
||
if (bitDepth != 16) { | ||
throw new Error(`Only support 16-bit samples. Given ${bitDepth}`); | ||
} | ||
}); | ||
|
||
fs.createReadStream(waveFilename, {'highWaterMark': 4096}) | ||
.pipe(reader) | ||
.on('finish', function(err) { | ||
// tail padding | ||
const floatSamples = | ||
new Float32Array(recognizer.config.featConfig.sampleRate * 0.5); | ||
|
||
buf.push(floatSamples); | ||
const flattened = | ||
Float32Array.from(buf.reduce((a, b) => [...a, ...b], [])); | ||
|
||
stream.acceptWaveform(recognizer.config.featConfig.sampleRate, flattened); | ||
recognizer.decode(stream); | ||
const text = recognizer.getResult(stream).text; | ||
console.log(text); | ||
|
||
stream.free(); | ||
recognizer.free(); | ||
}); | ||
|
||
readable.on('readable', function() { | ||
let chunk; | ||
while ((chunk = readable.read()) != null) { | ||
const int16Samples = new Int16Array( | ||
chunk.buffer, chunk.byteOffset, | ||
chunk.length / Int16Array.BYTES_PER_ELEMENT); | ||
|
||
const floatSamples = new Float32Array(int16Samples.length); | ||
for (let i = 0; i < floatSamples.length; i++) { | ||
floatSamples[i] = int16Samples[i] / 32768.0; | ||
} | ||
|
||
buf.push(floatSamples); | ||
} | ||
}); |
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
Oops, something went wrong.