move static thread declaration, recompile test and test --> worked.

This commit is contained in:
msqr1
2024-01-31 21:17:28 -08:00
parent 94a8a68170
commit 1b74781a9d
9 changed files with 192 additions and 220 deletions

View File

@@ -1,5 +1,5 @@
#include "global.h"
static pthread_t selfTID{pthread_self()};
void throwJS(const char* msg, bool err) {
EM_ASM({
if($1) {
@@ -11,24 +11,25 @@ void throwJS(const char* msg, bool err) {
}
void fireEv(const char *type, const char *content, int index) {
static ProxyingQueue pq{};
static pthread_t selfTID{pthread_self()};
static auto proxy{[index, type, content](){
auto proxy{[index, type, content](){
EM_ASM({
console.log(objs[$0]);
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"detail" : UTF8ToString($2)}))
console.log("Event type ", UTF8ToString($1), ", content " , UTF8ToString($2), ", sending to ", objs[$0]);
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"detail" : UTF8ToString($2)}));
},index, type, content);
}};
if(selfTID == pthread_self()) {
emscripten_console_log("Self dispatch");
proxy();
return;
}
emscripten_console_log("Thread dispatch");
pq.proxySync(selfTID, proxy);
}
int main() {
//vosk_set_log_level(-1);
std::thread t{[](){
wasmfs_create_directory("/opfs",0777,wasmfs_create_opfs_backend());
wasmfs_create_directory("/opfs", 0777, wasmfs_create_opfs_backend());
}};
t.detach();
emscripten_exit_with_live_runtime();

View File

@@ -11,9 +11,12 @@ bool model::checkModel() {
return genericModel::checkModel();
}
void model::load(bool newThrd) {
static auto main{[this](){
auto main{[this](){
mdl = vosk_model_new(".");
if(mdl == nullptr) fireEv("_continue", "Unable to load model for recognition", index);
if(mdl == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
fireEv("_continue", ".", index);
}};
if(!newThrd) {

View File

@@ -1,4 +1,4 @@
let objs = [new EventTarget()]
let objs = []
class Recognizer extends EventTarget {
constructor() {
super()
@@ -11,7 +11,7 @@ class Recognizer extends EventTarget {
async getNode(ctx, channelIndex = 0) {
if(typeof this.node === "undefined") {
let msgChannel = new MessageChannel()
ctx.AudioWorklet.addModule("src/processor.js")
ctx.AudioWorklet.addModule("../src/processor.js")
this.node = new AudioWorkletNode(ctx, 'BRProcessor', { channelCountMode: "max", numberOfInputs: 1, numberOfOutputs: 1 })
this.node.port.postMessage({cmd : "init", ptr: this.ptr, channel: channelIndex}, [msgChannel.port1])
msgChannel.port1.onmessage = (ev) => {
@@ -84,7 +84,7 @@ Module.makeModel = async (url, storepath, id) => {
}, {once : true})
if(mdl.obj.checkModel()) {
mdl.obj.load(true)
return resolve(mdl)
return;
}
(async () => {
let res = await fetch(url)
@@ -110,7 +110,7 @@ Module.makeSpkModel = async (url, storepath, id) => {
}, {once : true})
if(mdl.obj.checkModel()) {
mdl.obj.load(true)
return resolve(mdl)
return
}
(async () => {
let res = await fetch(url)
@@ -130,7 +130,7 @@ Module.makeRecognizer = (model, sampleRate) => {
rec.addEventListener("_continue", (ev) => {
if(ev.detail == ".") {
objs.push(rec)
resolve(rec)
return resolve(rec)
}
rec.delete()
reject(ev.detail)

View File

@@ -6,8 +6,8 @@ recognizer::recognizer(model* mdl, float sampleRate, int index) : index(index) {
fireEv("_continue", "Unable to initialize recognizer", this->index);
return;
}
fireEv("_continue", "." ,this->index);
while(!done.test()) {
fireEv("_continue", "." ,this->index);
controller1.wait(false, std::memory_order_relaxed);
controller2.wait(false, std::memory_order_relaxed);
if(!done.test()) {

View File

@@ -15,9 +15,12 @@ void spkModel::afterFetch(int addr, size_t size) {
genericModel::afterFetch(addr,size);
}
void spkModel::load(bool newThrd) {
static auto main{[this](){
auto main{[this](){
mdl = vosk_spk_model_new(".");
if(mdl == nullptr) fireEv("_continue", "Unable to load model for recognition", index);
if(mdl == nullptr) {
fireEv("_continue", "Unable to load model for recognition", index);
return;
}
fireEv("_continue", ".", index);
}};
if(!newThrd) {