-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusicplayer.js
239 lines (198 loc) · 7.19 KB
/
musicplayer.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
"use strict";
class TonePlayer {
static AudioCtx = null;
static MAX_UPDATE_WAIT = 100;
constructor(trackObj, secondsPerTick) {
if(TonePlayer.AudioCtx == null) TonePlayer.AudioCtx = new (window.AudioContext || window.webkitAudioContext)();
this.tracks = trackObj;
this.secondsPerTick = secondsPerTick;
this.millisPerTick = secondsPerTick * 1000;
this.offset = 0; // Offset in ticks...
this.currentIndexes = TonePlayer.initFrom(trackObj, (a, b) => 0);
this.oscillators = TonePlayer.initFrom(trackObj, (a, b) => null);
this._onUpdate = null;
this.locations = TonePlayer.getTrackCumulators(this.tracks);
this._length = Math.max(...Object.values(this.locations).map((a) => a[a.length - 1]));
this._playing = false;
this._timerId = null;
}
get length() {
return this._length;
}
static genOsc(type = "square", freq = 440, volume = 1) {
let osc = TonePlayer.AudioCtx.createOscillator();
let gain = TonePlayer.AudioCtx.createGain();
gain.gain.value = volume;
osc.type = type;
osc.connect(gain);
gain.connect(TonePlayer.AudioCtx.destination);
osc.frequency.setValueAtTime(freq, TonePlayer.AudioCtx.currentTime);
return osc;
}
static initFrom(otherObj, initialValGen, initObj = {}) {
for(let prop in otherObj) initObj[prop] = initialValGen(prop, otherObj[prop]);
return initObj;
}
static *objZip() {
if(arguments.length < 1) throw "Error: Must pass at least 1 object.";
let keysObj = arguments[0];
for(let prop in keysObj) {
let res = [prop];
for(let arg of arguments) {
res.push(arg[prop]);
}
yield res;
}
}
static getTrackCumulators(tracks) {
let cumSums = {};
let lengths = {};
for(let trackName in tracks) {
let track = tracks[trackName];
let cumSum = [];
let sum = 0;
if(track.notes == undefined) {
cumSums[trackName] = [0];
continue
}
for(let note of track.notes) {
cumSum.push(sum);
sum += (note[0] == "play")? note[2]: note[1];
}
cumSum.push(sum);
cumSums[trackName] = cumSum;
}
return cumSums;
}
static binarySearch(locations, offset) {
if(locations.length < 2) throw "Must have at least 2 elements.";
let low = 0;
let high = locations.length - 1;
let mid = 0;
while(low < high) {
mid = Math.floor((low + high) / 2);
let off2 = locations[mid];
let off3 = locations[mid + 1];
if(offset < off2) {
high = mid - 1;
}
else if(offset >= off3){
low = mid + 1;
}
else {
return mid;
}
}
return Math.max(Math.floor((high + low) / 2), 0);
}
set onUpdate(func) {
this._onUpdate = func;
}
get onUpdate() {
return this._onUpdate ?? (() => null);
}
play() {
if(!this._playing) {
this._playing = true;
this._execStep();
}
}
pause(evt = true) {
if(this._playing) {
this._playing = false;
if(this._timerId != null) clearTimeout(this._timerId);
// Kill all of the oscillators...
for(let [name, osc] of TonePlayer.objZip(this.oscillators)) {
if(osc != null) {
// Was a note playing? Kill the oscillator and move the index back...
osc.stop();
this.oscillators[name] = null;
this.currentIndexes[name]--;
}
}
if(evt) this.onUpdate(this._playing, this.offset);
}
}
stop() {
this.pause();
this.setLocation(0);
}
get playing() {
return this._playing;
}
setLocation(offset) {
offset = Number(offset);
if((offset < 0) || (offset > this.length)) {
throw "Error: offset not within track!";
}
let wasPlaying = this._playing;
// Pause it....
this.pause(false);
// Update offset...
this.offset = offset;
// Update indexes... We use a binary search...
for(let name in this.currentIndexes) {
if(this.locations[name].length > 1)
this.currentIndexes[name] = TonePlayer.binarySearch(this.locations[name], offset);
}
// All done... We now check if the code was playing. If so, begin playing again...
if(wasPlaying) {
this.play();
}
else {
this.onUpdate(this._playing, this.offset);
}
}
_execStep() {
if(!this._playing) return;
this.onUpdate(this._playing, this.offset);
// Compute the time until the next note..
let next = [];
for(let [name, trackOff, osc, index] of TonePlayer.objZip(this.locations, this.oscillators, this.currentIndexes)) {
if(index >= trackOff.length) {
if(osc != null) {
osc.stop();
this.oscillators[name] = null;
}
continue;
}
next.push([name, trackOff[index] - this.offset]);
}
// We finished the song...
if(next.length == 0) {
this.pause();
return;
}
// Sort by time...
next.sort((a, b) => a[1] - b[1]);
for(let [name, timeUntil] of next) {
if(timeUntil <= 0) {
let idx = this.currentIndexes[name];
let track = this.tracks[name].notes ?? [];
let vars = this.tracks[name].variables ?? {};
let osc = this.oscillators[name];
if(osc != null) {
osc.stop();
this.oscillators[name] = null;
}
if(idx < track.length) {
let cmd = track[idx];
if(cmd[0] == "play") {
let osc = TonePlayer.genOsc(vars.WAVE_TYPE ?? "square", cmd[1] + (vars.FREQUENCY_SHIFT ?? 0), vars.VOLUME ?? 1);
osc.start();
this.oscillators[name] = osc;
}
}
this.currentIndexes[name] = idx + 1;
}
else {
// Setup timeout for next note play...
let step = Math.min(timeUntil, TonePlayer.MAX_UPDATE_WAIT / this.millisPerTick);
this.offset += step;
this._timerId = setTimeout(() => this._execStep(), step * this.millisPerTick);
return;
}
}
this._execStep();
}
}