Skip to content

Instantly share code, notes, and snippets.

@super-dog-human
Last active January 31, 2021 00:31
Show Gist options
  • Save super-dog-human/f170b86e1a35cd55b2791f6760aad60b to your computer and use it in GitHub Desktop.
Save super-dog-human/f170b86e1a35cd55b2791f6760aad60b to your computer and use it in GitHub Desktop.
how to use lamejs in web worker with audioWorklet

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment