2021-06-25 22:38:35 +00:00
|
|
|
declare global {
|
|
|
|
interface AudioWorkletProcessor {
|
|
|
|
readonly port: MessagePort;
|
|
|
|
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Map<string, Float32Array>): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AudioWorkletProcessor: {
|
|
|
|
prototype: AudioWorkletProcessor;
|
|
|
|
new(options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
|
|
|
|
};
|
|
|
|
|
2022-05-10 15:04:20 +00:00
|
|
|
function registerProcessor(name: string, ctor :{ new(): AudioWorkletProcessor }): void;
|
2021-06-25 22:38:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 15:38:40 +00:00
|
|
|
export interface AppleAudioMessageEvent extends MessageEvent {
|
|
|
|
data: Float32Array;
|
|
|
|
}
|
|
|
|
|
2021-06-25 22:38:35 +00:00
|
|
|
export class AppleAudioProcessor extends AudioWorkletProcessor {
|
2021-11-29 00:20:25 +00:00
|
|
|
private samples: Float32Array[] = [];
|
2021-06-25 22:38:35 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
console.info('AppleAudioProcessor constructor');
|
2022-05-31 15:38:40 +00:00
|
|
|
this.port.onmessage = (ev: AppleAudioMessageEvent) => {
|
2021-06-25 22:38:35 +00:00
|
|
|
this.samples.push(ev.data);
|
|
|
|
if (this.samples.length > 256) {
|
|
|
|
this.samples.shift();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static get parameterDescriptors() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
process(_inputList: Float32Array[][], outputList: Float32Array[][], _parameters: Map<string, Float32Array>) {
|
|
|
|
const sample = this.samples.shift();
|
|
|
|
const output = outputList[0];
|
|
|
|
if (sample) {
|
|
|
|
for (let idx = 0; idx < sample.length; idx++) {
|
|
|
|
output[0][idx] = sample[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep alive indefinitely.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerProcessor('audio_worker', AppleAudioProcessor);
|