From 72d796552155fb83d6adc560248a6cccc77eee9e Mon Sep 17 00:00:00 2001 From: Chinmay Pendharkar Date: Sun, 7 Aug 2016 14:44:24 +0800 Subject: [PATCH] Stop trying to read if no inputs are connected If no inputs are connected, the `_read` function still keeps looping ( using `setImmediate(this._read.bind(this));` ) to wait for more samples to be available. This can cause the CPU usage to go up (60% on my 2014 MBP) even when no audio is being played. This fix stops the looping if `this.inputs` array is empty and starts it again when new inputs are connected. --- lib/mixer.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/mixer.js b/lib/mixer.js index 26d5452..224d0b5 100644 --- a/lib/mixer.js +++ b/lib/mixer.js @@ -73,7 +73,9 @@ Mixer.prototype._read = function() { this.push(mixedBuffer); } else { - setImmediate(this._read.bind(this)); + if (this.inputs.length){ + setImmediate(this._read.bind(this)); + } } }; @@ -91,6 +93,10 @@ Mixer.prototype.input = function (args) { input.on('finish', () =>{ this.inputs.splice(this.inputs.indexOf(input), 1); }); + + if (this.inputs.length === 1){ + setImmediate(this._read.bind(this)); + } return input; };