new AudioBuffer({ length: 1024, sampleRate: 44100, numberOfChannels: 2 })new AudioBuffer(2, 1024, 44100) // positional form
Properties
All read-only.
length — samples per channel
sampleRate — Hz
duration — seconds
numberOfChannels
Iterable over channels: for (let ch of buf), let [L, R] = buf.
Methods
buf.getChannelData(0) // Float32Array for channel 0buf.copyFromChannel(dest, 0, 100) // copy channel 0 from sample 100 into destbuf.copyToChannel(src, 1) // write src into channel 1
Utils
import { 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) → boolean
Check if object is an AudioBuffer instance or duck-typed compatible.
isAudioBuffer(buf) // true
from(source, fill?, options?) → AudioBuffer
Create 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 → monofrom([left, right], { sampleRate: 48000 }) // Float32Array[] → stereofrom(existingBuffer) // clonefrom(1024, 0.5) // 1024 samples buffer filled with 0.5from(1024, (s, i) => Math.sin(i * 0.1)) // generate sine wavefrom(buf, v => v * 0.5) // clone with half volumefrom(buf, 0) // same shape, zeroed (like)from({ length: 1024, numberOfChannels: 2 }) // from options