From 362a00c96796314784392dcc2861c9112462cef8 Mon Sep 17 00:00:00 2001 From: alfred Date: Tue, 31 Dec 2013 10:18:59 +0000 Subject: [PATCH] dont fill up our input buffers too much/too fast --- lib/input.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/input.js b/lib/input.js index 458220a..7f0c3d2 100644 --- a/lib/input.js +++ b/lib/input.js @@ -35,6 +35,8 @@ function Input(args) { this.channels = args.channels; this.bitDepth = args.bitDepth; this.sampleRate = args.sampleRate; + + this.getMoreData = null; } util.inherits(Input, Writable); @@ -44,6 +46,13 @@ Input.prototype.read = function (samples) { if (this.buffer.length < bytes) bytes = this.buffer.length; var r = this.buffer.slice(0, 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; } @@ -85,6 +94,12 @@ Input.prototype._write = function (chunk, encoding, next) { } */ this.buffer = Buffer.concat([this.buffer, chunk]); - next(); + if (this.buffer.length > 131072) { + this.getMoreData = next; + } else { + next(); + } + + }