/
Timeline.js
412 lines (382 loc) · 11.5 KB
/
Timeline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
define(["Tone/core/Tone"], function (Tone) {
"use strict";
/**
* @class A Timeline class for scheduling and maintaining state
* along a timeline. All events must have a "time" property.
* Internally, events are stored in time order for fast
* retrieval.
* @extends {Tone}
* @param {Positive} [memory=Infinity] The number of previous events that are retained.
*/
Tone.Timeline = function(){
var options = Tone.defaults(arguments, ["memory"], Tone.Timeline);
Tone.call(this);
/**
* The array of scheduled timeline events
* @type {Array}
* @private
*/
this._timeline = [];
/**
* An array of items to remove from the list.
* @type {Array}
* @private
*/
this._toRemove = [];
/**
* An array of items to add from the list (once it's done iterating)
* @type {Array}
* @private
*/
this._toAdd = [];
/**
* Flag if the timeline is mid iteration
* @private
* @type {Boolean}
*/
this._iterating = false;
/**
* The memory of the timeline, i.e.
* how many events in the past it will retain
* @type {Positive}
*/
this.memory = options.memory;
};
Tone.extend(Tone.Timeline);
/**
* the default parameters
* @static
* @const
*/
Tone.Timeline.defaults = {
"memory" : Infinity
};
/**
* The number of items in the timeline.
* @type {Number}
* @memberOf Tone.Timeline#
* @name length
* @readOnly
*/
Object.defineProperty(Tone.Timeline.prototype, "length", {
get : function(){
return this._timeline.length;
}
});
/**
* Insert an event object onto the timeline. Events must have a "time" attribute.
* @param {Object} event The event object to insert into the
* timeline.
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.add = function(event){
//the event needs to have a time attribute
if (Tone.isUndef(event.time)){
throw new Error("Tone.Timeline: events must have a time attribute");
}
if (this._iterating){
this._toAdd.push(event);
} else {
var index = this._search(event.time);
this._timeline.splice(index + 1, 0, event);
//if the length is more than the memory, remove the previous ones
if (this.length > this.memory){
var diff = this.length - this.memory;
this._timeline.splice(0, diff);
}
}
return this;
};
/**
* Remove an event from the timeline.
* @param {Object} event The event object to remove from the list.
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.remove = function(event){
if (this._iterating){
this._toRemove.push(event);
} else {
var index = this._timeline.indexOf(event);
if (index !== -1){
this._timeline.splice(index, 1);
}
}
return this;
};
/**
* Get the nearest event whose time is less than or equal to the given time.
* @param {Number} time The time to query.
* @param {String} comparator Which value in the object to compare
* @returns {Object} The event object set after that time.
*/
Tone.Timeline.prototype.get = function(time, comparator){
comparator = Tone.defaultArg(comparator, "time");
var index = this._search(time, comparator);
if (index !== -1){
return this._timeline[index];
} else {
return null;
}
};
/**
* Return the first event in the timeline without removing it
* @returns {Object} The first event object
*/
Tone.Timeline.prototype.peek = function(){
return this._timeline[0];
};
/**
* Return the first event in the timeline and remove it
* @returns {Object} The first event object
*/
Tone.Timeline.prototype.shift = function(){
return this._timeline.shift();
};
/**
* Get the event which is scheduled after the given time.
* @param {Number} time The time to query.
* @param {String} comparator Which value in the object to compare
* @returns {Object} The event object after the given time
*/
Tone.Timeline.prototype.getAfter = function(time, comparator){
comparator = Tone.defaultArg(comparator, "time");
var index = this._search(time, comparator);
if (index + 1 < this._timeline.length){
return this._timeline[index + 1];
} else {
return null;
}
};
/**
* Get the event before the event at the given time.
* @param {Number} time The time to query.
* @param {String} comparator Which value in the object to compare
* @returns {Object} The event object before the given time
*/
Tone.Timeline.prototype.getBefore = function(time, comparator){
comparator = Tone.defaultArg(comparator, "time");
var len = this._timeline.length;
//if it's after the last item, return the last item
if (len > 0 && this._timeline[len - 1][comparator] < time){
return this._timeline[len - 1];
}
var index = this._search(time, comparator);
if (index - 1 >= 0){
return this._timeline[index - 1];
} else {
return null;
}
};
/**
* Cancel events after the given time
* @param {Number} time The time to query.
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.cancel = function(after){
if (this._timeline.length > 1){
var index = this._search(after);
if (index >= 0){
if (this._timeline[index].time === after){
//get the first item with that time
for (var i = index; i >= 0; i--){
if (this._timeline[i].time === after){
index = i;
} else {
break;
}
}
this._timeline = this._timeline.slice(0, index);
} else {
this._timeline = this._timeline.slice(0, index + 1);
}
} else {
this._timeline = [];
}
} else if (this._timeline.length === 1){
//the first item's time
if (this._timeline[0].time >= after){
this._timeline = [];
}
}
return this;
};
/**
* Cancel events before or equal to the given time.
* @param {Number} time The time to cancel before.
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.cancelBefore = function(time){
var index = this._search(time);
if (index >= 0){
this._timeline = this._timeline.slice(index + 1);
}
return this;
};
/**
* Returns the previous event if there is one. null otherwise
* @param {Object} event The event to find the previous one of
* @return {Object} The event right before the given event
*/
Tone.Timeline.prototype.previousEvent = function(event){
var index = this._timeline.indexOf(event);
if (index > 0){
return this._timeline[index-1];
} else {
return null;
}
};
/**
* Does a binary search on the timeline array and returns the
* nearest event index whose time is after or equal to the given time.
* If a time is searched before the first index in the timeline, -1 is returned.
* If the time is after the end, the index of the last item is returned.
* @param {Number} time
* @param {String} comparator Which value in the object to compare
* @return {Number} the index in the timeline array
* @private
*/
Tone.Timeline.prototype._search = function(time, comparator){
if (this._timeline.length === 0){
return -1;
}
comparator = Tone.defaultArg(comparator, "time");
var beginning = 0;
var len = this._timeline.length;
var end = len;
if (len > 0 && this._timeline[len - 1][comparator] <= time){
return len - 1;
}
while (beginning < end){
// calculate the midpoint for roughly equal partition
var midPoint = Math.floor(beginning + (end - beginning) / 2);
var event = this._timeline[midPoint];
var nextEvent = this._timeline[midPoint + 1];
if (event[comparator] === time){
//choose the last one that has the same time
for (var i = midPoint; i < this._timeline.length; i++){
var testEvent = this._timeline[i];
if (testEvent[comparator] === time){
midPoint = i;
}
}
return midPoint;
} else if (event[comparator] < time && nextEvent[comparator] > time){
return midPoint;
} else if (event[comparator] > time){
//search lower
end = midPoint;
} else {
//search upper
beginning = midPoint + 1;
}
}
return -1;
};
/**
* Internal iterator. Applies extra safety checks for
* removing items from the array.
* @param {Function} callback
* @param {Number=} lowerBound
* @param {Number=} upperBound
* @private
*/
Tone.Timeline.prototype._iterate = function(callback, lowerBound, upperBound){
this._iterating = true;
lowerBound = Tone.defaultArg(lowerBound, 0);
upperBound = Tone.defaultArg(upperBound, this._timeline.length - 1);
for (var i = lowerBound; i <= upperBound; i++){
callback.call(this, this._timeline[i]);
}
this._iterating = false;
this._toRemove.forEach(function(event){
this.remove(event);
}.bind(this));
this._toRemove = [];
this._toAdd.forEach(function(event){
this.add(event);
}.bind(this));
this._toAdd = [];
};
/**
* Iterate over everything in the array
* @param {Function} callback The callback to invoke with every item
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.forEach = function(callback){
this._iterate(callback);
return this;
};
/**
* Iterate over everything in the array at or before the given time.
* @param {Number} time The time to check if items are before
* @param {Function} callback The callback to invoke with every item
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.forEachBefore = function(time, callback){
//iterate over the items in reverse so that removing an item doesn't break things
var upperBound = this._search(time);
if (upperBound !== -1){
this._iterate(callback, 0, upperBound);
}
return this;
};
/**
* Iterate over everything in the array after the given time.
* @param {Number} time The time to check if items are before
* @param {Function} callback The callback to invoke with every item
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.forEachAfter = function(time, callback){
//iterate over the items in reverse so that removing an item doesn't break things
var lowerBound = this._search(time);
this._iterate(callback, lowerBound + 1);
return this;
};
/**
* Iterate over everything in the array at or after the given time. Similar to
* forEachAfter, but includes the item(s) at the given time.
* @param {Number} time The time to check if items are before
* @param {Function} callback The callback to invoke with every item
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.forEachFrom = function(time, callback){
//iterate over the items in reverse so that removing an item doesn't break things
var lowerBound = this._search(time);
//work backwards until the event time is less than time
while (lowerBound >= 0 && this._timeline[lowerBound].time >= time){
lowerBound--;
}
this._iterate(callback, lowerBound + 1);
return this;
};
/**
* Iterate over everything in the array at the given time
* @param {Number} time The time to check if items are before
* @param {Function} callback The callback to invoke with every item
* @returns {Tone.Timeline} this
*/
Tone.Timeline.prototype.forEachAtTime = function(time, callback){
//iterate over the items in reverse so that removing an item doesn't break things
var upperBound = this._search(time);
if (upperBound !== -1){
this._iterate(function(event){
if (event.time === time){
callback.call(this, event);
}
}, 0, upperBound);
}
return this;
};
/**
* Clean up.
* @return {Tone.Timeline} this
*/
Tone.Timeline.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._timeline = null;
this._toRemove = null;
this._toAdd = null;
return this;
};
return Tone.Timeline;
});