Tone.js/Tone/instrument/SimpleSynth.js

119 lines
3 KiB
JavaScript
Raw Normal View History

2015-05-23 22:26:16 +00:00
define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/source/OmniOscillator", "Tone/signal/Signal", "Tone/instrument/Monophonic"],
function(Tone){
"use strict";
/**
2015-06-15 15:27:13 +00:00
* @class Tone.SimpleSynth is composed simply of a Tone.OmniOscillator
* routed through a Tone.AmplitudeEnvelope.
2015-06-22 05:20:20 +00:00
* <img src="https://docs.google.com/drawings/d/1-1_0YW2Z1J2EPI36P8fNCMcZG7N1w1GZluPs4og4evo/pub?w=1163&h=231">
2015-05-23 22:26:16 +00:00
*
* @constructor
* @extends {Tone.Monophonic}
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options available for the synth
2015-05-23 22:26:16 +00:00
* see defaults below
2015-06-16 02:36:20 +00:00
* @example
* var synth = new Tone.SimpleSynth().toMaster();
* synth.triggerAttackRelease("C4", "8n");
2015-05-23 22:26:16 +00:00
*/
Tone.SimpleSynth = function(options){
//get the defaults
options = this.defaultArg(options, Tone.SimpleSynth.defaults);
Tone.Monophonic.call(this, options);
/**
2015-06-14 03:15:57 +00:00
* The oscillator.
2015-05-23 22:26:16 +00:00
* @type {Tone.OmniOscillator}
*/
this.oscillator = new Tone.OmniOscillator(options.oscillator);
/**
2015-06-14 03:15:57 +00:00
* The frequency control.
2015-06-13 23:50:39 +00:00
* @type {Frequency}
* @signal
2015-05-23 22:26:16 +00:00
*/
this.frequency = this.oscillator.frequency;
/**
2015-06-14 03:15:57 +00:00
* The detune control.
2015-06-13 23:50:39 +00:00
* @type {Cents}
* @signal
2015-05-23 22:26:16 +00:00
*/
this.detune = this.oscillator.detune;
/**
2015-06-14 03:15:57 +00:00
* The amplitude envelope.
2015-06-15 15:27:13 +00:00
* @type {Tone.AmplitudeEnvelope}
2015-05-23 22:26:16 +00:00
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
//connect the oscillators to the output
this.oscillator.chain(this.envelope, this.output);
//start the oscillators
this.oscillator.start();
this._readOnly(["oscillator", "frequency", "detune", "envelope"]);
};
Tone.extend(Tone.SimpleSynth, Tone.Monophonic);
/**
* @const
* @static
* @type {Object}
*/
Tone.SimpleSynth.defaults = {
"oscillator" : {
"type" : "triangle"
},
"envelope" : {
"attack" : 0.005,
"decay" : 0.1,
"sustain" : 0.3,
"release" : 1
}
};
/**
* 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
2015-05-23 22:26:16 +00:00
* @param {number} [velocity=1] the velocity of the note (0-1)
* @returns {Tone.SimpleSynth} this
* @private
2015-05-23 22:26:16 +00:00
*/
Tone.SimpleSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
2015-05-23 22:26:16 +00:00
//the envelopes
this.envelope.triggerAttack(time, velocity);
return this;
};
/**
* 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.SimpleSynth} this
* @private
2015-05-23 22:26:16 +00:00
*/
Tone.SimpleSynth.prototype._triggerEnvelopeRelease = function(time){
2015-05-23 22:26:16 +00:00
this.envelope.triggerRelease(time);
return this;
};
/**
* clean up
* @returns {Tone.SimpleSynth} this
2015-05-23 22:26:16 +00:00
*/
Tone.SimpleSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
this._writable(["oscillator", "frequency", "detune", "envelope"]);
this.oscillator.dispose();
this.oscillator = null;
this.envelope.dispose();
this.envelope = null;
this.frequency = null;
this.detune = null;
return this;
};
return Tone.SimpleSynth;
});