Tone.js/Tone/instrument/DuoSynth.js

237 lines
5.8 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/instrument/MonoSynth", "Tone/component/LFO", "Tone/signal/Signal", "Tone/signal/Multiply", "Tone/instrument/Monophonic"],
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
* @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);
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);
this.voice0.setVolume("-10db");
2014-08-26 01:34:09 +00:00
/**
* the second voice
* @type {Tone.MonoSynth}
*/
this.voice1 = new Tone.MonoSynth(options.voice1);
this.voice1.setVolume("-10db");
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);
/**
2014-12-04 02:38:24 +00:00
* the amount of vibrato
2014-08-26 01:34:09 +00:00
* @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-12-01 02:32:09 +00:00
this.frequency.chain(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);
};
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" : {
"volume" : "-10db",
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" : {
"volume" : "-10db",
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
}
};
/**
* start the attack portion of the envelopes
2014-08-26 01:34:09 +00:00
*
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now] the time the attack should start
* @param {number} [velocity=1] the velocity of the note (0-1)
2014-08-26 01:34: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);
this.voice0.filterEnvelope.triggerAttack(time);
this.voice1.filterEnvelope.triggerAttack(time);
2014-08-26 01:34:09 +00:00
};
/**
* start the release portion of the envelopes
2014-08-26 01:34:09 +00:00
*
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now] the time the release should start
2014-08-26 01:34: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);
};
/**
* 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);
Tone.Monophonic.prototype.set.call(this, params);
2014-08-26 01:34:09 +00:00
};
/**
* clean up
*/
Tone.DuoSynth.prototype.dispose = function(){
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;
});