diff --git a/src/Recognizer.cc b/src/Recognizer.cc index 4a1925b..8e992a5 100644 --- a/src/Recognizer.cc +++ b/src/Recognizer.cc @@ -1,19 +1,20 @@ #include "Recognizer.h" #include +static const char* recognizerInitErr{"Unable to initialize recognizer"}; Recognizer::Recognizer(int index, float sampleRate, CommonModel* model) : rec{vosk_recognizer_new(std::get(model->mdl), sampleRate)} { - if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer"); + if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast(recognizerInitErr)); else globalPool.exec([this, index]{main(index);}); } Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, CommonModel* spkModel) : rec{vosk_recognizer_new_spk(std::get(model->mdl), sampleRate, std::get(spkModel->mdl))} { - if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer"); + if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast(recognizerInitErr)); else globalPool.exec([this, index]{main(index);}); } Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, const std::string& grm, int) : rec{vosk_recognizer_new_grm(std::get(model->mdl), sampleRate, grm.c_str())} { - if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer"); + if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast(recognizerInitErr)); else globalPool.exec([this, index]{main(index);}); } void Recognizer::safeDelete(bool _processCurrent) { diff --git a/src/Util.cc b/src/Util.cc index a2621f3..ee06605 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -1,25 +1,12 @@ #include "Util.h" -#include #include +#include -bool _isWorker() { - // First thread to flip this bool is the main thread - static bool test{true}; - if(test) { - test = false; - return true; - } - return false; -} -thread_local bool isWorker{_isWorker()}; - -EM_JS(void, _fireEv, (int index, int typeIdx, int content), { +EM_JS(void, mtFireEv, (int index, int typeIdx, int content), { objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) })); -}) +}); void fireEv(int index, int typeIdx, const char* content) { - int contentAddr{reinterpret_cast(content)}; - if(isWorker) emscripten_wasm_worker_post_function_viii(0, _fireEv, index, typeIdx, contentAddr); - else _fireEv(index, typeIdx, contentAddr); + emscripten_wasm_worker_post_function_viii(0, mtFireEv, index, typeIdx, reinterpret_cast(content)); } int untar(unsigned char* tar, int tarSize, const std::string& storepath) { if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat; @@ -61,9 +48,9 @@ int untar(unsigned char* tar, int tarSize, const std::string& storepath) { } return Successful; } -void Worker::startup(int _self, int _pool) { - Worker& self = *reinterpret_cast(_self); - WorkerPool& pool = *reinterpret_cast(_pool); +void Worker::startup(int _fn, int _pool) { + WorkerPool& pool{*reinterpret_cast(_pool)}; + std::function& fn{*reinterpret_cast*>(_fn)}; while(!pool.done) { // Wait until unlocked emscripten_atomic_wait_u32(&pool.qLock, true, -1); @@ -75,12 +62,12 @@ void Worker::startup(int _self, int _pool) { } // If this locks, the returned (loaded) value will be false, and we move on if(emscripten_atomic_cas_u32(&pool.qLock, false, true)) continue; - self.fn = pool.taskQ.front(); + fn = pool.taskQ.front(); pool.taskQ.pop(); // Unlock emscripten_atomic_store_u32(&pool.qLock, false); emscripten_atomic_notify(&pool.qLock, 1); - self.fn(); + fn(); } } static constexpr int workerStack{65536}; @@ -89,7 +76,7 @@ static std::array stacks; WorkerPool::WorkerPool() { for(int i = 0; i < workers.size(); i++) { workers[i].handle = emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack); - emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast(&workers[i]), reinterpret_cast(this)); + emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast(&workers[i].fn),reinterpret_cast(this)); } } WorkerPool::~WorkerPool() { diff --git a/src/Util.h b/src/Util.h index 16b71ff..3b7d463 100644 --- a/src/Util.h +++ b/src/Util.h @@ -6,6 +6,7 @@ #include #include + namespace fs = std::filesystem; struct AudioData { @@ -33,7 +34,7 @@ struct WorkerPool; struct Worker { int handle; std::function fn; - static void startup(int _self, int _pool); + static void startup(int _fn, int _pool); }; #ifndef MAX_WORKERS #define MAX_WORKERS 1 @@ -47,7 +48,12 @@ struct WorkerPool { ~WorkerPool(); void exec(std::function fn); }; +// Must be called from the main thread +extern "C" void mtFireEv(int index, int typeIdx, int content); + +// Must be called from a wasm worker void fireEv(int index, int typeIdx, const char* content = nullptr); + int untar(unsigned char* tar, int tarSize, const std::string& storepath); extern WorkerPool globalPool; \ No newline at end of file