Tone.js/Tone/instrument/MembraneSynth.js

124 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-10-20 15:45:28 +00:00
define(["Tone/core/Tone", "Tone/source/OmniOscillator", "Tone/instrument/Instrument",
2015-04-20 14:39:47 +00:00
"Tone/component/AmplitudeEnvelope"],
function(Tone){
"use strict";
/**
* @class Tone.MembraneSynth makes kick and tom sounds using a single oscillator
2016-10-20 15:45:28 +00:00
* with an amplitude envelope and frequency ramp. A Tone.OmniOscillator
2015-06-20 22:03:49 +00:00
* is routed through a Tone.AmplitudeEnvelope to the output. The drum
* quality of the sound comes from the frequency envelope applied
* during during Tone.MembraneSynth.triggerAttack(note). The frequency
* envelope starts at <code>note * .octaves</code> and ramps to
* <code>note</code> over the duration of <code>.pitchDecay</code>.
2015-04-20 14:39:47 +00:00
*
* @constructor
* @extends {Tone.Instrument}
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options available for the synth
2015-04-20 14:39:47 +00:00
* see defaults below
* @example
* var synth = new Tone.MembraneSynth().toMaster();
2015-06-16 02:36:20 +00:00
* synth.triggerAttackRelease("C2", "8n");
2015-04-20 14:39:47 +00:00
*/
Tone.MembraneSynth = function(options){
2015-04-20 14:39:47 +00:00
options = Tone.defaultArg(options, Tone.MembraneSynth.defaults);
2015-04-20 14:39:47 +00:00
Tone.Instrument.call(this, options);
/**
* The oscillator.
2016-10-20 15:45:28 +00:00
* @type {Tone.OmniOscillator}
2015-04-20 14:39:47 +00:00
*/
2016-10-20 15:45:28 +00:00
this.oscillator = new Tone.OmniOscillator(options.oscillator).start();
2015-04-20 14:39:47 +00:00
/**
2015-06-20 22:03:49 +00:00
* The amplitude envelope.
2015-04-20 14:39:47 +00:00
* @type {Tone.AmplitudeEnvelope}
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
/**
* The number of octaves the pitch envelope ramps.
2015-06-14 00:20:36 +00:00
* @type {Positive}
2015-04-20 14:39:47 +00:00
*/
this.octaves = options.octaves;
/**
2015-06-20 22:03:49 +00:00
* The amount of time the frequency envelope takes.
2015-06-14 00:20:36 +00:00
* @type {Time}
2015-04-20 14:39:47 +00:00
*/
this.pitchDecay = options.pitchDecay;
this.oscillator.chain(this.envelope, this.output);
this._readOnly(["oscillator", "envelope"]);
};
Tone.extend(Tone.MembraneSynth, Tone.Instrument);
2015-04-20 14:39:47 +00:00
/**
* @static
* @type {Object}
*/
Tone.MembraneSynth.defaults = {
2015-04-20 14:39:47 +00:00
"pitchDecay" : 0.05,
"octaves" : 10,
"oscillator" : {
"type" : "sine",
},
"envelope" : {
"attack" : 0.001,
"decay" : 0.4,
"sustain" : 0.01,
"release" : 1.4,
"attackCurve" : "exponential"
}
};
/**
2015-06-20 22:03:49 +00:00
* Trigger the note at the given time with the given velocity.
2015-04-20 14:39:47 +00:00
*
* @param {Frequency} note the note
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time, if not given is now
2015-04-20 14:39:47 +00:00
* @param {number} [velocity=1] velocity defaults to 1
* @returns {Tone.MembraneSynth} this
2015-04-20 14:39:47 +00:00
* @example
* kick.triggerAttack(60);
*/
Tone.MembraneSynth.prototype.triggerAttack = function(note, time, velocity) {
2015-04-20 14:39:47 +00:00
time = this.toSeconds(time);
note = this.toFrequency(note);
var maxNote = note * this.octaves;
this.oscillator.frequency.setValueAtTime(maxNote, time);
this.oscillator.frequency.exponentialRampToValueAtTime(note, time + this.toSeconds(this.pitchDecay));
this.envelope.triggerAttack(time, velocity);
return this;
};
/**
2015-06-20 22:03:49 +00:00
* Trigger the release portion of the note.
2015-04-20 14:39:47 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the note will release
* @returns {Tone.MembraneSynth} this
2015-04-20 14:39:47 +00:00
*/
Tone.MembraneSynth.prototype.triggerRelease = function(time){
2015-04-20 14:39:47 +00:00
this.envelope.triggerRelease(time);
return this;
};
/**
2015-06-20 22:03:49 +00:00
* Clean up.
* @returns {Tone.MembraneSynth} this
2015-04-20 14:39:47 +00:00
*/
Tone.MembraneSynth.prototype.dispose = function(){
2015-04-20 14:39:47 +00:00
Tone.Instrument.prototype.dispose.call(this);
this._writable(["oscillator", "envelope"]);
this.oscillator.dispose();
this.oscillator = null;
this.envelope.dispose();
this.envelope = null;
return this;
};
return Tone.MembraneSynth;
2015-04-20 14:39:47 +00:00
});