audio-buffer-sink
Advanced tools
Comparing version 0.0.1 to 0.1.0
77
index.js
@@ -7,38 +7,18 @@ module.exports = function(audioContext, opts) { | ||
var bufferSize = opts.bufferSize || 4096, | ||
channelCount = opts.channelCount || 2, | ||
sampleRate = opts.sampleRate || null, | ||
duration = opts.duration || null, | ||
length = opts.length || null; | ||
channelCount = opts.channelCount || 2; | ||
// output state | ||
var outputLength = null, | ||
outputBuffer = null, | ||
offset = 0; | ||
var outputBuffer = opts.buffer || null, | ||
outputLength, | ||
offset; | ||
function createOutputBuffer() { | ||
if (!sampleRate) { | ||
throw new Error("can't create output buffer - sample rate is unknown"); | ||
function reset() { | ||
if (outputBuffer) { | ||
outputLength = outputBuffer.length; | ||
} else { | ||
outputLength = null | ||
} | ||
if (duration) { | ||
outputLength = Math.floor(sampleRate * duration); | ||
outputBuffer = audioContext.createBuffer(channelCount, outputLength, sampleRate); | ||
return; | ||
} | ||
if (length) { | ||
outputLength = length; | ||
outputBuffer = audioContext.createBuffer(channelCount, outputLength, sampleRate); | ||
return; | ||
} | ||
throw new Error("can't create output buffer - either duration or length is required"); | ||
offset = 0; | ||
} | ||
if (sampleRate) { | ||
createOutputBuffer(); | ||
} | ||
var processor = audioContext.createScriptProcessor(bufferSize, channelCount, channelCount); | ||
@@ -55,18 +35,10 @@ | ||
if (offset >= outputLength) { | ||
if (offset >= outputLength || !outputBuffer) { | ||
return; | ||
} | ||
var inputBuffer = evt.inputBuffer, | ||
inputLength = inputBuffer.length; | ||
var inputBuffer = evt.inputBuffer, | ||
inputLength = inputBuffer.length, | ||
samplesRemaining = outputLength - offset; | ||
if (outputBuffer === null) { | ||
if (sampleRate === null) { | ||
sampleRate = inputBuffer.sampleRate; | ||
createOutputBuffer(); | ||
} | ||
} | ||
var samplesRemaining = outputLength - offset; | ||
if (inputLength > samplesRemaining) { | ||
@@ -92,17 +64,16 @@ for (var i = 0; i < channelCount; ++i) { | ||
processor.getOutputBuffer = function() { | ||
return outputBuffer; | ||
} | ||
processor.reset = function() { | ||
offset = 0; | ||
if (sampleRate) { | ||
createOutputBuffer(); | ||
} else { | ||
outputBuffer = null; | ||
Object.defineProperty(processor, 'buffer', { | ||
get: function() { return outputBuffer; }, | ||
set: function(b) { | ||
outputBuffer = b; | ||
reset(); | ||
} | ||
} | ||
}); | ||
processor.reset = reset; | ||
reset(); | ||
return processor; | ||
} |
{ | ||
"name": "audio-buffer-sink", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A web audio sink node that writes received data to an AudioBuffer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
2738
59