Add VAD and keyword spotting for the Node package with WebAssembly (#1286)
This commit is contained in:
@@ -49,6 +49,32 @@ set(exported_functions
|
||||
SherpaOnnxDestroyKeywordSpotter
|
||||
SherpaOnnxGetKeywordResult
|
||||
SherpaOnnxIsKeywordStreamReady
|
||||
# VAD
|
||||
SherpaOnnxCreateCircularBuffer
|
||||
SherpaOnnxDestroyCircularBuffer
|
||||
SherpaOnnxCircularBufferPush
|
||||
SherpaOnnxCircularBufferGet
|
||||
SherpaOnnxCircularBufferFree
|
||||
SherpaOnnxCircularBufferPop
|
||||
SherpaOnnxCircularBufferSize
|
||||
SherpaOnnxCircularBufferHead
|
||||
SherpaOnnxCircularBufferReset
|
||||
SherpaOnnxCreateVoiceActivityDetector
|
||||
SherpaOnnxDestroyVoiceActivityDetector
|
||||
SherpaOnnxVoiceActivityDetectorAcceptWaveform
|
||||
SherpaOnnxVoiceActivityDetectorEmpty
|
||||
SherpaOnnxVoiceActivityDetectorDetected
|
||||
SherpaOnnxVoiceActivityDetectorPop
|
||||
SherpaOnnxVoiceActivityDetectorClear
|
||||
SherpaOnnxVoiceActivityDetectorFront
|
||||
SherpaOnnxDestroySpeechSegment
|
||||
SherpaOnnxVoiceActivityDetectorReset
|
||||
SherpaOnnxVoiceActivityDetectorFlush
|
||||
#
|
||||
SherpaOnnxFileExists
|
||||
SherpaOnnxReadWave
|
||||
SherpaOnnxFreeWave
|
||||
SherpaOnnxWriteWave
|
||||
)
|
||||
|
||||
|
||||
@@ -82,6 +108,8 @@ install(
|
||||
${CMAKE_SOURCE_DIR}/wasm/asr/sherpa-onnx-asr.js
|
||||
${CMAKE_SOURCE_DIR}/wasm/tts/sherpa-onnx-tts.js
|
||||
${CMAKE_SOURCE_DIR}/wasm/kws/sherpa-onnx-kws.js
|
||||
${CMAKE_SOURCE_DIR}/wasm/vad/sherpa-onnx-vad.js
|
||||
${CMAKE_SOURCE_DIR}/wasm/nodejs/sherpa-onnx-wave.js
|
||||
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-nodejs>/sherpa-onnx-wasm-nodejs.js"
|
||||
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-nodejs>/sherpa-onnx-wasm-nodejs.wasm"
|
||||
DESTINATION
|
||||
|
||||
57
wasm/nodejs/sherpa-onnx-wave.js
Normal file
57
wasm/nodejs/sherpa-onnx-wave.js
Normal file
@@ -0,0 +1,57 @@
|
||||
// return an object
|
||||
// {
|
||||
// samples: a float32 array
|
||||
// sampleRate: an integer
|
||||
// }
|
||||
function readWave(filename, Module) {
|
||||
const filenameLen = Module.lengthBytesUTF8(filename) + 1;
|
||||
const pFilename = Module._malloc(filenameLen);
|
||||
Module.stringToUTF8(filename, pFilename, filenameLen);
|
||||
|
||||
const w = Module._SherpaOnnxReadWave(pFilename);
|
||||
Module._free(pFilename);
|
||||
|
||||
|
||||
const samplesPtr = Module.HEAP32[w / 4] / 4;
|
||||
const sampleRate = Module.HEAP32[w / 4 + 1];
|
||||
const numSamples = Module.HEAP32[w / 4 + 2];
|
||||
|
||||
const samples = new Float32Array(numSamples);
|
||||
for (let i = 0; i < numSamples; i++) {
|
||||
samples[i] = Module.HEAPF32[samplesPtr + i];
|
||||
}
|
||||
|
||||
Module._SherpaOnnxFreeWave(w);
|
||||
|
||||
|
||||
return {samples: samples, sampleRate: sampleRate};
|
||||
}
|
||||
|
||||
// data is an object
|
||||
// {
|
||||
// samples: a float32 array
|
||||
// sampleRate: an integer
|
||||
// }
|
||||
function writeWave(filename, data, Module) {
|
||||
const pSamples =
|
||||
Module._malloc(data.samples.length * data.samples.BYTES_PER_ELEMENT);
|
||||
Module.HEAPF32.set(data.samples, pSamples / data.samples.BYTES_PER_ELEMENT);
|
||||
|
||||
const filenameLen = Module.lengthBytesUTF8(filename) + 1;
|
||||
const pFilename = Module._malloc(filenameLen);
|
||||
Module.stringToUTF8(filename, pFilename, filenameLen);
|
||||
|
||||
Module._SherpaOnnxWriteWave(
|
||||
pSamples, data.samples.length, data.sampleRate, pFilename);
|
||||
|
||||
Module._free(pFilename);
|
||||
Module._free(pSamples);
|
||||
}
|
||||
|
||||
if (typeof process == 'object' && typeof process.versions == 'object' &&
|
||||
typeof process.versions.node == 'string') {
|
||||
module.exports = {
|
||||
readWave,
|
||||
writeWave,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user