based on https://github.com/zhuker/lamejs/tree/master/worker-example .
// mic.js
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
const context = new AudioContext();
const microphone = context.createMediaStreamSource(stream);
await context.audioWorklet.addModule('/voiceRecorderProcessor.js')
const recorder = new AudioWorkletNode(context, 'recorder');
recorder.port.onmessage = e => {
realTimeWorker.postMessage({cmd: 'encode', buf: e.data});
}
microphone.connect(recorder);
recorder.connect(context.destination);
// voiceRecorderProcessor.js
class Recorder extends AudioWorkletProcessor {
constructor() {
super();
}
process(allInputs) {
// allInputs has always latest value, but their belongs to same refenrences.
const inputs = allInputs[0][0]; // monoral
this.port.postMessage(inputs);
return true;
}
}
registerProcessor('recorder', Recorder);