Tone.js/Tone/instrument/MonoSynth.js

168 lines
4.3 KiB
JavaScript
Raw Normal View History

define(["../core/Tone", "../component/AmplitudeEnvelope", "../component/FrequencyEnvelope", "../source/OmniOscillator",
"../signal/Signal", "../component/Filter", "../instrument/Monophonic"], function(Tone){
2014-08-24 16:26:04 +00:00
"use strict";
2014-08-21 04:59:11 +00:00
/**
2015-06-14 04:32:17 +00:00
* @class Tone.MonoSynth is composed of one oscillator, one filter, and two envelopes.
2017-10-21 23:02:46 +00:00
* The amplitude of the Tone.Oscillator and the cutoff frequency of the
* Tone.Filter are controlled by Tone.Envelopes.
2015-06-20 22:03:49 +00:00
* <img src="https://docs.google.com/drawings/d/1gaY1DF9_Hzkodqf8JI1Cg2VZfwSElpFQfI94IQwad38/pub?w=924&h=240">
2017-10-21 23:02:46 +00:00
*
2014-08-21 04:59:11 +00:00
* @constructor
* @extends {Tone.Monophonic}
2017-10-21 23:02:46 +00:00
* @param {Object} [options] the options available for the synth
* see defaults below
2015-06-16 02:36:20 +00:00
* @example
2015-06-20 22:03:49 +00:00
* var synth = new Tone.MonoSynth({
* "oscillator" : {
* "type" : "square"
* },
* "envelope" : {
* "attack" : 0.1
* }
* }).toMaster();
2015-06-16 02:36:20 +00:00
* synth.triggerAttackRelease("C4", "8n");
2014-08-21 04:59:11 +00:00
*/
Tone.MonoSynth = function(options){
//get the defaults
options = Tone.defaultArg(options, Tone.MonoSynth.defaults);
Tone.Monophonic.call(this, options);
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The oscillator.
2014-10-01 02:49:48 +00:00
* @type {Tone.OmniOscillator}
*/
2014-10-01 02:49:48 +00:00
this.oscillator = new Tone.OmniOscillator(options.oscillator);
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The frequency control.
2015-06-14 00:20:36 +00:00
* @type {Frequency}
* @signal
2014-08-21 04:59:11 +00:00
*/
2014-09-02 04:25:38 +00:00
this.frequency = this.oscillator.frequency;
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The detune control.
2015-06-14 00:20:36 +00:00
* @type {Cents}
* @signal
2014-08-21 04:59:11 +00:00
*/
2014-09-02 04:25:38 +00:00
this.detune = this.oscillator.detune;
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The filter.
2014-08-24 16:26:04 +00:00
* @type {Tone.Filter}
2014-08-21 04:59:11 +00:00
*/
2014-09-02 04:25:38 +00:00
this.filter = new Tone.Filter(options.filter);
this.filter.frequency.value = 5000;
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The filter envelope.
* @type {Tone.FrequencyEnvelope}
2014-08-21 04:59:11 +00:00
*/
this.filterEnvelope = new Tone.FrequencyEnvelope(options.filterEnvelope);
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The amplitude envelope.
2015-06-15 15:27:13 +00:00
* @type {Tone.AmplitudeEnvelope}
2014-08-21 04:59:11 +00:00
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
2014-09-02 04:25:38 +00:00
2014-08-21 04:59:11 +00:00
//connect the oscillators to the output
2014-12-01 02:32:09 +00:00
this.oscillator.chain(this.filter, this.envelope, this.output);
//connect the filter envelope
2014-09-02 04:25:38 +00:00
this.filterEnvelope.connect(this.filter.frequency);
2015-04-18 14:54:08 +00:00
this._readOnly(["oscillator", "frequency", "detune", "filter", "filterEnvelope", "envelope"]);
2014-08-21 04:59:11 +00:00
};
Tone.extend(Tone.MonoSynth, Tone.Monophonic);
2014-08-21 04:59:11 +00:00
/**
2014-09-04 18:04:58 +00:00
* @const
2014-08-21 04:59:11 +00:00
* @static
2014-09-06 19:56:23 +00:00
* @type {Object}
2014-08-21 04:59:11 +00:00
*/
2014-09-05 15:32:33 +00:00
Tone.MonoSynth.defaults = {
2015-05-13 03:48:13 +00:00
"frequency" : "C4",
"detune" : 0,
2014-10-01 02:49:48 +00:00
"oscillator" : {
"type" : "square"
},
2014-08-24 16:26:04 +00:00
"filter" : {
"Q" : 6,
2014-09-02 04:25:38 +00:00
"type" : "lowpass",
"rolloff" : -24
2014-08-24 16:26:04 +00:00
},
"envelope" : {
"attack" : 0.005,
"decay" : 0.1,
"sustain" : 0.9,
"release" : 1
},
"filterEnvelope" : {
"attack" : 0.06,
"decay" : 0.2,
"sustain" : 0.5,
"release" : 2,
"baseFrequency" : 200,
"octaves" : 7,
"exponent" : 2
2014-08-24 16:26:04 +00:00
}
2014-08-21 04:59:11 +00:00
};
/**
* start the attack portion of the envelope
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.MonoSynth} this
* @private
2014-08-21 04:59:11 +00:00
*/
Tone.MonoSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
time = this.toSeconds(time);
2014-08-21 04:59:11 +00:00
//the envelopes
this.envelope.triggerAttack(time, velocity);
2017-10-21 23:02:46 +00:00
this.filterEnvelope.triggerAttack(time);
this.oscillator.start(time);
if (this.envelope.sustain === 0){
this.oscillator.stop(time + this.envelope.attack + this.envelope.decay);
}
2017-10-21 23:02:46 +00:00
return this;
2014-08-21 04:59:11 +00:00
};
/**
* start the release portion of the envelope
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the release should start
* @returns {Tone.MonoSynth} this
* @private
2014-08-21 04:59:11 +00:00
*/
Tone.MonoSynth.prototype._triggerEnvelopeRelease = function(time){
2014-08-21 04:59:11 +00:00
this.envelope.triggerRelease(time);
this.filterEnvelope.triggerRelease(time);
this.oscillator.stop(time + this.envelope.release);
2015-02-02 18:30:36 +00:00
return this;
2014-08-21 04:59:11 +00:00
};
/**
* clean up
* @returns {Tone.MonoSynth} this
2014-08-21 04:59:11 +00:00
*/
Tone.MonoSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["oscillator", "frequency", "detune", "filter", "filterEnvelope", "envelope"]);
2014-09-02 04:25:38 +00:00
this.oscillator.dispose();
this.oscillator = null;
2014-08-21 04:59:11 +00:00
this.envelope.dispose();
this.envelope = null;
2014-08-21 04:59:11 +00:00
this.filterEnvelope.dispose();
this.filterEnvelope = null;
this.filter.dispose();
2014-09-02 04:25:38 +00:00
this.filter = null;
this.frequency = null;
this.detune = null;
2015-02-02 18:30:36 +00:00
return this;
2014-08-21 04:59:11 +00:00
};
return Tone.MonoSynth;
2017-10-21 23:02:46 +00:00
});