2014-09-20 23:23:28 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/signal/Signal"], function(Tone){
|
2014-09-03 23:05:09 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2015-10-13 14:37:21 +00:00
|
|
|
* @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.
|
2014-09-03 23:05:09 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @abstract
|
2014-09-24 02:07:16 +00:00
|
|
|
* @extends {Tone.Instrument}
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic = function(options){
|
|
|
|
|
|
|
|
//get the defaults
|
2017-04-26 03:22:34 +00:00
|
|
|
options = Tone.defaultArg(options, Tone.Monophonic.defaults);
|
2015-06-27 21:25:47 +00:00
|
|
|
Tone.Instrument.call(this, options);
|
|
|
|
|
2014-09-03 23:05:09 +00:00
|
|
|
/**
|
2015-02-10 16:40:04 +00:00
|
|
|
* The glide time between notes.
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Time}
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
this.portamento = options.portamento;
|
|
|
|
};
|
|
|
|
|
2014-09-20 23:23:28 +00:00
|
|
|
Tone.extend(Tone.Monophonic, Tone.Instrument);
|
2014-09-03 23:05:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2014-09-03 23:05:09 +00:00
|
|
|
*
|
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.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @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);
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
2018-03-05 17:25:33 +00:00
|
|
|
Tone.Monophonic.prototype.triggerAttack = function(note, time, velocity){
|
2017-05-29 01:52:28 +00:00
|
|
|
time = this.toSeconds(time);
|
2015-06-14 02:47:07 +00:00
|
|
|
this._triggerEnvelopeAttack(time, velocity);
|
2014-09-03 23:05:09 +00:00
|
|
|
this.setNote(note, time);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-03 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Monophonic} this
|
2015-06-14 03:15:57 +00:00
|
|
|
* @example
|
|
|
|
* synth.triggerRelease();
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.triggerRelease = function(time){
|
2017-06-29 17:55:36 +00:00
|
|
|
time = this.toSeconds(time);
|
2015-06-14 02:47:07 +00:00
|
|
|
this._triggerEnvelopeRelease(time);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-03 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* override this method with the actual method
|
|
|
|
* @abstract
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
2018-03-05 17:25:33 +00:00
|
|
|
Tone.Monophonic.prototype._triggerEnvelopeAttack = function(){};
|
2014-09-03 23:05:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* override this method with the actual method
|
|
|
|
* @abstract
|
2015-06-14 02:47:07 +00:00
|
|
|
* @private
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
2018-03-05 17:25:33 +00:00
|
|
|
Tone.Monophonic.prototype._triggerEnvelopeRelease = function(){};
|
2014-09-03 23:05:09 +00:00
|
|
|
|
|
|
|
/**
|
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.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @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");
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.setNote = function(note, time){
|
|
|
|
time = this.toSeconds(time);
|
2018-03-06 04:56:42 +00:00
|
|
|
if (this.portamento > 0 && this.envelope.getValueAtTime(time) > 0.05){
|
2015-02-10 16:40:04 +00:00
|
|
|
var portTime = this.toSeconds(this.portamento);
|
2018-03-06 04:56:42 +00:00
|
|
|
this.frequency.exponentialRampTo(note, portTime, time);
|
2014-09-03 23:05:09 +00:00
|
|
|
} else {
|
|
|
|
this.frequency.setValueAtTime(note, time);
|
|
|
|
}
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-03 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Monophonic;
|
2017-10-26 20:02:01 +00:00
|
|
|
});
|