Recognizer design change

This commit is contained in:
msqr1
2024-01-25 23:30:45 -08:00
parent 48b54295f3
commit efb56e19f9
7 changed files with 67 additions and 49 deletions

View File

@@ -21,10 +21,10 @@ int main() {
}
EMSCRIPTEN_BINDINGS() {
function("setLogLevel", &vosk_set_log_level, allow_raw_pointers());
class_<model>("model")
class_<model>("Model")
.constructor<std::string, std::string, std::string>(allow_raw_pointers());
class_<spkModel>("spkModel")
class_<spkModel>("SpkModel")
.constructor<std::string, std::string, std::string>(allow_raw_pointers());
class_<recognizer>("recognizer")

View File

@@ -1,20 +1,28 @@
let objs = []
class recognizer extends EventTarget {
constructor(rec,ctx) {
class Recognizer extends EventTarget {
constructor(rec) {
super()
this.obj = rec
this.ptr = Module._malloc(512)
objs.push(this)
}
getNode(ctx) {
let channel = new MessageChannel()
this.copier = new AudioWorkletNode(ctx, 'BRCopier', { channelCount: 1, numberOfInputs: 1, numberOfOutputs: 0 })
this.copier.port.postMessage({cmd : "init", ptr: this.ptr},[channel.port1])
this.node = new AudioWorkletNode(ctx, 'BRProcessor', { channelCount: 1, numberOfInputs: 1, numberOfOutputs: 1 })
node.port.postMessage({cmd : "init", ptr: this.ptr},[channel.port1])
channel.port1.onmessage = (ev) => {
this.obj.acceptWaveForm(this.ptr, 512)
}
objs.push(this)
return this.node
}
recognize(buf) {
buf.copyFromChannel()
this.obj.acceptWaveForm(this.ptr, 512)
}
delete() {
this.obj.delete()
this.copier.port.postMessage({cmd : "deinit"})
if(typeof this.node !== "undefined") {
this.node.port.postMessage({cmd : "deinit"})
}
Module.free(this.ptr)
}
setWords(words) {
@@ -43,7 +51,7 @@ Module.deleteAll = () => {
Module.makeModel = async (url, path, id) => {
let mdl
try {
mdl = new Module.model(url, path, id)
mdl = new Module.Model(url, path, id)
}
catch(e) {
mdl.delete()
@@ -55,7 +63,7 @@ Module.makeModel = async (url, path, id) => {
Module.makeSpkModel = async (url, path, id) => {
let mdl
try {
mdl = new Module.spkModel(url, path, id)
mdl = new Module.SpkModel(url, path, id)
}
catch(e) {
mdl.delete()
@@ -64,7 +72,7 @@ Module.makeSpkModel = async (url, path, id) => {
objs.push(mdl)
return mdl
}, ctx.AudioWorklet
Module.makeRecognizer = async (model, ctx) => {
Module.makeRecognizer = async (model) => {
let rec
try {
rec = new Module.recognizer(model, ctx.sampleRate, objs.length)
@@ -73,4 +81,5 @@ Module.makeRecognizer = async (model, ctx) => {
rec.delete()
return Promise.reject(e)
}
await ctx.AudioWorklet.addModule(URL.createObjectURL(new Blob([`
return new Recognizer(rec)
}

View File

@@ -1,4 +1,5 @@
registerProcessor("BRCopier", class extends AudioWorkletProcessor {
// A copy and pass processor
registerProcessor("BRProcessor", class extends AudioWorkletProcessor {
constructor(options) {
super(options)
this.ret = true
@@ -7,6 +8,8 @@ registerProcessor("BRCopier", class extends AudioWorkletProcessor {
case "init":
this.recognizerPort = ev.ports[0]
this.wasmMem = new Float32Array(WebAssembly.Memory.buffer).subarray(ev.ptr, ev.ptr+512)
this.channel = ev.channel
this.input = ev.input
break
case "deinit":
this.ret = false
@@ -16,8 +19,9 @@ registerProcessor("BRCopier", class extends AudioWorkletProcessor {
}
process(inputs, outputs, params) {
if(!this.ret) return false;
inputs[0].copyFromChannel(this.wasmMem, 0)
this.recognizerPort.postMessage("done")
inputs[this.input].copyFromChannel(this.wasmMem, this.channel)
outputs = inputs
this.recognizerPort.postMessage(".") // A
return true
}
})

0
src/pre3.js Normal file
View File

View File

@@ -1,3 +0,0 @@
`])))
return new recognizer(rec,ctx)
}