dont fill up our input buffers too much/too fast

This commit is contained in:
alfred 2013-12-31 10:18:59 +00:00
parent 126266e4d9
commit 362a00c967

View File

@ -35,6 +35,8 @@ function Input(args) {
this.channels = args.channels; this.channels = args.channels;
this.bitDepth = args.bitDepth; this.bitDepth = args.bitDepth;
this.sampleRate = args.sampleRate; this.sampleRate = args.sampleRate;
this.getMoreData = null;
} }
util.inherits(Input, Writable); util.inherits(Input, Writable);
@ -44,6 +46,13 @@ Input.prototype.read = function (samples) {
if (this.buffer.length < bytes) bytes = this.buffer.length; if (this.buffer.length < bytes) bytes = this.buffer.length;
var r = this.buffer.slice(0, bytes); var r = this.buffer.slice(0, bytes);
this.buffer = this.buffer.slice(bytes); this.buffer = this.buffer.slice(bytes);
if (this.buffer.length <= 131072 && this.getMoreData !== null) {
var getMoreData = this.getMoreData;
this.getMoreData = null;
process.nextTick(getMoreData);
}
return r; return r;
} }
@ -85,6 +94,12 @@ Input.prototype._write = function (chunk, encoding, next) {
} }
*/ */
this.buffer = Buffer.concat([this.buffer, chunk]); this.buffer = Buffer.concat([this.buffer, chunk]);
next(); if (this.buffer.length > 131072) {
this.getMoreData = next;
} else {
next();
}
} }