Tone.js/Tone/instrument/AMSynth.js

198 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-11-02 01:56:23 +00:00
define(["Tone/core/Tone", "Tone/instrument/MonoSynth", "Tone/signal/Signal", "Tone/signal/Multiply",
2014-12-08 16:03:10 +00:00
"Tone/instrument/Monophonic", "Tone/signal/AudioToGain"],
2014-11-02 01:56:23 +00:00
function(Tone){
"use strict";
/**
2015-06-14 04:32:17 +00:00
* @class AMSynth uses the output of one Tone.MonoSynth to modulate the
* amplitude of another Tone.MonoSynth. The harmonicity (the ratio between
* the two signals) affects the timbre of the output signal the most.
* Read more about Amplitude Modulation Synthesis on
* <a href="http://www.soundonsound.com/sos/mar00/articles/synthsecrets.htm" target="_blank">SoundOnSound</a>.
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}
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options available for the synth
* 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){
options = this.defaultArg(options, Tone.AMSynth.defaults);
Tone.Monophonic.call(this, options);
/**
2015-06-14 03:15:57 +00:00
* The carrier voice.
2014-11-02 01:56:23 +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-11-02 01:56:23 +00:00
/**
2015-06-14 03:15:57 +00:00
* The modulator voice.
2014-11-02 01:56:23 +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-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
*/
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
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
* 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
* @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);
2014-12-01 02:32:09 +00:00
this.modulator.chain(this._modulationScale, this._modulationNode.gain);
this.carrier.chain(this._modulationNode, this.output);
this._readOnly(["carrier", "modulator", "frequency", "harmonicity"]);
2014-11-02 01:56:23 +00:00
};
Tone.extend(Tone.AMSynth, Tone.Monophonic);
/**
* @static
* @type {Object}
*/
Tone.AMSynth.defaults = {
"harmonicity" : 3,
"carrier" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-11-02 01:56:23 +00:00
"oscillator" : {
"type" : "sine"
},
"envelope" : {
"attack" : 0.01,
"decay" : 0.01,
"sustain" : 1,
"release" : 0.5
},
"filterEnvelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5,
"min" : 20000,
"max" : 20000
2015-05-13 03:48:13 +00:00
},
"filter" : {
"Q" : 6,
"type" : "lowpass",
"rolloff" : -24
},
2014-11-02 01:56:23 +00:00
},
"modulator" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-11-02 01:56:23 +00:00
"oscillator" : {
"type" : "square"
},
"envelope" : {
"attack" : 2,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
},
"filterEnvelope" : {
"attack" : 4,
"decay" : 0.2,
"sustain" : 0.5,
"release" : 0.5,
"min" : 20,
"max" : 1500
2015-05-13 03:48:13 +00:00
},
"filter" : {
"Q" : 6,
"type" : "lowpass",
"rolloff" : -24
},
2014-11-02 01:56:23 +00:00
}
};
/**
* 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
* @param {NormalRange} [velocity=1] the velocity of the note
* @private
* @returns {Tone.AMSynth} this
2014-11-02 01:56:23 +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
this.carrier.envelope.triggerAttack(time, velocity);
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-11-02 01:56:23 +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
* @private
* @returns {Tone.AMSynth} this
2014-11-02 01:56:23 +00:00
*/
Tone.AMSynth.prototype._triggerEnvelopeRelease = function(time){
2014-11-02 01:56:23 +00:00
this.carrier.triggerRelease(time);
this.modulator.triggerRelease(time);
2015-02-02 18:30:36 +00:00
return this;
2014-11-02 01:56:23 +00:00
};
/**
* clean up
* @returns {Tone.AMSynth} this
2014-11-02 01:56:23 +00:00
*/
Tone.AMSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
this._writable(["carrier", "modulator", "frequency", "harmonicity"]);
2014-11-02 01:56:23 +00:00
this.carrier.dispose();
this.carrier = null;
2014-11-03 16:43:42 +00:00
this.modulator.dispose();
2014-11-02 01:56:23 +00:00
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;
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;
2014-11-03 16:43:42 +00:00
this._modulationNode.disconnect();
this._modulationNode = null;
2015-02-02 18:30:36 +00:00
return this;
2014-11-02 01:56:23 +00:00
};
return Tone.AMSynth;
});