Socket
Socket
Sign inDemoInstall

audio-buffer-list

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

audio-buffer-list - npm Package Compare versions

Comparing version 1.5.2 to 2.0.0

354

index.js

@@ -11,9 +11,8 @@ /**

var inherit = require('inherits')
var BufferList = require('bl')
var util = require('audio-buffer-utils')
var AudioBuffer = require('audio-buffer')
var DuplexStream = require('readable-stream/duplex')
var extend = require('object-assign')
var nidx = require('negative-index')
var isPlainObj = require('is-plain-obj')
var Emitter = require('events')

@@ -23,36 +22,15 @@ module.exports = AudioBufferList

inherit(AudioBufferList, BufferList)
inherit(AudioBufferList, Emitter)
function AudioBufferList(callback, options) {
if (!(this instanceof AudioBufferList)) return new AudioBufferList(callback, options)
function AudioBufferList(arg, options) {
if (!(this instanceof AudioBufferList)) return new AudioBufferList(arg, options)
extend(this, options)
this._bufs = []
this.buffers = []
this.length = 0
this.duration = 0
if (typeof callback == 'function') {
this._callback = callback
var piper = function piper (err) {
if (this._callback) {
this._callback(err)
this._callback = null
}
}.bind(this)
this.on('pipe', function onPipe (src) {
src.on('error', piper)
})
this.on('unpipe', function onUnpipe (src) {
src.removeListener('error', piper)
})
} else {
this.append(callback)
}
//single line different from bl
DuplexStream.call(this, {objectMode: true})
this.append(arg)
}

@@ -68,7 +46,7 @@

if (startInChannel == null) startInChannel = 0
var offsets = this._offset(startInChannel)
var offsets = this.offset(startInChannel)
var offset = startInChannel - offsets[1]
var initialOffset = offsets[1]
for (var i = offsets[0], l = this._bufs.length; i < l; i++) {
var buf = this._bufs[i]
for (var i = offsets[0], l = this.buffers.length; i < l; i++) {
var buf = this.buffers[i]
var data = buf.getChannelData(channel)

@@ -86,6 +64,6 @@ if (startInChannel > offset) data = data.subarray(startInChannel)

if (startInChannel == null) startInChannel = 0
var offsets = this._offset(startInChannel)
var offsets = this.offset(startInChannel)
var offset = startInChannel - offsets[1]
for (var i = offsets[0], l = this._bufs.length; i < l; i++) {
var buf = this._bufs[i]
for (var i = offsets[0], l = this.buffers.length; i < l; i++) {
var buf = this.buffers[i]
var data = buf.getChannelData(channel)

@@ -106,15 +84,15 @@ if (channel < buf.numberOfChannels) {

if (!this._bufs.length || from === to) return new Float32Array()
if (!this.buffers.length || from === to) return new Float32Array()
//shortcut single buffer preserving subarraying
if (this._bufs.length === 1) {
return this._bufs[0].getChannelData(channel).subarray(from, to)
if (this.buffers.length === 1) {
return this.buffers[0].getChannelData(channel).subarray(from, to)
}
var floatArray = this._bufs[0].getChannelData(0).constructor
var floatArray = this.buffers[0].getChannelData(0).constructor
var data = new floatArray(to - from)
var fromOffset = this._offset(from)
var toOffset = this._offset(to)
var fromOffset = this.offset(from)
var toOffset = this.offset(to)
var firstBuf = this._bufs[fromOffset[0]]
var firstBuf = this.buffers[fromOffset[0]]
data.set(firstBuf.getChannelData(channel).subarray(fromOffset[1]))

@@ -124,7 +102,7 @@

for (var i = fromOffset[0] + 1, l = toOffset[0]; i < l; i++) {
var buf = this._bufs[i]
var buf = this.buffers[i]
data.set(buf.getChannelData(channel), offset);
offset += buf.length
}
var lastBuf = this._bufs[toOffset[0]]
var lastBuf = this.buffers[toOffset[0]]
data.set(lastBuf.getChannelData(channel).subarray(0, toOffset[1]), offset)

@@ -151,4 +129,4 @@

else if (buf instanceof AudioBufferList) {
for (; i < buf._bufs.length; i++)
this.append(buf._bufs[i])
for (; i < buf.buffers.length; i++)
this.append(buf.buffers[i])
}

@@ -164,7 +142,20 @@ //create AudioBuffer from arg

AudioBufferList.prototype.offset = function _offset (offset) {
var tot = 0, i = 0, _t
if (offset === 0) return [ 0, 0 ]
for (; i < this.buffers.length; i++) {
_t = tot + this.buffers[i].length
if (offset < _t || i == this.buffers.length - 1)
return [ i, offset - tot ]
tot = _t
}
}
AudioBufferList.prototype._appendBuffer = function (buf) {
// if (buf.sampleRate != this.sampleRate) throw Error('Required sample rate is ' + this.sampleRate + ', passed ' + buf.sampleRate)
this.buffers.push(buf)
this.length += buf.length
BufferList.prototype._appendBuffer.call(this, buf)
// if (buf.numberOfChannels != this.numberOfChannels) throw Error('Required number of channels is ' + this.numberOfChannels + ', passed ' + buf.numberOfChannels)

@@ -181,8 +172,2 @@ //update channels count

//get method here returns audio buffer with single sample if it makes any sense
AudioBufferList.prototype.get = function get (index, channel) {
let offset = this._offset(index)
return this._bufs[offset[0]].getChannelData(channel || 0)[offset[1]]
}
//copy data to destination audio buffer

@@ -200,3 +185,3 @@ AudioBufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {

var copy = !!dst
, off = this._offset(srcStart)
, off = this.offset(srcStart)
, len = srcEnd - srcStart

@@ -212,10 +197,10 @@ , bytes = len

if (!copy) { // slice, but full concat if multiple buffers
return this._bufs.length === 1
? util.slice(this._bufs[0])
: util.concat(this._bufs)
return this.buffers.length === 1
? util.slice(this.buffers[0])
: util.concat(this.buffers)
}
// copy, need to copy individual buffers
for (i = 0; i < this._bufs.length; i++) {
util.copy(this._bufs[i], dst, bufoff)
bufoff += this._bufs[i].length
for (i = 0; i < this.buffers.length; i++) {
util.copy(this.buffers[i], dst, bufoff)
bufoff += this.buffers[i].length
}

@@ -227,6 +212,6 @@

// easy, cheap case where it's a subset of one of the buffers
if (bytes <= this._bufs[off[0]].length - start) {
if (bytes <= this.buffers[off[0]].length - start) {
return copy
? util.copy(util.subbuffer(this._bufs[off[0]], start, start + bytes), dst, dstStart)
: util.slice(this._bufs[off[0]], start, start + bytes)
? util.copy(util.subbuffer(this.buffers[off[0]], start, start + bytes), dst, dstStart)
: util.slice(this.buffers[off[0]], start, start + bytes)
}

@@ -237,9 +222,9 @@

for (i = off[0]; i < this._bufs.length; i++) {
l = this._bufs[i].length - start
for (i = off[0]; i < this.buffers.length; i++) {
l = this.buffers[i].length - start
if (bytes > l) {
util.copy(util.subbuffer(this._bufs[i], start), dst, bufoff)
util.copy(util.subbuffer(this.buffers[i], start), dst, bufoff)
} else {
util.copy(util.subbuffer(this._bufs[i], start, start + bytes), dst, bufoff)
util.copy(util.subbuffer(this.buffers[i], start, start + bytes), dst, bufoff)
break

@@ -258,3 +243,4 @@ }

AudioBufferList.prototype.shallowSlice = function shallowSlice (start, end) {
//do superficial handle
AudioBufferList.prototype.slice = function slice (start, end) {
start = start || 0

@@ -270,5 +256,5 @@ end = end == null ? this.length : end

var startOffset = this._offset(start)
, endOffset = this._offset(end)
, buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
var startOffset = this.offset(start)
, endOffset = this.offset(end)
, buffers = this.buffers.slice(startOffset[0], endOffset[0] + 1)

@@ -289,25 +275,8 @@ if (endOffset[1] == 0) {

AudioBufferList.prototype.consume = function consume (bytes) {
while (this._bufs.length) {
if (bytes >= this._bufs[0].length) {
bytes -= this._bufs[0].length
this.length -= this._bufs[0].length
this._bufs.shift()
} else {
//util.subbuffer would remain buffer in memory though it is faster
this._bufs[0] = util.subbuffer(this._bufs[0], bytes)
this.length -= bytes
break
}
}
this.duration = this.length / this.sampleRate
return this
}
//clone with preserving data
AudioBufferList.prototype.duplicate = function duplicate () {
var i = 0, copy = new AudioBufferList()
AudioBufferList.prototype.clone = function clone (start, end) {
var i = 0, copy = new AudioBufferList(), sublist = this.slice(start, end)
for (; i < this._bufs.length; i++)
copy.append(this._bufs[i])
for (; i < sublist.buffers.length; i++)
copy.append(util.clone(sublist.buffers[i]))

@@ -317,35 +286,9 @@ return copy

//clean up
AudioBufferList.prototype.destroy = function destroy () {
this.buffers.length = 0
this.length = 0
}
;(function () {
var methods = {
'readDoubleBE' : 8
, 'readDoubleLE' : 8
, 'readFloatBE' : 4
, 'readFloatLE' : 4
, 'readInt32BE' : 4
, 'readInt32LE' : 4
, 'readUInt32BE' : 4
, 'readUInt32LE' : 4
, 'readInt16BE' : 2
, 'readInt16LE' : 2
, 'readUInt16BE' : 2
, 'readUInt16LE' : 2
, 'readInt8' : 1
, 'readUInt8' : 1
}
for (var m in methods) {
(function (m) {
AudioBufferList.prototype[m] = function (offset) {
throw Error('AudioBufferList does not support byte methods')
}
}(m))
}
}())
// Additional methods
//repeat contents N times

@@ -366,3 +309,3 @@ AudioBufferList.prototype.repeat = function (times) {

for (var i = 1; i < times; i++) {
data = new AudioBufferList(data.slice())
data = new AudioBufferList(data.copy())
this.append(data)

@@ -375,26 +318,22 @@ }

//insert new buffer/buffers at the offset
AudioBufferList.prototype.insert = function (source, offset) {
if (offset == null) return this.append(source)
AudioBufferList.prototype.insert = function (offset, source) {
if (source == null) {
source = offset
offset = 0
}
offset = nidx(offset, this.length)
var offsets = this._offset(offset)
var leftBuf = offsets[1] ? util.subbuffer(this._bufs[offsets[0]], 0, offsets[1]) : null
var rightBuf = offsets[1] !== this._bufs[offsets[0]].length ? util.subbuffer(this._bufs[offsets[0]], offsets[1]) : null
this.split(offset)
var offset = this.offset(offset)
//convert any type of source to audio buffer list
source = new AudioBufferList(source)
this.buffers.splice.apply(this.buffers, [offset[0], 0].concat(source.buffers))
//form new list
let bufs = this._bufs.slice(0, offsets[0])
if (leftBuf) bufs.push(leftBuf)
bufs = bufs.concat(source._bufs)
if (rightBuf) bufs.push(rightBuf)
bufs = bufs.concat(this._bufs.slice(offsets[0] + 1))
this._bufs = bufs
//update params
this.length += source.length
this.duration += source.duration
this.numberOfChannels = Math.max(source.numberOfChannels, this.numberOfChannels)

@@ -405,4 +344,7 @@ return this

//delete N samples from any position
AudioBufferList.prototype.delete = function (count, offset) {
if (offset == null) offset = 0
AudioBufferList.prototype.delete = function (offset, count) {
if (count == null) {
count = offset
offset = 0
}
if (!count) return this

@@ -416,18 +358,17 @@

offset = nidx(offset, this.length)
count = Math.min(this.length - offset, count)
var offsetsLeft = this._offset(offset)
var offsetsRight = this._offset(offset + count)
this.split(offset, offset + count)
//same segment slice
var leftBuf = offsetsLeft[1] ? util.subbuffer(this._bufs[offsetsLeft[0]], 0, offsetsLeft[1]) : null;
var rightBuf = this._bufs[offsetsRight[0]].length !== offsetsRight[1] ? util.subbuffer(this._bufs[offsetsRight[0]], offsetsRight[1]) : null;
var offsetLeft = this.offset(offset)
var offsetRight = this.offset(offset + count)
//delete buffers
let deleted = this._bufs.splice(offsetsLeft[0], offsetsRight[0] - offsetsLeft[0] + 1)
if (offsetRight[1] === this.buffers[offsetRight[0]].length) {
offsetRight[0] += 1
}
//insert buffers
if (rightBuf) this._bufs.splice(offsetsLeft[0], 0, rightBuf)
if (leftBuf) this._bufs.splice(offsetsLeft[0], 0, leftBuf)
let deleted = this.buffers.splice(offsetLeft[0], offsetRight[0] - offsetLeft[0])
deleted = new AudioBufferList(deleted)
this.length -= count
this.length -= deleted.length
this.duration = this.length / this.sampleRate

@@ -438,2 +379,21 @@

//remove N sampled from the beginning
AudioBufferList.prototype.consume = function consume (size) {
while (this.buffers.length) {
if (size >= this.buffers[0].length) {
size -= this.buffers[0].length
this.length -= this.buffers[0].length
this.buffers.shift()
} else {
//util.subbuffer would remain buffer in memory though it is faster
this.buffers[0] = util.subbuffer(this.buffers[0], size)
this.length -= size
break
}
}
this.duration = this.length / this.sampleRate
return this
}
//return new list via applying fn to each buffer from the indicated range

@@ -446,12 +406,12 @@ AudioBufferList.prototype.map = function map (fn, from, to) {

let fromOffset = this._offset(from)
let toOffset = this._offset(to)
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[0] + 1)
let before = this.buffers.slice(0, fromOffset[0])
let after = this.buffers.slice(toOffset[0] + 1)
let middle = this.buffers.slice(fromOffset[0], toOffset[0] + 1)
middle = middle.map((buf, idx) => {
let result = fn.call(this, buf, idx, offset, this._bufs, this)
let result = fn.call(this, buf, idx, offset, this.buffers, this)
if (result === undefined || result === true) result = buf

@@ -485,6 +445,6 @@ //ignore removed buffers

let fromOffset = this._offset(from)
let toOffset = this._offset(to)
let fromOffset = this.offset(from)
let toOffset = this.offset(to)
let middle = this._bufs.slice(fromOffset[0], toOffset[0] + 1)
let middle = this.buffers.slice(fromOffset[0], toOffset[0] + 1)

@@ -494,4 +454,4 @@ if (options.reversed) {

for (let i = toOffset[0], l = fromOffset[0]; i >= l; i--) {
let buf = this._bufs[i]
let res = fn.call(this, buf, i, offset, this._bufs, this)
let buf = this.buffers[i]
let res = fn.call(this, buf, i, offset, this.buffers, this)
if (res === false) break

@@ -504,4 +464,4 @@ offset -= buf.length

for (let i = fromOffset[0], l = toOffset[0]+1; i < l; i++) {
let buf = this._bufs[i]
let res = fn.call(this, buf, i, offset, this._bufs, this)
let buf = this.buffers[i]
let res = fn.call(this, buf, i, offset, this.buffers, this)
if (res === false) break

@@ -514,1 +474,67 @@ offset += buf.length

}
//reverse subpart
AudioBufferList.prototype.reverse = function reverse (from, to) {
if (from == null) from = 0
if (to == null) to = this.length
from = nidx(from, this.length)
to = nidx(to, this.length)
let sublist = this.slice(from, to)
.each((buf) => {
util.reverse(buf)
})
sublist.buffers.reverse()
this.delete(from, to-from)
this.insert(from, sublist)
return this
}
//split at the indicated indexes
AudioBufferList.prototype.split = function split () {
let args = arguments;
for (let i = 0; i < args.length; i++ ) {
let arg = args[i]
if (Array.isArray(arg)) {
this.split.apply(this, arg)
}
else if (typeof arg === 'number') {
let offset = this.offset(arg)
let buf = this.buffers[offset[0]]
if (offset[1] > 0 && offset[1] < buf.length) {
let left = util.subbuffer(buf, 0, offset[1])
let right = util.subbuffer(buf, offset[1])
this.buffers.splice(offset[0], 1, left, right)
}
}
}
return this
}
//join buffers within the subrange
AudioBufferList.prototype.join = function join (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 bufs = this.buffers.slice(fromOffset[0], toOffset[0])
let buf = util.concat(bufs)
this.buffers.splice.apply(this.buffers, [fromOffset[0], toOffset[0] - fromOffset[0] + (toOffset[1] ? 1 : 0)].concat(buf))
return this
}
{
"name": "audio-buffer-list",
"version": "1.5.2",
"version": "2.0.0",
"description": "Data structure for sequence of AudioBuffers",

@@ -37,3 +37,2 @@ "main": "index.js",

"audio-context": "^0.1.0",
"bl": "^1.2.0",
"inherits": "^2.0.3",

@@ -43,4 +42,3 @@ "is-audio-buffer": "^1.0.5",

"negative-index": "^1.0.2",
"object-assign": "^4.1.1",
"readable-stream": "^2.2.9"
"object-assign": "^4.1.1"
},

@@ -47,0 +45,0 @@ "devDependencies": {

@@ -1,4 +0,4 @@

# 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)
# audio-buffer-list [![Build Status](https://travis-ci.org/audiojs/audio-buffer-list.svg?branch=master)](https://travis-ci.org/audiojs/audio-buffer-list) [![unstable](http://badges.github.io/stability-badges/dist/unstable.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 (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.
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.

@@ -27,18 +27,54 @@ ## Usage

`source` can be _AudioBuffer_, _AudioBuffer_ array, _AudioBufferList_, _AudioBufferList_ array or callback.
`source` can be _AudioBuffer_, _AudioBuffer_ array, _AudioBufferList_ or _AudioBufferList_ array.
`options` may provide `numberOfChannels`, `context` for web audio API context and `sampleRate`.
### `list.insert(buffer, offset=0)`
### `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.
### `list.offset(sample)`
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.
### `list.append(buffer)`
Insert new AudioBuffer, AudioBufferList or array of them to the end.
### `list.insert(offset=0, buffer)`
Put new AudioBuffer, AudioBufferList or array of them at the offset.
### `list.delete(count, offset=0)`
### `list.delete(offset=0, count)`
Delete number of samples starting at the offset. `count` can possibly be negative, then items are deleted on the left side from the offset. `offset` can also be negative, meaning to start from the end.
Delete number of samples starting at the `offset`. `count` can possibly be negative, then items are deleted on the left side from the offset. `offset` can also be negative, meaning to start from the end. Retuns deleted _AudioBufferList_.
### `list.repeat(count)`
### `list.consume(count)`
Repeats contents of the list specified number of times. Modifies list in-place.
Delete data from the beginning.
### `list.slice(start=0, end=-0)`
Return sublist of the initial list. The data is not copied but returned as subarrays. `list.slice()` just creates a duplicate that way.
### `list.clone(start=0, end=-0)`
Return copy of the list, consisting of cloned buffers.
### `list.map((buffer, index, offset) => buffer|bool?, from=0, to=-0)`

@@ -68,67 +104,38 @@

## [AudioBuffer](https://github.com/audiojs/audio-buffer) properties & methods
### `list.reverse(start=0, end=-0)`
### `list.duration`
Reverse part of the list.
Total duration of the audio list, i.e. sum of buffers durations.
### `list.repeat(count)`
### `list.numberOfChannels`
Repeats contents of the list specified number of times. Modifies list in-place.
Detected from the buffer with max number of channels in the list. Can be set by options.
### `list.getChannelData(channel, from=0, to=-0)`
### `list.sampleRate`
Return _FloatArray_ with merged data for the channel.
Just for convenience with _AudioBuffer_ interface
### `list.copy(dest?, start=0, end=-0)`
### `list.getChannelData(channel)`
Put data into destination _AudioBuffer_ or create one.
Return _FloatArray_ with merged data for the channel.
### `list.copyFromChannel(arr, channel, startInChannel=0)`
### `list.copyFromChannel(destination, channel, startInChannel=0)`
Put data from the channel to destination _FloatArray_. Optional `startInChannel` defines offset in the channel to start from.
### `list.copyToChannel(source, channel, startInChannel=0)`
### `list.copyToChannel(arr, channel, startInChannel=0)`
Put data from the `source` _FloatArray_ into channel, optionally starting at `startInChannel` offset.
Put data from the source _FloatArray_ into channel, optionally starting at `startInChannel` offset.
### `list.split(a, b, c, ...)`
Split list at the indicated indexes, that just increases number of inner buffers and that's it.
## [BufferList](https://github.com/rvagg/bl) properties and methods
### `list.join(start=0, end=-0)`
### `list.length`
Joins fragments for the indicated range together.
Total length of list in samples, i.e. sum of inner buffer lengths.
### `list.destroy()`
### `list.append(buffer)`
Clean up list.
Insert new AudioBuffer, AudioBufferList or array of them to the end.
### `list.slice(start=0, end=-0)`
Return merged _AudioBuffer_ representing indicated interval.
### `list.shallowSlice(start=0, end=-0)`
Return sublist — a handle for the data of the indicated interval.
### `list.get(idx, channel)`
Get single sample value at the coordinate.
### `list.duplicate()`
Create new AudioBufferList consisting of the same AudioBuffers as the initial list.
### `list.copy(dest, destStart?, srcStart?, srcEnd?)`
Put data into destination AudioBuffer.
### `list.consume(length)`
Delete data from the beginning.
## Stream methods
## See also

@@ -135,0 +142,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc