audio-buffer
Advanced tools
Comparing version 2.4.6 to 3.0.0
87
index.js
@@ -6,11 +6,19 @@ /** | ||
*/ | ||
'use strict'; | ||
'use strict' | ||
var isBuffer = require('is-buffer'); | ||
var b2ab = require('buffer-to-arraybuffer'); | ||
var isBrowser = require('is-browser'); | ||
var isAudioBuffer = require('is-audio-buffer'); | ||
var context = require('audio-context'); | ||
var isBuffer = require('is-buffer') | ||
var b2ab = require('buffer-to-arraybuffer') | ||
var isBrowser = require('is-browser') | ||
var isAudioBuffer = require('is-audio-buffer') | ||
var context = require('audio-context') | ||
var isPlainObj = require('is-plain-obj') | ||
module.exports = AudioBuffer | ||
/** Type of storage to use */ | ||
var FloatArray = typeof Float64Array === 'undefined' ? Float32Array : Float64Array; | ||
/** | ||
@@ -21,8 +29,27 @@ * @constructor | ||
*/ | ||
function AudioBuffer (channels, data, sampleRate) { | ||
if (!(this instanceof AudioBuffer)) return new AudioBuffer(channels, data, sampleRate); | ||
function AudioBuffer (channels, data, sampleRate, options) { | ||
//enforce class | ||
if (!(this instanceof AudioBuffer)) return new AudioBuffer(channels, data, sampleRate, options); | ||
//detect last argument | ||
var c = arguments.length | ||
while (!arguments[c] && c) c--; | ||
var last = arguments[c]; | ||
//figure out options | ||
var ctx, isWAA, floatArray | ||
if (last && typeof last != 'number') { | ||
ctx = last.context || context | ||
isWAA = last.isWAA != null ? last.isWAA : !!(isBrowser && context.createBuffer) | ||
floatArray = last.floatArray || FloatArray | ||
} | ||
else { | ||
ctx = context | ||
isWAA = false | ||
floatArray = FloatArray | ||
} | ||
//if one argument only - it is surely data or length | ||
//having new AudioBuffer(2) does not make sense as 2 - number of channels | ||
if (data == null) { | ||
//having new AudioBuffer(2) does not make sense as 2 being number of channels | ||
if (data == null || isPlainObj(data)) { | ||
data = channels || 1; | ||
@@ -33,4 +60,4 @@ channels = null; | ||
else { | ||
if (sampleRate != null) this.sampleRate = sampleRate; | ||
else if (isBrowser) this.sampleRate = AudioBuffer.context.sampleRate; | ||
if (typeof sampleRate == 'number') this.sampleRate = sampleRate; | ||
else if (isBrowser) this.sampleRate = ctx.sampleRate; | ||
if (channels != null) this.numberOfChannels = channels; | ||
@@ -43,3 +70,3 @@ } | ||
this.length = data; | ||
this.data = new AudioBuffer.FloatArray(data * this.numberOfChannels); | ||
this.data = new floatArray(data * this.numberOfChannels); | ||
} | ||
@@ -53,3 +80,3 @@ //if other audio buffer passed - create fast clone of it | ||
this.data = new AudioBuffer.FloatArray(this.length * this.numberOfChannels); | ||
this.data = new floatArray(this.length * this.numberOfChannels); | ||
@@ -67,4 +94,5 @@ //copy channel's data | ||
} | ||
if (!(data instanceof AudioBuffer.FloatArray)) { | ||
data = new AudioBuffer.FloatArray(new Float32Array(data.buffer || data)); | ||
//convert non-float array to floatArray | ||
if (!(data instanceof Float32Array) && !(data instanceof Float64Array)) { | ||
data = new floatArray(data.buffer || data); | ||
} | ||
@@ -81,3 +109,3 @@ | ||
this.length = data[0].length; | ||
this.data = new AudioBuffer.FloatArray(this.length * this.numberOfChannels); | ||
this.data = new floatArray(this.length * this.numberOfChannels); | ||
for (var i = 0; i < this.numberOfChannels; i++ ) { | ||
@@ -92,3 +120,3 @@ this.data.set(data[i], i * this.length); | ||
if (data[0] == null) data = this.length; | ||
this.data = new AudioBuffer.FloatArray(data); | ||
this.data = new floatArray(data); | ||
} | ||
@@ -106,6 +134,6 @@ } | ||
//for browser - just return WAA buffer | ||
if (AudioBuffer.isWAA) { | ||
//for browser - return WAA buffer, no sub-buffering allowed | ||
if (isWAA) { | ||
//create WAA buffer | ||
var audioBuffer = AudioBuffer.context.createBuffer(this.numberOfChannels, this.length, this.sampleRate); | ||
var audioBuffer = ctx.createBuffer(this.numberOfChannels, this.length, this.sampleRate); | ||
@@ -121,16 +149,5 @@ //fill channels | ||
this.duration = this.length / this.sampleRate; | ||
}; | ||
} | ||
/** Type of storage to use */ | ||
AudioBuffer.FloatArray = typeof Float64Array === 'undefined' ? Float32Array : Float64Array; | ||
/** Set context, though can be redefined */ | ||
AudioBuffer.context = context; | ||
/** Whether WebAudioBuffer should be created */ | ||
AudioBuffer.isWAA = isBrowser && context.createBuffer; | ||
/** | ||
@@ -140,3 +157,3 @@ * Default params | ||
AudioBuffer.prototype.numberOfChannels = 2; | ||
AudioBuffer.prototype.sampleRate = AudioBuffer.context.sampleRate || 44100; | ||
AudioBuffer.prototype.sampleRate = context.sampleRate || 44100; | ||
@@ -183,3 +200,1 @@ | ||
module.exports = AudioBuffer; |
{ | ||
"name": "audio-buffer", | ||
"version": "2.4.6", | ||
"version": "3.0.0", | ||
"description": "Audio data container", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"test:browser": "beefy --index=test.html test.js", | ||
"watch": "watchify test.js -d -o bundle.js", | ||
"test": "node test", | ||
"test:browser": "budo test", | ||
"benchmark": "mocha benchmark.js", | ||
@@ -40,3 +39,4 @@ "benchmark:browser": "beefy --index=benchmark.html benchmark.js" | ||
"is-browser": "^2.0.1", | ||
"is-buffer": "^1.1.0" | ||
"is-buffer": "^1.1.0", | ||
"is-plain-obj": "^1.1.0" | ||
}, | ||
@@ -50,3 +50,2 @@ "devDependencies": { | ||
"isndarray": "^1.0.0", | ||
"mocha": "^3.0.2", | ||
"ndarray": "^1.0.18", | ||
@@ -57,4 +56,5 @@ "ndarray-fill": "^1.0.1", | ||
"performance-now": "^0.2.0", | ||
"tape": "^4.6.3", | ||
"xtend": "^4.0.1" | ||
} | ||
} |
@@ -15,3 +15,3 @@ # audio-buffer [![Build Status](https://travis-ci.org/audiojs/audio-buffer.svg?branch=master)](https://travis-ci.org/audiojs/audio-buffer) [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) | ||
//It can be Array, TypedArray, ArrayBuffer, Buffer, AudioBuffer, DataView, NDArray etc. | ||
var buffer = new AudioBuffer(channels = 2, data|length, sampleRate = 44100) | ||
var buffer = new AudioBuffer(channels = 2, data|length, sampleRate = 44100, options?) | ||
@@ -39,15 +39,12 @@ //Duration of the underlying audio data, in seconds | ||
``` | ||
//Some special properties, it’s unlikely you will ever need them. | ||
## Options | ||
//Type of array for data. Float64Array is faster for modern node/browsers. | ||
AudioBuffer.FloatArray = Float64Array | ||
Options object can be passed as last argument with the following: | ||
//In browser, you can set custom audio context (online/offline). | ||
AudioBuffer.context = require('audio-context') | ||
* `floatArray` — type of array for data, defaults to `Float64Array`. | ||
* `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. | ||
//Whether WebAudioAPI AudioBuffer should be created, if avail, instead of polyfilled structure | ||
AudioBuffer.isWAA = true | ||
``` | ||
## See also | ||
@@ -54,0 +51,0 @@ |
12141
7
201
6
58
+ Addedis-plain-obj@^1.1.0
+ Addedis-plain-obj@1.1.0(transitive)