Tone.js/Tone/instrument/MonoSynth.js

160 lines
4.1 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/source/OmniOscillator",
"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
* with a filter, and two envelopes (on the filter and the amplitude)
2014-08-21 04:59:11 +00:00
*
* @constructor
* @extends {Tone.Monophonic}
2014-08-21 04:59:11 +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
/**
2014-09-02 04:25:38 +00:00
* the first 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
/**
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}
*/
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
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();
//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
};
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
* @param {number=} velocity the velocity of the note (0-1)
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);
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
*/
Tone.MonoSynth.prototype.triggerEnvelopeRelease = function(time){
2014-08-21 04:59:11 +00:00
this.envelope.triggerRelease(time);
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);
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(){
Tone.Monophonic.prototype.dispose.call(this);
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.detune = null;
this.frequency = null;
this.detune = null;
2014-08-21 04:59:11 +00:00
};
return Tone.MonoSynth;
});