-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add WebAssembly + NodeJS example.
- Loading branch information
1 parent
185f5c7
commit 1e4d5e0
Showing
6 changed files
with
258 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env bash | ||
# Copyright (c) 2024 Xiaomi Corporation | ||
# | ||
# This script is to build sherpa-ncnn for WebAssembly (NodeJS) | ||
# | ||
# See also | ||
# https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-webassembly | ||
# | ||
# Please refer to | ||
# https://k2-fsa.github.io/sherpa/ncnn/wasm/index.html | ||
# for more details. | ||
|
||
set -ex | ||
|
||
if [ x"$EMSCRIPTEN" == x"" ]; then | ||
if ! command -v emcc &> /dev/null; then | ||
echo "Please install emscripten first" | ||
echo "" | ||
echo "You can use the following commands to install it:" | ||
echo "" | ||
echo "git clone https://github.com/emscripten-core/emsdk.git" | ||
echo "cd emsdk" | ||
echo "git pull" | ||
echo "./emsdk install latest" | ||
echo "./emsdk activate latest" | ||
echo "source ./emsdk_env.sh" | ||
exit 1 | ||
else | ||
EMSCRIPTEN=$(dirname $(realpath $(which emcc))) | ||
fi | ||
fi | ||
|
||
export EMSCRIPTEN=$EMSCRIPTEN | ||
echo "EMSCRIPTEN: $EMSCRIPTEN" | ||
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then | ||
echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake" | ||
echo "Please make sure you have installed emsdk correctly" | ||
exit 1 | ||
fi | ||
|
||
mkdir -p build-wasm-simd-for-nodejs | ||
pushd build-wasm-simd-for-nodejs | ||
|
||
export SHERPA_NCNN_IS_USING_BUILD_WASM_SH=ON | ||
|
||
cmake \ | ||
-DCMAKE_INSTALL_PREFIX=./install \ | ||
-DCMAKE_BUILD_TYPE=Release \ | ||
-DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \ | ||
-DNCNN_THREADS=OFF \ | ||
-DNCNN_OPENMP=OFF \ | ||
-DNCNN_SIMPLEOMP=OFF \ | ||
-DNCNN_RUNTIME_CPU=OFF \ | ||
-DNCNN_SSE2=ON \ | ||
-DNCNN_AVX2=OFF \ | ||
-DNCNN_AVX=OFF \ | ||
-DNCNN_BUILD_TOOLS=OFF \ | ||
-DNCNN_BUILD_EXAMPLES=OFF \ | ||
-DNCNN_BUILD_BENCHMARK=OFF \ | ||
\ | ||
-DSHERPA_NCNN_ENABLE_WASM=ON \ | ||
-DSHERPA_NCNN_ENABLE_WASM_FOR_NODEJS=ON \ | ||
-DBUILD_SHARED_LIBS=OFF \ | ||
-DSHERPA_NCNN_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_NCNN_ENABLE_JNI=OFF \ | ||
-DSHERPA_NCNN_ENABLE_BINARY=OFF \ | ||
-DSHERPA_NCNN_ENABLE_TEST=OFF \ | ||
-DSHERPA_NCNN_ENABLE_C_API=ON \ | ||
-DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \ | ||
-DSHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES=OFF \ | ||
.. | ||
|
||
make -j2 | ||
make install | ||
ls -lh install/bin/wasm |
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,75 @@ | ||
const fs = require('fs'); | ||
const wav = require('wav'); | ||
const {Readable} = require('stream'); | ||
|
||
let Module = require('./sherpa-ncnn-wasm-main.js')() | ||
let b = require('./sherpa-ncnn.js'); | ||
|
||
let recognizer = b.createRecognizer(Module); | ||
let stream = recognizer.createStream(); | ||
|
||
console.log(recognizer.config); | ||
|
||
const waveFilename = './0.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.samplingRate) { | ||
throw new Error(`Only support sampleRate ${ | ||
recognizer.config.featConfig.samplingRate}. 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.samplingRate * 0.5); | ||
|
||
buf.push(floatSamples); | ||
const flattened = | ||
Float32Array.from(buf.reduce((a, b) => [...a, ...b], [])); | ||
|
||
stream.acceptWaveform( | ||
recognizer.config.featConfig.samplingRate, flattened); | ||
while (recognizer.isReady(stream)) { | ||
recognizer.decode(stream); | ||
} | ||
const r = recognizer.getResult(stream); | ||
console.log('result', r); | ||
|
||
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
Oops, something went wrong.