Search code examples
javascriptaudiowebsocketaudiocontext

Convert audioContext back into Buffers


I have an audioContext that gets its media from createMediaElementSource. I want to parse this audio on the go into AudioBuffers or something similar that I can send over to another client over websockets.

let audioElement = document.querySelector('video')
let audioContext = new window.AudioContext()
let source = audioContext.createMediaElementSource(audioElement)
source.connect(deliverToOtherClientOrSomething)

I tried making a AudioWorkletNode, but the problem with this approach is that it doesn't allow me to end the chain there, but forces me to forward the audio to some other AudioContext element, which is unwanted.


Solution

  • So, in the end this problem was solved by using an audio worklet node. When creating an AudioWorkletNode it is possible to pass options to it. One of the options is numberOfOutputs. By doing this my question is completely answered.

    Mainfile

    const sendProcessor = new AudioWorkletNode(audioContext, 'send-processor', {numberOfOutputs:0})
    sendProcessor.port.onmessage = (event) => {
        callback(event.data);
    }
    

    Processor file

    process(inputs, outputs) {
        this.port.postMessage(inputs[0][0]);
        return true;
    }