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";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class this is a base class for monophonic instruments.
|
|
|
|
* it defines their interfaces
|
|
|
|
*
|
|
|
|
* @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){
|
|
|
|
|
2014-09-20 23:23:28 +00:00
|
|
|
Tone.Instrument.call(this);
|
|
|
|
|
2014-09-03 23:05:09 +00:00
|
|
|
//get the defaults
|
|
|
|
options = this.defaultArg(options, Tone.Monophonic.defaults);
|
|
|
|
|
|
|
|
/**
|
2015-02-10 16:40:04 +00:00
|
|
|
* The glide time between notes.
|
|
|
|
* @type {Tone.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
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger the attack. start the note, at the time with the velocity
|
|
|
|
*
|
|
|
|
* @param {string|string} note the note
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {Tone.Time} [time=now] the time, if not given is now
|
|
|
|
* @param {number} [velocity=1] velocity defaults to 1
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-03 23:05:09 +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;
|
2014-09-03 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger the release portion of the envelope
|
2014-09-20 23:23:28 +00:00
|
|
|
* @param {Tone.Time} [time=now] if no time is given, the release happens immediatly
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.triggerRelease = function(time){
|
|
|
|
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
|
2014-09-20 23:23:28 +00:00
|
|
|
* @param {Tone.Time} [time=now] the time the attack should happen
|
|
|
|
* @param {number} [velocity=1] the velocity of the envelope
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.triggerEnvelopeAttack = function() {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* override this method with the actual method
|
|
|
|
* @abstract
|
2014-09-20 23:23:28 +00:00
|
|
|
* @param {Tone.Time} [time=now] the time the attack should happen
|
|
|
|
* @param {number} [velocity=1] the velocity of the envelope
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.triggerEnvelopeRelease = function() {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the note to happen at a specific time
|
2014-09-04 02:35:38 +00:00
|
|
|
* @param {number|string} note if the note is a string, it will be
|
|
|
|
* parsed as (NoteName)(Octave) i.e. A4, C#3, etc
|
|
|
|
* otherwise it will be considered as the frequency
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-03 23:05:09 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.setNote = function(note, time){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
if (this.portamento > 0){
|
2015-02-10 16:40:04 +00:00
|
|
|
var currentNote = this.frequency.value;
|
2014-09-03 23:05:09 +00:00
|
|
|
this.frequency.setValueAtTime(currentNote, time);
|
2015-02-10 16:40:04 +00:00
|
|
|
var portTime = this.toSeconds(this.portamento);
|
|
|
|
this.frequency.exponentialRampToValueAtTime(note, time + portTime);
|
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
|
|
|
};
|
|
|
|
|
2014-09-04 23:14:11 +00:00
|
|
|
/**
|
|
|
|
* set the preset if it exists
|
|
|
|
* @param {string} presetName the name of the preset
|
2015-02-02 18:30:36 +00:00
|
|
|
* @returns {Tone.Monophonic} `this`
|
2014-09-04 23:14:11 +00:00
|
|
|
*/
|
|
|
|
Tone.Monophonic.prototype.setPreset = function(presetName){
|
|
|
|
if (!this.isUndef(this.preset) && this.preset.hasOwnProperty(presetName)){
|
|
|
|
this.set(this.preset[presetName]);
|
|
|
|
}
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-04 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
2014-09-03 23:05:09 +00:00
|
|
|
return Tone.Monophonic;
|
|
|
|
});
|