2017-08-15 05:02:00 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/instrument/Synth", "Tone/signal/Signal", "Tone/signal/Multiply",
|
2017-10-21 23:02:46 +00:00
|
|
|
"Tone/instrument/Monophonic", "Tone/signal/AudioToGain", "Tone/core/Gain"], function(Tone){
|
2014-11-02 01:56:23 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2016-05-23 23:49:41 +00:00
|
|
|
* @class AMSynth uses the output of one Tone.Synth to modulate the
|
|
|
|
* amplitude of another Tone.Synth. The harmonicity (the ratio between
|
2016-01-30 20:53:40 +00:00
|
|
|
* the two signals) affects the timbre of the output signal greatly.
|
2017-08-15 05:02:00 +00:00
|
|
|
* Read more about Amplitude Modulation Synthesis on
|
|
|
|
* [SoundOnSound](https://web.archive.org/web/20160404103653/http://www.soundonsound.com:80/sos/mar00/articles/synthsecrets.htm).
|
2015-06-20 22:03:49 +00:00
|
|
|
* <img src="https://docs.google.com/drawings/d/1TQu8Ed4iFr1YTLKpB3U1_hur-UwBrh5gdBXc8BxfGKw/pub?w=1009&h=457">
|
2014-11-02 01:56:23 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Monophonic}
|
2017-08-15 05:02:00 +00:00
|
|
|
* @param {Object} [options] the options available for the synth
|
2015-06-14 03:15:57 +00:00
|
|
|
* see defaults below
|
2015-02-28 04:24:51 +00:00
|
|
|
* @example
|
2015-06-16 02:36:20 +00:00
|
|
|
* var synth = new Tone.AMSynth().toMaster();
|
|
|
|
* synth.triggerAttackRelease("C4", "4n");
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
|
|
|
Tone.AMSynth = function(options){
|
|
|
|
|
2017-04-26 03:22:34 +00:00
|
|
|
options = Tone.defaultArg(options, Tone.AMSynth.defaults);
|
2014-11-02 01:56:23 +00:00
|
|
|
Tone.Monophonic.call(this, options);
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:02:00 +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-11-02 01:56:23 +00:00
|
|
|
*/
|
2016-05-23 23:49:41 +00:00
|
|
|
this._carrier = new Tone.Synth();
|
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
|
2017-02-16 17:04:12 +00:00
|
|
|
* @type {Tone.AmplitudeEnvelope}
|
2016-01-30 20:53:40 +00:00
|
|
|
*/
|
|
|
|
this.envelope = this._carrier.envelope.set(options.envelope);
|
2014-11-02 01:56:23 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-15 05:02:00 +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
|
2016-01-30 20:53:40 +00:00
|
|
|
*/
|
2016-05-23 23:49:41 +00:00
|
|
|
this._modulator = new Tone.Synth();
|
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}
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
2016-01-30 20:53:40 +00:00
|
|
|
this.modulation = this._modulator.oscillator.set(options.modulation);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The modulator's envelope
|
2017-02-16 17:04:12 +00:00
|
|
|
* @type {Tone.AmplitudeEnvelope}
|
2016-01-30 20:53:40 +00:00
|
|
|
*/
|
|
|
|
this.modulationEnvelope = this._modulator.envelope.set(options.modulationEnvelope);
|
2014-11-02 01:56:23 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-14 03:15:57 +00:00
|
|
|
* The frequency.
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Frequency}
|
|
|
|
* @signal
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
2015-05-23 22:57:05 +00:00
|
|
|
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
|
2014-11-02 01:56:23 +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-11-02 01:56:23 +00:00
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* Harmonicity is the ratio between the two voices. A harmonicity of
|
2017-08-15 05:02:00 +00:00
|
|
|
* 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-11-02 01:56:23 +00:00
|
|
|
*/
|
2015-06-14 02:30:53 +00:00
|
|
|
this.harmonicity = new Tone.Multiply(options.harmonicity);
|
|
|
|
this.harmonicity.units = Tone.Type.Positive;
|
2014-11-02 01:56:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* convert the -1,1 output to 0,1
|
2014-12-08 16:03:10 +00:00
|
|
|
* @type {Tone.AudioToGain}
|
2014-11-02 01:56:23 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-12-08 16:03:10 +00:00
|
|
|
this._modulationScale = new Tone.AudioToGain();
|
2014-11-02 01:56:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the node where the modulation happens
|
2016-09-20 03:53:07 +00:00
|
|
|
* @type {Tone.Gain}
|
2014-11-02 01:56:23 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2016-09-20 03:53:07 +00:00
|
|
|
this._modulationNode = new Tone.Gain();
|
2014-11-02 01:56:23 +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);
|
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.chain(this._modulationScale, this._modulationNode.gain);
|
|
|
|
this._carrier.chain(this._modulationNode, this.output);
|
2016-05-15 00:10:31 +00:00
|
|
|
this._readOnly(["frequency", "harmonicity", "oscillator", "envelope", "modulation", "modulationEnvelope", "detune"]);
|
2014-11-02 01:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.AMSynth, Tone.Monophonic);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.AMSynth.defaults = {
|
|
|
|
"harmonicity" : 3,
|
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
|
|
|
|
},
|
2017-01-01 01:35:57 +00:00
|
|
|
"modulation" : {
|
2016-01-30 20:53:40 +00:00
|
|
|
"type" : "square"
|
2014-11-02 01:56:23 +00:00
|
|
|
},
|
2016-01-30 20:53:40 +00:00
|
|
|
"modulationEnvelope" : {
|
|
|
|
"attack" : 0.5,
|
|
|
|
"decay" : 0.0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5
|
2014-11-02 01:56:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger the attack portion of the note
|
2017-08-15 05:02:00 +00:00
|
|
|
*
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] the time the note will occur
|
|
|
|
* @param {NormalRange} [velocity=1] the velocity of the note
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.AMSynth} this
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
2015-06-14 02:47:07 +00:00
|
|
|
Tone.AMSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
|
2014-11-02 01:56:23 +00:00
|
|
|
//the port glide
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
//the envelopes
|
2016-01-30 20:53:40 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this.modulationEnvelope.triggerAttack(time, velocity);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-11-02 01:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger the release portion of the note
|
2017-08-15 05:02:00 +00:00
|
|
|
*
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] the time the note will release
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.AMSynth} this
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
2015-06-14 02:47:07 +00:00
|
|
|
Tone.AMSynth.prototype._triggerEnvelopeRelease = function(time){
|
2016-01-30 20:53:40 +00:00
|
|
|
this.envelope.triggerRelease(time);
|
|
|
|
this.modulationEnvelope.triggerRelease(time);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-11-02 01:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.AMSynth} this
|
2014-11-02 01:56:23 +00:00
|
|
|
*/
|
|
|
|
Tone.AMSynth.prototype.dispose = function(){
|
|
|
|
Tone.Monophonic.prototype.dispose.call(this);
|
2016-05-15 00:10:31 +00:00
|
|
|
this._writable(["frequency", "harmonicity", "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;
|
2014-11-03 16:43:42 +00:00
|
|
|
this.frequency.dispose();
|
2014-11-02 01:56:23 +00:00
|
|
|
this.frequency = null;
|
2016-05-15 00:10:31 +00:00
|
|
|
this.detune.dispose();
|
|
|
|
this.detune = null;
|
2015-06-14 02:30:53 +00:00
|
|
|
this.harmonicity.dispose();
|
|
|
|
this.harmonicity = null;
|
2014-11-03 16:43:42 +00:00
|
|
|
this._modulationScale.dispose();
|
2014-11-02 01:56:23 +00:00
|
|
|
this._modulationScale = null;
|
2016-09-20 03:53:07 +00:00
|
|
|
this._modulationNode.dispose();
|
2014-11-03 16:43:42 +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-11-02 01:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.AMSynth;
|
2017-01-01 01:35:57 +00:00
|
|
|
});
|