From 5de00a7da6a0162a1be237f6450269166cd54d06 Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Sun, 16 Jun 2024 12:42:25 +0200 Subject: [PATCH] working example (add cylinder, clone, delete... rest is trickier) --- index.html | 94 ++++++++++++++++++++++++----------------- recognizer-processor.js | 39 +++++++++++++++++ 2 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 recognizer-processor.js diff --git a/index.html b/index.html index dec6c5a..c9ec33f 100644 --- a/index.html +++ b/index.html @@ -23,7 +23,7 @@ - + diff --git a/recognizer-processor.js b/recognizer-processor.js new file mode 100644 index 0000000..41be1f6 --- /dev/null +++ b/recognizer-processor.js @@ -0,0 +1,39 @@ +class RecognizerAudioProcessor extends AudioWorkletProcessor { + constructor(options) { + super(options); + + this.port.onmessage = this._processMessage.bind(this); + } + + _processMessage(event) { + // console.debug(`Received event ${JSON.stringify(event.data, null, 2)}`); + if (event.data.action === "init") { + this._recognizerId = event.data.recognizerId; + this._recognizerPort = event.ports[0]; + } + } + + process(inputs, outputs, parameters) { + const data = inputs[0][0]; + if (this._recognizerPort && data) { + // AudioBuffer samples are represented as floating point numbers between -1.0 and 1.0 whilst + // Kaldi expects them to be between -32768 and 32767 (the range of a signed int16) + const audioArray = data.map((value) => value * 0x8000); + + this._recognizerPort.postMessage( + { + action: "audioChunk", + data: audioArray, + recognizerId: this._recognizerId, + sampleRate, // Part of AudioWorkletGlobalScope + }, + { + transfer: [audioArray.buffer], + } + ); + } + return true; + } +} + +registerProcessor('recognizer-processor', RecognizerAudioProcessor) \ No newline at end of file