audio-buffer-list
Advanced tools
Comparing version 1.3.4 to 1.4.0
116
index.js
@@ -16,3 +16,2 @@ /** | ||
var extend = require('object-assign') | ||
var context = require('audio-context') | ||
var nidx = require('negative-index') | ||
@@ -62,3 +61,3 @@ | ||
AudioBufferList.prototype.numberOfChannels = 0 | ||
AudioBufferList.prototype.sampleRate = context.sampleRate || 44100 | ||
AudioBufferList.prototype.sampleRate = null | ||
@@ -98,19 +97,31 @@ //copy from channel into destination array | ||
//return float array with channel data | ||
AudioBufferList.prototype.getChannelData = function (channel) { | ||
if (!this._bufs.length) return new Float32Array() | ||
AudioBufferList.prototype.getChannelData = function (channel, from, to) { | ||
if (from == null) from = 0 | ||
if (to == null) to = this.length | ||
from = nidx(from, this.length) | ||
to = nidx(to, this.length) | ||
if (!this._bufs.length || from === to) return new Float32Array() | ||
//shortcut single buffer preserving subarraying | ||
if (this._bufs.length === 1) return this._bufs[0].getChannelData(channel) | ||
if (this._bufs.length === 1) { | ||
return this._bufs[0].getChannelData(channel).subarray(from, to) | ||
} | ||
var floatArray = this._bufs[0].getChannelData(0).constructor | ||
var data = new floatArray(this.length) | ||
var data = new floatArray(to - from) | ||
var fromOffset = this._offset(from) | ||
var toOffset = this._offset(to) | ||
var offset = 0 | ||
for (var i = 0, l = this._bufs.length; i < l; i++) { | ||
var firstBuf = this._bufs[fromOffset[0]] | ||
data.set(firstBuf.getChannelData(channel).subarray(fromOffset[1])) | ||
var offset = -fromOffset[1] + firstBuf.length | ||
for (var i = fromOffset[0] + 1, l = toOffset[0]; i < l; i++) { | ||
var buf = this._bufs[i] | ||
if (channel < buf.numberOfChannels) { | ||
data.set(buf.getChannelData(channel), offset); | ||
} | ||
data.set(buf.getChannelData(channel), offset); | ||
offset += buf.length | ||
} | ||
var lastBuf = this._bufs[toOffset[0]] | ||
data.set(lastBuf.getChannelData(channel).subarray(0, toOffset[1]), offset) | ||
@@ -152,3 +163,3 @@ return data | ||
AudioBufferList.prototype._appendBuffer = function (buf) { | ||
if (buf.sampleRate != this.sampleRate) throw Error('Required sample rate is ' + this.sampleRate + ', passed ' + buf.sampleRate) | ||
// if (buf.sampleRate != this.sampleRate) throw Error('Required sample rate is ' + this.sampleRate + ', passed ' + buf.sampleRate) | ||
@@ -162,2 +173,5 @@ BufferList.prototype._appendBuffer.call(this, buf) | ||
//init sampleRate | ||
if (!this.sampleRate) this.sampleRate = buf.sampleRate | ||
return this | ||
@@ -247,3 +261,3 @@ } | ||
if (start == end) { | ||
let res = new AudioBufferList([], {context: this.context}) | ||
let res = new AudioBufferList([]) | ||
return res | ||
@@ -284,5 +298,5 @@ } | ||
//clone with preserving data | ||
AudioBufferList.prototype.duplicate = function duplicate () { | ||
var i = 0 | ||
, copy = new AudioBufferList() | ||
var i = 0, copy = new AudioBufferList() | ||
@@ -296,2 +310,3 @@ for (; i < this._bufs.length; i++) | ||
;(function () { | ||
@@ -410,29 +425,58 @@ var methods = { | ||
//return new buffer by mapping it | ||
//return new list via applying fn to each buffer from the indicated range | ||
AudioBufferList.prototype.map = function map (fn, from, to) { | ||
let before, after, middle | ||
if (from != null) { | ||
before = this.shallowSlice(0, from) | ||
} | ||
if (to != null) { | ||
after = this.shallowSlice(to) | ||
} | ||
if (before || after) middle = this.shallowSlice(from, to) | ||
else middle = this | ||
if (from == null) from = 0 | ||
if (to == null) to = this.length | ||
from = nidx(from, this.length) | ||
to = nidx(to, this.length) | ||
let maxChannels = 0 | ||
middle._bufs = middle._bufs.map((buf, idx) => { | ||
buf = fn.call(this, buf, idx, this._bufs, this) | ||
if (buf.numberOfChannels > maxChannels) maxChannels = buf.numberOfChannels | ||
return buf | ||
let fromOffset = this._offset(from) | ||
let toOffset = this._offset(to) | ||
let offset = from - fromOffset[1] | ||
let before = this._bufs.slice(0, fromOffset[0]) | ||
let after = this._bufs.slice(toOffset[0] + 1) | ||
let middle = this._bufs.slice(fromOffset[0], toOffset[1] + 1) | ||
middle = middle.map((buf, idx) => { | ||
let result = fn.call(this, buf, idx, offset, this._bufs, this) | ||
if (result === undefined || result === true) result = buf | ||
//ignore removed buffers | ||
if (!result) { | ||
return null; | ||
} | ||
//track offset | ||
offset += result.length | ||
return result | ||
}) | ||
if (before) middle = before.append(middle) | ||
if (after) middle = middle.append(after) | ||
.filter((buf) => { | ||
return buf ? !!buf.length : false | ||
}) | ||
if (!before && !after) { | ||
this.numberOfChannels = maxChannels | ||
return new AudioBufferList(before.concat(middle).concat(after)) | ||
} | ||
//apply fn to every buffer for the indicated range | ||
AudioBufferList.prototype.each = function each (fn, from, to) { | ||
if (from == null) from = 0 | ||
if (to == null) to = this.length | ||
from = nidx(from, this.length) | ||
to = nidx(to, this.length) | ||
let fromOffset = this._offset(from) | ||
let toOffset = this._offset(to) | ||
let offset = from - fromOffset[1] | ||
let middle = this._bufs.slice(fromOffset[0], toOffset[1] + 1) | ||
for (let i = fromOffset[0], l = toOffset[0]+1; i < l; i++) { | ||
let buf = this._bufs[i] | ||
fn.call(this, buf, i, offset, this._bufs, this) | ||
offset += buf.length | ||
} | ||
return middle | ||
return this; | ||
} | ||
{ | ||
"name": "audio-buffer-list", | ||
"version": "1.3.4", | ||
"version": "1.4.0", | ||
"description": "Data structure for sequence of AudioBuffers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# audio-buffer-list [![Build Status](https://travis-ci.org/audiojs/audio-buffer-list.svg?branch=master)](https://travis-ci.org/audiojs/audio-buffer-list) [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges) | ||
Extension of [BufferList](https://npmjs.org/package/bl) for [AudioBuffers](https://npmjs.org/package/audio-buffer). Handy and performant to deal with sequence of audio buffers − accumulate, read, stream, modify, delete etc. It provides interfaces both of _AudioBuffer_ and _BufferList_, as well as bunch of other useful methods. | ||
Extension of [BufferList](https://npmjs.org/package/bl) for [AudioBuffers](https://npmjs.org/package/audio-buffer). Handy and performant to deal with (possibly long) sequence of audio buffers − accumulate, read, stream, modify, delete etc. It provides interfaces of both _AudioBuffer_ and _BufferList_, as well as bunch of other useful methods. | ||
@@ -43,7 +43,27 @@ ## Usage | ||
### `list.map((buffer, index) => buffer, from=0, to=-0)` | ||
### `list.map((buffer, index, offset) => buffer|bool?, from=0, to=-0)` | ||
Create new list by mapping every buffer. Optionally pass offsets `from` and `to` to map only subset. | ||
Create new list by mapping every buffer. Optionally pass offsets `from` and `to` to map only buffers covering the subset, keeping the rest unchanged. If no buffer returned from the mapper function then the old buffer will be preserved. If `null` returned then the buffer will be discarded. `offset` tracks `buffer` offset in new list, `index` reflects buffer count. | ||
```js | ||
list = list.map((buf, idx, offset) => { | ||
for (let c = 0; c < channels; c++) { | ||
let data = buf.getChannelData(channel) | ||
//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 = Math.max(from - offset, 0), | ||
l = Math.min(to - offset, buf.length); | ||
i < l; i++) { | ||
data[i] = process(data[i]) | ||
} | ||
} | ||
}, from, to) | ||
``` | ||
### `list.each((buffer, index, offset) => {}, from=0, to=-0)` | ||
Iterate over buffers from the indicated range. Buffers can be modified in-place during the iterating. | ||
## [AudioBuffer](https://github.com/audiojs/audio-buffer) properties & methods | ||
@@ -50,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20998
420
139