Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
audio-feeder
Advanced tools
Abstraction for streaming raw PCM audio in browser via Web Audio API.
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.
options.backendFactory
.initSharedAudioContext
audioNode
option allows attaching to non-default destinationstop()
/start()
buffered audio recoverystop()
/start()
more reliable, doesn't drop audioplaybackPosition
no longer advances while pausedstop()
/start()
flush()
method; use to clear buffers after a stop when seeking etconstarved
callback for Flash backendonbufferlow
callback when buffered data gets low, but not yet emptybufferThreshold
property to get/set the threshold in secondsdurationBuffered
property to track amount of data left to playplaybackPosition
property mirroring getPlaybackState().playbackPositionIf 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');
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.
// 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.
WebAudio-specific:
audioContext
: an AudioContext
object to be use instead of creating a new oneoutput
: an AudioNode
object to attach output to instead of the defaultGeneral:
backendFactory
: a function to call to provide a custom backend, for testing or advanced usage. Should take params numChannels
, sampleRate
, and options
and return an object conforming to the internal backend class protocol. This is not considered a stable API at this time.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.
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.
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.
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:
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.
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.
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.
FAQs
Abstraction for streaming raw PCM audio in browser via Web Audio API.
The npm package audio-feeder receives a total of 2,762 weekly downloads. As such, audio-feeder popularity was classified as popular.
We found that audio-feeder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.