diff --git a/README.md b/README.md index 224905d..d7cb967 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ # Browser-recognizer - A speech recognizer built on Vosk that can be run on the browser, inspired by [vosk-browser](https://github.com/ccoreilly/vosk-browser), but built from scratch and no code taken! -- Browser-recognizer can run both in the browser main thread and web workers - The API is also designed with strong exception safety ## Global and all objects' common interface | Function signature (global) | Description | |---|---| | ```Promise makeModel(path: string, url: string, id: string)```

```Promise makeSpkModel(path: string, url: string, id: string)``` | Make a ```Model``` or ```SpkModel```
- If **path** contains valid model files and **id** is the same, there will not be a fetch from **url**.
- If **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**. | -| ```Promise makeRecognizer(model: Model, sampleRate: float)``` | Make a ```Recognizer```, it will use **model**'s thread if it's the first to use **model**, else it will use a new thread. -| ```setLogLevel(lvl: int)``` | Set Vosk's log level (default: -1)
- 2: Error
- 1: Warning
- 0: Info
- 1: Verbose
- 2: More verbose
- 3: Debug | +| ```Promise makeRecognizer(model: Model, 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)
```-2```: Error
```-1```: Warning
```1```: Verbose
```2```: More verbose
```3```: Debug | | ```cleanUp()``` | Call ```delete()``` on all objects and revoke all Blob URLs. | | Function signature (all objects) | 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. ## ```Recognizer``` object | Function signature | Description | |---|---| @@ -41,7 +40,7 @@ cd Browser-recognizer && | 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) | | COMPILE_JOBS | Set the number of jobs (threads) when compiling | ```$(nproc)``` | -| EMSDK | Set EMSDK's path (will install EMSDK in root folder if unset) | ```emsdk``` | +| EMSDK | Set EMSDK's path (will install EMSDK in root folder if unset) | ```../emsdk``` | ## Response headers Browser-recognizer require SharedArrayBuffer, so these response headers must be set: - ***Cross-Origin-Embedder-Policy*** ---> ***require-corp*** @@ -57,7 +56,6 @@ If you can't set them, you may use a VERY HACKY workaround at *src/addCOI.js*. ``` - ``` diff --git a/emsdk b/emsdk new file mode 160000 index 0000000..2aa7490 --- /dev/null +++ b/emsdk @@ -0,0 +1 @@ +Subproject commit 2aa74907151b2caa9da865fd0d36436fdce792f0 diff --git a/src/Makefile b/src/Makefile index fd75127..ca665de 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,97 +5,95 @@ # #### # # ### #### # #### ##### #### # #### # # #### # 1 hour build time -sudo apt install shtool libtool autogen autotools-dev pkg-config make && +SHELL=/bin/bash +MAX_MEMORY?=300mb +MAX_THREADS?=2 +EMSDK?=../emsdk +COMPILE_JOBS?:=$(nproc) -MAX_MEMORY?=300mb && -MAX_THREADS?=2 && -EMSDK?=$(realpath emsdk) && -COMPILE_JOBS?=$(nproc) && +SRC:=$(realpath src) +KALDI:=$(realpath kaldi) +VOSK:=$(realpath vosk-api) +OPENFST:=$(KALDI)/tools/openfst +LIBARCHIVE:=$(realpath libarchive) +ZSTD:=$(realpath zstd) +CLAPACK_WASM:=$(realpath clapack-wasm) -SRC=$(realpath src) && -KALDI=$(realpath kaldi) && -VOSK=$(realpath vosk-api) && -OPENFST=$KALDI/tools/openfst && -LIBARCHIVE=$(realpath libarchive) && -ZSTD=$(realpath zstd) && -CLAPACK_WASM=$(realpath clapack-wasm) && +browser-recognizer: vosk libarchive + cd $(SRC) && \ + em++ -O3 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sTRUSTED_TYPES -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 -sEXPORT_NAME=loadBR -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sMALLOC=emmaloc -sEXPORTED_FUNCTIONS=_malloc,_main -sENVIRONMENT=web -I. -I$(LIBARCHIVE)/include -I$(VOSK)/src -L$(LIBARCHIVE)/lib -larchive -L$(ZSTD)/lib -lzstd -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 --pre-js pre.js ../BrowserRecognizer.js && \ + cd .. && \ + rm -f BrowserRecognizer.worker.js && \ + sed -i 's/locateFile("BrowserRecognizer.worker.js")/pthreadUrl/g' BrowserRecognizer.js && \ + sed -i 's/let root/var root/g' BrowserRecognizer.js -if [ ! -d $EMSDK_PATH ]; then - echo "Invalid EMSDK path" && - exit 1 -fi -if [ $MAX_THREAD -lt 2 ]; then - echo "MAX_THREADS must be greater than or equal to 2" && - exit 1 -fi -if [ $COMPILE_JOBS -lt 1 ]; then - echo "COMPILE_JOBS must be greater than or equal to 1" && - exit 1 -fi -if ! [[ $MAX_MEMORY =~ ^[0-9]+([kmgt]b)?$ ]]; then - echo "MAX_MEMORY valid suffixes are kb, mb, gb, tb, none (bytes)" && - exit 1 -fi -if [ $(realpath $EMSDK) == $(realpath emsdk) ]; then - echo "EMSDK is current directory, installing emsdk and Emscripten..." && - git clone --depth=1 https://github.com/emscripten-core/emsdk.git && - cd emsdk && - ./emsdk install 3.1.53 && - ./emsdk activate 3.1.53 -fi +prepare: + sudo apt install shtool libtool autogen autotools-dev pkg-config make && \ + [ $(EMSDK) != emsdk -a ! -d $(EMSDK) ] && \ + echo "Invalid emsdk path"; \ + exit 1; \ + [ $(MAX_THREAD) -lt 2 ] && \ + echo "MAX_THREADS must be greater than or equal to 2"; \ + exit 1; \ + [ $(COMPILE_JOBS) -lt 1 ] && \ + echo "COMPILE_JOBS must be greater than or equal to 1"; \ + exit 1; \ + [[ ! $(MAX_MEMORY) =~ "$(^[0-9]+([kmgt]b)?$$)" ]] && \ + echo "MAX_MEMORY valid suffixes are kb, mb, gb, tb, none (bytes)"; \ + exit 1; \ + [ $(EMSDK) = emsdk ] && \ + echo "Installing emsdk + Emscripten..."; \ + git clone --depth=1 https://github.com/emscripten-core/emsdk.git && \ + cd emsdk && \ + ./emsdk install 3.1.53 && \ + ./emsdk activate 3.1.53; \ + . $(EMSDK)/emsdk_env.sh && \ + export PATH=:$$PATH:$(EMSDK)/upstream/bin -. $EMSDK/emsdk_env.sh && -export PATH=:$PATH:$EMSDK/upstream/bin && +zstd: prepare + rm -rf /tmp/zstd && \ + git clone -b v1.5.5 --depth=1 https://github.com/facebook/zstd /tmp/zstd && \ + cd /tmp/zstd && \ + HAVE_THREAD=0 ZSTD_LEGACY_SUPPORT=0 HAVE_ZLIB=0 HAVE_LZMA=0 HAVE_LZ4=0 ZSTD_NOBENCH=1 ZSTD_NODICT=1 ZSTD_NOCOMPRESS=1 BACKTRACE=0 PREFIX=$(ZSTD) CPPFLAGS="-O3 -flto" LDFLAGS="-O3 -flto" emmake make -j$(COMPILE_JOBS) install && \ + rm -rf /tmp/zstd -zstd: - rm -rf /tmp/zstd && - git clone -b v1.5.5 --depth=1 https://github.com/facebook/zstd /tmp/zstd && - cd /tmp/zstd && - HAVE_THREAD=0 ZSTD_LEGACY_SUPPORT=0 HAVE_ZLIB=0 HAVE_LZMA=0 HAVE_LZ4=0 ZSTD_NOBENCH=1 ZSTD_NODICT=1 ZSTD_NOCOMPRESS=1 BACKTRACE=0 PREFIX=$ZSTD CPPFLAGS="-O3 -flto" LDFLAGS="-O3 -flto" emmake make -j$COMPILE_JOBS install && - rm -rf /tmp/zstd && +libarchive: zstd + rm -rf /tmp/libarchive && \ + git clone -b v3.7.2 --depth=1 https://github.com/libarchive/libarchive /tmp/libarchive && \ + cd /tmp/libarchive && \ + build/autogen.sh && \ + CPPFLAGS="-I$(ZSTD)/include -flto" LDFLAGS="-L$(ZSTD)/lib -flto" emconfigure ./configure --prefix=$(LIBARCHIVE) --without-lz4 --without-lzma --without-zlib --without-bz2lib --without-xml2 --without-expat --without-cng --without-openssl --without-libb2 --disable-bsdunzip --disable-xattr --disable-acl --disable-bsdcpio --disable-bsdcat --disable-rpath --disable-maintainer-mode --disable-dependency-tracking --enable-static --disable-shared && \ + emmake make -j$(COMPILE_JOBS) install && \ + rm -rf /tmp/libarchive -libarchive: - rm -rf /tmp/libarchive && - git clone -b v3.7.2 --depth=1 https://github.com/libarchive/libarchive /tmp/libarchive && - cd /tmp/libarchive && - build/autogen.sh && - CPPFLAGS="-I$ZSTD/include -flto" LDFLAGS="-L$ZSTD/lib -flto" emconfigure ./configure --prefix=$LIBARCHIVE --without-lz4 --without-lzma --without-zlib --without-bz2lib --without-xml2 --without-expat --without-cng --without-openssl --without-libb2 --disable-bsdunzip --disable-xattr --disable-acl --disable-bsdcpio --disable-bsdcat --disable-rpath --disable-maintainer-mode --disable-dependency-tracking --enable-static --disable-shared && - emmake make -j$COMPILE_JOBS install && - rm -rf /tmp/libarchive && +clapack-wasm: prepare + git clone --depth=1 https://gitlab.inria.fr/multispeech/kaldi.web/clapack-wasm.git $(CLAPACK_WASM) && \ + cd $(CLAPACK_WASM) && \ + bash ./install_repo.sh emcc -clapack-wasm: - git clone --depth=1 https://gitlab.inria.fr/multispeech/kaldi.web/clapack-wasm.git $CLAPACK_WASM && - cd $CLAPACK_WASM && - bash ./install_repo.sh emcc && - -openfst: - rm -rf /tmp/openfst && - git clone --depth=1 https://github.com/alphacep/openfst /tmp/openfst && - cd /tmp/openfst && - autoreconf -i && - CXXFLAGS="-pthread -r -O3 -flto" LDFLAGS="-O3 -pthread -flto" emconfigure ./configure --prefix=$OPENFST --enable-static --disable-shared --enable-ngram-fsts --enable-lookahead-fsts --disable-bin --with-pic && - emmake make -j$COMPILE_JOBS install && - echo "PACKAGE_VERSION = 1.8.0" >> $OPENFST/Makefile && - rm -rf /tmp/openfst && +openfst: prepare + rm -rf /tmp/openfst && \ + git clone --depth=1 https://github.com/alphacep/openfst /tmp/openfst && \ + cd /tmp/openfst && \ + autoreconf -i && \ + CXXFLAGS="-pthread -r -O3 -flto" LDFLAGS="-O3 -pthread -flto" emconfigure ./configure --prefix=$(OPENFST) --enable-static --disable-shared --enable-ngram-fsts --enable-lookahead-fsts --disable-bin --with-pic && \ + emmake make -j$(COMPILE_JOBS) install && \ + echo "PACKAGE_VERSION = 1.8.0" >> $(OPENFST)/Makefile && \ + rm -rf /tmp/openfst -kaldi: - git clone -b vosk --depth=1 https://github.com/alphacep/kaldi $KALDI && - cd $KALDI/src && - git apply $SRC/kaldi.patch && - CXXFLAGS="-O3 -msimd128 -UHAVE_EXECINFO_H -pthread -flto" LDFLAGS="-O3 -sERROR_ON_UNDEFINED_SYMBOLS=0 -lembind -pthread -flto" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math=yes --static-fst=yes --debug-level=0 --double-precision=yes --clapack-root=$CLAPACK_WASM --host=WASM && - emmake make -j$COMPILE_JOBS online2 lm rnnlm +kaldi: openfst clapack-wasm + git clone -b vosk --depth=1 https://github.com/alphacep/kaldi $(KALDI) && \ + cd $(KALDI)/src && \ + git apply $(SRC)/kaldi.patch && \ + CXXFLAGS="-O3 -msimd128 -UHAVE_EXECINFO_H -pthread -flto" LDFLAGS="-O3 -sERROR_ON_UNDEFINED_SYMBOLS=0 -lembind -pthread -flto" emconfigure ./configure --use-cuda=no --with-cudadecoder=no --static --static-math=yes --static-fst=yes --debug-level=0 --double-precision=yes --clapack-root=$(CLAPACK_WASM) --host=WASM && \ + emmake make -j$(COMPILE_JOBS) online2 lm rnnlm -vosk: - git clone -b go/v0.3.46 --depth=1 https://github.com/alphacep/vosk-api $VOSK && - cd $VOSK/src && - git apply $SRC/vosk.patch && - VOSK_FILES="recognizer.cc language_model.cc model.cc spk_model.cc vosk_api.cc" && - em++ -pthread -O3 -flto -Wno-deprecated -I. -I$KALDI/src -I$OPENFST/include $VOSK_FILES -c && - emar -rcs vosk.a ${VOSK_FILES//.cc/.o} +vosk: kaldi + git clone -b go/v0.3.46 --depth=1 https://github.com/alphacep/vosk-api $(VOSK) && \ + cd $(VOSK)/src && \ + git apply $(SRC)/vosk.patch && \ + VOSK_FILES="recognizer.cc language_model.cc model.cc spk_model.cc vosk_api.cc" && \ + em++ -pthread -O3 -flto -Wno-deprecated -I. -I$(KALDI)/src -I$(OPENFST)/include $(VOSK_FILES) -c && \ + emar -rcs vosk.a $(VOSK_FILES:.cc=.o) -browser-recognizer: - cd $SRC && - em++ -O3 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sMODULARIZE -sTRUSTED_TYPES -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sALLOW_BLOCKING_ON_MAIN_THREAD -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPOLYFILL=0 -sPTHREAD_POOL_SIZE_STRICT=0 -sSUPPORT_LONGJMP=0 -sEXPORTED_RUNTIME_METHODS=UTF8ToString,stringToUTF8OnStack -sEXPORTED_FUNCTIONS=_malloc,_main -sEXPORT_NAME=loadBR -sENVIRONMENT=web,worker -I. -I$LIBARCHIVE/include -I$VOSK/src -L$LIBARCHIVE/lib -larchive -L$ZSTD/lib -lzstd -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 --pre-js pre.js ../BrowserRecognizer.js && - cd .. && - rm -f BrowserRecognizer.worker.js && - sed -i 's/locateFile("BrowserRecognizer.worker.js")/pthreadUrl/g' BrowserRecognizer.js +.PHONY: prepare \ No newline at end of file diff --git a/src/genericModel.cc b/src/genericModel.cc index 68ad1e2..3d6d7ea 100644 --- a/src/genericModel.cc +++ b/src/genericModel.cc @@ -1,4 +1,5 @@ #include "genericModel.h" + genericModel::genericModel(const std::string& storepath, const std::string &id, int index) : storepath(storepath), id(id), index(index) { fs::current_path("/opfs", tank); if(tank.value() != 0) { @@ -25,16 +26,12 @@ bool genericModel::checkModel() { file.read(&oldid[0], size); return id.compare(oldid) == 0 ? true : false; } -void genericModel::afterFetch(int memAddr, size_t size) { - // FIXME: Recognizer reuse this thread if possible - std::thread t{[this, memAddr, size](){ - char* modelData = reinterpret_cast(memAddr); - if(!extractModel(modelData, size)) { - free(modelData); +void genericModel::afterFetch() { + thrd.setTask1([this](){ + if(!extractModel()) { fireEv("_continue", "Unable to extract model", index); return; } - free(modelData); std::ofstream idFile("id"); if(!idFile.is_open()) { fs::current_path("/opfs"); @@ -45,16 +42,15 @@ void genericModel::afterFetch(int memAddr, size_t size) { idFile << id; idFile.close(); load(false); - }}; - t.detach(); + }); } -bool genericModel::extractModel(const char* fileBuf, size_t size) { +bool genericModel::extractModel() { std::string path{}; archive* src {archive_read_new()}; archive_entry* entry {}; archive_read_support_filter_all(src); archive_read_support_format_all(src); - archive_read_open_memory(src, fileBuf, size); + archive_read_open_filename(src, "m0dEl.tzst", 10240); if(archive_errno(src) != 0) return false; while (archive_read_next_header(src, &entry) == ARCHIVE_OK) { path = archive_entry_pathname(entry); diff --git a/src/genericModel.h b/src/genericModel.h index e0fc2ba..3e40e90 100644 --- a/src/genericModel.h +++ b/src/genericModel.h @@ -14,12 +14,13 @@ namespace fs = std::filesystem; struct genericModel { const std::string storepath{}; const std::string id{}; + twiceThrd thrd{}; int index{}; - static bool extractModel(const char* fileBuf, size_t size); + static bool extractModel(); virtual bool checkModelFiles() = 0; virtual void load(bool) = 0; bool checkModel(); - void afterFetch(int memAddr, size_t size); + void afterFetch(); genericModel(const std::string &storepath, const std::string &id, int index); }; diff --git a/src/global.cc b/src/global.cc index a553c83..0970c04 100644 --- a/src/global.cc +++ b/src/global.cc @@ -1,5 +1,4 @@ #include "global.h" - void throwJS(const char* msg, bool err) { EM_ASM({ if($1) { @@ -22,12 +21,24 @@ void fireEv(const char *type, const char *content, int index) { } pq.proxySync(selfTID, proxy); } - int main() { - //vosk_set_log_level(-1); std::thread t{[](){ wasmfs_create_directory("/opfs", 0777, wasmfs_create_opfs_backend()); }}; t.detach(); emscripten_exit_with_live_runtime(); -} \ No newline at end of file +} +void twiceThrd::setTask1(std::function task1) { + blocker.lock(); + std::thread t{[this, task1](){ + task1(); + blocker.lock(); + task2(); + }}; + t.detach(); +} +void twiceThrd::setTask2(std::function task2) { + this->task2 = task2; + blocker.unlock(); + reusable = false; +} diff --git a/src/global.h b/src/global.h index f04f474..58b618f 100644 --- a/src/global.h +++ b/src/global.h @@ -13,3 +13,11 @@ static std::error_code tank{}; void throwJS(const char* msg, bool err = false); void fireEv(const char *type, const char *content, int index); int main(); + +struct twiceThrd { // A minimal std::thread wrapper to run exactly 2 tasks + bool reusable{true}; + std::mutex blocker{}; + std::function task2{}; + void setTask1(std::function task1); + void setTask2(std::function task2); +}; diff --git a/src/model.cc b/src/model.cc index f7cb635..ddb1321 100644 --- a/src/model.cc +++ b/src/model.cc @@ -4,8 +4,8 @@ model::model(const std::string& storepath, const std::string& id, int index) : g model::~model() { vosk_model_free(mdl); } -void model::afterFetch(int addr, size_t size) { - genericModel::afterFetch(addr,size); +void model::afterFetch() { + genericModel::afterFetch(); } bool model::checkModel() { return genericModel::checkModel(); @@ -23,9 +23,7 @@ void model::load(bool newThrd) { main(); return; } - // FIXME: Recognizer reuse this thread if possible - std::thread t{main}; - t.detach(); + thrd.setTask1(main); } bool model::checkModelFiles() { return fs::exists("am/final.mdl", tank) && diff --git a/src/model.h b/src/model.h index 22ffab7..484c731 100644 --- a/src/model.h +++ b/src/model.h @@ -6,7 +6,7 @@ struct model : genericModel { VoskModel* mdl{}; model(const std::string& storepath, const std::string& id, int index); bool checkModel(); - void afterFetch(int addr, size_t size); + void afterFetch(); void load(bool newThrd); ~model(); }; diff --git a/src/pre.js b/src/pre.js index 7d613d4..7e8514d 100644 --- a/src/pre.js +++ b/src/pre.js @@ -1,25 +1,9 @@ -let objs = [] -let processorUrl = URL.createObjectURL(new Blob([ - (() => { - registerProcessor("BRProcessor", class extends AudioWorkletProcessor { - constructor(options) { - super(options) - this.done = false - this.port.onmessage = (ev) => { - if(ev.cmd === "deinit") this.done = false - } - this.ptr = options.processorOptions.ptr - } - process(inputs, outputs, params) { - if(this.done) return false; - this.wasmMem.set(inputs[0].getChannelData(this.channel)); - this.recognizerPort.postMessage(".") - outputs = inputs - return true - } - }) - }).toString() -], {type : "text/javascript"})) +let objs = [] +Module.cleanUp = () => { + objs.forEach(obj => obj.delete()) + URL.revokeObjectURL(pthreadUrl) + URL.revokeObjectURL(processorUrl) +} class Recognizer extends EventTarget { constructor() { super() @@ -114,10 +98,8 @@ Module.makeModel = async (url, storepath, id) => { if(!res.ok) { return reject("Unable to download model") } - let arr = await res.arrayBuffer() - let mdlMem = Module._malloc(arr.byteLength) // Will free in C++ - Module.HEAP8.set(new Int8Array(arr), mdlMem) - mdl.obj.afterFetch(mdlMem, arr.byteLength) + await (await (await root.getFileHandle("m0dEl.tzst", {create : true})).createWritable()).write(await res.arrayBuffer()) + mdl.obj.afterFetch() })() }) } @@ -167,6 +149,27 @@ Module.makeRecognizer = (model, sampleRate) => { rec._init(model.obj, sampleRate) return retval } +let processorUrl = URL.createObjectURL(new Blob([ + (() => { + registerProcessor("BRProcessor", class extends AudioWorkletProcessor { + constructor(options) { + super(options) + this.done = false + this.port.onmessage = (ev) => { + if(ev.cmd === "deinit") this.done = false + } + this.ptr = options.processorOptions.ptr + } + process(inputs, outputs, params) { + if(this.done) return false; + this.wasmMem.set(inputs[0].getChannelData(this.channel)); + this.recognizerPort.postMessage(".") + outputs = inputs + return true + } + }) + }).toString() +], {type : "text/javascript"})) // Taken from the worker.js file let pthreadUrl = URL.createObjectURL(new Blob([ (() => { @@ -340,8 +343,3 @@ let pthreadUrl = URL.createObjectURL(new Blob([ self.onmessage = handleMessage; }).toString() ], {type : "text/javascript"})) -Module.cleanUp = () => { - objs.forEach(obj => obj.delete()) - URL.revokeObjectURL(pthreadUrl) - URL.revokeObjectURL(processorUrl) -} diff --git a/src/recognizer.cc b/src/recognizer.cc index 9093098..d467b84 100644 --- a/src/recognizer.cc +++ b/src/recognizer.cc @@ -1,7 +1,7 @@ #include "recognizer.h" recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) { - std::thread t{[this](VoskModel* mdl, VoskRecognizer* rec, float sampleRate){ - rec = vosk_recognizer_new(mdl,sampleRate); + auto main{[this, mdl, sampleRate](){ + rec = vosk_recognizer_new(mdl->mdl,sampleRate); if(rec == nullptr) { fireEv("_continue", "Unable to initialize recognizer", this->index); return; @@ -20,7 +20,12 @@ recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) { } } } - },mdl->mdl, rec, sampleRate}; + }}; + if(mdl->thrd.reusable) { + mdl->thrd.setTask2(main); + return; + } + std::thread t{main}; t.detach(); } recognizer::~recognizer() { diff --git a/src/spkModel.cc b/src/spkModel.cc index df81613..141ad8b 100644 --- a/src/spkModel.cc +++ b/src/spkModel.cc @@ -11,8 +11,8 @@ spkModel::~spkModel() { bool spkModel::checkModel() { return genericModel::checkModel(); } -void spkModel::afterFetch(int addr, size_t size) { - genericModel::afterFetch(addr,size); +void spkModel::afterFetch() { + genericModel::afterFetch(); } void spkModel::load(bool newThrd) { auto main{[this](){ @@ -27,9 +27,7 @@ void spkModel::load(bool newThrd) { main(); return; } - // FIXME: Recognizer reuse this thread if possible - std::thread t{main}; - t.detach(); + thrd.setTask1(main); } bool spkModel::checkModelFiles() { return fs::exists("mfcc.conf", tank) && diff --git a/src/spkModel.h b/src/spkModel.h index 006f8ba..be9fc57 100644 --- a/src/spkModel.h +++ b/src/spkModel.h @@ -6,7 +6,7 @@ struct spkModel : genericModel { VoskSpkModel* mdl{}; spkModel(const std::string& storepath, const std::string& id, int index); bool checkModel(); - void afterFetch(int addr, size_t size); + void afterFetch(); void load(bool newThrd); ~spkModel(); };