package
AudioBuffer ponyfill with operations toolkit
npm install audio-bufferGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
29.9 kB
Includes dependencies.
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 — secondsnumberOfChannelsIterable 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.
isAudioBuffer(buf) → booleanCheck if object is an AudioBuffer instance or duck-typed compatible.
isAudioBuffer(buf) // truefrom(source, fill?, options?) → AudioBufferCreate 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 optionsslice(buffer, start?, end?) → newBufferExtract sample range into new buffer.
slice(buf, 100, 200) // samples 100–199
slice(buf, -50) // last 50 samplesconcat(...buffers) → newBufferJoin buffers (same sampleRate and channels).
concat(a, b) // a + b end-to-end
concat(a, b, c, d) // join manyset(buffer, other, offset?) → bufferOverwrite samples from another buffer.
set(buf, patch, 1000) // write patch at sample 1000fill(buffer, value, start?, end?) → bufferFill 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-invertnormalize(buffer, start?, end?) → bufferPeak-normalize to 1.0, preserving inter-channel balance.
normalize(buf) // quiet recording → full scaletrim(buffer, threshold?) → newBufferRemove silence from both ends.
trim(buf) // remove exact zeros
trim(buf, 0.01) // remove near-silencereverse(buffer, start?, end?) → bufferReverse samples.
reverse(buf) // full reverse
reverse(buf, 0, 100) // reverse first 100 samplesmix(a, b, ratio?, offset?) → aBlend 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 1000remix(buffer, channels, interpretation?) → newBufferUpmix/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(buffer, length, value?, side?) → newBufferPad to target length.
pad(buf, 44100) // zero-pad to 1 second
pad(buf, 44100, 0, 'start') // pad at startrepeat(buffer, times) → newBufferRepeat N times.
repeat(buf, 4) // loop 4xrotate(buffer, offset) → bufferCircular shift. Positive = right.
rotate(buf, 100) // shift right 100 samples
rotate(buf, -50) // shift left 50removeDC(buffer, start?, end?) → bufferRemove DC offset (subtract mean per channel).
removeDC(buf) // center waveform at zeroisEqual(a, b) → booleanDeep 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.
ahdinosaur
mikey@mikey.nz