Extension of BufferList for AudioBuffers. Handy and performant to deal with (possibly long) sequence of audio buffers − accumulate, read, stream, modify, delete etc.
import AudioBufferList from 'audio-buffer-list'
let abl = new AudioBufferList([
new AudioBuffer({length: 4, sampleRate: 44100 }),
new AudioBuffer({length: 100, sampleRate: 44100 })
])
abl.append(
new AudioBuffer({length: 100})
)
abl.length // 204
abl.slice() // <AudioBuffer 0, ...>
Creates new audio buffer list instance, new
is not strictly required.
source
can be AudioBuffer, AudioBuffer array, AudioBufferList or AudioBufferList array.
options
may provide numberOfChannels
, context
for web audio API context and sampleRate
.
The created list instance contains the following properties:
list.buffers
− sequence of audio buffers with actual data.list.length
− total length of list in samples, i.e. sum of inner buffer lengths.list.duration
− total duration of the audio list, i.e. sum of buffers durations.list.numberOfChannels
− detected from the buffer with max number of channels in the list. Can be set by options.list.sampleRate
− just for convenience with AudioBuffer interface.
Insert new AudioBuffer, AudioBufferList or array of them to the end.
Put new AudioBuffer, AudioBufferList or array of them at the offset.
Remove number of samples from the list starting at the offset
. count
can possibly be negative, then items are removed on the left side from the offset. offset
can also be negative, meaning to remove from the end. Retuns removed sublist instance.
Return sublist of the initial list. The data is not copied but returned as subarrays.
Map buffers from the interval defined by from
and to
arguments. Modifies list in-place.
Mapper function has signature (buffer, idx, offset) => buffer
. buffer
is an audio buffer to process, idx
is buffer number, and offset
is first buffer sample absolute offset. If mapper returns undefined
, the buffer is preserved. If mapper returns null
, the buffer is discarded. If mapper returns false
, iterations are stopped.
Pass {reversed: true}
option to walk in reversed order.
list = list.map((buf, idx, offset) => {
for (let c = 0; c < channels; c++) {
let data = buf.getChannelData(c)
//start buffer from the subset may start earlier than the subset
//end buffer from the subset may end later than the subset
for (let i = 0, l = buf.length; i < l; i++) {
data[i] = process(data[i])
}
}
}, from, to)
Same as map, but runs from tail (to break preliminarily).
Repeats contents of the list specified number of times. Modifies list in-place, returns self.
Put data into destination AudioBuffer or create one. It is like slice
, but returns an AudioBuffer.
Put data from the channel to destination FloatArray. Optional startInChannel
defines offset in the channel to start from.
Put data from the source FloatArray into channel, optionally starting at startInChannel
offset.
Split list at the indicated indexes. That increases number of inner buffers.
Joins buffers from the indicated range. Returns an AudioBuffer with joined data.
Return [bufIdx, offset]
pair for any sample number. bufIdx
is the number of buffer in the list, offset
is sample offset inside of that buffer.
Clean up list.
- audio-buffer-utils — toolset for audio buffers.
- buffer-list — canonical BufferList implementation.