-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.js
More file actions
84 lines (74 loc) · 2.5 KB
/
schedule.js
File metadata and controls
84 lines (74 loc) · 2.5 KB
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
function ContinuedScheduler() {
this._processTick = function(tickTime) {
if (this._clock.ticks === this._loopEnd) {
this._clock.ticks = this._loopStart;
}
var ticks = this._clock.ticks;
this._continuedEvents.forEachOverlap(ticks, function(event) {
if ((ticks - event.time) % (event.duration + 1) == 0) {
event.callback(tickTime);
}
});
};
this.schedule = function(callback, duration) {
var time = 0;
if (this._lastEvent != null)
time = this._lastEvent.time + this._lastEvent.duration + 1;
Tone.Transport.bpm = this.bpm;
var event = {
"time" : time,
"duration" : Tone.Transport.toTicks(duration) - 1,
"callback" : callback
};
this._lastEvent = event;
this._continuedEvents.addEvent(event);
this._loopEnd = event.time + event.duration;
return event;
};
this.clear = function() {
if (this._lastEvent != null) {
var lastItem = this._continuedEvents.getEvent(this._loopEnd - this._lastEvent.duration - 1);
this._continuedEvents.removeEvent(this._lastEvent);
if (lastItem != null) {
this._loopEnd = lastItem.time + lastItem.duration;
this._lastEvent = lastItem;
} else {
this._lastEvent = null;
}
}
};
this.clearAll = function() {
this._lastEvent = null;
this._continuedEvents.dispose();
};
this.start = function() {
var time = Tone.Transport.toSeconds(undefined);
this._clock.start(time, 0);
return this;
};
this.stop = function() {
var time = Tone.Transport.toSeconds(undefined);
this._clock.stop(time);
return this;
};
this._fromUnits = function(bpm){
return 1 / (60 / bpm / this._ppq);
};
this._toUnits = function(freq){
return (freq / this._ppq) * 60;
};
this._loopEnd = 0;
this._loopStart = 0;
this._clock = new Tone.Clock({"callback" : this._processTick.bind(this),
"frequency" : 0,
});
this._ppq = "48";
this.bpm = this._clock.frequency;
this.bpm._toUnits = this._toUnits.bind(this);
this.bpm._fromUnits = this._fromUnits.bind(this);
this.bpm.units = Tone.Type.BPM;
this.bpm.value = 120;
this._timeSignature = "4";
this._lastEvent = null;
this._continuedEvents = new Tone.IntervalTimeline();
}