This commit is contained in:
msqr1
2024-01-31 22:49:07 -08:00
parent 1b74781a9d
commit 9d21692993
5 changed files with 29 additions and 35 deletions

View File

@@ -1,4 +1,5 @@
#include "global.h" #include "global.h"
static pthread_t selfTID{pthread_self()}; static pthread_t selfTID{pthread_self()};
void throwJS(const char* msg, bool err) { void throwJS(const char* msg, bool err) {
EM_ASM({ EM_ASM({
@@ -13,16 +14,13 @@ void fireEv(const char *type, const char *content, int index) {
static ProxyingQueue pq{}; static ProxyingQueue pq{};
auto proxy{[index, type, content](){ auto proxy{[index, type, content](){
EM_ASM({ EM_ASM({
console.log("Event type ", UTF8ToString($1), ", content " , UTF8ToString($2), ", sending to ", objs[$0]);
objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"detail" : UTF8ToString($2)})); objs[$0].dispatchEvent(new CustomEvent(UTF8ToString($1), {"detail" : UTF8ToString($2)}));
},index, type, content); },index, type, content);
}}; }};
if(selfTID == pthread_self()) { if(selfTID == pthread_self()) {
emscripten_console_log("Self dispatch");
proxy(); proxy();
return; return;
} }
emscripten_console_log("Thread dispatch");
pq.proxySync(selfTID, proxy); pq.proxySync(selfTID, proxy);
} }

View File

@@ -11,7 +11,7 @@ class Recognizer extends EventTarget {
async getNode(ctx, channelIndex = 0) { async getNode(ctx, channelIndex = 0) {
if(typeof this.node === "undefined") { if(typeof this.node === "undefined") {
let msgChannel = new MessageChannel() let msgChannel = new MessageChannel()
ctx.AudioWorklet.addModule("../src/processor.js") await ctx.audioWorklet.addModule(processorUrl)
this.node = new AudioWorkletNode(ctx, 'BRProcessor', { channelCountMode: "max", numberOfInputs: 1, numberOfOutputs: 1 }) 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]) this.node.port.postMessage({cmd : "init", ptr: this.ptr, channel: channelIndex}, [msgChannel.port1])
msgChannel.port1.onmessage = (ev) => { msgChannel.port1.onmessage = (ev) => {

View File

@@ -1,28 +1,26 @@
// A copy and pass processor, check if already registered // A copy and pass processor
if(typeof BRProcessor === "undefined") { registerProcessor("BRProcessor", class extends AudioWorkletProcessor {
var BRProcessor = class extends AudioWorkletProcessor { constructor(options) {
constructor(options) { super(options)
super(options) this.done = false
this.done = false this.port.onmessage = (ev) => {
this.port.onmessage = (ev) => { switch(ev.cmd) {
switch(ev.cmd) { case "init":
case "init": this.recognizerPort = ev.ports[0]
this.recognizerPort = ev.ports[0] this.wasmMem = new Float32Array(WebAssembly.Memory.buffer).subarray(ev.ptr, ev.ptr+512)
this.wasmMem = new Float32Array(WebAssembly.Memory.buffer).subarray(ev.ptr, ev.ptr+512) this.channel = ev.channel
this.channel = ev.channel;
break break
case "deinit": case "deinit":
this.done = true this.done = true
}
} }
} }
process(inputs, outputs, params) {
if(this.done) return false;
this.wasmMem.set(inputs[0].getChannelData(this.channel));
this.recognizerPort.postMessage(".")
outputs = inputs
return true
}
} }
registerProcessor("BRProcessor", BRProcessor) process(inputs, outputs, params) {
} if(this.done) return false;
this.wasmMem.set(inputs[0].getChannelData(this.channel));
this.recognizerPort.postMessage(".")
outputs = inputs
return true
}
})

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,6 @@
const model = await BrRec.makeModel("en-model.tzst","model","en0.0.1") const model = await BrRec.makeModel("en-model.tzst","model","en0.0.1")
let ctx = new AudioContext() let ctx = new AudioContext()
const recognizer = await BrRec.makeRecognizer(model, ctx.sampleRate) const recognizer = await BrRec.makeRecognizer(model, ctx.sampleRate)
recognizer.addEventListener("result", e => { recognizer.addEventListener("result", e => {
console.log("Result: ",e.details) console.log("Result: ",e.details)
@@ -16,7 +15,6 @@
recognizer.addEventListener("partialResult", e => { recognizer.addEventListener("partialResult", e => {
console.log("Partial result: ",e.details) console.log("Partial result: ",e.details)
}) })
let micNode = ctx.createMediaStreamSource(await navigator.mediaDevices.getUserMedia({ let micNode = ctx.createMediaStreamSource(await navigator.mediaDevices.getUserMedia({
video: false, video: false,
audio: { audio: {