Registry / http-networking / audio-buffer-utils

audio-buffer-utils

JSON →
library5.1.2jsnpmunverified

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.

npm install audio-buffer-utils
INSTALL
IMPORT
SIG · AUDIO-BUFFER-UTILS
A
audio-buffer-utils
http-networkingjavascriptv5.1.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

util
import util from 'audio-buffer-utils';
import { create, clone } from 'audio-buffer-utils'; // Incorrect named imports
The library exports a single default object containing all utility functions. Access them as properties of this 'util' object.
util
const util = require('audio-buffer-utils');
For CommonJS environments, the entire utility collection is exported as the module's default export.
create
import util from 'audio-buffer-utils'; const newBuffer = util.create(100);
import { create } from 'audio-buffer-utils'; // 'create' is not a named export
All functions like `create`, `clone`, `slice` are methods on the default-exported 'util' object, not individual named exports.

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]}`);
Debug
Known issues
gotchaThe project's stability badge explicitly indicates 'unstable', suggesting that internal APIs, function signatures, or behaviors might change in minor versions without adhering strictly to semantic versioning for non-major releases. Users should exercise caution and thoroughly test when upgrading.
fix
Refer to the GitHub repository and release notes for each version upgrade. Pin to specific patch versions (`~x.y.z`) to mitigate unexpected changes in production, and dedicate time for testing new minor versions.
affects: >=1.0.0
gotchaThe `util.copy(fromBuffer, toBuffer, offset)` function can throw a `RangeError` (or similar) if the data from `fromBuffer`, when combined with the specified `offset`, extends beyond the length of `toBuffer`. This is not an automatic resize.
fix
Always ensure the `toBuffer` argument provided to `util.copy` has sufficient allocated length to accommodate all data being copied from `fromBuffer`, considering the starting `offset`.
affects: >=1.0.0
gotchaThe `util.subbuffer` function returns a 'null-context buffer' – a plain JavaScript object that references a segment of the original buffer's data. This object is *not* a true Web Audio API `AudioBuffer` instance and cannot be used directly with `AudioContext` methods (e.g., `decodeAudioData`) or connected to `AudioNode`s.
fix
To use a subbuffer with the Web Audio API, you must explicitly convert its data into a new, proper `AudioBuffer` instance using `AudioContext.createBuffer` and copying the data (e.g., `newBuffer.copyToChannel(...)`) or by using `util.create` with the subbuffer's data.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: util.create is not a function
Attempting to destructure or access `create` as a named export from 'audio-buffer-utils', when the library provides a single default export (an object containing all utility functions).
fix
Correct the import statement to `import util from 'audio-buffer-utils';` (ESM) or `const util = require('audio-buffer-utils');` (CommonJS), and then access the function as `util.create(...)`.
RangeError: Offset + length out of bounds
When using `util.copy(fromBuffer, toBuffer, offset)`, the target `toBuffer` is not large enough to hold all the data from `fromBuffer` starting at the specified `offset`.
fix
Before calling `util.copy`, ensure that `toBuffer.length` is greater than or equal to `offset + fromBuffer.length`. You might need to resize `toBuffer` or create a larger one.
TypeError: Failed to execute 'connect' on 'AudioNode': parameter 1 is not of type 'AudioNode'.
Attempting to connect the return value of `util.subbuffer` directly to a Web Audio API `AudioNode`. `subbuffer` returns a plain JavaScript object, not an `AudioBuffer` suitable for the Web Audio graph.
fix
After getting a subbuffer with `util.subbuffer`, create a new `AudioBuffer` instance using `AudioContext.createBuffer()` and copy the data from the subbuffer into this new `AudioBuffer` before connecting it to an `AudioNode`.
Upgrade
Version history
5.1.2latest on npm
Audit
Dependencies
audio-buffer-fromrequiredUsed internally by `util.create` for flexible AudioBuffer instantiation from various data sources.
ndarrayrequiredUsed for efficient multi-dimensional array operations on audio data.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
audio-buffer-utils — npm install audio-buffer-utils · libregistry