Tone.js/Tone/instrument/NoiseSynth.js

154 lines
4.2 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/component/AmplitudeEnvelope", "Tone/component/FrequencyEnvelope",
2014-11-30 03:03:28 +00:00
"Tone/source/Noise", "Tone/signal/Signal", "Tone/component/Filter", "Tone/instrument/Instrument"],
function(Tone){
"use strict";
/**
2015-06-20 22:03:49 +00:00
* @class Tone.NoiseSynth is composed of a noise generator (Tone.Noise), one filter (Tone.Filter),
* and two envelopes (Tone.Envelop). One envelope controls the amplitude
* of the noise and the other is controls the cutoff frequency of the filter.
* <img src="https://docs.google.com/drawings/d/1rqzuX9rBlhT50MRvD2TKml9bnZhcZmzXF1rf_o7vdnE/pub?w=918&h=242">
2014-11-30 03:03:28 +00:00
*
* @constructor
* @extends {Tone.Instrument}
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options available for the synth
2014-11-30 03:03:28 +00:00
* see defaults below
2015-02-28 04:24:51 +00:00
* @example
2015-06-16 02:36:20 +00:00
* var noiseSynth = new Tone.NoiseSynth().toMaster();
* noiseSynth.triggerAttackRelease("8n");
2014-11-30 03:03:28 +00:00
*/
Tone.NoiseSynth = function(options){
//get the defaults
options = this.defaultArg(options, Tone.NoiseSynth.defaults);
Tone.Instrument.call(this, options);
2014-11-30 03:03:28 +00:00
/**
2015-06-20 22:03:49 +00:00
* The noise source.
2014-11-30 03:03:28 +00:00
* @type {Tone.Noise}
2015-06-20 22:03:49 +00:00
* @example
* noiseSynth.set("noise.type", "brown");
2014-11-30 03:03:28 +00:00
*/
this.noise = new Tone.Noise();
/**
2015-06-20 22:03:49 +00:00
* The filter.
2014-11-30 03:03:28 +00:00
* @type {Tone.Filter}
*/
this.filter = new Tone.Filter(options.filter);
/**
2015-02-10 16:40:04 +00:00
* The filter envelope.
* @type {Tone.FrequencyEnvelope}
2014-11-30 03:03:28 +00:00
*/
this.filterEnvelope = new Tone.FrequencyEnvelope(options.filterEnvelope);
2014-11-30 03:03:28 +00:00
/**
2015-02-10 16:40:04 +00:00
* The amplitude envelope.
2015-06-15 15:27:13 +00:00
* @type {Tone.AmplitudeEnvelope}
2014-11-30 03:03:28 +00:00
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
//connect the noise to the output
2014-12-01 02:32:09 +00:00
this.noise.chain(this.filter, this.envelope, this.output);
2014-11-30 03:03:28 +00:00
//start the noise
this.noise.start();
//connect the filter envelope
this.filterEnvelope.connect(this.filter.frequency);
2015-04-18 14:54:08 +00:00
this._readOnly(["noise", "filter", "filterEnvelope", "envelope"]);
2014-11-30 03:03:28 +00:00
};
Tone.extend(Tone.NoiseSynth, Tone.Instrument);
/**
* @const
* @static
* @type {Object}
*/
Tone.NoiseSynth.defaults = {
"noise" : {
"type" : "white"
},
"filter" : {
"Q" : 6,
"type" : "highpass",
"rolloff" : -24
},
"envelope" : {
"attack" : 0.005,
"decay" : 0.1,
"sustain" : 0.0,
},
"filterEnvelope" : {
"attack" : 0.06,
"decay" : 0.2,
"sustain" : 0,
"release" : 2,
"baseFrequency" : 20,
"octaves" : 5,
2014-11-30 03:03:28 +00:00
}
};
/**
2015-06-20 22:03:49 +00:00
* Start the attack portion of the envelopes. Unlike other
* instruments, Tone.NoiseSynth doesn't have a note.
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the attack should start
2014-12-02 06:42:08 +00:00
* @param {number} [velocity=1] the velocity of the note (0-1)
* @returns {Tone.NoiseSynth} this
2015-06-20 22:03:49 +00:00
* @example
* noiseSynth.triggerAttack();
2014-11-30 03:03:28 +00:00
*/
Tone.NoiseSynth.prototype.triggerAttack = function(time, velocity){
//the envelopes
this.envelope.triggerAttack(time, velocity);
2015-02-02 18:30:36 +00:00
this.filterEnvelope.triggerAttack(time);
return this;
2014-11-30 03:03:28 +00:00
};
/**
2015-06-20 22:03:49 +00:00
* Start the release portion of the envelopes.
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the release should start
* @returns {Tone.NoiseSynth} this
2014-11-30 03:03:28 +00:00
*/
Tone.NoiseSynth.prototype.triggerRelease = function(time){
this.envelope.triggerRelease(time);
this.filterEnvelope.triggerRelease(time);
2015-02-02 18:30:36 +00:00
return this;
2014-11-30 03:03:28 +00:00
};
2014-11-30 19:53:54 +00:00
/**
2015-06-20 22:03:49 +00:00
* Trigger the attack and then the release.
2015-06-14 00:20:36 +00:00
* @param {Time} duration the duration of the note
* @param {Time} [time=now] the time of the attack
2014-12-02 06:42:08 +00:00
* @param {number} [velocity=1] the velocity
* @returns {Tone.NoiseSynth} this
2014-11-30 19:53:54 +00:00
*/
Tone.NoiseSynth.prototype.triggerAttackRelease = function(duration, time, velocity){
time = this.toSeconds(time);
duration = this.toSeconds(duration);
this.triggerAttack(time, velocity);
this.triggerRelease(time + duration);
2015-02-02 18:30:36 +00:00
return this;
2014-11-30 19:53:54 +00:00
};
2014-11-30 03:03:28 +00:00
/**
2015-06-20 22:03:49 +00:00
* Clean up.
* @returns {Tone.NoiseSynth} this
2014-11-30 03:03:28 +00:00
*/
Tone.NoiseSynth.prototype.dispose = function(){
Tone.Instrument.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["noise", "filter", "filterEnvelope", "envelope"]);
2014-11-30 03:03:28 +00:00
this.noise.dispose();
this.noise = null;
this.envelope.dispose();
this.envelope = null;
this.filterEnvelope.dispose();
this.filterEnvelope = null;
this.filter.dispose();
this.filter = null;
2015-02-02 18:30:36 +00:00
return this;
2014-11-30 03:03:28 +00:00
};
return Tone.NoiseSynth;
});