Large commit

This commit is contained in:
msqr1
2024-03-24 00:58:13 -07:00
parent 638de69e67
commit 8bb9733cd1
14 changed files with 4203 additions and 4966 deletions

6
devel/Vosklet.d.ts vendored
View File

@@ -12,6 +12,11 @@ interface WasmModule {
___set_stack_limits(_0: number, _1: number): void;
}
export interface epModeValue<T extends number> {
value: T;
}
export type epMode = epModeValue<0>|epModeValue<1>|epModeValue<2>|epModeValue<3>;
export interface genericModel {
extractAndLoad(_0: number, _1: number): void;
delete(): void;
@@ -29,6 +34,7 @@ export interface recognizer {
}
interface EmbindModule {
epMode: {ANSWER_DEFAULT: epModeValue<0>, ANSWER_SHORT: epModeValue<1>, ANSWER_LONG: epModeValue<2>, ANSWER_VERY_LONG: epModeValue<3>};
genericModel: {new(_0: number, _1: boolean, _2: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _3: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): genericModel};
recognizer: {new(_0: number, _1: number, _2: genericModel): recognizer; new(_0: number, _1: number, _2: genericModel, _3: genericModel): recognizer; new(_0: number, _1: number, _2: genericModel, _3: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _4: number): recognizer};
setLogLevel(_0: number): void;

File diff suppressed because one or more lines are too long

View File

@@ -15,10 +15,6 @@ 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);
@@ -27,13 +23,8 @@ 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.
@@ -82,19 +73,14 @@ function handleMessage(e) {
// 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['wasmOffsetData'] = e.data.wasmOffsetConverter;
Module['buffer'] = Module['wasmMemory'].buffer;
Module['workerID'] = e.data.workerID;
Module['ENVIRONMENT_IS_PTHREAD'] = true;
if (typeof e.data.urlOrBlob == 'string') {
@@ -113,7 +99,6 @@ function handleMessage(e) {
// 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);
@@ -135,7 +120,6 @@ function handleMessage(e) {
// 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']()) {
@@ -155,8 +139,6 @@ function handleMessage(e) {
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;
}

View File

@@ -1,4 +1,4 @@
# Build the js file into this directory, debug mode or MODE mode
# Build the js file into this directory
cd .. &&
SRC=$(realpath src) &&
@@ -9,15 +9,15 @@ LIBARCHIVE=$(realpath libarchive) &&
CLAPACK_WASM=$(realpath clapack-wasm) &&
MAX_MEMORY=${MAX_MEMORY:-500mb} &&
MAX_THREADS=${MAX_THREADS:-2} &&
MAX_THREADS=${MAX_THREADS:-1} &&
EMSDK=${EMSDK:-$(realpath emsdk)} &&
if [ ! -d $EMSDK ]; then
echo "Invalid EMSDK path"
exit 1
fi
if [ $MAX_THREADS -lt 2 ]; then
echo "MAX_THREAD must be greater or equal to 2" &&
if [ $MAX_THREADS -lt 1 ]; then
echo "MAX_THREAD must be greater than or equal to 1" &&
exit 1
fi
if ! [[ $MAX_MEMORY =~ ^[0-9]+([kmgt]b)?$ ]]; then
@@ -28,12 +28,12 @@ fi
. $EMSDK/emsdk_env.sh &&
cd $SRC &&
MODE=0 && # 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 proxier.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sGZIP_EMBEDDINGS -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 -g3 --pre-js pre.js -o ../devel/Vosklet.js
em++ -O0 proxier.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 -g3 --pre-js pre.js -o ../devel/Vosklet.js
elif [ $MODE = 1 ]; then
em++ -O3 proxier.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sGZIP_EMBEDDINGS -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 --pre-js pre.js -o ../devel/Vosklet.js
em++ -O3 proxier.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 --pre-js pre.js -o ../devel/Vosklet.js
else
em++ -O0 proxier.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sGZIP_EMBEDDINGS -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sERROR_ON_UNDEFINED_SYMBOLS=0 -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 --pre-js pre.js -o ../devel/Vosklet.js
em++ -O3 proxier.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 -g3 --pre-js pre.js -o ../devel/Vosklet.js
fi

View File

@@ -20,10 +20,9 @@ CLAPACK_WASM:=$(realpath clapack-wasm)
Vosklet.js: | vosk libarchive
cd $(SRC) && \
em++ -O3 global.cc genericModel.cc model.cc spkModel.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 -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 -lopfs.js -lembind -pthread -flto -msimd128 --pre-js pre.js -o ../Vosklet.js && \
em++ -O3 proxier.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS \ -sWASM_BIGINT -sSINGLE_FILE -sGZIP_EMBEDDINGS -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 -lopfs.js -lembind -pthread -flto -msimd128 --pre-js pre.js -o ../Vosklet.js && \
cd .. && \
rm -f Vosklet.worker.js && \
sed -i 's/locateFile("Vosklet.worker.js")/pthreadUrl/g' Vosklet.js && \
prepare:
sudo apt install shtool libtool autogen autotools-dev pkg-config make && \

View File

@@ -6,6 +6,13 @@ using namespace emscripten;
EMSCRIPTEN_BINDINGS() {
function("setLogLevel", &vosk_set_log_level, allow_raw_pointers());
enum_<VoskEndpointerMode>("epMode")
.value("ANSWER_DEFAULT", VOSK_EP_ANSWER_DEFAULT)
.value("ANSWER_SHORT", VOSK_EP_ANSWER_SHORT)
.value("ANSWER_LONG", VOSK_EP_ANSWER_LONG)
.value("ANSWER_VERY_LONG", VOSK_EP_ANSWER_VERY_LONG);
class_<genericModel>("genericModel")
.constructor<int, bool, std::string, std::string>(allow_raw_pointers())
.function("extractAndLoad", &genericModel::extractAndLoad, allow_raw_pointers());

View File

@@ -5,20 +5,22 @@ void genericModel::extractAndLoad(int tarStart, int tarSize) {
static fs::path path{};
static int fd{};
thrd.addTask([this, tarStart, tarSize](){
emscripten_console_log("Untaring...");
archive* src {archive_read_new()};
archive_read_support_format_tar(src);
archive_read_open_memory(src, (void*)tarStart, tarSize);
free((void*)tarStart);
archive_read_open_memory(src, reinterpret_cast<void*>(tarStart), tarSize);
if(archive_errno(src) != 0) {
emscripten_console_logf("Unable to open tar in WASM memory: %s", archive_error_string(src));
free(reinterpret_cast<void*>(tarStart));
fireEv(index, "Unable to open tar in WASM memory");
return;
}
while(1) {
if(archive_read_next_header2(src, entry) != ARCHIVE_OK) {
emscripten_console_logf("Tar header read failed: %s", archive_error_string(src));
fireEv(index, "Tar header read failed");
return;
int headerRes {archive_read_next_header2(src, entry)};
if(headerRes == ARCHIVE_EOF) break;
if(headerRes < ARCHIVE_OK) {
fireEv(index, archive_error_string(src));
break;
}
path = archive_entry_pathname(entry);
path = storepath + path.generic_string().substr(path.generic_string().find("/"));
@@ -41,13 +43,14 @@ void genericModel::extractAndLoad(int tarStart, int tarSize) {
return;
}
}
free(reinterpret_cast<void*>(tarStart));
fs::remove(storepath + "/README");
archive_read_free(src);
/*if(normalMdl) mdl = vosk_model_new(storepath.c_str());
if(normalMdl) mdl = vosk_model_new(storepath.c_str());
else vosk_spk_model_new(storepath.c_str());
emscripten_console_log("Loading finished!");
if(normalMdl ? std::get<0>(mdl) == nullptr : std::get<1>(mdl) == nullptr) fireEv(index, "Unable to load model for recognition");
else fireEv(index, "0");*/
else fireEv(index, "0");
});
}
genericModel::~genericModel() {

View File

@@ -1,10 +1,10 @@
#pragma once
#include "proxier.h"
#include "jsLink.h"
#include <filesystem>
#include <fstream>
#include <fcntl.h>
#include <variant>
#include <fcntl.h>
#include <vosk_api.h>
#include <archive.h>
@@ -17,7 +17,6 @@ struct genericModel {
std::string storepath;
std::string id;
std::variant<VoskModel*, VoskSpkModel*> mdl;
reusableThrd thrd;
archive_entry* entry;
void extractAndLoad(int tarStart, int tarSize);
genericModel(int index, bool normalMdl, std::string storepath, std::string id);

View File

@@ -1,4 +1,4 @@
#include "proxier.h"
#include <jsLink.h>
pthread_t dstThrd{pthread_self()};
ProxyingQueue glbQ{};
@@ -13,21 +13,3 @@ void fireEv(int index, const char* content, const char* type) {
if(dstThrd == pthread_self()) proxy();
else glbQ.proxySync(dstThrd, proxy);
}
reusableThrd::reusableThrd() {
std::thread thrd{[this](){
while(!done) {
while(!queue.empty()) {
emscripten_console_log("==========Executing task===========");
queue.front()();
queue.pop();
}
}
}};
thrd.detach();
}
void reusableThrd::addTask(std::function<void()>&& task) {
queue.emplace(task);
}
reusableThrd::~reusableThrd() {
done = true;
}

View File

@@ -9,14 +9,4 @@
#include <emscripten/proxying.h>
using namespace emscripten;
extern pthread_t dstThrd;
extern ProxyingQueue glbQ;
void fireEv(int index, const char* content, const char* type = nullptr);
struct reusableThrd {
std::queue<std::function<void()>> queue{};
bool done{};
void addTask(std::function<void()>&& task);
reusableThrd();
~reusableThrd();
};

View File

@@ -27,22 +27,26 @@ class genericModel extends EventTarget {
}
static async _init(url, storepath, id, normalMdl) {
let mdl = new genericModel(url, storepath, id, normalMdl)
let tar
mdl.addEventListener("0", async (ev) => {
let result = new Promise((resolve, reject) => {
mdl.addEventListener("0", ev => {
switch(ev.detail) {
case "0":
return mdl
return resolve(mdl)
default:
mdl.delete()
throw ev.detail
reject(ev.detail)
}
}, { once : true })
})
let tar
mdl.obj = new Module.genericModel(objs.length - 1, normalMdl, "/" + storepath, id)
try {
console.log("Getting Data file")
let dataFile = await (await getFileHandle(storepath + "/model.tgz")).getFile()
console.log("Getting ID file")
let idFile = await (await getFileHandle(storepath + "/id")).getFile()
if(await idFile.text() !== id) throw ""
tar = await new Response(dataFile.stream().pipeThrough(new DecompressionStream("gzip"))).arrayBuffer()
tar = dataFile.stream()
}
catch {
try {
@@ -50,22 +54,25 @@ class genericModel extends EventTarget {
if(!res.ok) {
throw "Unable to download model"
}
let teedBody = res.body.tee()
let newDataFile = await (await getFileHandle(storepath + "/model.tgz", true)).createWritable()
tar = await new Response(res.body.pipeThrough(new DecompressionStream("gzip"))).arrayBuffer()
await newDataFile.write(tar)
await newDataFile.write(await new Response(teedBody[0]).arrayBuffer())
await newDataFile.close()
let newIDFile = await (await getFileHandle(storepath + "/id", true)).createWritable()
await newIDFile.write(id)
await newIDFile.close()
tar = teedBody[1]
}
catch(e) {
mdl.obj.delete()
throw e
}
}
tar = await new Response(tar.pipeThrough(new DecompressionStream("gzip"))).arrayBuffer()
let tarStart = Module._malloc(tar.byteLength)
Module.HEAPU8.set(new Uint8Array(tar), tarStart)
mdl.obj.extractAndLoad(tarStart, tar.byteLength)
return result
}
delete() {
if (this.obj) this.obj.delete()
@@ -84,33 +91,34 @@ class Recognizer extends EventTarget {
}
static async _init(model, sampleRate, mode, grammar, spkModel) {
let rec = new Recognizer()
rec.addEventListener("0", (ev) => {
if(ev.detail === "0") {
rec.ptr = Module._malloc(512)
return rec
let result = new Promise((resolve, reject) => {
rec.addEventListener("0", ev => {
if(ev.detail.indexOf(",") === -1) {
let loadInfo = ev.detail.split(",")
rec.dataBuf = Module.HEAPF32.subarray(parseInt(loadInfo[0]), parseInt(loadInfo[0]) + 128)
rec.state = Module.HEAP8.subarray(parseInt(loadInfo[1]), parseInt(loadInfo[1]) + 1) // State is an array with 1 element, there is no other way to get a reference to a single element
return resolve(rec)
}
rec.delete()
throw ev.detail
reject(rec)
}, { once : true })
})
switch(mode) {
case 1:
rec.obj = new Module.recognizer(model, sampleRate, objs.length-1)
rec.obj = new Module.recognizer(objs.length - 1, sampleRate, model)
break
case 2:
rec.obj = new Module.recognizer(model, spkModel, sampleRate, objs.length-1)
rec.obj = new Module.recognizer(objs.length -1, sampleRate, model, spkModel)
break
default:
rec.obj = new Module.recognizer(model, grammar, sampleRate, objs.length-1, 0)
rec.obj = new Module.recognizer(objs.length - 1, sampleRate, grammar, 0)
}
return result
}
async getNode(ctx, channelIndex = 0) {
if(typeof this.node === "undefined") {
let msgChannel = new MessageChannel()
await ctx.audioWorklet.addModule(processorUrl)
this.node = new AudioWorkletNode(ctx, 'BRProcessor', { channelCountMode: "max", numberOfInputs: 1, numberOfOutputs: 1, processorOptions: { ptr: this.ptr, channel: channelIndex, recognizerPort: msgChannel.port1 } })
msgChannel.port1.onmessage = () => {
this.obj.acceptWaveForm()
}
this.node = new AudioWorkletNode(ctx, 'VoskletProcessor', { channelCountMode: "max", numberOfInputs: 1, numberOfOutputs: 0, processorOptions: { dataBuf: this.dataBuf, state: this.state, channel: channelIndex }})
}
return this.node
}
@@ -152,19 +160,16 @@ Module.makeRecognizerWithGrm = (model, sampleRate, grammar) => {
}
let processorUrl = URL.createObjectURL(new Blob(['(',
(() => {
registerProcessor("BRProcessor", class extends AudioWorkletProcessor {
registerProcessor("VoskletProcessor", class extends AudioWorkletProcessor {
constructor(options) {
this.done = false
this.port.onmessage = (ev) => this.done = true
this.ptr = options.processorOptions.ptr
this.recognizerPort = options.processorOptions.recognizerPort
this.channelIndex = options.processorOptions.channelIndex
this.dataBuf = options.processorOptions.dataBuf
this.state = options.processorOptions.state
}
process(inputs, outputs, params) {
if(this.done) return false
this.wasmMem.set(inputs[0].getChannelData(this.channelIndex))
this.recognizerPort.postMessage("0")
outputs = inputs
while(state[0])
inputs.copyFromChannel(this.dataBuf, this.channelIndex)
state[0] = 1
return true
}
})

View File

@@ -1,19 +1,16 @@
#include "recognizer.h"
recognizer::recognizer(int index, float sampleRate, genericModel* model) : index(index) {
emscripten_console_log("Recognizer constructor called...");
rec = vosk_recognizer_new(std::get<0>(model->mdl),sampleRate);
finishConstruction(model, nullptr);
finishConstruction(model);
}
recognizer::recognizer(int index, float sampleRate, genericModel* model, genericModel* spkMdl) : index(index) {
emscripten_console_log("Recognizer constructor called...");
rec = vosk_recognizer_new_spk(std::get<0>(model->mdl), sampleRate, std::get<1>(spkMdl->mdl));
finishConstruction(model, spkMdl);
}
recognizer::recognizer(int index, float sampleRate, genericModel* model, const std::string& grm, int dummy) : index(index) {
emscripten_console_log("Recognizer constructor called...");
rec = vosk_recognizer_new_grm(std::get<0>(model->mdl), sampleRate, grm.c_str());
finishConstruction(model, nullptr);
finishConstruction(model);
}
recognizer::~recognizer() {
done.test_and_set(std::memory_order_relaxed);
@@ -21,27 +18,30 @@ recognizer::~recognizer() {
controller.test_and_set(std::memory_order_relaxed);
controller.notify_one();
vosk_recognizer_free(rec);
free(dataPtr);
}
void recognizer:: finishConstruction(genericModel* model, genericModel* spkModel) {
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, "0");
std::string loadInfo{};
loadInfo += reinterpret_cast<int>(dataBuf);
loadInfo += ",";
loadInfo += reinterpret_cast<int>(&state);
fireEv(index, loadInfo.c_str());
while(!done.test(std::memory_order_relaxed)) {
controller.wait(!done.test(std::memory_order_relaxed), std::memory_order_relaxed);
controller.clear(std::memory_order_relaxed);
if(done.test(std::memory_order_relaxed)) continue;
switch(vosk_recognizer_accept_waveform_f(rec, dataPtr, 512)) {
switch(vosk_recognizer_accept_waveform_f(rec, dataBuf, 512)) {
case 0:
fireEv(index, vosk_recognizer_result(rec), "result");
break;
case 1:
fireEv(index, vosk_recognizer_partial_result(rec), "partialResult");
}
state = 0;
}
}};
if(!model->recognizerUsedThrd) {
@@ -60,6 +60,12 @@ void recognizer:: finishConstruction(genericModel* model, genericModel* spkModel
std::thread t{main};
t.detach();
}
void recognizer::setEndpointerMode(VoskEndpointerMode mode) {
vosk_recognizer_set_endpointer_mode(rec, mode);
}
void recognizer::setEndpointerDelays(float tStartMax, float tEnd, float tMax) {
vosk_recognizer_set_endpointer_delays(rec, tStartMax, tEnd, tMax);
}
void recognizer::acceptWaveForm() {
controller.test_and_set(std::memory_order_relaxed);
controller.notify_one();

View File

@@ -4,14 +4,17 @@
struct recognizer {
std::atomic_flag done{};
std::atomic_flag controller{};
float* dataPtr;
float dataBuf[128];
char state; // 0: Copying data from JS, 1: Processing from C++
int index;
VoskRecognizer* rec;
recognizer(int index, float sampleRate, genericModel* model);
recognizer(int index, float sampleRate, genericModel* model, genericModel* spkModel);
recognizer(int index, float sampleRate, genericModel* model, const std::string& grm, int dummy);
~recognizer();
void finishConstruction(genericModel* model, genericModel* spkModel);
void finishConstruction(genericModel* model, genericModel* spkModel = nullptr);
void setEndpointerMode(VoskEndpointerMode mode);
void setEndpointerDelays(float tStartMax, float tEnd, float tMax);
void acceptWaveForm();
void setSpkModel(genericModel* model);
void setGrm(const std::string& grm);

View File

@@ -1,33 +1,36 @@
# API interface
## JS' ```window``` object
| Function signature | Description |
| Function/Object | Description |
|---|---|
|```Promise<Module> loadVosklet()``` | Load Emscripten's Module |
|```Promise<Module> loadVosklet()``` | Load Vosklet module interface |
## Shared interface
| Function signature | Description |
| Function/Object | Description |
|---|---|
| ```delete()``` | Delete this object, see [why](https://emscripten.org/docs/getting_started/FAQ.html#what-does-exiting-the-runtime-mean-why-don-t-atexit-s-run) this is neccessary.
| ```delete()``` | Delete this object, see [why](https://emscripten.org/docs/getting_started/FAQ.html#what-does-exiting-the-runtime-mean-why-don-t-atexit-s-run) this is neccessary. |
## ```Module``` object
| Function signature | Description |
| Function/Object | Description |
|---|---|
| ```Promise<Model> makeModel(path: string, url: string, id: string)```<br><br>```Promise<SpkModel> makeSpkModel(path: string, url: string, id: string)``` | Make a ```Model``` or ```SpkModel```, model files must be directly under the model root, and compressed model must be in .tgz format. If:<br>- **path** contains valid model files and **id** is the same, there will not be a fetch from **url**.<br>- **path** doesn't contain valid model files, or if it contains valid model files but **id** is different, there will be a fetch from **url**, and the model is stored with **id**. Models are thread-safe, reuse them as much as possible! |
| ```Promise<Recognizer> makeRecognizer(model: Model, sampleRate: float)```<br><br>```Promise<Recognizer> makeRecognizerWithSpkModel(model: Model, spkModel: spkModel, sampleRate: float)```<br><br>```Promise<Recognizer> makeRecognizerWithGrm(model: Model, grammar: string, sampleRate: float)``` | Make a ```Recognizer```, it will use **model**'s thread if it's the first user of **model**, else it will use a new thread. |
| ```setLogLevel(lvl: int)``` | Set Vosk's log level (default: ```0```: Info) <br>```-2```: Error<br>```-1```: Warning<br>```1```: Verbose<br>```2```: More verbose<br>```3```: Debug |
| ```cleanUp()``` | A convenience function that call ```delete()``` on all objects and revoke all URLs. **Put this at the end of your code!** |
| ```epMode``` | Endpointer modes (enum) | See Vosk's description |
## ```Recognizer``` object
| Function signature | Description |
| Function/Object | Description |
|---|---|
| ```Promise<AudioWorkletNode> getNode(ctx: AudioContext, channelIndex = 0: int)``` | Get a pass-through node that recognize audio and is connectable to a processing graph. It has 1 input and 1 output, **channelIndex** must point to a 16-bit mono channel of the input |
| ```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**
| ```setPartialWords(partialWords: bool)``` | Return words' information in a partialResult event (default: false) |
| ```setWords(words: bool)``` | Return words' information in a result event (default: false) |
| ```setNLSML(nlsml: bool)``` | Return result and partialResult in NLSML form (default: false) |
| ```setMaxAlternatives(alts: int)``` | Set the max number of alternatives for result event (default: false) |
| ```setGrm(grm: string)``` | Set the grammar of the recognizer, override (default: none) |
| ```setSpkModel(mdl: SpkModel)``` | Set the speaker model of the recognizer (default: none) |
| ```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) |
| ```setMaxAlternatives(alts: int)``` | See Vosk's description (default: false) |
| ```setGrm(grm: string)``` | See Vosk's description (default: none) |
| ```setSpkModel(mdl: SpkModel)``` | See Vosk's description (default: none) |
| ```setEndpointerMode(mode: epMode)``` | See Vosk's description (default: ANSWER_DEFAULT) |
| ```setEndpointerDelays(tStartMax: float, tEnd: float, tMax: float)``` | See Vosk's description |
| Event | Description |
|---|---|
@@ -40,12 +43,7 @@ Vosklet require SharedArrayBuffer to share thread's data, so these response head
- ***Cross-Origin-Embedder-Policy*** ---> ***require-corp***
- ***Cross-Origin-Opener-Policy*** ---> ***same-origin***
If you can't set them, you may use a HACKY workaround at *src/addCOI.js*.
## Origin Private Filesystem (OPFS)
Vosklet needs the Emscripten WASMFS' OPFS to store its model, IDBFS was considered. For safety with this, always:
- Try catch ```window.loadVosklet()``` to to check for OPFS availability.
- Check if there is enough space via ```navigator.storage.estimate()``` for TWICE THE MODEL SIZE before calling ```Module.makeModel```
If you can't set them, you may use a hacky workaround at *src/addCOI.js*.
# Compilation
Changing any option to non-default values requires recompilation
@@ -57,6 +55,6 @@ cd Vosklet/src &&
| Option | Description | Default value |
|---|---|---|
| MAX_MEMORY | Set max memory, valid suffixes: kb, mb, gb, tb or none (bytes) | ```300mb```, as [recommended](https://alphacephei.com/vosk/models) |
| MAX_THREADS | Set the max number of thread (2 min) | ```2``` (1 OPFS thread + 1 model/recognizer thread) |
| MAX_THREADS | Set the max number of thread, this should be equal to the number of model or speaker model that is used in the program (>1) | ```1``` |
| COMPILE_JOBS | Set the number of jobs (threads) when building | ```$(nproc)``` |
| EMSDK | Set EMSDK's path (will install EMSDK in root folder if unset) | ```../emsdk``` |