From 4c0659a0ed45b0a8f7e72b8c05a0911a2c2c64c5 Mon Sep 17 00:00:00 2001 From: tambien Date: Sat, 26 Jan 2019 20:06:07 -0500 Subject: [PATCH] merge optionallly accepts more than 2 channels --- Tone/component/Merge.js | 54 ++++++++++++++++++++++------------------- test/component/Merge.js | 16 ++++++++++++ 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Tone/component/Merge.js b/Tone/component/Merge.js index 0c74f06c..b9c3678a 100644 --- a/Tone/component/Merge.js +++ b/Tone/component/Merge.js @@ -8,6 +8,7 @@ define(["../core/Tone", "../core/AudioNode"], function(Tone){ * * @constructor * @extends {Tone.AudioNode} + * @param {number} [channels=2] The number of channels to merge. * @example * var merge = new Tone.Merge().toMaster(); * //routing a sine tone in the left channel @@ -18,40 +19,42 @@ define(["../core/Tone", "../core/AudioNode"], function(Tone){ * noise.start(); * osc.start(); */ - Tone.Merge = function(){ + Tone.Merge = function(channels){ + //defaults to 2 channels + channels = Tone.defaultArg(channels, 2); + Tone.AudioNode.call(this); - this.createInsOuts(2, 0); - - /** - * The left input channel. - * Alias for input[0] - * @type {GainNode} - */ - this.left = this.input[0] = new Tone.Gain(); - - /** - * The right input channel. - * Alias for input[1]. - * @type {GainNode} - */ - this.right = this.input[1] = new Tone.Gain(); + this.createInsOuts(channels, 0); /** * the merger node for the two channels * @type {ChannelMergerNode} * @private */ - this._merger = this.output = this.context.createChannelMerger(2); + this._merger = this.output = this.context.createChannelMerger(channels); //connections - this.left.connect(this._merger, 0, 0); - this.right.connect(this._merger, 0, 1); + for (var i = 0; i < channels; i++){ + this.input[i] = new Tone.Gain(); + this.input[i].connect(this._merger, 0, i); + this.input[i].channelCount = 1; + this.input[i].channelCountMode = "explicit"; + } - this.left.channelCount = 1; - this.right.channelCount = 1; - this.left.channelCountMode = "explicit"; - this.right.channelCountMode = "explicit"; + /** + * The left input channel. + * Alias for input[0] + * @type {GainNode} + */ + this.left = this.input[0]; + + /** + * The right input channel. + * Alias for input[1]. + * @type {GainNode} + */ + this.right = this.input[1]; }; Tone.extend(Tone.Merge, Tone.AudioNode); @@ -61,10 +64,11 @@ define(["../core/Tone", "../core/AudioNode"], function(Tone){ * @returns {Tone.Merge} this */ Tone.Merge.prototype.dispose = function(){ + this.input.forEach(function(input){ + input.dispose(); + }); Tone.AudioNode.prototype.dispose.call(this); - this.left.dispose(); this.left = null; - this.right.dispose(); this.right = null; this._merger.disconnect(); this._merger = null; diff --git a/test/component/Merge.js b/test/component/Merge.js index 987e8a60..1d35d440 100644 --- a/test/component/Merge.js +++ b/test/component/Merge.js @@ -14,6 +14,22 @@ function(Merge, Basic, PassAudio, PassAudioStereo, Test, Offline, Signal){ merge.dispose(); }); + it("defaults to two channels", function(){ + var merge = new Merge(); + expect(merge.numberOfInputs).to.equal(2); + merge.dispose(); + }); + + it("can pass in more channels", function(){ + var merge = new Merge(4); + expect(merge.numberOfInputs).to.equal(4); + Test.connect(merge, 0, 0); + Test.connect(merge, 0, 1); + Test.connect(merge, 0, 2); + Test.connect(merge, 0, 3); + merge.dispose(); + }); + it("passes the incoming signal through", function(){ return PassAudio(function(input){ var merge = new Merge().toMaster();