2017-05-08 15:49:08 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/type/Type", "Tone/core/Master"], function(Tone){
|
2014-09-17 04:19:30 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Base-class for all instruments
|
2017-08-27 21:50:31 +00:00
|
|
|
*
|
2014-09-17 04:19:30 +00:00
|
|
|
* @constructor
|
2017-08-27 21:50:31 +00:00
|
|
|
* @extends {Tone.AudioNode}
|
2014-09-17 04:19:30 +00:00
|
|
|
*/
|
2015-06-27 21:25:47 +00:00
|
|
|
Tone.Instrument = function(options){
|
|
|
|
|
|
|
|
//get the defaults
|
2017-04-26 03:22:34 +00:00
|
|
|
options = Tone.defaultArg(options, Tone.Instrument.defaults);
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.AudioNode.call(this);
|
2014-09-17 04:19:30 +00:00
|
|
|
|
2015-11-03 01:08:53 +00:00
|
|
|
/**
|
|
|
|
* The output and volume triming node
|
|
|
|
* @type {Tone.Volume}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._volume = this.output = new Tone.Volume(options.volume);
|
|
|
|
|
2015-02-10 16:40:04 +00:00
|
|
|
/**
|
2015-09-03 15:05:05 +00:00
|
|
|
* The volume of the output in decibels.
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Decibels}
|
|
|
|
* @signal
|
2015-09-03 15:05:05 +00:00
|
|
|
* @example
|
|
|
|
* source.volume.value = -6;
|
2015-02-10 16:40:04 +00:00
|
|
|
*/
|
2015-11-03 01:08:53 +00:00
|
|
|
this.volume = this._volume.volume;
|
2015-09-03 15:05:05 +00:00
|
|
|
this._readOnly("volume");
|
2018-01-02 21:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of all events scheduled to the transport
|
|
|
|
* when the instrument is 'synced'
|
|
|
|
* @type {Array<Number>}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._scheduledEvents = [];
|
2014-09-17 04:19:30 +00:00
|
|
|
};
|
|
|
|
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.extend(Tone.Instrument, Tone.AudioNode);
|
2014-09-17 04:19:30 +00:00
|
|
|
|
2015-05-23 22:16:34 +00:00
|
|
|
/**
|
|
|
|
* the default attributes
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
Tone.Instrument.defaults = {
|
|
|
|
/** the volume of the output in decibels */
|
|
|
|
"volume" : 0
|
|
|
|
};
|
|
|
|
|
2014-09-17 04:19:30 +00:00
|
|
|
/**
|
|
|
|
* @abstract
|
2014-09-20 22:57:44 +00:00
|
|
|
* @param {string|number} note the note to trigger
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] the time to trigger the ntoe
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {number} [velocity=1] the velocity to trigger the note
|
2014-09-17 04:19:30 +00:00
|
|
|
*/
|
2015-06-14 05:54:36 +00:00
|
|
|
Tone.Instrument.prototype.triggerAttack = Tone.noOp;
|
2014-09-17 04:19:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] when to trigger the release
|
2014-09-17 04:19:30 +00:00
|
|
|
*/
|
2015-06-14 05:54:36 +00:00
|
|
|
Tone.Instrument.prototype.triggerRelease = Tone.noOp;
|
2014-09-17 04:19:30 +00:00
|
|
|
|
2018-01-02 21:49:41 +00:00
|
|
|
/**
|
|
|
|
* Sync the instrument to the Transport. All subsequent calls of
|
|
|
|
* [triggerAttack](#triggerattack) and [triggerRelease](#triggerrelease)
|
|
|
|
* will be scheduled along the transport.
|
|
|
|
* @example
|
|
|
|
* instrument.sync()
|
|
|
|
* //schedule 3 notes when the transport first starts
|
|
|
|
* instrument.triggerAttackRelease('C4', '8n', 0)
|
|
|
|
* instrument.triggerAttackRelease('E4', '8n', '8n')
|
|
|
|
* instrument.triggerAttackRelease('G4', '8n', '4n')
|
|
|
|
* //start the transport to hear the notes
|
|
|
|
* Transport.start()
|
|
|
|
* @returns {Tone.Instrument} this
|
|
|
|
*/
|
|
|
|
Tone.Instrument.prototype.sync = function(){
|
|
|
|
this._syncMethod("triggerAttack", 1);
|
|
|
|
this._syncMethod("triggerRelease", 0);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the given method so that it can be synchronized
|
|
|
|
* @param {String} method Which method to wrap and sync
|
|
|
|
* @param {Number} timePosition What position the time argument appears in
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Instrument.prototype._syncMethod = function(method, timePosition){
|
|
|
|
var originalMethod = this["_original_"+method] = this[method];
|
|
|
|
this[method] = function(){
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var time = args[timePosition];
|
|
|
|
var id = Tone.Transport.schedule(function(t){
|
|
|
|
args[timePosition] = t;
|
|
|
|
originalMethod.apply(this, args);
|
|
|
|
}.bind(this), time);
|
|
|
|
this._scheduledEvents.push(id);
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsync the instrument from the Transport
|
|
|
|
* @returns {Tone.Instrument} this
|
|
|
|
*/
|
|
|
|
Tone.Instrument.prototype.unsync = function(){
|
|
|
|
this._scheduledEvents.forEach(function(id){
|
|
|
|
Tone.Transport.clear(id);
|
|
|
|
});
|
|
|
|
this._scheduledEvents = [];
|
|
|
|
if (this._original_triggerAttack){
|
|
|
|
this.triggerAttack = this._original_triggerAttack;
|
|
|
|
this.triggerRelease = this._original_triggerRelease;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-09-17 04:19:30 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Trigger the attack and then the release after the duration.
|
2015-06-20 19:50:57 +00:00
|
|
|
* @param {Frequency} note The note to trigger.
|
|
|
|
* @param {Time} duration How long the note should be held for before
|
2017-08-27 21:50:31 +00:00
|
|
|
* triggering the release. This value must be greater than 0.
|
2015-06-20 19:50:57 +00:00
|
|
|
* @param {Time} [time=now] When the note should be triggered.
|
|
|
|
* @param {NormalRange} [velocity=1] The velocity the note should be triggered at.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Instrument} this
|
2015-06-14 03:15:57 +00:00
|
|
|
* @example
|
|
|
|
* //trigger "C4" for the duration of an 8th note
|
|
|
|
* synth.triggerAttackRelease("C4", "8n");
|
2014-09-17 04:19:30 +00:00
|
|
|
*/
|
2014-09-20 22:57:44 +00:00
|
|
|
Tone.Instrument.prototype.triggerAttackRelease = function(note, duration, time, velocity){
|
2017-05-29 01:52:28 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-09-20 22:57:44 +00:00
|
|
|
duration = this.toSeconds(duration);
|
|
|
|
this.triggerAttack(note, time, velocity);
|
|
|
|
this.triggerRelease(time + duration);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-20 22:57:44 +00:00
|
|
|
};
|
2014-09-17 04:19:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Instrument} this
|
2014-09-17 04:19:30 +00:00
|
|
|
*/
|
|
|
|
Tone.Instrument.prototype.dispose = function(){
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.AudioNode.prototype.dispose.call(this);
|
2015-11-03 01:08:53 +00:00
|
|
|
this._volume.dispose();
|
|
|
|
this._volume = null;
|
2015-04-18 14:54:08 +00:00
|
|
|
this._writable(["volume"]);
|
2015-02-10 16:40:04 +00:00
|
|
|
this.volume = null;
|
2018-01-02 21:49:41 +00:00
|
|
|
this.unsync();
|
|
|
|
this._scheduledEvents = null;
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-17 04:19:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Instrument;
|
2017-08-27 21:50:31 +00:00
|
|
|
});
|