Permalink
Browse files

Updated sink.js

  • Loading branch information...
jussi-kalliokoski committed Apr 7, 2012
1 parent ae44ac0 commit e803d33af27a74cb552a28ed8479a0a6b8896cdd
Showing with 88 additions and 35 deletions.
  1. +1 −1 integration/sink.js
  2. +87 −34 src/io/sink.js
View
@@ -62,7 +62,7 @@ SinkClass.prototype = Sink.prototype = {
this.sampleRate = isNaN(sampleRate) || sampleRate === null ? this.sampleRate : sampleRate;
this.readFn = readFn;
this.activeRecordings = [];
- this.previousHit = +new Date;
+ this.previousHit = +new Date();
Sink.EventEmitter.call(this);
Sink.emit('init', [this].concat([].slice.call(arguments)));
},
@@ -72,19 +72,30 @@ SinkClass.prototype = Sink.prototype = {
*/
process: function (soundData, channelCount) {
this.emit('preprocess', arguments);
- this.ringBuffer && (this.channelMode === 'interleaved' ? this.ringSpin : this.ringSpinInterleaved).apply(this, arguments);
+
+ if (this.ringBuffer) {
+ (this.channelMode === 'interleaved' ? this.ringSpin : this.ringSpinInterleaved).apply(this, arguments);
+ }
+
if (this.channelMode === 'interleaved') {
this.emit('audioprocess', arguments);
- this.readFn && this.readFn.apply(this, arguments);
+
+ if (this.readFn) {
+ this.readFn.apply(this, arguments);
+ }
} else {
var soundDataSplit = Sink.deinterleave(soundData, this.channelCount),
args = [soundDataSplit].concat([].slice.call(arguments, 1));
this.emit('audioprocess', args);
- this.readFn && this.readFn.apply(this, args);
+
+ if (this.readFn) {
+ this.readFn.apply(this, args);
+ }
+
Sink.interleave(soundDataSplit, this.channelCount, soundData);
}
this.emit('postprocess', arguments);
- this.previousHit = +new Date;
+ this.previousHit = +new Date();
this.writePosition += soundData.length / channelCount;
},
/**
@@ -126,11 +137,14 @@ function sinks (type, constructor, prototype, disabled, priority) {
constructor.prototype = new Sink.SinkClass();
constructor.prototype.type = type;
constructor.enabled = !disabled;
- for (disabled in prototype) {
- if (prototype.hasOwnProperty(disabled)) {
- constructor.prototype[disabled] = prototype[disabled];
+
+ var k;
+ for (k in prototype) {
+ if (prototype.hasOwnProperty(k)) {
+ constructor.prototype[k] = prototype[k];
}
}
+
sinks[type] = constructor;
sinks.list[priority ? 'unshift' : 'push'](constructor);
}
@@ -169,7 +183,7 @@ function EventEmitter () {
}
}
this._listeners = {};
-};
+}
EventEmitter.prototype = {
_listeners: null,
@@ -216,12 +230,16 @@ EventEmitter.prototype = {
delete this._listeners[name];
return this;
}
+
for (var i=0; i<this._listeners[name].length; i++) {
if (this._listeners[name][i] === listener) {
this._listeners[name].splice(i--, 1);
}
}
- this._listeners[name].length || delete this._listeners[name];
+
+ if (!this._listeners[name].length) {
+ delete this._listeners[name];
+ }
}
return this;
}
@@ -296,8 +314,8 @@ Sink.Error = SinkError;
(function (Sink) {
var BlobBuilder = typeof window === 'undefined' ? undefined :
- window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder || window.OBlobBuilder || window.BlobBuilder,
- URL = typeof window === 'undefined' ? undefined : (window.MozURL || window.webkitURL || window.MSURL || window.OURL || window.URL);
+ window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder || window.OBlobBuilder,
+ URL = typeof window === 'undefined' ? undefined : (window.URL || window.MozURL || window.webkitURL || window.MSURL || window.OURL);
/**
* Creates an inline worker using a data/blob URL, if possible.
@@ -359,7 +377,11 @@ inlineWorker.test = function () {
inlineWorker.working = success;
inlineWorker.emit('ready', [success]);
inlineWorker.off('ready');
- success && worker && worker.terminate();
+
+ if (success && worker) {
+ worker.terminate();
+ }
+
worker = null;
}
@@ -416,15 +438,21 @@ Sink.doInterval = function (callback, timeout) {
}
}
- Sink.inlineWorker.ready ? create() : Sink.inlineWorker.on('ready', function () {
+ if (Sink.inlineWorker.ready) {
create();
- });
+ } else {
+ Sink.inlineWorker.on('ready', function () {
+ create();
+ });
+ }
return function () {
if (!kill) {
- Sink.inlineWorker.ready || Sink.inlineWorker.on('ready', function () {
- kill && kill();
- });
+ if (!Sink.inlineWorker.ready) {
+ Sink.inlineWorker.on('ready', function () {
+ if (kill) kill();
+ });
+ }
} else {
kill();
}
@@ -461,7 +489,11 @@ Sink.sinks('audiodata', function () {
currentPosition = audioDevice.mozCurrentSampleOffset();
available = Number(currentPosition + (prevPos !== currentPosition ? self.bufferSize : self.preBufferSize) * self.channelCount - currentWritePosition);
- currentPosition === prevPos && self.emit('error', [Sink.Error(0x10)]);
+
+ if (currentPosition === prevPos) {
+ self.emit('error', [Sink.Error(0x10)]);
+ }
+
if (available > 0 || prevPos === currentPosition){
self.ready();
@@ -489,7 +521,7 @@ Sink.sinks('audiodata', function () {
this._timers.push(Sink.doInterval(function () {
// Check for complete death of the output
- if (+new Date - self.previousHit > 2000) {
+ if (+new Date() - self.previousHit > 2000) {
self._audio = audioDevice = new Audio();
audioDevice.mozSetup(self.channelCount, self.sampleRate);
currentWritePosition = 0;
@@ -506,7 +538,7 @@ Sink.sinks('audiodata', function () {
bufferSize: 24576,
preBufferSize: 24576,
forceBufferSize: false,
- interval: 20,
+ interval: 100,
kill: function () {
while (this._timers.length) {
@@ -526,7 +558,7 @@ Sink.sinks.moz = Sink.sinks.audiodata;
}(this.Sink));
(function (Sink, sinks) {
-var sinks = Sink.sinks;
+sinks = Sink.sinks;
function newAudio (src) {
var audio = document.createElement('audio');
@@ -536,7 +568,7 @@ function newAudio (src) {
return audio;
}
-/* TODO: Implement a <BGSOUND> hack for IE8.
+/* TODO: Implement a <BGSOUND> hack for IE8. */
/**
* A sink class for WAV data URLs
@@ -545,7 +577,7 @@ function newAudio (src) {
*/
sinks('wav', function () {
var self = this,
- audio = new sinks.wav.wavAudio,
+ audio = new sinks.wav.wavAudio(),
PCMData = typeof PCMData === 'undefined' ? audioLib.PCMData : PCMData;
self.start.apply(self, arguments);
var soundData = new Float32Array(self.bufferSize * self.channelCount),
@@ -631,7 +663,7 @@ sinks.wav.wavAudio = wavAudio;
*/
Sink.sinks('dummy', function () {
- var self = this;
+ var self = this;
self.start.apply(self, arguments);
function bufferFill () {
@@ -652,6 +684,8 @@ Sink.sinks('dummy', function () {
}(this.Sink));
(function (sinks, fixChrome82795) {
+var AudioContext = typeof window === 'undefined' ? null : window.webkitAudioContext || window.AudioContext;
+
/**
* A sink class for the Web Audio API
*/
@@ -705,9 +739,13 @@ sinks('webaudio', function (readFn, channelCount, bufferSize, sampleRate) {
}, {
kill: function () {
this._node.disconnect(0);
+
for (var i=0; i<fixChrome82795.length; i++) {
- fixChrome82795[i] === this._node && fixChrome82795.splice(i--, 1);
+ if (fixChrome82795[i] === this._node) {
+ fixChrome82795.splice(i--, 1);
+ }
}
+
this._node = this._context = null;
this.emit('kill');
},
@@ -723,7 +761,7 @@ sinks.webaudio.fix82795 = fixChrome82795;
sinks.webaudio.getContext = function () {
// For now, we have to accept that the AudioContext is at 48000Hz, or whatever it decides.
- var context = new (window.AudioContext || webkitAudioContext)(/*sampleRate*/);
+ var context = new AudioContext(/*sampleRate*/);
sinks.webaudio.getContext = function () {
return context;
@@ -752,7 +790,10 @@ Sink.sinks('worker', function () {
importScripts();
function mspBufferFill (e) {
- self.isReady || self.initMSP(e);
+ if (!self.isReady) {
+ self.initMSP(e);
+ }
+
self.ready();
var channelCount = self.channelCount,
@@ -778,7 +819,10 @@ Sink.sinks('worker', function () {
}
function waBufferFill(e) {
- self.isReady || self.initWA(e);
+ if (!self.isReady) {
+ self.initWA(e);
+ }
+
self.ready();
var outputBuffer = e.outputBuffer,
@@ -1141,10 +1185,15 @@ Proxy.prototype = {
},
process: function (buffer, channelCount) {
- this.offset === null && this.loadBuffer();
+ if (this.offset === null) {
+ this.loadBuffer();
+ }
for (var i=0; i<buffer.length; i++) {
- this.offset >= this.buffer.length && this.loadBuffer();
+ if (this.offset >= this.buffer.length) {
+ this.loadBuffer();
+ }
+
buffer[i] = this.buffer[this.offset++];
}
},
@@ -1180,7 +1229,9 @@ Sink.prototype.createProxy = function (bufferSize) {
(function (Sink) {
function processRingBuffer () {
- this.ringBuffer && (this.channelMode === 'interleaved' ? this.ringSpin : this.ringSpinInterleaved).apply(this, arguments);
+ if (this.ringBuffer) {
+ (this.channelMode === 'interleaved' ? this.ringSpin : this.ringSpinInterleaved).apply(this, arguments);
+ }
}
Sink.on('init', function (sink) {
@@ -1275,7 +1326,9 @@ proto.writeBuffersAsync = function (buffer) {
buffer[n + offset] += buf.b[n];
}
buf.b = buf.b.subarray(n + offset);
- i >= bufLength && buffers.splice(i--, 1);
+ if (i >= bufLength) {
+ buffers.splice(i--, 1);
+ }
}
}
};
@@ -1322,7 +1375,7 @@ proto.writeBufferAsync = function (buffer, delay) {
var buffers = this.asyncBuffers;
buffers.push({
b: buffer,
- d: isNaN(delay) ? ~~((+new Date - this.previousHit) / 1000 * this.sampleRate) : delay
+ d: isNaN(delay) ? ~~((+new Date() - this.previousHit) / 1000 * this.sampleRate) : delay
});
return buffers.length;
};

0 comments on commit e803d33

Please sign in to comment.