2016-05-23 23:49:41 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/instrument/Synth", "Tone/signal/Signal", "Tone/signal/Multiply", "Tone/instrument/Monophonic"],
|
2014-09-04 04:41:40 +00:00
|
|
|
function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
2014-09-03 18:54:46 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-23 23:49:41 +00:00
|
|
|
* @class FMSynth is composed of two Tone.Synths where one Tone.Synth modulates
|
|
|
|
* the frequency of a second Tone.Synth. A lot of spectral content
|
2015-06-15 15:27:13 +00:00
|
|
|
* can be explored using the modulationIndex parameter. Read more about
|
2015-07-04 19:25:37 +00:00
|
|
|
* frequency modulation synthesis on [SoundOnSound](http://www.soundonsound.com/sos/apr00/articles/synthsecrets.htm).
|
2015-06-20 22:03:49 +00:00
|
|
|
* <img src="https://docs.google.com/drawings/d/1h0PUDZXPgi4Ikx6bVT6oncrYPLluFKy7lj53puxj-DM/pub?w=902&h=462">
|
2014-09-03 18:54:46 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-09-03 23:05:09 +00:00
|
|
|
* @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
|
2015-06-16 02:36:20 +00:00
|
|
|
* var fmSynth = new Tone.FMSynth().toMaster();
|
|
|
|
* fmSynth.triggerAttackRelease("C5", "4n");
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
|
|
|
Tone.FMSynth = function(options){
|
|
|
|
|
|
|
|
options = this.defaultArg(options, Tone.FMSynth.defaults);
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.call(this, options);
|
2014-09-03 18:54:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-14 03:15:57 +00:00
|
|
|
* The carrier voice.
|
2016-05-23 23:49:41 +00:00
|
|
|
* @type {Tone.Synth}
|
2016-10-05 05:31:11 +00:00
|
|
|
* @private
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2016-05-23 23:49:41 +00:00
|
|
|
this._carrier = new Tone.Synth(options.carrier);
|
2016-01-30 20:53:40 +00:00
|
|
|
this._carrier.volume.value = -10;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The carrier's oscillator
|
|
|
|
* @type {Tone.Oscillator}
|
|
|
|
*/
|
|
|
|
this.oscillator = this._carrier.oscillator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The carrier's envelope
|
|
|
|
* @type {Tone.Oscillator}
|
|
|
|
*/
|
|
|
|
this.envelope = this._carrier.envelope.set(options.envelope);
|
2014-09-03 18:54:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-14 03:15:57 +00:00
|
|
|
* The modulator voice.
|
2016-05-23 23:49:41 +00:00
|
|
|
* @type {Tone.Synth}
|
2016-10-05 05:31:11 +00:00
|
|
|
* @private
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2016-05-23 23:49:41 +00:00
|
|
|
this._modulator = new Tone.Synth(options.modulator);
|
2016-01-30 20:53:40 +00:00
|
|
|
this._modulator.volume.value = -10;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The modulator's oscillator which is applied
|
|
|
|
* to the amplitude of the oscillator
|
|
|
|
* @type {Tone.Oscillator}
|
|
|
|
*/
|
|
|
|
this.modulation = this._modulator.oscillator.set(options.modulation);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The modulator's envelope
|
|
|
|
* @type {Tone.Oscillator}
|
|
|
|
*/
|
|
|
|
this.modulationEnvelope = this._modulator.envelope.set(options.modulationEnvelope);
|
2014-09-03 18:54:46 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-20 19:50:57 +00:00
|
|
|
* The frequency control.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Frequency}
|
|
|
|
* @signal
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2015-05-23 22:57:05 +00:00
|
|
|
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
|
2014-09-03 18:54:46 +00:00
|
|
|
|
2016-05-15 00:10:31 +00:00
|
|
|
/**
|
|
|
|
* The detune in cents
|
|
|
|
* @type {Cents}
|
|
|
|
* @signal
|
|
|
|
*/
|
|
|
|
this.detune = new Tone.Signal(options.detune, Tone.Type.Cents);
|
|
|
|
|
2014-09-03 18:54:46 +00:00
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* Harmonicity is the ratio between the two voices. A harmonicity of
|
|
|
|
* 1 is no change. Harmonicity = 2 means a change of an octave.
|
2015-06-14 02:30:53 +00:00
|
|
|
* @type {Positive}
|
|
|
|
* @signal
|
2015-06-20 22:03:49 +00:00
|
|
|
* @example
|
|
|
|
* //pitch voice1 an octave below voice0
|
|
|
|
* synth.harmonicity.value = 0.5;
|
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-20 22:03:49 +00:00
|
|
|
* The modulation index which essentially the depth or amount of the modulation. It is the
|
2015-06-14 04:32:17 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2016-09-20 03:53:07 +00:00
|
|
|
this._modulationNode = new Tone.Gain(0);
|
2014-09-03 18:54:46 +00:00
|
|
|
|
|
|
|
//control the two voices frequency
|
2016-01-30 20:53:40 +00:00
|
|
|
this.frequency.connect(this._carrier.frequency);
|
|
|
|
this.frequency.chain(this.harmonicity, this._modulator.frequency);
|
2015-06-14 04:32:17 +00:00
|
|
|
this.frequency.chain(this.modulationIndex, this._modulationNode);
|
2016-05-15 00:10:31 +00:00
|
|
|
this.detune.fan(this._carrier.detune, this._modulator.detune);
|
2016-01-30 20:53:40 +00:00
|
|
|
this._modulator.connect(this._modulationNode.gain);
|
|
|
|
this._modulationNode.connect(this._carrier.frequency);
|
|
|
|
this._carrier.connect(this.output);
|
2016-05-15 00:10:31 +00:00
|
|
|
this._readOnly(["frequency", "harmonicity", "modulationIndex", "oscillator", "envelope", "modulation", "modulationEnvelope", "detune"]);
|
2014-09-03 18:54:46 +00:00
|
|
|
};
|
|
|
|
|
2014-09-03 23:05:09 +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,
|
2016-05-15 00:10:31 +00:00
|
|
|
"detune" : 0,
|
2016-01-30 20:53:40 +00:00
|
|
|
"oscillator" : {
|
|
|
|
"type" : "sine"
|
|
|
|
},
|
|
|
|
"envelope" : {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.01,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-09-03 18:54:46 +00:00
|
|
|
},
|
2017-01-01 01:36:24 +00:00
|
|
|
"modulation" : {
|
2016-01-30 20:53:40 +00:00
|
|
|
"type" : "square"
|
|
|
|
},
|
|
|
|
"modulationEnvelope" : {
|
|
|
|
"attack" : 0.5,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-09-03 18:54:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-20 19:50:57 +00:00
|
|
|
* trigger the attack portion of the note
|
2014-09-03 18:54:46 +00:00
|
|
|
*
|
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
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.FMSynth} this
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2015-06-14 02:47:07 +00:00
|
|
|
Tone.FMSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
|
2014-09-03 21:31:29 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-09-03 18:54:46 +00:00
|
|
|
//the envelopes
|
2016-01-30 20:53:40 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this.modulationEnvelope.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
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.FMSynth} this
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2015-06-14 02:47:07 +00:00
|
|
|
Tone.FMSynth.prototype._triggerEnvelopeRelease = function(time){
|
2016-01-30 20:53:40 +00:00
|
|
|
time = this.toSeconds(time);
|
|
|
|
this.envelope.triggerRelease(time);
|
|
|
|
this.modulationEnvelope.triggerRelease(time);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-03 18:54:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.FMSynth} this
|
2014-09-03 18:54:46 +00:00
|
|
|
*/
|
2014-09-09 19:30:36 +00:00
|
|
|
Tone.FMSynth.prototype.dispose = function(){
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.prototype.dispose.call(this);
|
2016-05-15 00:10:31 +00:00
|
|
|
this._writable(["frequency", "harmonicity", "modulationIndex", "oscillator", "envelope", "modulation", "modulationEnvelope", "detune"]);
|
2016-01-30 20:53:40 +00:00
|
|
|
this._carrier.dispose();
|
|
|
|
this._carrier = null;
|
|
|
|
this._modulator.dispose();
|
|
|
|
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;
|
2016-05-15 00:10:31 +00:00
|
|
|
this.detune.dispose();
|
|
|
|
this.detune = 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;
|
2016-09-20 03:53:07 +00:00
|
|
|
this._modulationNode.dispose();
|
2014-09-03 18:54:46 +00:00
|
|
|
this._modulationNode = null;
|
2016-01-30 20:53:40 +00:00
|
|
|
this.oscillator = null;
|
|
|
|
this.envelope = null;
|
|
|
|
this.modulationEnvelope = null;
|
|
|
|
this.modulation = null;
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-03 18:54:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.FMSynth;
|
2017-01-01 01:36:24 +00:00
|
|
|
});
|