Tone.js/Tone/instrument/Monophonic.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-09-20 23:23:28 +00:00
define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/signal/Signal"], function(Tone){
"use strict";
/**
* @class This is an abstract base class for other monophonic instruments to
* extend. IMPORTANT: It does not make any sound on its own and
* shouldn't be directly instantiated.
*
* @constructor
* @abstract
2014-09-24 02:07:16 +00:00
* @extends {Tone.Instrument}
*/
Tone.Monophonic = function(options){
//get the defaults
options = Tone.defaultArg(options, Tone.Monophonic.defaults);
Tone.Instrument.call(this, options);
/**
2015-02-10 16:40:04 +00:00
* The glide time between notes.
2015-06-14 00:20:36 +00:00
* @type {Time}
*/
this.portamento = options.portamento;
};
2014-09-20 23:23:28 +00:00
Tone.extend(Tone.Monophonic, Tone.Instrument);
/**
* @static
* @const
* @type {Object}
*/
Tone.Monophonic.defaults = {
"portamento" : 0
};
/**
2015-06-20 19:50:57 +00:00
* Trigger the attack of the note optionally with a given velocity.
*
2015-06-20 19:50:57 +00:00
*
* @param {Frequency} note The note to trigger.
* @param {Time} [time=now] When the note should start.
* @param {number} [velocity=1] velocity The velocity scaler
* determines how "loud" the note
* will be triggered.
* @returns {Tone.Monophonic} this
2015-06-14 03:15:57 +00:00
* @example
* synth.triggerAttack("C4");
2015-06-20 19:50:57 +00:00
* @example
* //trigger the note a half second from now at half velocity
* synth.triggerAttack("C4", "+0.5", 0.5);
*/
2018-03-05 17:25:33 +00:00
Tone.Monophonic.prototype.triggerAttack = function(note, time, velocity){
time = this.toSeconds(time);
this._triggerEnvelopeAttack(time, velocity);
this.setNote(note, time);
2015-02-02 18:30:36 +00:00
return this;
};
/**
2015-06-14 03:15:57 +00:00
* Trigger the release portion of the envelope
2015-06-20 19:50:57 +00:00
* @param {Time} [time=now] If no time is given, the release happens immediatly
* @returns {Tone.Monophonic} this
2015-06-14 03:15:57 +00:00
* @example
* synth.triggerRelease();
*/
Tone.Monophonic.prototype.triggerRelease = function(time){
time = this.toSeconds(time);
this._triggerEnvelopeRelease(time);
2015-02-02 18:30:36 +00:00
return this;
};
/**
* override this method with the actual method
* @abstract
* @private
*/
2018-03-05 17:25:33 +00:00
Tone.Monophonic.prototype._triggerEnvelopeAttack = function(){};
/**
* override this method with the actual method
* @abstract
* @private
*/
2018-03-05 17:25:33 +00:00
Tone.Monophonic.prototype._triggerEnvelopeRelease = function(){};
/**
2015-06-20 19:50:57 +00:00
* Set the note at the given time. If no time is given, the note
* will set immediately.
* @param {Frequency} note The note to change to.
2015-06-14 03:15:57 +00:00
* @param {Time} [time=now] The time when the note should be set.
* @returns {Tone.Monophonic} this
2015-06-20 19:50:57 +00:00
* @example
* //change to F#6 in one quarter note from now.
* synth.setNote("F#6", "+4n");
* @example
* //change to Bb4 right now
* synth.setNote("Bb4");
*/
Tone.Monophonic.prototype.setNote = function(note, time){
time = this.toSeconds(time);
if (this.portamento > 0 && this.envelope.getValueAtTime(time) > 0.05){
2015-02-10 16:40:04 +00:00
var portTime = this.toSeconds(this.portamento);
this.frequency.exponentialRampTo(note, portTime, time);
} else {
this.frequency.setValueAtTime(note, time);
}
2015-02-02 18:30:36 +00:00
return this;
};
return Tone.Monophonic;
2017-10-26 20:02:01 +00:00
});