I am trying to provide a callback function that can be invoked when an audio processor has completed. I initially went down the route of doing:
let complete = false;
const callbackPtr = module.addFunction(() => {
complete = true;
}, 'v');
...
const ptr = module._malloc(lpc.length);
const copy = new Uint8Array(module.HEAPU8.buffer, ptr, lpc.length);
copy.set(lpc);
speak(ptr, lpc.length, callbackPtr);
module._free(ptr);
However, I was getting table index is out of bounds
despite having -sALLOW_MEMORY_GROWTH=1 -sALLOW_TABLE_GROWTH=1
options… And then I realized, this is happening because the function pointer is getting executed in the audio thread and does not exist there…
So, then I thought perhaps I need to do this with postMessage
, so I was going to try to do:
EM_BOOL AudioCallback(int numInputs, const AudioSampleFrame *inputs,
int numOutputs, AudioSampleFrame *outputs,
int numParams, const AudioParamFrame *params,
void *userData) {
SampleStreamable *sampleStreamable = static_cast<SampleStreamable *>(userData);
int frames = 128;
for (int i = 0; i < numOutputs; i++) {
sampleStreamable->process(outputs[i].data, frames*outputs[i].numberOfChannels);
}
EM_ASM({
if ($0) {
postMessage('audio-processing-complete');
}
}, sampleStreamable->isComplete)
return EM_TRUE;
}
However, that results in postMessage is not defined
.
How can I properly execute a callback on the main thread?