diff --git a/src/vocoder.js b/src/vocoder.js index 1963c8a..4c3b020 100644 --- a/src/vocoder.js +++ b/src/vocoder.js @@ -1,68 +1,30 @@ var Tone = require('tone'); -var modulatorNode = null; -var carrierNode = null; -var vocoding = false; - -var FILTER_QUALITY = 6; // The Q value for the carrier and modulator filters - -// These are "placeholder" gain nodes - because the modulator and carrier will get swapped in -// as they are loaded, it's easier to connect these nodes to all the bands, and the "real" -// modulator & carrier AudioBufferSourceNodes connect to these. -var modulatorInput = null; -var carrierInput = null; - -var modulatorGain = null; -var modulatorGainValue = 1.0; - -// noise node added to the carrier signal -var noiseBuffer = null; -var noiseNode = null; -var noiseGain = null; -var noiseGainValue = 0.2; - -// Carrier Synth oscillator stuff -var oscillatorNode = null; -var oscillatorType = 4; // CUSTOM -var oscillatorGain = null; -var oscillatorGainValue = 1.0; -var oscillatorDetuneValue = 0; -var FOURIER_SIZE = 4096; -var SAWTOOTHBOOST = 0.40; - -// These are the arrays of nodes - the "columns" across the frequency band "rows" -var modFilterBands = null; // tuned bandpass filters -var modFilterPostGains = null; // post-filter gains. -var heterodynes = null; // gain nodes used to multiply bandpass X sine -var powers = null; // gain nodes used to multiply prev out by itself -var lpFilters = null; // tuned LP filters to remove doubled copy of product -var lpFilterPostGains = null; // gain nodes for tuning input to waveshapers -var carrierBands = null; // tuned bandpass filters, same as modFilterBands but in carrier chain -var carrierFilterPostGains = null; // post-bandpass gain adjustment -var carrierBandGains = null; // these are the "control gains" driven by the lpFilters - -var vocoderBands; -var numVocoderBands; - -var hpFilterGain = null; - -var outputGain; - -function Vocoder() { +function Vocoder () { Tone.Effect.call(this); - outputGain = new Tone.Gain(); + this.FILTER_QUALITY = 6; // The Q value for the carrier and modulator filters - this.generateVocoderBands(55, 7040, 28); + this.modulatorInput = new Tone.Gain(); + this.carrierInput = new Tone.Gain(); + this.outputGain = new Tone.Gain(); + + this.oscillatorNode; + + this.vocoderBands = this.generateVocoderBands(55, 7040, 10); this.initBandpassFilters(); this.createCarrier(); - this.effectSend.connect(modulatorInput); - outputGain.connect(this.effectReturn); + this.effectSend.connect(this.modulatorInput); + this.outputGain.connect(this.effectReturn); } Tone.extend(Vocoder, Tone.Effect); +Vocoder.prototype.setCarrierOscFrequency = function (freq) { + this.oscillatorNode.frequency.rampTo(freq, 0.05); +}; + // this function will algorithmically re-calculate vocoder bands, distributing evenly // from startFreq to endFreq, splitting evenly (logarhythmically) into a given numBands. // The function places this info into the vocoderBands and numVocoderBands variables. @@ -73,77 +35,54 @@ Vocoder.prototype.generateVocoderBands = function (startFreq, endFreq, numBands) var centsPerBand = totalRangeInCents / numBands; var scale = Math.pow( 2, centsPerBand / 1200 ); // This is the scaling for successive bands - vocoderBands = []; + var vocoderBands = []; var currentFreq = startFreq; for (var i=0; i0)?i:-i)/32768; + for (i=-32768; i<32768; i++) { + rectifierCurve[i+32768] = ((i>0)?i:-i)/32768; + } - for (var i=0; i