From 10cd3a2c3192deaf216b3e736dc404ebe323feae Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Wed, 4 Jan 2017 18:34:11 -0500 Subject: [PATCH] separate out main audioengine from audioplayer for each clone --- src/index.js | 158 +++++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 87 deletions(-) diff --git a/src/index.js b/src/index.js index a4503f2..d8e69f0 100644 --- a/src/index.js +++ b/src/index.js @@ -2,51 +2,57 @@ var log = require('./log'); var Tone = require('tone'); var PitchEffect = require('./effects/PitchEffect'); -var EchoEffect = require('./effects/EchoEffect'); var PanEffect = require('./effects/PanEffect'); var RoboticEffect = require('./effects/RoboticEffect'); -var ReverbEffect = require('./effects/ReverbEffect'); var FuzzEffect = require('./effects/FuzzEffect'); -var WobbleEffect = require('./effects/WobbleEffect'); +var EchoEffect = require('./effects/EchoEffect'); +var ReverbEffect = require('./effects/ReverbEffect'); var SoundPlayer = require('./SoundPlayer'); var Soundfont = require('soundfont-player'); var ADPCMSoundLoader = require('./ADPCMSoundLoader'); -function AudioEngine (sounds) { - - // tone setup - - this.tone = new Tone(); - - // effects setup - this.pitchEffect = new PitchEffect(); - this.echoEffect = new EchoEffect(); - this.panEffect = new PanEffect(); - this.reverbEffect = new ReverbEffect(); - this.fuzzEffect = new FuzzEffect(); - this.wobbleEffect = new WobbleEffect(); +function AudioEngine () { this.roboticEffect = new RoboticEffect(); + this.fuzzEffect = new FuzzEffect(); + this.echoEffect = new EchoEffect(); + this.reverbEffect = new ReverbEffect(); - // the effects are chained to an effects node for this clone, then to the master output + this.input = new Tone.Gain(); + this.input.chain ( + this.roboticEffect, this.fuzzEffect, this.echoEffect, this.reverbEffect, + Tone.Master + ); + + // this.input = new Tone.Gain(); + // this.input.connect(Tone.Master); + +} + +AudioEngine.prototype.createPlayer = function () { + return new AudioPlayer(this); +}; + +function AudioPlayer (audioEngine) { + + this.audioEngine = audioEngine; + + // effects setup + this.pitchEffect = new PitchEffect(); + this.panEffect = new PanEffect(); + + // the effects are chained to an effects node for this player, then to the master output // so audio is sent from each player or instrument, through the effects in order, then out // note that the pitch effect works differently - it sets the playback rate for each player this.effectsNode = new Tone.Gain(); - this.effectsNode.chain( - this.roboticEffect, this.fuzzEffect, this.echoEffect, - this.wobbleEffect, this.panEffect, this.reverbEffect, Tone.Master); + this.effectsNode.chain(this.panEffect, this.audioEngine.input); // reset effects to their default parameters this.clearEffects(); - this.effectNames = ['PITCH', 'PAN', 'ECHO', 'REVERB', 'FUZZ', 'TELEPHONE', 'WOBBLE', 'ROBOTIC']; + this.effectNames = ['PITCH', 'PAN', 'ECHO', 'REVERB', 'FUZZ', 'WOBBLE', 'ROBOTIC']; - // load sounds - - this.soundPlayers = []; - this.loadSounds(sounds); - - // soundfont setup + // soundfont instrument setup // instrument names used by Musyng Kite soundfont, in order to // match scratch instruments @@ -65,7 +71,7 @@ function AudioEngine (sounds) { this.currentTempo = 60; } -AudioEngine.prototype.loadSounds = function (sounds) { +AudioPlayer.prototype.loadSounds = function (sounds) { this.soundPlayers = []; @@ -73,17 +79,20 @@ AudioEngine.prototype.loadSounds = function (sounds) { // the sound buffers will be added asynchronously as they load for (var i=0; i