switch to makefile instead, reuse threads
This commit is contained in:
12
README.md
12
README.md
@@ -1,13 +1,12 @@
|
||||
# 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<Model> makeModel(path: string, url: string, id: string)```<br><br>```Promise<SpkModel> makeSpkModel(path: string, url: string, id: string)``` | Make a ```Model``` or ```SpkModel```<br>- If **path** contains valid model files and **id** is the same, there will not be a fetch from **url**.<br>- 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<Recognizer> 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) <br>- 2: Error<br>- 1: Warning<br>- 0: Info <br>- 1: Verbose<br>- 2: More verbose<br>- 3: Debug |
|
||||
| ```Promise<Recognizer> 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) <br>```-2```: Error<br>```-1```: Warning<br>```1```: Verbose<br>```2```: More verbose<br>```3```: Debug |
|
||||
| ```cleanUp()``` | Call ```delete()``` on all objects and revoke all Blob URLs. |
|
||||
|
||||
| Function signature (all objects) | 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*.
|
||||
```
|
||||
<!--Load this from a script tag-->
|
||||
<script src="BrowserRecognizer.js"></script>
|
||||
<!-->
|
||||
<script>
|
||||
// Select name
|
||||
const BrRec = await loadBR()
|
||||
@@ -72,7 +70,7 @@ If you can't set them, you may use a VERY HACKY workaround at *src/addCOI.js*.
|
||||
console.log("Partial result: ",e.details)
|
||||
})
|
||||
|
||||
// Process audio
|
||||
// Microphone setup
|
||||
media = await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: {
|
||||
@@ -81,7 +79,7 @@ If you can't set them, you may use a VERY HACKY workaround at *src/addCOI.js*.
|
||||
channelCount: 1,
|
||||
sampleRate: 16000
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
</script>
|
||||
```
|
||||
|
||||
1
emsdk
Submodule
1
emsdk
Submodule
Submodule emsdk added at 2aa7490715
166
src/Makefile
166
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: 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
|
||||
|
||||
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 &&
|
||||
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
|
||||
|
||||
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
|
||||
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)
|
||||
|
||||
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}
|
||||
|
||||
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
|
||||
@@ -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<char*>(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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
void twiceThrd::setTask1(std::function<void()> task1) {
|
||||
blocker.lock();
|
||||
std::thread t{[this, task1](){
|
||||
task1();
|
||||
blocker.lock();
|
||||
task2();
|
||||
}};
|
||||
t.detach();
|
||||
}
|
||||
void twiceThrd::setTask2(std::function<void()> task2) {
|
||||
this->task2 = task2;
|
||||
blocker.unlock();
|
||||
reusable = false;
|
||||
}
|
||||
|
||||
@@ -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<void()> task2{};
|
||||
void setTask1(std::function<void()> task1);
|
||||
void setTask2(std::function<void()> task2);
|
||||
};
|
||||
|
||||
@@ -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) &&
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
56
src/pre.js
56
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
|
||||
Module.cleanUp = () => {
|
||||
objs.forEach(obj => obj.delete())
|
||||
URL.revokeObjectURL(pthreadUrl)
|
||||
URL.revokeObjectURL(processorUrl)
|
||||
}
|
||||
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"}))
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) &&
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user