Tuesday, 14 May 2019

How to Stream Live Audio With Low Latency

I have programmed a server that creates an audio stream from my MacBook's audio input, using express, osx-audio and lame:

const http = require("http");
const express = require("express");
const audio = require("osx-audio");
const lame = require("lame");
const audioInput = new audio.Input();

const encoder = new lame.Encoder({
  channels: 2,
  bitDepth: 16,
  sampleRate: 44100,
  bitRate: 128,
  outSampleRate: 22050,
  mode: lame.STEREO
});

audioInput.pipe(encoder);

const app = express();
const server = http.Server(app);

app.get("/stream.mp3", (req, res) => {
  res.set({
    "Content-Type": "audio/mpeg",
    "Transfer-Encoding": "chunked"
  });
  encoder.pipe(res);
});

server.listen(3000);

On the client side, the sound from this audio stream is included as an <audio> element like so:

<audio controls autoplay preload="none">
  <source src="./stream.mp3" type="audio/mpeg" />
  <p>Oops – your browser doesn't support HTML5 audio!</p>
</audio>

This works – I can hear the sound from the input source I've selected on my laptop from any browser that is connected to the server when I click the “play” button on the audio element.

However the audio played by the browser lags several seconds behind the original signal. It seems although I'm using preload="none", the browser buffers quite a lot of the audio stream before it starts playing it.

Is there something obvious missing here? Is there a better way to achieve real-time audio with only a few milliseconds latency instead of several seconds?

If you're interested, my project's complete source code is here on GitHub.



from How to Stream Live Audio With Low Latency

No comments:

Post a Comment