Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm / Technique for Buffered audio playback with node.js

I am using node.js to playback an audio stream such as a radio station or an mp3 stream from the web. The flow of data is like so:

Radio station (mp3 audio) -> My node.js script -> lame mp3 decoder (node-lame) -> PCM output to soundcard (node-sound).

The problem is that due to network latency, the playback is sometimes broken. So what I did was to buffer all the data chunks into a variable. Once the variable size reaches around (1Mb) I start playing sound back from the variable instead of the stream directly. The solved the broken playback.

My question is - I probably don't need wait until the buffer size reaches 1Mb. Im sure there is an algorithm or some calculation to detect the average network speed or latency and determine an optimal buffer size for uninterrupted playback. Ofcourse I can come up with some sloppy solution out of trial and error but Im wondering if there are any algorithms or techniques for what I am trying to accomplish?

I googled around to find out some whitepapers about Frame size of DSP etc but suggestions from the community here would be great.

like image 310
Rohit Manohar Avatar asked Nov 01 '22 06:11

Rohit Manohar


1 Answers

Buffering requires prediction, but "Prediction is very difficult, especially if it's about the future". :-)

If you're doing something simple, your buffer size determines the latency from when you hit play until you hear audio. If, as a user, you can tolerate a long delay there, set a big buffer accordingly. If not, many of the better buffering algorithms (likely including your phone's voice channel) vary the playback rate, playing audio back slightly slower than the nominal rate, until a large buffer is built up. If you have that kind of control of your audio hardware, it's the best solution--you can slowly build up several MB of buffer without impacting the latency from clicking play to hearing audio. Users normally don't notice moderate rate changes--most US over-the-air stations speed up song playback by 2%+ to fit in more commercials, and almost no one notices. At 5% many people do notice. At a certain buffer size you can return to the nominal rate, and enjoy uninterrupted playback.

There are many schemes that attempt to train to the perfect size, but your local wifi, and the reliability of the site's playback, make a one-size-fits-all algorithm difficult. People will point to YouTube, Netflix, Hulu, etc--but those aren't live so that's a different problem. Twitch.tv has live content, and there's a buffering latency on start.

like image 164
jimm101 Avatar answered Nov 11 '22 09:11

jimm101