case "opmintb":
opmIntB (opmPortArray[command.p]);
break;
case "pcmset":
pcmSet (command.d, command.f, command.l, command.r);
break;
case "pcmstart":
pcmStart ();
break;
case "pcmstop":
pcmStop ();
break;
}
}
class OpmpcmProcessor extends AudioWorkletProcessor {
constructor (options) {
super (options);
opmpcmProcessor = this;
opmInit ();
pcmInit ();
internalBufferLeft = new Float32Array (INTERNAL_BUFFER_FRAMES);
internalBufferRight = new Float32Array (INTERNAL_BUFFER_FRAMES);
internalBufferTime = currentTime;
externalSampleRate = sampleRate;
externalBufferFrames = floor (externalSampleRate * INTERNAL_BUFFER_MSEC / 1000);
externalToInternal = new Int32Array (externalBufferFrames);
for (let i = 0; i < externalBufferFrames; i++) {
externalToInternal[i] = floor ((i * INTERNAL_BUFFER_FRAMES) / externalBufferFrames);
}
externalBufferPointer = externalBufferFrames;
commandQueue = [];
commandTime = 0;
this.port.onmessage = e => {
addCommand (e.data);
};
this.port.start ();
}
process (inputs, outputs, parameters) {
let outputBufferLeft = outputs[0][0];
let outputBufferRight = outputs[0][1];
let p = externalBufferPointer;
let o = 0;
let r = outputBufferLeft.length;
while (r) {
if (p == externalBufferFrames) {
let startTime = internalBufferTime;
let endTime = startTime + INTERNAL_BUFFER_MSEC / 1000;
internalBufferTime = endTime;
let from = 0;
if (false) {
while (0 < commandQueue.length && commandQueue[0].t < endTime) {
let command = commandQueue.shift ();
let to = floor ((command.t - startTime) * INTERNAL_SAMPLE_RATE);
if (from < to) {
audioAdvance (from, to);
from = to;
}
execCommand (command);
}
} else {
while (0 < commandQueue.length) {
let command = commandQueue.shift ();
execCommand (command);
}
}
if (from < INTERNAL_BUFFER_FRAMES) {
audioAdvance (from, INTERNAL_BUFFER_FRAMES);
}
p = 0;
}
let n = min2 (r, externalBufferFrames - p);
let q = p + n;
while (p < q) {
let s = externalToInternal[p];
outputBufferLeft[o] = internalBufferLeft[s];
outputBufferRight[o] = internalBufferRight[s];
o++;
p++;
}
r -= n;
}
externalBufferPointer = p;
return true;
}
}
registerProcessor ("opmpcm-processor", OpmpcmProcessor);