Audio data container with planar float32 layout.
Web Audio API AudioBuffer ponyfill for node, bun and other envs.
Comes with optional utils.
import AudioBuffer from 'audio-buffer'
import { from, trim, normalize } from 'audio-buffer/util'
let buf = from([0, 0, 0.5, 0.8, 0.3, 0, 0])
buf = normalize(trim(buf))new AudioBuffer({ length: 1024, sampleRate: 44100, numberOfChannels: 2 })
new AudioBuffer(2, 1024, 44100) // positional formAll read-only.
length— samples per channelsampleRate— Hzduration— secondsnumberOfChannels
Iterable over channels: for (let ch of buf), let [L, R] = buf.
buf.getChannelData(0) // Float32Array for channel 0
buf.copyFromChannel(dest, 0, 100) // copy channel 0 from sample 100 into dest
buf.copyToChannel(src, 1) // write src into channel 1import { from, slice, concat, set } from 'audio-buffer/util'Same-size ops mutate and return the buffer. Size-changing ops return a new buffer.
Check if object is an AudioBuffer instance or duck-typed compatible.
isAudioBuffer(buf) // trueCreate buffer from anything — number, Float32Array, Array, AudioBuffer, ArrayBuffer. Optional second arg fills samples — a number for constant value, or fn(sample, index, channel, channelData) to map (like Array.from).
from([0.1, -0.3, 0.5]) // array of samples → mono
from([left, right], { sampleRate: 48000 }) // Float32Array[] → stereo
from(existingBuffer) // clone
from(1024, 0.5) // 1024 samples buffer filled with 0.5
from(1024, (s, i) => Math.sin(i * 0.1)) // generate sine wave
from(buf, v => v * 0.5) // clone with half volume
from(buf, 0) // same shape, zeroed (like)
from({ length: 1024, numberOfChannels: 2 }) // from optionsExtract sample range into new buffer.
slice(buf, 100, 200) // samples 100–199
slice(buf, -50) // last 50 samplesJoin buffers (same sampleRate and channels).
concat(a, b) // a + b end-to-end
concat(a, b, c, d) // join manyOverwrite samples from another buffer.
set(buf, patch, 1000) // write patch at sample 1000Fill with constant or per-sample function.
fill(buf, 0) // silence
fill(buf, (s, i, ch) => Math.sin(i * 0.1)) // sine wave
fill(buf, () => Math.random() * 2 - 1) // white noise
fill(buf, v => -v) // phase-invertPeak-normalize to 1.0, preserving inter-channel balance.
normalize(buf) // quiet recording → full scaleRemove silence from both ends.
trim(buf) // remove exact zeros
trim(buf, 0.01) // remove near-silenceReverse samples.
reverse(buf) // full reverse
reverse(buf, 0, 100) // reverse first 100 samplesBlend b into a. Ratio 0 = keep a, 1 = replace with b.
mix(track, reverb, 0.3) // 70% dry, 30% wet
mix(a, b, (sa, sb) => Math.max(sa, sb)) // custom blend function
mix(a, b, 0.5, 1000) // mix starting at sample 1000Upmix/downmix channels per Web Audio spec speaker rules.
remix(stereo, 1) // stereo → mono
remix(mono, 2) // mono → stereo
remix(buf, 6) // → 5.1 surround
remix(buf, 4, 'discrete') // copy channels, silence restPad to target length.
pad(buf, 44100) // zero-pad to 1 second
pad(buf, 44100, 0, 'start') // pad at startRepeat N times.
repeat(buf, 4) // loop 4xCircular shift. Positive = right.
rotate(buf, 100) // shift right 100 samples
rotate(buf, -50) // shift left 50Remove DC offset (subtract mean per channel).
removeDC(buf) // center waveform at zeroDeep equality — same shape, same samples.
isEqual(buf, clone) // true if identicalimport play from 'audio-buffer/play'
let ctrl = await play(buf, { volume: 0.8, loop: true })
ctrl.pause()
ctrl.play()
ctrl.stop()
ctrl.currentTime // seconds
ctrl.playing // booleanOptions: { volume, loop, start, end, autoplay, onended }.
Uses Web Audio API in browsers. In Node.js, install audio-speaker: npm i audio-speaker.
stop() resets to start. play() restarts from beginning.