Use std::variant, reorder struct member

This commit is contained in:
msqr1
2024-03-01 22:15:15 -08:00
parent 111961e217
commit 894fc1df99
15 changed files with 13623 additions and 8611 deletions

18
devel/Vosklet.d.ts vendored
View File

@@ -10,22 +10,15 @@ interface WasmModule {
___set_stack_limits(_0: number, _1: number): void; ___set_stack_limits(_0: number, _1: number): void;
} }
export interface model { export interface genericModel {
checkModel(): void; check(): void;
afterFetch(): void;
load(_0: boolean): void;
delete(): void;
}
export interface spkModel {
checkModel(): void;
afterFetch(): void; afterFetch(): void;
load(_0: boolean): void; load(_0: boolean): void;
delete(): void; delete(): void;
} }
export interface recognizer { export interface recognizer {
setSpkModel(_0: spkModel): void; setSpkModel(_0: genericModel): void;
acceptWaveForm(): void; acceptWaveForm(): void;
setWords(_0: boolean): void; setWords(_0: boolean): void;
setPartialWords(_0: boolean): void; setPartialWords(_0: boolean): void;
@@ -36,9 +29,8 @@ export interface recognizer {
} }
interface EmbindModule { interface EmbindModule {
model: {new(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _1: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _2: number): model}; genericModel: {new(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _1: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _2: number, _3: boolean): genericModel};
spkModel: {new(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _1: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _2: number): spkModel}; recognizer: {new(_0: genericModel, _1: number, _2: number): recognizer; new(_0: genericModel, _1: genericModel, _2: number, _3: number): recognizer; new(_0: genericModel, _1: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _2: number, _3: number, _4: number): recognizer};
recognizer: {new(_0: model, _1: number, _2: number): recognizer; new(_0: model, _1: spkModel, _2: number, _3: number): recognizer; new(_0: model, _1: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string, _2: number, _3: number, _4: number): recognizer};
setLogLevel(_0: number): void; setLogLevel(_0: number): void;
} }
export type MainModule = WasmModule & EmbindModule; export type MainModule = WasmModule & EmbindModule;

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -1 +1,165 @@
"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)}Vosklet(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['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') {
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;

View File

@@ -30,7 +30,7 @@ fi
cd $SRC && cd $SRC &&
RELEASE=0 && RELEASE=0 &&
if [ $RELEASE = 0 ]; then if [ $RELEASE = 0 ]; then
em++ -O0 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE=2 -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sSAFE_HEAP_LOG -sSAFE_HEAP -sRUNTIME_DEBUG -sSTACK_OVERFLOW_CHECK=2 -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sASSERTIONS=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPOLYFILL=0 -sSUPPORT_LONGJMP=0 -sINVOKE_RUN=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 --emit-symbol-map --embind-emit-tsd Vosklet.d.ts --pre-js pre.js -fsanitize=undefined -o ../devel/Vosklet.js em++ -O0 global.cc genericModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE -sGZIP_EMBEDDINGS -sMODULARIZE -sEMBIND_STD_STRING_IS_UTF8 -sPTHREAD_POOL_DELAY_LOAD -sSAFE_HEAP_LOG -sSAFE_HEAP -sRUNTIME_DEBUG -sSTACK_OVERFLOW_CHECK=2 -sTEXTDECODER=2 -sPTHREAD_POOL_SIZE_STRICT=2 -sASSERTIONS=2 -sINITIAL_MEMORY=$MAX_MEMORY -sPTHREAD_POOL_SIZE=$MAX_THREADS -sPOLYFILL=0 -sSUPPORT_LONGJMP=0 -sINVOKE_RUN=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 --emit-symbol-map --embind-emit-tsd Vosklet.d.ts --pre-js pre.js -fsanitize=undefined -o ../devel/Vosklet.js
else else
em++ -O3 global.cc genericModel.cc model.cc spkModel.cc recognizer.cc bindings.cc -sWASMFS -sWASM_BIGINT -sSINGLE_FILE=2 -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 -sINVOKE_RUN=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=Vosklet -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 ../devel/Vosklet.js em++ -O3 global.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 -sSUPPORT_LONGJMP=0 -sINVOKE_RUN=0 -sEXPORTED_FUNCTIONS=_malloc -sEXPORT_NAME=Vosklet -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 ../devel/Vosklet.js
fi fi

View File

@@ -1,5 +1,4 @@
#include "spkModel.h" #include "genericModel.h"
#include "model.h"
#include "recognizer.h" #include "recognizer.h"
#include <emscripten/bind.h> #include <emscripten/bind.h>
@@ -7,22 +6,16 @@ using namespace emscripten;
EMSCRIPTEN_BINDINGS() { EMSCRIPTEN_BINDINGS() {
function("setLogLevel", &vosk_set_log_level, allow_raw_pointers()); function("setLogLevel", &vosk_set_log_level, allow_raw_pointers());
class_<model>("model") class_<genericModel>("genericModel")
.constructor<std::string, std::string, int>(allow_raw_pointers()) .constructor<std::string, std::string, int, bool>(allow_raw_pointers())
.function("checkModel", &model::checkModel, allow_raw_pointers()) .function("check", &genericModel::check, allow_raw_pointers())
.function("afterFetch", &model::afterFetch, allow_raw_pointers()) .function("afterFetch", &genericModel::afterFetch, allow_raw_pointers())
.function("load", &model::load, allow_raw_pointers()); .function("load", &genericModel::load, allow_raw_pointers());
class_<spkModel>("spkModel")
.constructor<std::string, std::string, int>(allow_raw_pointers())
.function("checkModel", &spkModel::checkModel, allow_raw_pointers())
.function("afterFetch", &spkModel::afterFetch, allow_raw_pointers())
.function("load", &spkModel::load, allow_raw_pointers());
class_<recognizer>("recognizer") class_<recognizer>("recognizer")
.constructor<model*, float, int>(allow_raw_pointers()) .constructor<genericModel*, float, int>(allow_raw_pointers())
.constructor<model*, spkModel*, float, int>(allow_raw_pointers()) .constructor<genericModel*, genericModel*, float, int>(allow_raw_pointers())
.constructor<model*, std::string, float, int, int>(allow_raw_pointers()) .constructor<genericModel*, std::string, float, int, int>(allow_raw_pointers())
.function("setWords", &recognizer::setWords, allow_raw_pointers()) .function("setWords", &recognizer::setWords, allow_raw_pointers())
.function("setPartialWords", &recognizer::setPartialWords, allow_raw_pointers()) .function("setPartialWords", &recognizer::setPartialWords, allow_raw_pointers())
.function("setGrm", &recognizer::setGrm, allow_raw_pointers()) .function("setGrm", &recognizer::setGrm, allow_raw_pointers())

View File

@@ -1,7 +1,70 @@
#include "genericModel.h" #include "genericModel.h"
genericModel::genericModel(const std::string& storepath, const std::string &id, int index) : storepath(storepath), id(id), index(index) {} genericModel::genericModel(std::string storepath, std::string id, int index, bool normalMdl) : index(index), normalMdl(normalMdl) {
void genericModel::checkModel() { this->storepath = new char[storepath.size()];
this->id = new char[id.size()];
memcpy(this->storepath, storepath.c_str(), storepath.size());
memcpy(this->id, id.c_str(), id.size());
}
void genericModel::load(bool newTask) {
emscripten_console_log(storepath);
emscripten_console_log(id);
auto main{[this](){
if(normalMdl) {
VoskModel* temp {vosk_model_new(".")};
if(temp == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
mdl = temp;
}
else {
VoskSpkModel* temp {vosk_spk_model_new(".")};
if(temp == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
mdl = temp;
}
fireEv("_continue", nullptr, index);
}};
if(!newTask) {
main();
return;
}
thrd.addTask(main);
}
bool genericModel::checkFiles() {
if(std::holds_alternative<VoskModel*>(mdl)) {
return fs::exists("am/final.mdl", tank) &&
fs::exists("conf/mfcc.conf", tank) &&
fs::exists("conf/model.conf", tank) &&
fs::exists("graph/phones/word_boundary.int", tank) &&
fs::exists("graph/Gr.fst", tank) &&
fs::exists("graph/HCLr.fst", tank) &&
fs::exists("graph/disambig_tid.int", tank) &&
fs::exists("ivector/final.dubm", tank) &&
fs::exists("ivector/final.ie", tank) &&
fs::exists("ivector/final.mat", tank) &&
fs::exists("ivector/global_cmvn.stats", tank) &&
fs::exists("ivector/online_cmvn.conf", tank) &&
fs::exists("ivector/splice.conf", tank);
}
return fs::exists("mfcc.conf", tank) &&
fs::exists("final.ext.raw", tank) &&
fs::exists("mean.vec", tank) &&
fs::exists("transform.mat", tank);
}
genericModel::~genericModel() {
if(std::holds_alternative<VoskModel*>(mdl)) {
vosk_model_free(std::get<0>(mdl));
return;
}
vosk_spk_model_free(std::get<1>(mdl));
delete[] storepath;
delete[] id;
}
void genericModel::check() {
thrd.addTask([this](){ thrd.addTask([this](){
if(OPFSTried && !OPFSOk) { if(OPFSTried && !OPFSOk) {
fireEv("_checkMdl", "OPFS isn't available", index); fireEv("_checkMdl", "OPFS isn't available", index);
@@ -28,7 +91,7 @@ void genericModel::checkModel() {
if(tank.value() != 0) { if(tank.value() != 0) {
fireEv("_continue", "Unable to cd storepath", index); fireEv("_continue", "Unable to cd storepath", index);
} }
if(!checkModelFiles() && !fs::exists("id", tank)) { if(!checkFiles() && !fs::exists("id", tank)) {
fireEv("_checkMdl", "fetch", index); fireEv("_checkMdl", "fetch", index);
return; return;
} }
@@ -51,28 +114,31 @@ void genericModel::checkModel() {
}; };
fread(oldid, 1, oldsize, idFile); fread(oldid, 1, oldsize, idFile);
fclose(idFile); fclose(idFile);
if(strcmp(oldid, id.c_str()) != 0) fireEv("_checkMdl", "fetch", index); if(strcmp(oldid, id) != 0) fireEv("_checkMdl", "fetch", index);
else fireEv("_checkMdl", nullptr, index); else fireEv("_checkMdl", nullptr, index);
delete[] oldid; delete[] oldid;
}); });
} }
void genericModel::afterFetch() { void genericModel::afterFetch() {
thrd.addTask([this](){ thrd.addTask([this](){
if(!extractModel()) { emscripten_console_log("1");
if(!extract()) {
fs::remove("/opfs/m0dEl.tar",tank); fs::remove("/opfs/m0dEl.tar",tank);
fs::current_path("/opfs", tank); fs::current_path("/opfs", tank);
fs::remove_all(storepath, tank); fs::remove_all(storepath, tank);
fireEv("_continue", "Unable to extract model", index); fireEv("_continue", "Unable to extract model", index);
return; return;
} }
emscripten_console_log("2");
fs::remove("/opfs/m0dEl.tar",tank); fs::remove("/opfs/m0dEl.tar",tank);
fs::remove("README",tank); fs::remove("README",tank);
if(!checkModelFiles()) { if(!checkFiles()) {
fireEv("_continue", "URL points to invalid model files", index); fireEv("_continue", "URL points to invalid model files", index);
return; return;
} }
emscripten_console_log("3");
int idFd {open("id", O_WRONLY | O_TRUNC)}; int idFd {open("id", O_WRONLY | O_TRUNC)};
if(write(idFd, id.c_str(), id.size()) == -1) { if(write(idFd, id, strlen(id)) == -1) {
fireEv("_continue", "Unable to write new ID", index); fireEv("_continue", "Unable to write new ID", index);
close(idFd); close(idFd);
return; return;
@@ -81,7 +147,7 @@ void genericModel::afterFetch() {
load(false); load(false);
}); });
} }
bool genericModel::extractModel() { bool genericModel::extract() {
std::string path{}; std::string path{};
archive* src {archive_read_new()}; archive* src {archive_read_new()};
archive_entry* entry{}; archive_entry* entry{};

View File

@@ -5,6 +5,7 @@
#include <filesystem> #include <filesystem>
#include <cstring> #include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <variant>
#include <vosk_api.h> #include <vosk_api.h>
#include <archive.h> #include <archive.h>
@@ -12,16 +13,19 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
struct genericModel { struct genericModel {
const std::string storepath{}; bool normalMdl{};
const std::string id{};
reusableThrd thrd{};
bool recognizerUsedThrd{}; bool recognizerUsedThrd{};
int index{}; int index{};
static bool extractModel(); char* storepath{};
virtual bool checkModelFiles() = 0; char* id{};
virtual void load(bool newTask) = 0; std::variant<VoskModel*, VoskSpkModel*> mdl{};
void checkModel(); reusableThrd thrd{};
static bool extract();
void load(bool newTask);
void check();
bool checkFiles();
void afterFetch(); void afterFetch();
genericModel(const std::string &storepath, const std::string &id, int index); genericModel(std::string storepath, std::string id, int index, bool normalMdl);
~genericModel();
}; };

View File

@@ -1,42 +0,0 @@
#include "model.h"
model::model(const std::string& storepath, const std::string& id, int index) : genericModel(storepath, id, index) {}
model::~model() {
vosk_model_free(mdl);
}
void model::afterFetch() {
genericModel::afterFetch();
}
void model::checkModel() {
genericModel::checkModel();
}
void model::load(bool newTask) {
auto main{[this](){
mdl = vosk_model_new(".");
if(mdl == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
fireEv("_continue", nullptr, index);
}};
if(!newTask) {
main();
return;
}
thrd.addTask(main);
}
bool model::checkModelFiles() {
return fs::exists("am/final.mdl", tank) &&
fs::exists("conf/mfcc.conf", tank) &&
fs::exists("conf/model.conf", tank) &&
fs::exists("graph/phones/word_boundary.int", tank) &&
fs::exists("graph/Gr.fst", tank) &&
fs::exists("graph/HCLr.fst", tank) &&
fs::exists("graph/disambig_tid.int", tank) &&
fs::exists("ivector/final.dubm", tank) &&
fs::exists("ivector/final.ie", tank) &&
fs::exists("ivector/final.mat", tank) &&
fs::exists("ivector/global_cmvn.stats", tank) &&
fs::exists("ivector/online_cmvn.conf", tank) &&
fs::exists("ivector/splice.conf", tank);
}

View File

@@ -1,15 +0,0 @@
#pragma once
#include "genericModel.h"
struct model : genericModel {
bool checkModelFiles();
VoskModel* mdl{};
model(const std::string& storepath, const std::string& id, int index);
void checkModel();
void afterFetch();
void load(bool newTask);
~model();
};

View File

@@ -48,9 +48,8 @@ class genericModel extends EventTarget {
reject(ev.detail) reject(ev.detail)
} }
}, {once : true}) }, {once : true})
if(normalMdl) mdl.obj = new Module.model(storepath, id, objs.length-1) mdl.obj = new Module.genericModel(storepath, id, objs.length-1, normalMdl)
else mdl.obj = new Module.spkModel(storepath, id, objs.length-1) mdl.obj.check()
mdl.obj.checkModel()
}) })
} }
delete() { delete() {
@@ -122,7 +121,7 @@ class Recognizer extends EventTarget {
return this.node return this.node
} }
recognize(buf, channelIndex = 0) { recognize(buf, channelIndex = 0) {
Module.HEAPF32.set(buf.getChannelData(channelIndex).subarray(0, 512), this.ptr); Module.HEAPF32.set(buf.getChannelData(channelIndex).subarray(0, 512), this.ptr)
this.obj.acceptWaveForm() this.obj.acceptWaveForm()
} }
delete() { delete() {
@@ -168,8 +167,8 @@ let processorUrl = URL.createObjectURL(new Blob(['(',
this.channelIndex = options.processorOptions.channelIndex this.channelIndex = options.processorOptions.channelIndex
} }
process(inputs, outputs, params) { process(inputs, outputs, params) {
if(this.done) return false; if(this.done) return false
this.wasmMem.set(inputs[0].getChannelData(this.channelIndex)); this.wasmMem.set(inputs[0].getChannelData(this.channelIndex))
this.recognizerPort.postMessage("0") this.recognizerPort.postMessage("0")
outputs = inputs outputs = inputs
return true return true

View File

@@ -1,25 +1,25 @@
#include "recognizer.h" #include "recognizer.h"
recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) { recognizer::recognizer(genericModel* model, float sampleRate, int index) : index(index) {
auto main{[this, mdl, sampleRate](){ auto main{[this, model, sampleRate](){
rec = vosk_recognizer_new(mdl->mdl,sampleRate); rec = vosk_recognizer_new(std::get<0>(model->mdl),sampleRate);
finishConstruction(); finishConstruction();
}}; }};
tryStealMdlThrd(main, mdl); tryStealMdlThrd(main, model);
} }
recognizer::recognizer(model* mdl, spkModel* spkMdl, float sampleRate, int index) { recognizer::recognizer(genericModel* model, genericModel* spkMdl, float sampleRate, int index) {
auto main{[this, mdl, sampleRate, spkMdl](){ auto main{[this, model, sampleRate, spkMdl](){
rec = vosk_recognizer_new_spk(mdl->mdl, sampleRate, spkMdl->mdl); rec = vosk_recognizer_new_spk(std::get<0>(model->mdl), sampleRate, std::get<1>(spkMdl->mdl));
finishConstruction(); finishConstruction();
}}; }};
tryStealMdlThrd(main, mdl); tryStealMdlThrd(main, model);
} }
recognizer::recognizer(model* mdl, const std::string& grm, float sampleRate, int index, int dummy) { recognizer::recognizer(genericModel* model, const std::string& grm, float sampleRate, int index, int dummy) {
auto main{[this, mdl, sampleRate, grm](){ auto main{[this, model, sampleRate, grm](){
rec = vosk_recognizer_new_grm(mdl->mdl, sampleRate, grm.c_str()); rec = vosk_recognizer_new_grm(std::get<0>(model->mdl), sampleRate, grm.c_str());
finishConstruction(); finishConstruction();
}}; }};
tryStealMdlThrd(main, mdl); tryStealMdlThrd(main, model);
} }
recognizer::~recognizer() { recognizer::~recognizer() {
done.test_and_set(std::memory_order_relaxed); done.test_and_set(std::memory_order_relaxed);
@@ -29,10 +29,10 @@ recognizer::~recognizer() {
vosk_recognizer_free(rec); vosk_recognizer_free(rec);
free(dataPtr); free(dataPtr);
} }
void recognizer::tryStealMdlThrd(std::function<void()>&& main, model* mdl) { void recognizer::tryStealMdlThrd(std::function<void()>&& main, genericModel* model) {
if(mdl->recognizerUsedThrd) { if(model->recognizerUsedThrd) {
mdl->thrd.addTask(std::move(main)); model->thrd.addTask(std::move(main));
mdl->recognizerUsedThrd = true; model->recognizerUsedThrd = true;
return; return;
} }
std::thread t{main}; std::thread t{main};
@@ -64,8 +64,8 @@ void recognizer::acceptWaveForm() {
void recognizer::setGrm(const std::string& grm) { void recognizer::setGrm(const std::string& grm) {
vosk_recognizer_set_grm(rec, grm.c_str()); vosk_recognizer_set_grm(rec, grm.c_str());
} }
void recognizer::setSpkModel(spkModel* mdl) { void recognizer::setSpkModel(genericModel* spkModel) {
vosk_recognizer_set_spk_model(rec, mdl->mdl); vosk_recognizer_set_spk_model(rec, std::get<1>(spkModel->mdl));
} }
void recognizer::setWords(bool words) { void recognizer::setWords(bool words) {
vosk_recognizer_set_words(rec,words); vosk_recognizer_set_words(rec,words);

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "model.h" #include "genericModel.h"
#include "spkModel.h"
#include "global.h" #include "global.h"
struct recognizer { struct recognizer {
@@ -9,14 +8,14 @@ struct recognizer {
float* dataPtr{}; float* dataPtr{};
int index{}; int index{};
VoskRecognizer* rec{}; VoskRecognizer* rec{};
recognizer(model* model, float sampleRate, int index); recognizer(genericModel* model, float sampleRate, int index);
recognizer(model* model, spkModel* spkModel, float sampleRate, int index); recognizer(genericModel* model, genericModel* spkModel, float sampleRate, int index);
recognizer(model* model, const std::string& grm, float sampleRate, int index, int dummy); recognizer(genericModel* model, const std::string& grm, float sampleRate, int index, int dummy);
~recognizer(); ~recognizer();
void finishConstruction(); void finishConstruction();
void tryStealMdlThrd(std::function<void()>&& main, model* mdl); void tryStealMdlThrd(std::function<void()>&& main, genericModel* mdl);
void acceptWaveForm(); void acceptWaveForm();
void setSpkModel(spkModel* model); void setSpkModel(genericModel* model);
void setGrm(const std::string& grm); void setGrm(const std::string& grm);
void setWords(bool words); void setWords(bool words);
void setPartialWords(bool partialWords); void setPartialWords(bool partialWords);

View File

@@ -1,37 +0,0 @@
#include "spkModel.h"
spkModel::spkModel(const std::string& storepath, const std::string& id, int index) : genericModel(storepath, id, index) {
mdl = vosk_spk_model_new(".");
if(mdl == nullptr) {
fireEv("_continue", "Unable to initialize speaker model", index);
}
}
spkModel::~spkModel() {
vosk_spk_model_free(mdl);
}
void spkModel::checkModel() {
genericModel::checkModel();
}
void spkModel::afterFetch() {
genericModel::afterFetch();
}
void spkModel::load(bool newTask) {
auto main{[this](){
mdl = vosk_spk_model_new(".");
if(mdl == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
fireEv("_continue", nullptr, index);
}};
if(!newTask) {
main();
return;
}
thrd.addTask(main);
}
bool spkModel::checkModelFiles() {
return fs::exists("mfcc.conf", tank) &&
fs::exists("final.ext.raw", tank) &&
fs::exists("mean.vec", tank) &&
fs::exists("transform.mat", tank);
}

View File

@@ -1,12 +0,0 @@
#pragma once
#include "genericModel.h"
struct spkModel : genericModel {
bool checkModelFiles();
VoskSpkModel* mdl{};
spkModel(const std::string& storepath, const std::string& id, int index);
void checkModel();
void afterFetch();
void load(bool newTask);
~spkModel();
};