a promising run

This commit is contained in:
msqr1
2024-01-28 22:05:58 -08:00
parent 8796f35445
commit c34d3f8974
20 changed files with 6317 additions and 167 deletions

View File

@@ -1,44 +1,57 @@
#include "recognizer.h"
recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) {
rec = vosk_recognizer_new(mdl->mdl,sampleRate);
if(rec == nullptr) {
throwJS("Unable to initialize recognizer");
return;
}
controller.lock();
std::thread t{[this](const pthread_t& caller){
fs::current_path("/opfs");
fs::current_path(mdl->storepath);
std::thread t{[this](VoskModel* mdl, VoskRecognizer* rec, float sampleRate){
if(mdl == nullptr) {
mdl = vosk_model_new(".");
if(mdl == nullptr) {
throwJS("Unable to load model");
return;
}
}
rec = vosk_recognizer_new(mdl,sampleRate);
if(rec == nullptr) {
throwJS("Unable to initialize recognizer");
return;
}
while(!done.test()) {
controller.lock();
emscripten_console_log("In loop");
controller.wait(false, std::memory_order_relaxed);
if(!done.test()) {
switch(vosk_recognizer_accept_waveform_f(rec, dataPtr, 512)) {
case 0:
fireEv("result", vosk_recognizer_result(rec), caller);
fireEv("result", vosk_recognizer_result(rec));
break;
case 1:
fireEv("partialResult", vosk_recognizer_partial_result(rec), caller);
fireEv("partialResult", vosk_recognizer_partial_result(rec));
}
}
}
},pthread_self()};
},mdl->mdl, rec, sampleRate};
t.detach();
}
recognizer::~recognizer() {
done.test_and_set(std::memory_order_relaxed);
controller.unlock();
controller.notify_one();
vosk_recognizer_free(rec);
free(dataPtr);
}
void recognizer::fireEv(const char *type, const char *content, const pthread_t& caller) {
void recognizer::fireEv(const char *type, const char *content) {
static pthread_t targetThrd{pthread_self()};
static ProxyingQueue pq{};
pq.proxyAsync(caller, [&](){
pq.proxyAsync(targetThrd, [&](){
EM_ASM({
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"details" : UTF8ToString($2)}));
let ev = new CustomEvent(UTF8ToString($1), {"details" : UTF8ToString($2)});
objs[$0].dispatchEvent(ev);
console.log(objs[$0], ev)
},index, type, content);
});
}
void recognizer::acceptWaveForm() {
controller.unlock();
controller.lock();
controller.notify_one();
emscripten_console_log("Unblocked");
fireEv("result", "Test event");
}
void recognizer::setGrm(const std::string& grm) {
vosk_recognizer_set_grm(rec, grm.c_str());