Search code examples
audio-streamingweb-audio-api

Send PCM Data via Websocket using AudioProcessor


I am trying to record audio data from the microphone and send it via a websocket as signed 16 bit PCM data with 16000HZ (using javascript in the browser).

I can record the audio, resample it and send it to the speakers:

const audioStream = await navigator.mediaDevices.getUserMedia({
    audio: true,
    video: false
});
const audioContext = new AudioContext({ 16000 });
const audioStreamSource = audioContext.createMediaStreamSource(audioStream);
audioStreamSource.connect(audioContext.destination);

This works, but is it also possible to get the Raw audio data in regular intervals and send it via websocket?


Solution

  • You can use an AudioWorkletProcessor to access the raw PCM data.

    An AudioWorkletProcessor runs on the audio thread which means you would need to send the audio data to the main thread or a Web Worker before you can send it over a WebSocket connection since there is no way to setup a WebSocket connection on the audio thread.