2018-03-05 17:25:33 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Timeline", "Tone/type/Type"], function(Tone){
|
2015-08-17 02:21:54 +00:00
|
|
|
|
2015-10-21 16:11:19 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-08-17 02:21:54 +00:00
|
|
|
/**
|
2015-08-18 20:28:55 +00:00
|
|
|
* @class A Timeline State. Provides the methods: <code>setStateAtTime("state", time)</code>
|
2016-12-19 03:14:14 +00:00
|
|
|
* and <code>getValueAtTime(time)</code>.
|
2015-08-17 02:21:54 +00:00
|
|
|
*
|
2015-08-18 20:28:55 +00:00
|
|
|
* @extends {Tone.Timeline}
|
|
|
|
* @param {String} initial The initial state of the TimelineState.
|
2015-08-17 02:21:54 +00:00
|
|
|
* Defaults to <code>undefined</code>
|
|
|
|
*/
|
2015-08-18 20:28:55 +00:00
|
|
|
Tone.TimelineState = function(initial){
|
2015-08-17 02:21:54 +00:00
|
|
|
|
2015-08-18 20:28:55 +00:00
|
|
|
Tone.Timeline.call(this);
|
2015-08-17 02:21:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial state
|
|
|
|
* @private
|
|
|
|
* @type {String}
|
|
|
|
*/
|
|
|
|
this._initial = initial;
|
|
|
|
};
|
|
|
|
|
2015-08-18 20:28:55 +00:00
|
|
|
Tone.extend(Tone.TimelineState, Tone.Timeline);
|
2015-08-17 02:21:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the scheduled state scheduled before or at
|
|
|
|
* the given time.
|
2016-09-20 23:12:43 +00:00
|
|
|
* @param {Number} time The time to query.
|
2015-08-17 02:21:54 +00:00
|
|
|
* @return {String} The name of the state input in setStateAtTime.
|
|
|
|
*/
|
2016-12-19 03:14:14 +00:00
|
|
|
Tone.TimelineState.prototype.getValueAtTime = function(time){
|
2016-12-19 02:56:22 +00:00
|
|
|
var event = this.get(time);
|
2015-08-17 02:21:54 +00:00
|
|
|
if (event !== null){
|
|
|
|
return event.state;
|
|
|
|
} else {
|
|
|
|
return this._initial;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-06-21 14:22:10 +00:00
|
|
|
* Add a state to the timeline.
|
2015-08-17 02:21:54 +00:00
|
|
|
* @param {String} state The name of the state to set.
|
2016-09-20 23:12:43 +00:00
|
|
|
* @param {Number} time The time to query.
|
2017-06-21 14:22:10 +00:00
|
|
|
* @returns {Tone.TimelineState} this
|
2015-08-17 02:21:54 +00:00
|
|
|
*/
|
2015-08-18 20:28:55 +00:00
|
|
|
Tone.TimelineState.prototype.setStateAtTime = function(state, time){
|
2018-03-01 19:11:07 +00:00
|
|
|
//all state changes need to be >= the previous state time
|
|
|
|
//TODO throw error if time < the previous event time
|
2016-12-19 02:56:22 +00:00
|
|
|
this.add({
|
2015-08-17 02:21:54 +00:00
|
|
|
"state" : state,
|
2016-09-20 23:12:43 +00:00
|
|
|
"time" : time
|
2015-08-17 02:21:54 +00:00
|
|
|
});
|
2017-06-21 14:22:10 +00:00
|
|
|
return this;
|
2015-08-17 02:21:54 +00:00
|
|
|
};
|
|
|
|
|
2018-02-18 21:01:27 +00:00
|
|
|
/**
|
|
|
|
* Return the event before the time with the given state
|
|
|
|
* @param {Tone.State} state The state to look for
|
|
|
|
* @param {Time} time When to check before
|
|
|
|
* @return {Object} The event with the given state before the time
|
|
|
|
*/
|
|
|
|
Tone.TimelineState.prototype.getLastState = function(state, time){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
var index = this._search(time);
|
|
|
|
for (var i = index; i >= 0; i--){
|
|
|
|
var event = this._timeline[i];
|
|
|
|
if (event.state === state){
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-01 19:11:07 +00:00
|
|
|
/**
|
|
|
|
* Return the event after the time with the given state
|
|
|
|
* @param {Tone.State} state The state to look for
|
|
|
|
* @param {Time} time When to check from
|
|
|
|
* @return {Object} The event with the given state after the time
|
|
|
|
*/
|
|
|
|
Tone.TimelineState.prototype.getNextState = function(state, time){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
var index = this._search(time);
|
|
|
|
for (var i = index; i < this._timeline.length; i++){
|
|
|
|
var event = this._timeline[i];
|
|
|
|
if (event.state === state){
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-18 20:28:55 +00:00
|
|
|
return Tone.TimelineState;
|
2017-10-26 20:02:01 +00:00
|
|
|
});
|