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-utilsVerified import paths — ran on the pinned version, not inferred.
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.
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.
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`.
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.
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(...)`.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.
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`.