2014-09-03 23:05:09 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/instrument/MonoSynth", "Tone/component/LFO", "Tone/signal/Signal", "Tone/signal/Multiply", "Tone/instrument/Monophonic"],
|
2014-09-04 04:41:40 +00:00
|
|
|
function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class the DuoSynth is a monophonic synth composed of two
|
|
|
|
* MonoSynths run in parallel with control over the
|
|
|
|
* frequency ratio between the two voices and vibrato effect.
|
|
|
|
*
|
|
|
|
* @constructor
|
2014-09-03 23:05:09 +00:00
|
|
|
* @extends {Tone.Monophonic}
|
2014-08-26 01:34:09 +00:00
|
|
|
* @param {Object} options the options available for the synth
|
|
|
|
* see defaults below
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth = function(options){
|
|
|
|
|
|
|
|
options = this.defaultArg(options, Tone.DuoSynth.defaults);
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.call(this, options);
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the first voice
|
|
|
|
* @type {Tone.MonoSynth}
|
|
|
|
*/
|
|
|
|
this.voice0 = new Tone.MonoSynth(options.voice0);
|
2014-09-02 20:57:46 +00:00
|
|
|
this.voice0.setVolume(-10);
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the second voice
|
|
|
|
* @type {Tone.MonoSynth}
|
|
|
|
*/
|
|
|
|
this.voice1 = new Tone.MonoSynth(options.voice1);
|
2014-09-02 20:57:46 +00:00
|
|
|
this.voice1.setVolume(-10);
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the vibrato lfo
|
|
|
|
* @type {Tone.LFO}
|
2014-09-05 15:32:33 +00:00
|
|
|
* @private
|
2014-08-26 01:34:09 +00:00
|
|
|
*/
|
|
|
|
this._vibrato = new Tone.LFO(options.vibratoRate, -50, 50);
|
|
|
|
this._vibrato.start();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the vibrato gain
|
|
|
|
* @type {GainNode}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._vibratoGain = this.context.createGain();
|
|
|
|
this._vibratoGain.gain.value = options.vibratoAmount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the delay before the vibrato starts
|
|
|
|
* @type {number}
|
2014-09-05 15:32:33 +00:00
|
|
|
* @private
|
2014-08-26 01:34:09 +00:00
|
|
|
*/
|
|
|
|
this._vibratoDelay = this.toSeconds(options.vibratoDelay);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the amount before the vibrato starts
|
|
|
|
* @type {number}
|
2014-09-05 15:32:33 +00:00
|
|
|
* @private
|
2014-08-26 01:34:09 +00:00
|
|
|
*/
|
|
|
|
this._vibratoAmount = options.vibratoAmount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the frequency control
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
|
|
|
this.frequency = new Tone.Signal(440);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the ratio between the two voices
|
|
|
|
* @type {Tone.Multiply}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-09-03 18:53:05 +00:00
|
|
|
this._harmonicity = new Tone.Multiply(options.harmonicity);
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
//control the two voices frequency
|
2014-09-03 18:53:05 +00:00
|
|
|
this.frequency.connect(this.voice0.frequency);
|
2014-11-30 22:37:29 +00:00
|
|
|
this.connectSeries(this.frequency, this._harmonicity, this.voice1.frequency);
|
2014-08-26 01:34:09 +00:00
|
|
|
this._vibrato.connect(this._vibratoGain);
|
2014-12-01 02:20:12 +00:00
|
|
|
this._vibratoGain.fan(this.voice0.detune, this.voice1.detune);
|
2014-08-26 01:34:09 +00:00
|
|
|
this.voice0.connect(this.output);
|
|
|
|
this.voice1.connect(this.output);
|
|
|
|
};
|
|
|
|
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.extend(Tone.DuoSynth, Tone.Monophonic);
|
2014-08-26 01:34:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.defaults = {
|
|
|
|
"vibratoAmount" : 0.5,
|
|
|
|
"vibratoRate" : 5,
|
|
|
|
"vibratoDelay" : 1,
|
2014-09-03 18:53:05 +00:00
|
|
|
"harmonicity" : 1.5,
|
2014-08-26 01:34:09 +00:00
|
|
|
"voice0" : {
|
2014-09-02 20:57:46 +00:00
|
|
|
"volume" : -10,
|
2014-08-26 01:34:09 +00:00
|
|
|
"portamento" : 0,
|
2014-10-01 02:49:17 +00:00
|
|
|
"oscillator" : {
|
|
|
|
"type" : "sine"
|
|
|
|
},
|
2014-08-26 01:47:45 +00:00
|
|
|
"filterEnvelope" : {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-09-02 20:57:46 +00:00
|
|
|
},
|
|
|
|
"envelope" : {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-08-26 01:47:45 +00:00
|
|
|
}
|
2014-08-26 01:34:09 +00:00
|
|
|
},
|
|
|
|
"voice1" : {
|
2014-09-02 20:57:46 +00:00
|
|
|
"volume" : -10,
|
2014-08-26 01:34:09 +00:00
|
|
|
"portamento" : 0,
|
2014-10-01 02:49:17 +00:00
|
|
|
"oscillator" : {
|
|
|
|
"type" : "sine"
|
|
|
|
},
|
2014-08-26 01:47:45 +00:00
|
|
|
"filterEnvelope" : {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-09-02 20:57:46 +00:00
|
|
|
},
|
|
|
|
"envelope" : {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-08-26 01:47:45 +00:00
|
|
|
}
|
2014-08-26 01:34:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-09-03 23:05:09 +00:00
|
|
|
* start the attack portion of the envelopes
|
2014-08-26 01:34:09 +00:00
|
|
|
*
|
2014-09-03 23:05:09 +00:00
|
|
|
* @param {Tone.Time=} [time=now] the time the attack should start
|
|
|
|
* @param {number=} velocity the velocity of the note (0-1)
|
2014-08-26 01:34:09 +00:00
|
|
|
*/
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.DuoSynth.prototype.triggerEnvelopeAttack = function(time, velocity){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
this.voice0.envelope.triggerAttack(time, velocity);
|
|
|
|
this.voice1.envelope.triggerAttack(time, velocity);
|
2014-09-03 21:31:29 +00:00
|
|
|
this.voice0.filterEnvelope.triggerAttack(time);
|
|
|
|
this.voice1.filterEnvelope.triggerAttack(time);
|
2014-08-26 01:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-09-03 23:05:09 +00:00
|
|
|
* start the release portion of the envelopes
|
2014-08-26 01:34:09 +00:00
|
|
|
*
|
2014-09-03 23:05:09 +00:00
|
|
|
* @param {Tone.Time=} [time=now] the time the release should start
|
2014-08-26 01:34:09 +00:00
|
|
|
*/
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.DuoSynth.prototype.triggerEnvelopeRelease = function(time){
|
2014-08-26 01:34:09 +00:00
|
|
|
this.voice0.triggerRelease(time);
|
|
|
|
this.voice1.triggerRelease(time);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the ratio between the two oscillator
|
|
|
|
* @param {number} ratio
|
|
|
|
*/
|
2014-09-03 18:53:05 +00:00
|
|
|
Tone.DuoSynth.prototype.setHarmonicity = function(ratio){
|
|
|
|
this._harmonicity.setValue(ratio);
|
2014-08-26 01:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the glide time between frequencies
|
|
|
|
* @param {Tone.Time} port
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.setPortamento = function(port){
|
|
|
|
this.portamento = this.toSeconds(port);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the delay before the vibrato kicks in
|
|
|
|
* @param {Tone.Time} delay
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.setVibratoDelay = function(delay){
|
|
|
|
this._vibratoDelay = this.toSeconds(delay);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the vibrato amount. 1 is full vib. 0 is none.
|
|
|
|
* @param {number} amount an amount between 0-1
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.setVibratoAmount = function(amount){
|
|
|
|
this._vibratoAmount = amount;
|
|
|
|
this._vibratoGain.gain.setValueAtTime(amount, this.now());
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the rate of the vibrato
|
|
|
|
* @param {number} rate
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.setVibratoRate = function(rate){
|
|
|
|
this._vibrato.setFrequency(rate);
|
|
|
|
};
|
|
|
|
|
2014-09-02 20:57:46 +00:00
|
|
|
/**
|
|
|
|
* set the volume of the instrument.
|
|
|
|
* borrowed from {@link Tone.Source}
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.setVolume = Tone.Source.prototype.setVolume;
|
|
|
|
|
2014-08-26 01:34:09 +00:00
|
|
|
/**
|
|
|
|
* bulk setter
|
|
|
|
* @param {Object} param
|
|
|
|
*/
|
|
|
|
Tone.DuoSynth.prototype.set = function(params){
|
2014-09-03 18:53:05 +00:00
|
|
|
if (!this.isUndef(params.harmonicity)) this.setHarmonicity(params.harmonicity);
|
2014-08-26 01:34:09 +00:00
|
|
|
if (!this.isUndef(params.vibratoRate)) this.setVibratoRate(params.vibratoRate);
|
|
|
|
if (!this.isUndef(params.vibratoAmount)) this.setVibratoAmount(params.vibratoAmount);
|
|
|
|
if (!this.isUndef(params.vibratoDelay)) this.setVibratoDelay(params.vibratoDelay);
|
|
|
|
if (!this.isUndef(params.voice0)) this.voice0.set(params.voice0);
|
|
|
|
if (!this.isUndef(params.voice1)) this.voice1.set(params.voice1);
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.prototype.set.call(this, params);
|
2014-08-26 01:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
2014-09-09 19:30:36 +00:00
|
|
|
Tone.DuoSynth.prototype.dispose = function(){
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.prototype.dispose.call(this);
|
2014-08-26 01:34:09 +00:00
|
|
|
this.voice0.dispose();
|
|
|
|
this.voice1.dispose();
|
|
|
|
this.frequency.dispose();
|
|
|
|
this._vibrato.dispose();
|
|
|
|
this._vibratoGain.disconnect();
|
2014-09-03 18:53:05 +00:00
|
|
|
this._harmonicity.dispose();
|
2014-08-26 01:34:09 +00:00
|
|
|
this.voice0 = null;
|
|
|
|
this.voice1 = null;
|
|
|
|
this.frequency = null;
|
|
|
|
this._vibrato = null;
|
|
|
|
this._vibratoGain = null;
|
2014-09-03 18:53:05 +00:00
|
|
|
this._harmonicity = null;
|
2014-08-26 01:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.DuoSynth;
|
|
|
|
});
|