2014-10-28 18:51:22 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/source/OmniOscillator",
|
2014-09-03 23:05:09 +00:00
|
|
|
"Tone/signal/Signal", "Tone/component/Filter", "Tone/signal/Add", "Tone/instrument/Monophonic"],
|
2014-08-24 16:26:04 +00:00
|
|
|
function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
2014-08-21 04:59:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class the MonoSynth is a single oscillator, monophonic synthesizer
|
2014-09-03 23:05:09 +00:00
|
|
|
* with a filter, and two envelopes (on the filter and the amplitude)
|
2014-08-21 04:59:11 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-09-03 23:05:09 +00:00
|
|
|
* @extends {Tone.Monophonic}
|
2014-08-21 04:59:11 +00:00
|
|
|
* @param {Object} options the options available for the synth
|
2014-08-25 14:23:37 +00:00
|
|
|
* 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);
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.call(this, options);
|
2014-08-21 04:59:11 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-02 04:25:38 +00:00
|
|
|
* the first oscillator
|
2014-10-01 02:49:48 +00:00
|
|
|
* @type {Tone.OmniOscillator}
|
2014-08-26 01:19:18 +00:00
|
|
|
*/
|
2014-10-01 02:49:48 +00:00
|
|
|
this.oscillator = new Tone.OmniOscillator(options.oscillator);
|
2014-08-26 01:19:18 +00:00
|
|
|
|
2014-08-21 04:59:11 +00:00
|
|
|
/**
|
2014-09-02 04:25:38 +00:00
|
|
|
* the frequency control signal
|
|
|
|
* @type {Tone.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
|
|
|
|
|
|
|
/**
|
2014-09-02 04:25:38 +00:00
|
|
|
* the detune control signal
|
|
|
|
* @type {Tone.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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
|
|
|
* the filter envelope
|
|
|
|
* @type {Tone.Envelope}
|
|
|
|
*/
|
2014-08-24 16:26:04 +00:00
|
|
|
this.filterEnvelope = new Tone.Envelope(options.filterEnvelope);
|
2014-08-21 04:59:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the amplitude envelope
|
|
|
|
* @type {Tone.Envelope}
|
|
|
|
*/
|
2014-10-28 18:51:22 +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-10-28 18:51:22 +00:00
|
|
|
this.chain(this.oscillator, 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();
|
2014-10-28 18:51:22 +00:00
|
|
|
//connect the filter envelope
|
2014-09-02 04:25:38 +00:00
|
|
|
this.filterEnvelope.connect(this.filter.frequency);
|
2014-08-21 04:59:11 +00:00
|
|
|
};
|
|
|
|
|
2014-09-03 23:05:09 +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 = {
|
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,
|
2014-08-24 16:26:04 +00:00
|
|
|
"max" : 4000
|
|
|
|
}
|
2014-08-21 04:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the attack portion of the envelope
|
|
|
|
* @param {Tone.Time=} [time=now] the time the attack should start
|
2014-09-03 23:05:09 +00:00
|
|
|
* @param {number=} velocity the velocity of the note (0-1)
|
2014-08-21 04:59:11 +00:00
|
|
|
*/
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.MonoSynth.prototype.triggerEnvelopeAttack = function(time, velocity){
|
2014-08-21 04:59:11 +00:00
|
|
|
//the envelopes
|
2014-09-03 23:05:09 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this.filterEnvelope.triggerAttack(time);
|
2014-08-21 04:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the release portion of the envelope
|
|
|
|
* @param {Tone.Time=} [time=now] the time the release should start
|
|
|
|
*/
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.MonoSynth.prototype.triggerEnvelopeRelease = function(time){
|
2014-08-21 04:59:11 +00:00
|
|
|
this.envelope.triggerRelease(time);
|
2014-09-03 21:31:29 +00:00
|
|
|
this.filterEnvelope.triggerRelease(time);
|
2014-08-21 04:59:11 +00:00
|
|
|
};
|
|
|
|
|
2014-08-24 16:26:04 +00:00
|
|
|
/**
|
|
|
|
* set the oscillator type
|
|
|
|
* @param {string} oscType the type of oscillator
|
|
|
|
*/
|
2014-09-02 04:25:38 +00:00
|
|
|
Tone.MonoSynth.prototype.setOscType = function(type){
|
|
|
|
this.oscillator.setType(type);
|
2014-08-24 16:26:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the members at once
|
|
|
|
* @param {Object} params all of the parameters as an object.
|
|
|
|
* params for envelope and filterEnvelope
|
|
|
|
* should be nested objects.
|
|
|
|
*/
|
|
|
|
Tone.MonoSynth.prototype.set = function(params){
|
2014-09-02 16:10:43 +00:00
|
|
|
if (!this.isUndef(params.detune)) this.detune.setValue(params.detune);
|
2014-10-01 02:49:48 +00:00
|
|
|
if (!this.isUndef(params.oscillator)) this.oscillator.set(params.oscillator);
|
2014-08-24 16:26:04 +00:00
|
|
|
if (!this.isUndef(params.filterEnvelope)) this.filterEnvelope.set(params.filterEnvelope);
|
|
|
|
if (!this.isUndef(params.envelope)) this.envelope.set(params.envelope);
|
2014-09-02 04:25:38 +00:00
|
|
|
if (!this.isUndef(params.filter)) this.filter.set(params.filter);
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.prototype.set.call(this, params);
|
2014-08-24 16:26:04 +00:00
|
|
|
};
|
|
|
|
|
2014-08-21 04:59:11 +00:00
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
|
|
|
Tone.MonoSynth.prototype.dispose = function(){
|
2014-09-03 23:05:09 +00:00
|
|
|
Tone.Monophonic.prototype.dispose.call(this);
|
2014-09-02 04:25:38 +00:00
|
|
|
this.oscillator.dispose();
|
2014-10-28 18:51:22 +00:00
|
|
|
this.oscillator = null;
|
2014-08-21 04:59:11 +00:00
|
|
|
this.envelope.dispose();
|
2014-10-28 18:51:22 +00:00
|
|
|
this.envelope = null;
|
2014-08-21 04:59:11 +00:00
|
|
|
this.filterEnvelope.dispose();
|
|
|
|
this.filterEnvelope = null;
|
2014-10-28 18:51:22 +00:00
|
|
|
this.filter.dispose();
|
2014-09-02 04:25:38 +00:00
|
|
|
this.filter = null;
|
2014-08-26 01:19:18 +00:00
|
|
|
this.detune = null;
|
2014-09-09 19:30:36 +00:00
|
|
|
this.frequency = null;
|
|
|
|
this.detune = null;
|
2014-08-21 04:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.MonoSynth;
|
|
|
|
});
|