Tone.js/Tone/instrument/MonoSynth.js

153 lines
3.7 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/component/ScaledEnvelope",
"Tone/source/OmniOscillator", "Tone/signal/Signal", "Tone/component/Filter", "Tone/instrument/Monophonic"],
2014-08-24 16:26:04 +00:00
function(Tone){
"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.
* The amplitude of the Tone.Oscillator and the cutoff frequency of the
* Tone.Filter are controlled by Tone.Envelopes.
2015-06-14 00:20:36 +00:00
*
2014-08-21 04:59:11 +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
2014-08-21 04:59:11 +00:00
*/
Tone.MonoSynth = function(options){
//get the defaults
2014-09-05 15:32:33 +00:00
options = this.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);
2014-08-21 04:59:11 +00:00
/**
2015-06-14 03:15:57 +00:00
* The filter envelope.
2015-06-15 15:27:13 +00:00
* @type {Tone.ScaledEnvelope}
2014-08-21 04:59:11 +00:00
*/
this.filterEnvelope = new Tone.ScaledEnvelope(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);
2014-08-21 04:59:11 +00:00
//start the oscillators
2014-09-02 04:25:38 +00:00
this.oscillator.start();
//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,
2014-09-12 00:36:17 +00:00
"min" : 20,
"max" : 4000,
"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){
2014-08-21 04:59:11 +00:00
//the envelopes
this.envelope.triggerAttack(time, velocity);
2015-02-02 18:30:36 +00:00
this.filterEnvelope.triggerAttack(time);
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);
2015-02-02 18:30:36 +00:00
return this;
2014-08-21 04:59:11 +00:00
};
2014-08-24 16:26:04 +00:00
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;
});