audio-buffer
Version:
Audio data container
65 lines (43 loc) • 3.35 kB
Markdown
[](https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer) ponyfill. Provides useful constructor for Web-Audio API _AudioBuffer_, if available, otherwise provides optimal _AudioBuffer_ implementation for node/browsers. Useful instead of _Buffer_ in audio streams (see [**@audiojs**](https://github.com/audiojs) components).
[](https://npmjs.org/package/audio-buffer/)
```js
var AudioBuffer = require('audio-buffer')
//Create audio buffer from a data source or of a length.
//Data is interpreted as a planar sequence of float32 samples.
//It can be Array, TypedArray, ArrayBuffer, Buffer, AudioBuffer, DataView, NDArray etc.
var buffer = new AudioBuffer(channels = 2, data|length, sampleRate = 44100, options?)
//Duration of the underlying audio data, in seconds
buffer.duration
//Number of samples per channel
buffer.length
//Default sample rate is 44100
buffer.sampleRate
//Default number of channels is 2
buffer.numberOfChannels
//Get array containing the data for the channel (not copied)
buffer.getChannelData(channel)
//Place data from channel to destination Float32Array
buffer.copyFromChannel(destination, channelNumber, startInChannel = 0)
//Place data from source Float32Array to the channel
buffer.copyToChannel(source, channelNumber, startInChannel = 0)
```
Options object can be passed as last argument with the following:
* `floatArray` — type of array for data, defaults to _Float32Array_. Note that to use _Float64Array_ in browser you would have to disable WebAudioAPI fallback by passing `isWAA: false`.
* `context` — custom audio context, default context is [audio-context](https://npmjs.org/package/audio-context) module.
* `isWAA` — if WebAudioAPI _AudioBuffer_ should be created. Use `false` for emulated buffers - in nodejs that is always false. That can be handy in case if you need to create buffer from subarrays to avoid cloning the data, like so:
```js
var a = new AudioBuffer(1, [0, .1, .2, .3], {isWAA: false})
var b = new AudioBuffer(1, [a.getChannelData(0).subarray(1,2)], {isWAA: false})
b.getChannelData(0)[0] = .4
a.getChannelData(0) // [0, .4, .2, .3]
```
* [audio-buffer-utils](https://github.com/audiojs/audio-buffer-utils) — utils for audio buffers
* [pcm-util](https://npmjs.org/package/pcm-util) — utils for audio format convertions.
* [ndsamples](https://github.com/livejs/ndsamples) — audio-wrapper for ndarrays. A somewhat alternative approach to wrap audio data, based on ndarrays, used by some modules in [livejs](https://github.com/livejs).
* [1](https://www.npmjs.com/package/audiobuffer), [2](https://www.npmjs.com/package/audio-buffer), [3](https://github.com/sebpiq/node-web-audio-api/blob/master/lib/AudioBuffer.js), [4](https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer) — other AudioBuffer implementations.
* [audiodata](https://www.npmjs.com/package/audiodata) alternative data holder from @mohayonao.