Audio Buffer Utilities

5.1.2 · active · verified Sun Apr 19

audio-buffer-utils is a JavaScript utility library providing a comprehensive set of functions for manipulating `AudioBuffer` objects, primarily for Web Audio API contexts and Node.js environments. Currently at version 5.1.2, it offers operations such as creation, cloning, slicing, concatenation, mathematical transformations (reverse, invert, normalize), and various data manipulations (fill, resize, trim, pad, shift, rotate). The library focuses on performance optimization for audio processing tasks and is part of the `audiojs` organization, which maintains other Web Audio related packages. While offering robust functionality, its internal API stability is noted as 'unstable' in its badges, indicating potential API changes in minor versions, though the package is actively maintained and has a steady release cadence for bug fixes and minor features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create, clone, slice, and fill `AudioBuffer` objects using the library's utility functions. It covers basic instantiation for mono and stereo buffers, copying data, extracting sub-sections, and modifying sample values.

import util from 'audio-buffer-utils';

// Create a mono buffer with 100 samples
const sampleRate = 44100;
const monoBuffer = util.create(100, 1, sampleRate);
console.log(`Created mono buffer: ${monoBuffer.length} samples, ${monoBuffer.numberOfChannels} channel(s).`);

// Create a stereo buffer with 2 seconds duration
const stereoBuffer = util.create(2 * sampleRate, 2, sampleRate);
console.log(`Created stereo buffer: ${stereoBuffer.length} samples, ${stereoBuffer.numberOfChannels} channel(s).`);

// Clone a buffer
const clonedBuffer = util.clone(monoBuffer);
console.log(`Cloned buffer is equal to original: ${util.equal(monoBuffer, clonedBuffer)}`);

// Slice a portion of the buffer
const slicedBuffer = util.slice(stereoBuffer, 0.5 * sampleRate, 1.5 * sampleRate);
console.log(`Sliced buffer from 0.5s to 1.5s: ${slicedBuffer.length} samples.`);

// Example of filling a channel with a value
util.fill(monoBuffer, null, 0.5); // Fill monoBuffer with 0.5
console.log(`Mono buffer first sample after fill: ${monoBuffer.getChannelData(0)[0]}`);

view raw JSON →