Tone.js/Tone/instrument/DuoSynth.js

218 lines
5.2 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
2015-02-28 04:24:51 +00:00
* @example
* var duoSynth = new Tone.DuoSynth();
2014-08-26 01:34:09 +00:00
*/
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);
2015-02-10 16:40:04 +00:00
this.voice0.volume.value = -10;
2014-08-26 01:34:09 +00:00
/**
* the second voice
* @type {Tone.MonoSynth}
*/
this.voice1 = new Tone.MonoSynth(options.voice1);
2015-02-10 16:40:04 +00:00
this.voice1.volume.value = -10;
2014-08-26 01:34:09 +00:00
/**
2015-02-10 16:40:04 +00:00
* The vibrato LFO.
2014-08-26 01:34:09 +00:00
* @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();
2015-02-10 16:40:04 +00:00
/**
* the vibrato frequency
2015-06-14 00:20:36 +00:00
* @type {Frequency}
* @signal
2015-02-10 16:40:04 +00:00
*/
this.vibratoRate = this._vibrato.frequency;
2014-08-26 01:34:09 +00:00
/**
* the vibrato gain
* @type {GainNode}
* @private
*/
this._vibratoGain = this.context.createGain();
/**
2015-02-10 16:40:04 +00:00
* The amount of vibrato
2015-06-14 00:20:36 +00:00
* @type {Gain}
* @signal
2014-08-26 01:34:09 +00:00
*/
this.vibratoAmount = new Tone.Signal(this._vibratoGain.gain, Tone.Type.Gain);
2015-02-10 16:40:04 +00:00
this.vibratoAmount.value = options.vibratoAmount;
2014-08-26 01:34:09 +00:00
/**
2015-02-10 16:40:04 +00:00
* the delay before the vibrato starts
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
*/
2015-02-10 16:40:04 +00:00
this._vibratoDelay = this.toSeconds(options.vibratoDelay);
2014-08-26 01:34:09 +00:00
/**
* the frequency control
2015-06-14 00:20:36 +00:00
* @type {Frequency}
* @signal
2014-08-26 01:34:09 +00:00
*/
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
2014-08-26 01:34:09 +00:00
/**
* 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);
2015-04-18 14:54:08 +00:00
this._readOnly(["voice0", "voice1", "frequency", "vibratoAmount", "vibratoRate"]);
2014-08-26 01:34: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" : {
2015-01-06 04:33:05 +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" : {
2015-01-06 04:33:05 +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
}
};
/**
* start the attack portion of the envelopes
2014-08-26 01:34:09 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the attack should start
* @param {NormalRange} [velocity=1] the velocity of the note (0-1)
* @returns {Tone.DuoSynth} this
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);
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
/**
* start the release portion of the envelopes
2014-08-26 01:34:09 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the release should start
* @returns {Tone.DuoSynth} this
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);
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
/**
2015-02-10 16:40:04 +00:00
* The ratio between the two carrier and the modulator.
* @memberOf Tone.DuoSynth#
2015-06-14 00:20:36 +00:00
* @type {Positive}
2015-02-10 16:40:04 +00:00
* @name harmonicity
2014-08-26 01:34:09 +00:00
*/
2015-02-10 16:40:04 +00:00
Object.defineProperty(Tone.DuoSynth.prototype, "harmonicity", {
get : function(){
return this._harmonicity.value;
},
set : function(harm){
this._harmonicity.value = harm;
}
});
2014-08-26 01:34:09 +00:00
/**
* clean up
* @returns {Tone.DuoSynth} this
2014-08-26 01:34:09 +00:00
*/
Tone.DuoSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["voice0", "voice1", "frequency", "vibratoAmount", "vibratoRate"]);
2014-08-26 01:34:09 +00:00
this.voice0.dispose();
this.voice0 = null;
2015-02-10 16:40:04 +00:00
this.voice1.dispose();
2014-08-26 01:34:09 +00:00
this.voice1 = null;
2015-02-10 16:40:04 +00:00
this.frequency.dispose();
2014-08-26 01:34:09 +00:00
this.frequency = null;
2015-02-10 16:40:04 +00:00
this._vibrato.dispose();
2014-08-26 01:34:09 +00:00
this._vibrato = null;
2015-02-10 16:40:04 +00:00
this._vibratoGain.disconnect();
2014-08-26 01:34:09 +00:00
this._vibratoGain = null;
2015-02-10 16:40:04 +00:00
this._harmonicity.dispose();
2014-09-03 18:53:05 +00:00
this._harmonicity = null;
2015-02-10 16:40:04 +00:00
this.vibratoAmount.dispose();
this.vibratoAmount = null;
this.vibratoRate = null;
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
return Tone.DuoSynth;
});