Tone.js/Tone/event/Event.js

402 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-11-03 02:53:39 +00:00
define(["Tone/core/Tone", "Tone/core/Transport", "Tone/core/Type", "Tone/core/TimelineState"], function (Tone) {
2015-08-17 00:31:31 +00:00
"use strict";
/**
2015-11-18 03:52:22 +00:00
* @class Tone.Event abstracts away Tone.Transport.schedule and provides a schedulable
* callback for a single or repeatable events along the timeline.
2015-08-17 00:31:31 +00:00
*
2015-11-18 03:52:22 +00:00
* @extends {Tone}
2015-08-17 00:31:31 +00:00
* @param {function} callback The callback to invoke at the time.
2015-11-03 02:53:39 +00:00
* @param {*} value The value or values which should be passed to
2015-08-17 00:31:31 +00:00
* the callback function on invocation.
* @example
2015-11-03 02:53:39 +00:00
* var chord = new Tone.Event(function(time, chord){
2015-08-17 00:31:31 +00:00
* //the chord as well as the exact time of the event
* //are passed in as arguments to the callback function
2015-11-18 03:52:22 +00:00
* }, ["D4", "E4", "F4"]);
2015-08-17 00:31:31 +00:00
* //start the chord at the beginning of the transport timeline
* chord.start();
* //loop it every measure for 8 measures
* chord.loop = 8;
* chord.loopEnd = "1m";
*/
2015-11-03 02:53:39 +00:00
Tone.Event = function(){
2015-08-17 00:31:31 +00:00
2015-11-03 02:53:39 +00:00
var options = this.optionsObject(arguments, ["callback", "value"], Tone.Event.defaults);
2015-08-17 00:31:31 +00:00
/**
* Loop value
* @type {Boolean|Positive}
* @private
*/
this._loop = options.loop;
/**
* The callback to invoke.
* @type {Function}
*/
2015-11-03 02:53:39 +00:00
this.callback = options.callback;
2015-08-17 00:31:31 +00:00
/**
* The value which is passed to the
* callback function.
* @type {*}
* @private
*/
this.value = options.value;
/**
* When the note is scheduled to start.
* @type {Number}
* @private
*/
this._loopStart = this.toTicks(options.loopStart);
2015-08-17 00:31:31 +00:00
/**
* When the note is scheduled to start.
* @type {Number}
* @private
*/
this._loopEnd = this.toTicks(options.loopEnd);
2015-08-17 00:31:31 +00:00
/**
2015-09-05 23:17:45 +00:00
* Tracks the scheduled events
2015-11-03 02:53:39 +00:00
* @type {Tone.TimelineState}
2015-08-17 00:31:31 +00:00
* @private
*/
2015-11-03 02:53:39 +00:00
this._state = new Tone.TimelineState(Tone.State.Stopped);
2015-08-17 00:31:31 +00:00
/**
* The playback speed of the note. A speed of 1
* is no change.
* @private
* @type {Positive}
*/
this._playbackRate = 1;
2015-11-03 02:53:39 +00:00
/**
* A delay time from when the event is scheduled to start
* @type {Ticks}
* @private
*/
this._startOffset = 0;
2015-11-03 02:53:39 +00:00
2015-08-17 00:31:31 +00:00
/**
* The probability that the callback will be invoked
* at the scheduled time.
* @type {NormalRange}
*/
this.probability = options.probability;
2015-11-03 02:53:39 +00:00
/**
2015-11-26 02:46:33 +00:00
* If set to true, will apply small (+/-0.02 seconds) random variation
2015-11-18 03:52:22 +00:00
* to the callback time. If the value is given as a time, it will randomize
* by that amount.
* @example
* event.humanize = true;
2015-11-03 02:53:39 +00:00
* @type {Boolean|Time}
*/
this.humanize = options.humanize;
/**
2015-11-18 03:52:22 +00:00
* If mute is true, the callback won't be
* invoked.
2015-11-03 02:53:39 +00:00
* @type {Boolean}
*/
this.mute = options.mute;
2015-08-17 00:31:31 +00:00
//set the initial values
this.playbackRate = options.playbackRate;
};
2015-11-03 02:53:39 +00:00
Tone.extend(Tone.Event);
2015-08-17 00:31:31 +00:00
/**
* The default values
* @type {Object}
* @const
*/
2015-11-03 02:53:39 +00:00
Tone.Event.defaults = {
2015-08-17 00:31:31 +00:00
"callback" : Tone.noOp,
"loop" : false,
"loopEnd" : "1m",
"loopStart" : 0,
"playbackRate" : 1,
2015-11-03 02:53:39 +00:00
"value" : null,
"probability" : 1,
"mute" : false,
"humanize" : false,
2015-08-17 00:31:31 +00:00
};
2015-09-05 23:17:45 +00:00
/**
* Reschedule all of the events along the timeline
* with the updated values.
* @param {Time} after Only reschedules events after the given time.
2015-11-03 02:53:39 +00:00
* @return {Tone.Event} this
2015-09-05 23:17:45 +00:00
* @private
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype._rescheduleEvents = function(after){
2015-09-05 23:17:45 +00:00
//if no argument is given, schedules all of the events
after = this.defaultArg(after, -1);
2015-11-03 02:53:39 +00:00
this._state.forEachFrom(after, function(event){
2015-09-05 23:17:45 +00:00
var duration;
if (event.state === Tone.State.Started){
if (!this.isUndef(event.id)){
Tone.Transport.clear(event.id);
}
var startTick = event.time + Math.round(this.startOffset / this._playbackRate);
2015-09-05 23:17:45 +00:00
if (this._loop){
duration = Infinity;
2015-11-03 02:53:39 +00:00
if (this.isNumber(this._loop)){
2015-09-05 23:17:45 +00:00
duration = (this._loop - 1) * this._getLoopDuration();
}
2015-11-03 02:53:39 +00:00
var nextEvent = this._state.getEventAfter(startTick);
2015-09-05 23:17:45 +00:00
if (nextEvent !== null){
2015-11-03 02:53:39 +00:00
duration = Math.min(duration, nextEvent.time - startTick);
}
if (duration !== Infinity){
//schedule a stop since it's finite duration
this._state.setStateAtTime(Tone.State.Stopped, startTick + duration + 1);
2015-11-03 02:53:39 +00:00
duration += "i";
}
event.id = Tone.Transport.scheduleRepeat(this._tick.bind(this), this._getLoopDuration().toString() + "i", startTick + "i", duration);
2015-09-05 23:17:45 +00:00
} else {
2015-11-03 02:53:39 +00:00
event.id = Tone.Transport.schedule(this._tick.bind(this), startTick + "i");
2015-09-05 23:17:45 +00:00
}
}
}.bind(this));
return this;
};
2015-11-03 02:53:39 +00:00
/**
* Returns the playback state of the note, either "started" or "stopped".
* @type {String}
* @readOnly
* @memberOf Tone.Event#
* @name state
*/
Object.defineProperty(Tone.Event.prototype, "state", {
get : function(){
return this._state.getStateAtTime(Tone.Transport.ticks);
}
});
/**
* The start from the scheduled start time
* @type {Ticks}
* @memberOf Tone.Event#
* @name startOffset
* @private
*/
Object.defineProperty(Tone.Event.prototype, "startOffset", {
get : function(){
return this._startOffset;
},
set : function(offset){
this._startOffset = offset;
}
});
2015-08-17 00:31:31 +00:00
/**
* Start the note at the given time.
* @param {Time} time When the note should start.
2015-11-03 02:53:39 +00:00
* @return {Tone.Event} this
2015-08-17 00:31:31 +00:00
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype.start = function(time){
time = this.toTicks(time);
if (this._state.getStateAtTime(time) === Tone.State.Stopped){
this._state.addEvent({
2015-09-05 23:17:45 +00:00
"state" : Tone.State.Started,
"time" : time,
"id" : undefined,
});
2015-11-03 02:53:39 +00:00
this._rescheduleEvents(time);
2015-08-17 00:31:31 +00:00
}
return this;
};
/**
2015-11-18 03:52:22 +00:00
* Stop the Event at the given time.
2015-08-17 00:31:31 +00:00
* @param {Time} time When the note should stop.
2015-11-03 02:53:39 +00:00
* @return {Tone.Event} this
2015-08-17 00:31:31 +00:00
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype.stop = function(time){
2015-12-05 18:09:25 +00:00
this.cancel(time);
2015-11-03 02:53:39 +00:00
time = this.toTicks(time);
if (this._state.getStateAtTime(time) === Tone.State.Started){
this._state.setStateAtTime(Tone.State.Stopped, time);
var previousEvent = this._state.getEventBefore(time);
2015-09-05 23:17:45 +00:00
var reschedulTime = time;
if (previousEvent !== null){
reschedulTime = previousEvent.time;
}
2015-11-03 02:53:39 +00:00
this._rescheduleEvents(reschedulTime);
2015-09-05 23:17:45 +00:00
}
return this;
};
/**
* Cancel all scheduled events greater than or equal to the given time
* @param {Time} [time=0] The time after which events will be cancel.
2015-11-03 02:53:39 +00:00
* @return {Tone.Event} this
2015-09-05 23:17:45 +00:00
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype.cancel = function(time){
2015-09-05 23:17:45 +00:00
time = this.defaultArg(time, -Infinity);
time = this.toTicks(time);
2015-11-03 02:53:39 +00:00
this._state.forEachFrom(time, function(event){
2015-09-05 23:17:45 +00:00
Tone.Transport.clear(event.id);
});
2015-11-03 02:53:39 +00:00
this._state.cancel(time);
2015-08-17 00:31:31 +00:00
return this;
};
/**
* The callback function invoker. Also
2015-11-18 03:52:22 +00:00
* checks if the Event is done playing
2015-08-17 00:31:31 +00:00
* @param {Number} time The time of the event in seconds
* @private
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype._tick = function(time){
if (!this.mute && this._state.getStateAtTime(Tone.Transport.ticks) === Tone.State.Started){
if (this.probability < 1 && Math.random() > this.probability){
return;
}
if (this.humanize){
2015-11-26 02:46:33 +00:00
var variation = 0.02;
2015-11-03 02:53:39 +00:00
if (!this.isBoolean(this.humanize)){
variation = this.toSeconds(this.humanize);
2015-08-17 00:31:31 +00:00
}
2015-11-03 02:53:39 +00:00
time += (Math.random() * 2 - 1) * variation;
2015-08-17 00:31:31 +00:00
}
2015-11-03 02:53:39 +00:00
this.callback(time, this.value);
2015-08-17 00:31:31 +00:00
}
};
/**
* Get the duration of the loop.
2015-11-03 02:53:39 +00:00
* @return {Ticks}
2015-08-17 00:31:31 +00:00
* @private
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype._getLoopDuration = function(){
2015-08-17 00:31:31 +00:00
return Math.round((this._loopEnd - this._loopStart) / this._playbackRate);
};
/**
* If the note should loop or not
2015-11-03 02:53:39 +00:00
* between Tone.Event.loopStart and
* Tone.Event.loopEnd. An integer
2015-08-17 00:31:31 +00:00
* value corresponds to the number of
2015-11-18 03:52:22 +00:00
* loops the Event does after it starts.
2015-11-03 02:53:39 +00:00
* @memberOf Tone.Event#
2015-08-17 00:31:31 +00:00
* @type {Boolean|Positive}
* @name loop
*/
2015-11-03 02:53:39 +00:00
Object.defineProperty(Tone.Event.prototype, "loop", {
2015-08-17 00:31:31 +00:00
get : function(){
return this._loop;
},
set : function(loop){
this._loop = loop;
2015-09-05 23:17:45 +00:00
this._rescheduleEvents();
2015-08-17 00:31:31 +00:00
}
});
/**
* The playback rate of the note. Defaults to 1.
2015-11-03 02:53:39 +00:00
* @memberOf Tone.Event#
2015-08-17 00:31:31 +00:00
* @type {Positive}
* @name playbackRate
* @example
* note.loop = true;
* //repeat the note twice as fast
* note.playbackRate = 2;
*/
2015-11-03 02:53:39 +00:00
Object.defineProperty(Tone.Event.prototype, "playbackRate", {
2015-08-17 00:31:31 +00:00
get : function(){
return this._playbackRate;
},
set : function(rate){
this._playbackRate = rate;
this._rescheduleEvents();
2015-08-17 00:31:31 +00:00
}
});
/**
2015-11-18 03:52:22 +00:00
* The loopEnd point is the time the event will loop.
* Note: only loops if Tone.Event.loop is true.
2015-11-03 02:53:39 +00:00
* @memberOf Tone.Event#
2015-08-17 00:31:31 +00:00
* @type {Boolean|Positive}
* @name loopEnd
*/
2015-11-03 02:53:39 +00:00
Object.defineProperty(Tone.Event.prototype, "loopEnd", {
2015-08-17 00:31:31 +00:00
get : function(){
return this.toNotation(this._loopEnd + "i");
},
set : function(loopEnd){
this._loopEnd = this.toTicks(loopEnd);
2015-09-05 23:17:45 +00:00
if (this._loop){
this._rescheduleEvents();
}
2015-08-17 00:31:31 +00:00
}
});
/**
2015-11-18 03:52:22 +00:00
* The time when the loop should start.
2015-11-03 02:53:39 +00:00
* @memberOf Tone.Event#
2015-08-17 00:31:31 +00:00
* @type {Boolean|Positive}
* @name loopStart
*/
2015-11-03 02:53:39 +00:00
Object.defineProperty(Tone.Event.prototype, "loopStart", {
2015-08-17 00:31:31 +00:00
get : function(){
return this.toNotation(this._loopStart + "i");
},
set : function(loopStart){
this._loopStart = this.toTicks(loopStart);
2015-09-05 23:17:45 +00:00
if (this._loop){
this._rescheduleEvents();
}
2015-08-17 00:31:31 +00:00
}
});
/**
* The current progress of the loop interval.
* Returns 0 if the atom is not started yet or the
* atom is not set to loop.
2015-11-03 02:53:39 +00:00
* @memberOf Tone.Event#
2015-08-17 00:31:31 +00:00
* @type {NormalRange}
* @name progress
* @readOnly
*/
2015-11-03 02:53:39 +00:00
Object.defineProperty(Tone.Event.prototype, "progress", {
2015-08-17 00:31:31 +00:00
get : function(){
if (this._loop){
var ticks = Tone.Transport.ticks;
2015-11-03 02:53:39 +00:00
var lastEvent = this._state.getEvent(ticks);
2015-09-05 23:17:45 +00:00
if (lastEvent !== null && lastEvent.state === Tone.State.Started){
var loopDuration = this._getLoopDuration();
var progress = (ticks - lastEvent.time) % loopDuration;
2015-08-17 00:31:31 +00:00
return progress / loopDuration;
} else {
return 0;
}
} else {
return 0;
}
}
});
/**
* Clean up
2015-11-03 02:53:39 +00:00
* @return {Tone.Event} this
2015-08-17 00:31:31 +00:00
*/
2015-11-03 02:53:39 +00:00
Tone.Event.prototype.dispose = function(){
2015-09-05 23:17:45 +00:00
this.cancel();
2015-11-03 02:53:39 +00:00
this._state.dispose();
this._state = null;
this.callback = null;
2015-08-17 00:31:31 +00:00
this.value = null;
};
2015-11-03 02:53:39 +00:00
return Tone.Event;
2015-08-17 00:31:31 +00:00
});