More testing
This commit is contained in:
5915
devel/Vosklet.js
5915
devel/Vosklet.js
File diff suppressed because one or more lines are too long
@@ -1 +1,163 @@
|
||||
"use strict";var Module={};var initializedJS=false;function threadPrintErr(...args){var text=args.join(" ");console.error(text)}function threadAlert(...args){var text=args.join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=(info,receiveInstance)=>{var module=Module["wasmModule"];Module["wasmModule"]=null;var instance=new WebAssembly.Instance(module,info);return receiveInstance(instance)};self.onunhandledrejection=e=>{throw e.reason||e};function handleMessage(e){try{if(e.data.cmd==="load"){let messageQueue=[];self.onmessage=e=>messageQueue.push(e);self.startWorker=instance=>{Module=instance;postMessage({"cmd":"loaded"});for(let msg of messageQueue){handleMessage(msg)}self.onmessage=handleMessage};Module["wasmModule"]=e.data.wasmModule;for(const handler of e.data.handlers){Module[handler]=(...args)=>{postMessage({cmd:"callHandler",handler:handler,args:args})}}Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob=="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}loadVosklet(Module)}else if(e.data.cmd==="run"){Module["__emscripten_thread_init"](e.data.pthread_ptr,0,0,1);Module["__emscripten_thread_mailbox_await"](e.data.pthread_ptr);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInitTLS();if(!initializedJS){Module["__embind_initialize_bindings"]();initializedJS=true}try{Module["invokeEntryPoint"](e.data.start_routine,e.data.arg)}catch(ex){if(ex!="unwind"){throw ex}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["__emscripten_thread_exit"](-1)}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="checkMailbox"){if(initializedJS){Module["checkMailbox"]()}}else if(e.data.cmd){err(`worker.js received unknown command ${e.data.cmd}`);err(e.data)}}catch(ex){Module["__emscripten_thread_crashed"]?.();throw ex}}self.onmessage=handleMessage;
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2015 The Emscripten Authors
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
// Pthread Web Worker startup routine:
|
||||
// This is the entry point file that is loaded first by each Web Worker
|
||||
// that executes pthreads on the Emscripten application.
|
||||
|
||||
'use strict';
|
||||
|
||||
var Module = {};
|
||||
|
||||
// Thread-local guard variable for one-time init of the JS state
|
||||
var initializedJS = false;
|
||||
|
||||
function assert(condition, text) {
|
||||
if (!condition) abort('Assertion failed: ' + text);
|
||||
}
|
||||
|
||||
function threadPrintErr(...args) {
|
||||
var text = args.join(' ');
|
||||
console.error(text);
|
||||
}
|
||||
function threadAlert(...args) {
|
||||
var text = args.join(' ');
|
||||
postMessage({cmd: 'alert', text, threadId: Module['_pthread_self']()});
|
||||
}
|
||||
// We don't need out() for now, but may need to add it if we want to use it
|
||||
// here. Or, if this code all moves into the main JS, that problem will go
|
||||
// away. (For now, adding it here increases code size for no benefit.)
|
||||
var out = () => { throw 'out() is not defined in worker.js.'; }
|
||||
var err = threadPrintErr;
|
||||
self.alert = threadAlert;
|
||||
var dbg = threadPrintErr;
|
||||
|
||||
Module['instantiateWasm'] = (info, receiveInstance) => {
|
||||
// Instantiate from the module posted from the main thread.
|
||||
// We can just use sync instantiation in the worker.
|
||||
var module = Module['wasmModule'];
|
||||
// We don't need the module anymore; new threads will be spawned from the main thread.
|
||||
Module['wasmModule'] = null;
|
||||
var instance = new WebAssembly.Instance(module, info);
|
||||
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
|
||||
// the above line no longer optimizes out down to the following line.
|
||||
// When the regression is fixed, we can remove this if/else.
|
||||
return receiveInstance(instance);
|
||||
}
|
||||
|
||||
// Turn unhandled rejected promises into errors so that the main thread will be
|
||||
// notified about them.
|
||||
self.onunhandledrejection = (e) => {
|
||||
throw e.reason || e;
|
||||
};
|
||||
|
||||
function handleMessage(e) {
|
||||
try {
|
||||
if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
|
||||
|
||||
// Until we initialize the runtime, queue up any further incoming messages.
|
||||
let messageQueue = [];
|
||||
self.onmessage = (e) => messageQueue.push(e);
|
||||
|
||||
// And add a callback for when the runtime is initialized.
|
||||
self.startWorker = (instance) => {
|
||||
Module = instance;
|
||||
// Notify the main thread that this thread has loaded.
|
||||
postMessage({ 'cmd': 'loaded' });
|
||||
// Process any messages that were queued before the thread was ready.
|
||||
for (let msg of messageQueue) {
|
||||
handleMessage(msg);
|
||||
}
|
||||
// Restore the real message handler.
|
||||
self.onmessage = handleMessage;
|
||||
};
|
||||
|
||||
// Module and memory were sent from main thread
|
||||
Module['wasmModule'] = e.data.wasmModule;
|
||||
|
||||
// Use `const` here to ensure that the variable is scoped only to
|
||||
// that iteration, allowing safe reference from a closure.
|
||||
for (const handler of e.data.handlers) {
|
||||
Module[handler] = (...args) => {
|
||||
dbg(`calling handler on main thread: ${handler}`);
|
||||
postMessage({ cmd: 'callHandler', handler, args: args });
|
||||
}
|
||||
}
|
||||
|
||||
Module['wasmMemory'] = e.data.wasmMemory;
|
||||
|
||||
Module['buffer'] = Module['wasmMemory'].buffer;
|
||||
|
||||
Module['workerID'] = e.data.workerID;
|
||||
|
||||
Module['ENVIRONMENT_IS_PTHREAD'] = true;
|
||||
|
||||
if (typeof e.data.urlOrBlob == 'string') {
|
||||
importScripts(e.data.urlOrBlob);
|
||||
} else {
|
||||
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
|
||||
importScripts(objectUrl);
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
}
|
||||
loadVosklet(Module);
|
||||
} else if (e.data.cmd === 'run') {
|
||||
// Pass the thread address to wasm to store it for fast access.
|
||||
Module['__emscripten_thread_init'](e.data.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1);
|
||||
|
||||
// Await mailbox notifications with `Atomics.waitAsync` so we can start
|
||||
// using the fast `Atomics.notify` notification path.
|
||||
Module['__emscripten_thread_mailbox_await'](e.data.pthread_ptr);
|
||||
|
||||
assert(e.data.pthread_ptr);
|
||||
// Also call inside JS module to set up the stack frame for this pthread in JS module scope
|
||||
Module['establishStackSpace']();
|
||||
Module['PThread'].receiveObjectTransfer(e.data);
|
||||
Module['PThread'].threadInitTLS();
|
||||
|
||||
if (!initializedJS) {
|
||||
// Embind must initialize itself on all threads, as it generates support JS.
|
||||
// We only do this once per worker since they get reused
|
||||
Module['__embind_initialize_bindings']();
|
||||
initializedJS = true;
|
||||
}
|
||||
|
||||
try {
|
||||
Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
|
||||
} catch(ex) {
|
||||
if (ex != 'unwind') {
|
||||
// The pthread "crashed". Do not call `_emscripten_thread_exit` (which
|
||||
// would make this thread joinable). Instead, re-throw the exception
|
||||
// and let the top level handler propagate it back to the main thread.
|
||||
throw ex;
|
||||
}
|
||||
dbg(`Pthread 0x${Module['_pthread_self']().toString(16)} completed its main entry point with an 'unwind', keeping the worker alive for asynchronous operation.`);
|
||||
}
|
||||
} else if (e.data.cmd === 'cancel') { // Main thread is asking for a pthread_cancel() on this thread.
|
||||
if (Module['_pthread_self']()) {
|
||||
Module['__emscripten_thread_exit'](-1);
|
||||
}
|
||||
} else if (e.data.target === 'setimmediate') {
|
||||
// no-op
|
||||
} else if (e.data.cmd === 'checkMailbox') {
|
||||
if (initializedJS) {
|
||||
Module['checkMailbox']();
|
||||
}
|
||||
} else if (e.data.cmd) {
|
||||
// The received message looks like something that should be handled by this message
|
||||
// handler, (since there is a e.data.cmd field present), but is not one of the
|
||||
// recognized commands:
|
||||
err(`worker.js received unknown command ${e.data.cmd}`);
|
||||
err(e.data);
|
||||
}
|
||||
} catch(ex) {
|
||||
err(`worker.js onmessage() captured an uncaught exception: ${ex}`);
|
||||
if (ex?.stack) err(ex.stack);
|
||||
Module['__emscripten_thread_crashed']?.();
|
||||
throw ex;
|
||||
}
|
||||
};
|
||||
|
||||
self.onmessage = handleMessage;
|
||||
|
||||
@@ -3,8 +3,20 @@
|
||||
<head>
|
||||
<script src="Vosklet.js"></script>
|
||||
<script>
|
||||
window.onload = async () => {
|
||||
window.ctx = new AudioContext()
|
||||
async function func() {
|
||||
let processorURL = URL.createObjectURL(new Blob(['(',
|
||||
(() => {
|
||||
registerProcessor('processor', class extends AudioWorkletProcessor {
|
||||
process(inputs, outputs) {
|
||||
for (let channel = 0; channel < outputs[0].length; ++channel) {
|
||||
outputs[0][channel].set(inputs[0][channel])
|
||||
}
|
||||
return true
|
||||
}
|
||||
})
|
||||
}).toString(),
|
||||
')()'], { type : "text/javascript" }))
|
||||
window.ctx = new AudioContext({sampleRate : 16000})
|
||||
let micNode = ctx.createMediaStreamSource(await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: {
|
||||
@@ -14,17 +26,26 @@
|
||||
sampleRate: 16000
|
||||
},
|
||||
}))
|
||||
window.module = await loadVosklet()
|
||||
await ctx.audioWorklet.addModule(processorURL)
|
||||
window.node = new AudioWorkletNode(ctx, "processor", { channelCountMode: "explicit", numberOfInputs : 1, numberOfOutputs : 1})
|
||||
micNode.connect(node).connect(ctx.destination)
|
||||
ctx.resume()
|
||||
}
|
||||
window.onload = async () => {
|
||||
/*window.module = await loadVosklet()
|
||||
window.model = await module.makeModel("../usage/en-model.tgz","model","ID")
|
||||
window.recognizer = await module.makeRecognizer(model, ctx.sampleRate)
|
||||
window.recognizer = await module.makeRecognizer(model, 16000)
|
||||
recognizer.addEventListener("result", e => {
|
||||
console.log("Result: ", e.detail)
|
||||
})
|
||||
recognizer.addEventListener("partialResult", e => {
|
||||
console.log("Partial result: ", e.detail)
|
||||
})
|
||||
//let recNode = recognizer.getNode(ctx);
|
||||
})*/
|
||||
//let recNode = await recognizer.getNode(ctx)
|
||||
//recNode.connect(ctx.destination)
|
||||
//micNode.connect(recNode)
|
||||
}
|
||||
</script>
|
||||
<button onclick="func()">Just another button</button>
|
||||
</head>
|
||||
</html>
|
||||
@@ -28,12 +28,12 @@ fi
|
||||
. $EMSDK/emsdk_env.sh &&
|
||||
|
||||
cd $SRC &&
|
||||
MODE=1 && # 0: Ultra debug info, 1: Extremely optimized release, else custom
|
||||
MODE=2 && # 0: Ultra debug info, 1: Extremely optimized release, else custom
|
||||
echo "Mode = $MODE" &&
|
||||
if [ $MODE = 0 ]; then
|
||||
em++ -O0 link.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sRUNTIME_DEBUG -sSTACK_OVERFLOW_CHECK=2 -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sASSERTIONS=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sDISABLE_EXCEPTION_CATCHING=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sPOLYFILL=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sENVIRONMENT=web,worker -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lembind -pthread -flto -msimd128 --embind-emit-tsd Vosklet.d.ts -fsanitize=undefined -fsanitize=address -fsanitize=leak -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -g3 --pre-js pre.js -o ../devel/Vosklet.js
|
||||
elif [ $MODE = 1 ]; then
|
||||
em++ -O3 link.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sENVIRONMENT=web,worker -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lembind -pthread -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext --pre-js pre.js -o ../devel/Vosklet.js
|
||||
else
|
||||
em++ -O3 link.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sENVIRONMENT=web,worker -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lembind -pthread -flto -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -g3 --pre-js pre.js -o ../devel/Vosklet.js
|
||||
em++ -O0 link.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sRUNTIME_DEBUG -sSTACK_OVERFLOW_CHECK=2 -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sASSERTIONS=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sDISABLE_EXCEPTION_CATCHING=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sPOLYFILL=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sENVIRONMENT=web,worker -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$KALDI/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L$OPENFST/lib -l:libfst.a -l:libfstngram.a -L$CLAPACK_WASM -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L$VOSK/src -l:vosk.a -lembind -pthread -flto -msimd128 -msimd128 -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -g3 --pre-js pre.js -o ../devel/Vosklet.js
|
||||
fi
|
||||
@@ -21,6 +21,7 @@ EMSCRIPTEN_BINDINGS() {
|
||||
.constructor<int, float, genericModel*>(allow_raw_pointers())
|
||||
.constructor<int, float, genericModel*, genericModel*>(allow_raw_pointers())
|
||||
.constructor<int, float, genericModel*, std::string, int>(allow_raw_pointers())
|
||||
.function("acceptWaveform", &recognizer::acceptWaveform, allow_raw_pointers())
|
||||
.function("reset", &recognizer::reset, allow_raw_pointers())
|
||||
.function("setEndpointerMode", &recognizer::setEndpointerMode, allow_raw_pointers())
|
||||
.function("setEndpointerDelays", &recognizer::setEndpointerDelays, allow_raw_pointers())
|
||||
|
||||
@@ -49,7 +49,7 @@ void genericModel::extractAndLoad(int tarStart, int tarSize) {
|
||||
archive_read_free(src);
|
||||
if(normalMdl) mdl = vosk_model_new(storepath.c_str());
|
||||
else vosk_spk_model_new(storepath.c_str());
|
||||
emscripten_console_log("Loading finished!");
|
||||
emscripten_console_log("Model loaded!");
|
||||
if(normalMdl ? std::get<0>(mdl) == nullptr : std::get<1>(mdl) == nullptr) fireEv(index, "Unable to load model for recognition");
|
||||
else fireEv(index, "0");
|
||||
};
|
||||
|
||||
11
src/link.cc
11
src/link.cc
@@ -1,6 +1,6 @@
|
||||
#include "link.h"
|
||||
|
||||
pthread_t dstThrd{pthread_self()};
|
||||
auto dstThrd = pthread_self();
|
||||
ProxyingQueue glbQ{};
|
||||
void fireEv(int index, const char* content, const char* type) {
|
||||
auto proxy{[index, content, type](){
|
||||
@@ -11,12 +11,3 @@ void fireEv(int index, const char* content, const char* type) {
|
||||
if(dstThrd == pthread_self()) proxy();
|
||||
else glbQ.proxySync(dstThrd, proxy);
|
||||
}
|
||||
void fireEv(int index, std::atomic_int* state, float* dataBuf) {
|
||||
auto proxy{[index, state, dataBuf](){
|
||||
EM_ASM({
|
||||
objs[$0].dispatchEvent(new CustomEvent("0", { "detail" : $1 + "," + $2}));
|
||||
}, index, state, dataBuf);
|
||||
}};
|
||||
if(dstThrd == pthread_self()) proxy();
|
||||
else glbQ.proxySync(dstThrd, proxy);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <thread>
|
||||
|
||||
#include <emscripten/em_asm.h>
|
||||
#include <emscripten/proxying.h>
|
||||
using namespace emscripten;
|
||||
|
||||
void fireEv(int index, const char* content, const char* type = nullptr); // Normal
|
||||
void fireEv(int index, std::atomic_int* state, float* dataBuf); // For recognizer's success initialization
|
||||
|
||||
37
src/pre.js
37
src/pre.js
@@ -29,13 +29,9 @@ class genericModel extends EventTarget {
|
||||
let mdl = new genericModel(url, storepath, id, normalMdl)
|
||||
let result = new Promise((resolve, reject) => {
|
||||
mdl.addEventListener("0", ev => {
|
||||
switch(ev.detail) {
|
||||
case "0":
|
||||
return resolve(mdl)
|
||||
default:
|
||||
if(ev.detail === "0") return resolve(mdl)
|
||||
mdl.delete()
|
||||
reject(ev.detail)
|
||||
}
|
||||
}, { once : true })
|
||||
})
|
||||
let tar
|
||||
@@ -93,12 +89,7 @@ class Recognizer extends EventTarget {
|
||||
let rec = new Recognizer()
|
||||
let result = new Promise((resolve, reject) => {
|
||||
rec.addEventListener("0", ev => {
|
||||
if(ev.detail.indexOf(",") !== -1) {
|
||||
let loadInfo = ev.detail.split(",")
|
||||
rec.state = Module.HEAP32.subarray(parseInt(loadInfo[0]), parseInt(loadInfo[0]) + 1) // State is an array with 1 element, there is no other way to get a reference to a single element
|
||||
rec.dataBuf = Module.HEAPF32.subarray(parseInt(loadInfo[1]), parseInt(loadInfo[1]) + 128)
|
||||
return resolve(rec)
|
||||
}
|
||||
if(ev.detail === "0") return resolve(rec)
|
||||
rec.delete()
|
||||
reject(ev.detail)
|
||||
}, { once : true })
|
||||
@@ -115,16 +106,15 @@ class Recognizer extends EventTarget {
|
||||
}
|
||||
return result
|
||||
}
|
||||
async getNode(ctx, channelIndex = 0) {
|
||||
async getNode(ctx) {
|
||||
if(typeof this.node === "undefined") {
|
||||
await ctx.audioWorklet.addModule(processorUrl)
|
||||
this.node = new AudioWorkletNode(ctx, 'VoskletProcessor', { channelCountMode: "max", numberOfInputs: 1, numberOfOutputs: 0, processorOptions: { dataBuf: this.dataBuf, state: this.state, channel: channelIndex }})
|
||||
await ctx.audioWorklet.addModule("../src/processor.js", { credentials : "omit"})
|
||||
this.node = new AudioWorkletNode(ctx, 'VoskletProcessor', { channelCountMode: "explicit", channelCount: 1, numberOfInputs: 1, numberOfOutputs: 1, processorOptions: { dataBuf: this.dataBuf, state: this.state }})
|
||||
}
|
||||
return this.node
|
||||
}
|
||||
recognize(buf, channelIndex = 0) {
|
||||
Module.HEAPF32.set(buf.getChannelData(channelIndex).subarray(0, 512), this.ptr)
|
||||
this.obj.acceptWaveForm()
|
||||
recognize(buf) {
|
||||
Module.HEAPF32.set(buf.getChannelData(0).subarray(0, 512), this.ptr)
|
||||
}
|
||||
delete() {
|
||||
if (this.obj) this.obj.delete()
|
||||
@@ -158,7 +148,7 @@ Module.makeRecognizerWithSpkModel = (model, sampleRate, spkModel) => {
|
||||
Module.makeRecognizerWithGrm = (model, sampleRate, grammar) => {
|
||||
return Recognizer._init(model.obj, sampleRate, 3, grammar, null)
|
||||
}
|
||||
let processorUrl = URL.createObjectURL(new Blob(['(',
|
||||
/*let processorURL = URL.createObjectURL(new Blob(['(',
|
||||
(() => {
|
||||
registerProcessor("VoskletProcessor", class extends AudioWorkletProcessor {
|
||||
constructor(options) {
|
||||
@@ -167,15 +157,14 @@ let processorUrl = URL.createObjectURL(new Blob(['(',
|
||||
this.state = options.processorOptions.state
|
||||
}
|
||||
process(inputs, outputs, params) {
|
||||
Atomics.wait(state, 0)
|
||||
inputs.copyFromChannel(this.dataBuf, this.channelIndex)
|
||||
|
||||
while(state[0])
|
||||
inputs.copyFromChannel(this.dataBuf, 0)
|
||||
return true
|
||||
}
|
||||
})
|
||||
}).toString()
|
||||
, ')()'], {type : "text/javascript"}))
|
||||
/*let pthreadUrl = URL.createObjectURL(new Blob(['(',
|
||||
}).toString(),
|
||||
')()'], {type : "text/javascript"}))
|
||||
let pthreadURL = URL.createObjectURL(new Blob(['(',
|
||||
(() => {
|
||||
{ PTHREAD_SCRIPT }
|
||||
}).toString()
|
||||
|
||||
@@ -1,39 +1,36 @@
|
||||
#include "recognizer.h"
|
||||
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model) : index{index}, sampleRate{sampleRate}, rec{vosk_recognizer_new(std::get<0>(model->mdl),sampleRate)} {
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model) : index{index}, rec{vosk_recognizer_new(std::get<0>(model->mdl),sampleRate)} {
|
||||
finishConstruction(model);
|
||||
}
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model, genericModel* spkModel) : index(index), sampleRate{sampleRate}, rec{vosk_recognizer_new_spk(std::get<0>(model->mdl), sampleRate, std::get<1>(spkModel->mdl))} {
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model, genericModel* spkModel) : index{index}, rec{vosk_recognizer_new_spk(std::get<0>(model->mdl), sampleRate, std::get<1>(spkModel->mdl))} {
|
||||
finishConstruction(model, spkModel);
|
||||
}
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model, const std::string& grm, int dummy) : index{index}, sampleRate{sampleRate}, rec{vosk_recognizer_new_grm(std::get<0>(model->mdl), sampleRate, grm.c_str())} {
|
||||
recognizer::recognizer(int index, float sampleRate, genericModel* model, const std::string& grm, int dummy) : index{index}, rec{vosk_recognizer_new_grm(std::get<0>(model->mdl), sampleRate, grm.c_str())} {
|
||||
finishConstruction(model);
|
||||
}
|
||||
recognizer::~recognizer() {
|
||||
done = true;
|
||||
vosk_recognizer_free(rec);
|
||||
}
|
||||
void recognizer::reset() {
|
||||
vosk_recognizer_reset(rec);
|
||||
}
|
||||
void recognizer::finishConstruction(genericModel* model, genericModel* spkModel) {
|
||||
if(rec == nullptr) {
|
||||
fireEv(index, "Unable to initialize recognizer");
|
||||
return;
|
||||
}
|
||||
auto main {[this](){
|
||||
emscripten_console_log("Recognizer loaded!");
|
||||
fireEv(index, &state, dataBuf);
|
||||
while(!done) {
|
||||
switch(vosk_recognizer_accept_waveform_f(rec, dataBuf, 512)) {
|
||||
while(!dataQ.empty()) {
|
||||
switch(vosk_recognizer_accept_waveform_f(rec, dataQ.front().data, dataQ.front().len)) {
|
||||
case 0:
|
||||
fireEv(index, vosk_recognizer_result(rec), "result");
|
||||
break;
|
||||
case 1:
|
||||
fireEv(index, vosk_recognizer_partial_result(rec), "partialResult");
|
||||
}
|
||||
state = 0;
|
||||
state.wait(0, std::memory_order_relaxed);
|
||||
free(dataQ.front().data);
|
||||
dataQ.pop();
|
||||
}
|
||||
}
|
||||
}};
|
||||
if(!model->resourceUsed) {
|
||||
@@ -54,6 +51,12 @@ void recognizer::finishConstruction(genericModel* model, genericModel* spkModel)
|
||||
std::thread t{main};
|
||||
t.detach();
|
||||
}
|
||||
void recognizer::acceptWaveform(int start, int len) {
|
||||
dataQ.emplace(start, len);
|
||||
}
|
||||
void recognizer::reset() {
|
||||
vosk_recognizer_reset(rec);
|
||||
}
|
||||
void recognizer::setEndpointerMode(VoskEndpointerMode mode) {
|
||||
vosk_recognizer_set_endpointer_mode(rec, mode);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
#pragma once
|
||||
#include "genericModel.h"
|
||||
#include <condition_variable>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
struct audioData {
|
||||
float* data;
|
||||
int len;
|
||||
audioData(int start, int len) : data{reinterpret_cast<float*>(start)}, len{len} {}
|
||||
};
|
||||
struct recognizer {
|
||||
std::atomic_bool done;
|
||||
std::atomic_int state; // 0: Copying data from JS, 1: Processing from C++
|
||||
float dataBuf[128];
|
||||
float sampleRate;
|
||||
std::queue<audioData> dataQ{};
|
||||
int index;
|
||||
VoskRecognizer* rec;
|
||||
recognizer(int index, float sampleRate, genericModel* model);
|
||||
@@ -14,6 +18,7 @@ struct recognizer {
|
||||
recognizer(int index, float sampleRate, genericModel* model, const std::string& grm, int dummy);
|
||||
~recognizer();
|
||||
void finishConstruction(genericModel* model, genericModel* spkModel = nullptr);
|
||||
void acceptWaveform(int start, int len);
|
||||
void reset();
|
||||
void setEndpointerMode(VoskEndpointerMode mode);
|
||||
void setEndpointerDelays(float tStartMax, float tEnd, float tMax);
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
## ```Recognizer``` object
|
||||
| Function/Object | Description |
|
||||
|---|---|
|
||||
| ```Promise<AudioWorkletNode> getNode(ctx: AudioContext, channelIndex = 0: int)``` | Get a node that process audio. It has 1 input and no output, **channelIndex** must point to a 16-bit mono channel of the input |
|
||||
| ```recognize(buf: AudioBuffer, channelIndex = 0: int)``` | Recognize an AudioBuffer, usually from something like ```BaseAudioContext.decodeAudioData()```, **channelIndex** must point to a 16-bit mono channel of **buf**
|
||||
| ```acceptWaveform(buf: AudioBuffer || Float32Array )``` | Recognize an AudioBuffer, usually from something like ```BaseAudioContext.decodeAudioData()```, **channelIndex** must point to a 16-bit mono channel of **buf**
|
||||
| ```setPartialWords(partialWords: bool)``` | See Vosk's description (default: false) |
|
||||
| ```setWords(words: bool)``` | See Vosk's description (default: false) |
|
||||
| ```setNLSML(nlsml: bool)``` | See Vosk's description (default: false) |
|
||||
|
||||
Reference in New Issue
Block a user