Tone.js/Tone/instrument/FMSynth.js

189 lines
4.9 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/instrument/MonoSynth", "Tone/signal/Signal", "Tone/signal/Multiply", "Tone/instrument/Monophonic"],
function(Tone){
"use strict";
2014-09-03 18:54:46 +00:00
/**
2015-06-14 04:32:17 +00:00
* @class FMSynth is composed of two Tone.MonoSynths where one Tone.MonoSynth modulates
* the frequency of a second Tone.MonoSynth. A lot of spectral content
* can be acheived using the modulationIndex parameter. Read more about
* Frequency Modulation Synthesis on <a href="http://www.soundonsound.com/sos/apr00/articles/synthsecrets.htm">SoundOnSound</a>
2014-09-03 18:54:46 +00:00
*
* @constructor
* @extends {Tone.Monophonic}
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options available for the synth
2014-09-03 18:54:46 +00:00
* see defaults below
2015-02-28 04:24:51 +00:00
* @example
* var fmSynth = new Tone.FMSynth();
2014-09-03 18:54:46 +00:00
*/
Tone.FMSynth = function(options){
options = this.defaultArg(options, Tone.FMSynth.defaults);
Tone.Monophonic.call(this, options);
2014-09-03 18:54:46 +00:00
/**
2015-06-14 03:15:57 +00:00
* The carrier voice.
2014-09-03 18:54:46 +00:00
* @type {Tone.MonoSynth}
*/
this.carrier = new Tone.MonoSynth(options.carrier);
2015-02-10 16:40:04 +00:00
this.carrier.volume.value = -10;
2014-09-03 18:54:46 +00:00
/**
2015-06-14 03:15:57 +00:00
* The modulator voice.
2014-09-03 18:54:46 +00:00
* @type {Tone.MonoSynth}
*/
this.modulator = new Tone.MonoSynth(options.modulator);
2015-02-10 16:40:04 +00:00
this.modulator.volume.value = -10;
2014-09-03 18:54:46 +00:00
/**
* the frequency control
2015-06-13 23:50:39 +00:00
* @type {Frequency}
* @signal
2014-09-03 18:54:46 +00:00
*/
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
2014-09-03 18:54:46 +00:00
/**
2015-06-14 02:30:53 +00:00
* The ratio between the two carrier and the modulator.
* @type {Positive}
* @signal
2014-09-03 18:54:46 +00:00
*/
2015-06-14 02:30:53 +00:00
this.harmonicity = new Tone.Multiply(options.harmonicity);
this.harmonicity.units = Tone.Type.Positive;
2014-09-03 18:54:46 +00:00
/**
2015-06-14 04:32:17 +00:00
* The modulation index which is in essence the depth or amount of the modulation. In other terms it is the
* ratio of the frequency of the modulating signal (mf) to the amplitude of the
* modulating signal (ma) -- as in ma/mf.
* @type {Positive}
* @signal
2014-09-03 18:54:46 +00:00
*/
2015-06-14 04:32:17 +00:00
this.modulationIndex = new Tone.Multiply(options.modulationIndex);
this.modulationIndex.units = Tone.Type.Positive;
2014-09-03 18:54:46 +00:00
/**
* the node where the modulation happens
* @type {GainNode}
* @private
*/
this._modulationNode = this.context.createGain();
//control the two voices frequency
this.frequency.connect(this.carrier.frequency);
2015-06-14 02:30:53 +00:00
this.frequency.chain(this.harmonicity, this.modulator.frequency);
2015-06-14 04:32:17 +00:00
this.frequency.chain(this.modulationIndex, this._modulationNode);
2014-09-03 18:54:46 +00:00
this.modulator.connect(this._modulationNode.gain);
this._modulationNode.gain.value = 0;
this._modulationNode.connect(this.carrier.frequency);
this.carrier.connect(this.output);
2015-04-18 14:54:08 +00:00
this._readOnly(["carrier", "modulator", "frequency"]);
2014-09-03 18:54:46 +00:00
};
Tone.extend(Tone.FMSynth, Tone.Monophonic);
2014-09-03 18:54:46 +00:00
/**
* @static
* @type {Object}
*/
Tone.FMSynth.defaults = {
"harmonicity" : 3,
"modulationIndex" : 10,
"carrier" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-09-03 18:54:46 +00:00
"portamento" : 0,
2014-10-01 02:49:17 +00:00
"oscillator" : {
"type" : "sine"
},
2014-09-03 18:54:46 +00:00
"envelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
},
"filterEnvelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5,
"min" : 20000,
"max" : 20000
}
},
"modulator" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-09-03 18:54:46 +00:00
"portamento" : 0,
2014-10-01 02:49:17 +00:00
"oscillator" : {
"type" : "triangle"
},
2014-09-03 18:54:46 +00:00
"envelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
},
"filterEnvelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5,
"min" : 20000,
"max" : 20000
}
}
};
/**
* trigger the attack portion of the note
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the note will occur
2014-12-02 06:42:08 +00:00
* @param {number} [velocity=1] the velocity of the note
* @returns {Tone.FMSynth} this
* @private
2014-09-03 18:54:46 +00:00
*/
Tone.FMSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
//the port glide
time = this.toSeconds(time);
2014-09-03 18:54:46 +00:00
//the envelopes
this.carrier.envelope.triggerAttack(time, velocity);
2014-09-03 18:54:46 +00:00
this.modulator.envelope.triggerAttack(time);
this.carrier.filterEnvelope.triggerAttack(time);
this.modulator.filterEnvelope.triggerAttack(time);
2015-02-02 18:30:36 +00:00
return this;
2014-09-03 18:54:46 +00:00
};
/**
* trigger the release portion of the note
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the note will release
* @returns {Tone.FMSynth} this
* @private
2014-09-03 18:54:46 +00:00
*/
Tone.FMSynth.prototype._triggerEnvelopeRelease = function(time){
2014-09-03 18:54:46 +00:00
this.carrier.triggerRelease(time);
this.modulator.triggerRelease(time);
2015-02-02 18:30:36 +00:00
return this;
2014-09-03 18:54:46 +00:00
};
/**
* clean up
* @returns {Tone.FMSynth} this
2014-09-03 18:54:46 +00:00
*/
Tone.FMSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["carrier", "modulator", "frequency"]);
2014-09-03 18:54:46 +00:00
this.carrier.dispose();
this.carrier = null;
2015-02-10 16:40:04 +00:00
this.modulator.dispose();
2014-09-03 18:54:46 +00:00
this.modulator = null;
2015-02-10 16:40:04 +00:00
this.frequency.dispose();
2014-09-03 18:54:46 +00:00
this.frequency = null;
2015-06-14 04:32:17 +00:00
this.modulationIndex.dispose();
this.modulationIndex = null;
2015-06-14 02:30:53 +00:00
this.harmonicity.dispose();
this.harmonicity = null;
2015-02-10 16:40:04 +00:00
this._modulationNode.disconnect();
2014-09-03 18:54:46 +00:00
this._modulationNode = null;
2015-02-02 18:30:36 +00:00
return this;
2014-09-03 18:54:46 +00:00
};
return Tone.FMSynth;
});