Home

Awesome

audio-feeder

The AudioFeeder class abstracts a buffered output pipe for uncompressed PCM audio in the browser, supporting the standard W3C Web Audio API.

AudioFeeder was written for the ogv.js in-browser Ogg/WebM media player, and is suitable for use in custom audio and video playback.

Copyright and license

Updates

Installing with webpack or browserify

If your project is built with webpack or browserify, it's easy to bundle up AudioFeeder's JavaScript classes; you will have to manually ensure that the Flash shim for IE is bundled alongside it.

Add to your npm dependencies:

npm install audio-feeder

and in your using code, set up the class like so:

var AudioFeeder = require('audio-feeder');

Including AudioFeeder manually in a project

Grab AudioFeeder.js or AudioFeeder.min.js (minified) from the ZIP download or from dist/ subdir in the npm module.

Include either as a module (CommonJS or AMD) or a standalone script.

Usage

// Create a feeder object
var feeder = new AudioFeeder();

// Set up 2-channel stereo, 48 kHz sampling rate
feeder.init(2, 48000);

// Flash mode for IE 10/11 requires waiting.
feeder.waitUntilReady(function() {

  // Buffer some data before we start playback...
  //
  // Each channel gets its own 32-bit float array of samples;
  // this will be 0.25 seconds of silence at 2ch/48kHz.
  //
  // Note it's ok for each bufferData() call to have a different
  // number of samples, such as when working with a data format
  // with variable packet size (Vorbis).
  //
  feeder.bufferData([
    new Float32Array(12000),
    new Float32Array(12000)
  ]);

  // Start playback...
  feeder.start();

  document.querySelector('button.stop').addEventListener('click', function() {
    // You can pause output at any time:
    feeder.stop();
    // to release resources, call feeder.close() instead.
  });

  // Callback when buffered data runs below feeder.bufferThreshold seconds:
  feeder.onbufferlow = function() {
    while (feeder.durationBuffered < feeder.bufferThreshold) {
      feeder.bufferData([
        new Float32Array(12000),
        new Float32Array(12000)
      ]);
    }
  };

});

See also the included demo.html file for a live sample web page.

Options

WebAudio-specific:

General:

Data format

AudioFeeder works with 32-bit floating point PCM audio. Data packets are represented as an array containing a separate Float32Array for each channel.

Warning: this may change to use a wrapper class before 1.0.

Status and audio/video synchronization

The current playback position in seconds, and the duration of buffered but not yet played data, are available through the playbackPosition and durationBuffered properties.

Additional playback state can be retrieved from the getPlaybackState() method:

{
  playbackPosition: Float /* seconds of sample data that have played back so far */,
  samplesQueued: Float /* samples remaining before the buffer empties out, approximate */,
  dropped: Integer /* count of buffer underrun events */,
  delayed: Float /* total seconds of silence played to cover underruns */
}

Warning: this structure may change before 1.0.

playbackPosition tracks the time via actual samples output, corrected for drops and underruns. This value is suitable for use in scheduling output of synchronized video frames.

This high-level pseudocode shows a simplified version of the playback sync logic from the ogv.js video player:

function processMediaData() {
  while (codec.audioReady && audioFeeder.durationBuffered < audioFeeder.bufferThreshold) {
    // When our audio buffer gets low, feed it some more audio data.
    audioFeeder.bufferData(decodeAudioPacket());
  }

  if (codec.frameReady && audioFeeder.playbackPosition >= codec.nextFrameTimestamp) {
    // When the audio playback has reached the scheduled time position
    // of the next frame, decode and draw it.
    player.drawFrame(codec.decodeVideoPacket());
  }

  // And check back in before the next frame!
  if (codec.dataPending) {
    requestAnimationFrame(processMediaData);
  }
}

// Fire off an animation-based loop...
requestAnimationFrame(processMediaData);

// If in a background thread, animation loops will be throttled.
// Also fire when audio gets low!
audioFeeder.onbufferlow = processMediaData;

The caller is responsible for maintaining a loop and scheduling any decoding, frame drawing, etc.

Performance considerations

Beware that setTimeout, setInterval, and requestAnimationFrame may be throttled on background tabs, leading to spotty performance if scheduling decoding based on them.

To avoid background tab throttling, use the onbufferlow event callback to run additional decoding/buffering. This is fired asynchronously when the available buffered data runs below bufferThreshold seconds.

You can buffer an arbitrarily large amount of audio data, but for non-trivial examples it's best to decode or generate audio in smallish chunks and buffer them over time. Pre-buffering will eat more memory, and could lead to slowness on the main thread if you process a lot of data on the main thread in one function call.

Performing other slow tasks on the foreground thread may also prevent the Web Audio API callbacks from being called in a timely fashion, resulting in audio underruns even if lots of data has been buffered up.

Events

There are currently two supported events, set via the 'onstarved' and 'onbufferlow' properties.

'onstarved' is called when buffered data runs out during playback, giving a last-chance opportunity to buffer more data. This is a synchronous call in the audio path, and may not be enough to guarantee good performance.

'onbufferlow' is called asynchronously when the buffered data runs lower than a configurable threshold, which is more flexible. This threshold is available for get and set via the bufferThreshold property, defaulting to twice the low-level buffer duration.

You can use these events to buffer additional data at the last minute, or to trigger a close-out of the feeder when no more data is available.

Todo:

Flash and Internet Explorer 10/11

An earlier version of AudioFeeder supported a Flash backend for supporting Internet Explorer 10/11. This is no longer possible since the Flash plugin was disabled by Adobe in 2021.

Rate control

As of 0.4.21, the tempo or playback rate can be modified without altering pitch, suitable for use in implementing video playback control.

Set the tempo property to a value larger than 1 to multiply speed, or less than 1 to decrease it. The playbackPosition property is returned in input units, making it fairly easy to report back a current time suitable for A/V sync; however some properties such as durationBuffered will return output units and should be used carefully when at non-default empos.

Currently the tempo control is not applied to data that has already been buffered, which can produce a "lag time" before tempo changes take effect.

Note that stereo or multi-channel input is mixed down to mono for processing when the tempo is not set at 1; see audio-tempo-changer#1.

Rebuilding pre-packed AudioFeeder.js

The pre-packed AudioFeeder.js included in ZIP and npm releases can be rebuilt from the source files. This is known to work on Mac, Linux, and Windows.

Build prerequisites:

# Fetch build dependencies (webpack, eslint etc)
npm install

# Lint and rebuild
npx grunt

This will produce a 'dist' subdirectory containing a ready to use AudioFeeder.js, and AudioFeeder.min.js as well as a demo.html example page.