-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgandalf.html
365 lines (300 loc) · 9.78 KB
/
gandalf.html
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
<!DOCTYPE html>
<html>
<head>
<title>Gandalf</title>
<style>
body {
margin: 0 0;
padding: 0 0;
}
div.gandalf-video {
display: block;
}
div.gandalf-video > video {
display: block;
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript">
/* "A wizard is never late, nor is he early, he arrives precisely when he means to." */
window.AudioContext = window.AudioContext || window.webkitAudioContext;
//Media must be a "good enough" loop
const SOUND_URL = './epic_sax_guy.wav';
const VIDEO_URL = 'anim1.mp4';
//If you want to run all connected instances at once using P2P, set to false
const GANDALF_AUTOPLAY = true;
//Display debug canvas below video
const GANDALF_DEBUG_ENABLE = true;
class SynchronizedAudioPlayer {
#START_BPM = 131;
constructor(autostart = true) {
this.autostart = autostart;
this.context = new AudioContext();
//Audio source
this.aud = new Audio();
//MediaSource Node: Connects to the audio element
this.src = this.context.createMediaElementSource(this.aud);
//Lowpass Biquad filter to detect beats
//this.filter0 = this.context.createBiquadFilter(); this.filter0.type = "lowpass";
//FFT analyzer
//this.fft0 = this.context.createAnalyser(); this.fft0.fftSize = 2048;
//Destination Node: Device Speakers for AudioContext or AudioBuffer for OfflineAudioContext
this.dest = this.context.destination;
/**
* Connection:
* Source node[Audio element] -+-> Destination node[Speaker]
* +-> BiquadFilter[Lowpass] -> AudioBuffer node
*/
//TODO: once I figure out how to do this, connect properly
this.src.connect(this.dest);
//this.src.connect(this.filter0);
//this.filter0.connect(this.fft0);
//Never allow to pause
this.aud.addEventListener('pause', event => this.aud.play());
this.bpm = this.#START_BPM;
this.beat_start_time = 0;
this.last_ct = this.beat_start_time;
setInterval(this.#bpmPulse.bind(this), 20);
}
#bpmPulse() {
if (this.last_ct > this.aud.currentTime)
this.last_ct = this.beat_start_time;
else if (this.aud.currentTime - this.last_ct >= 60 / (this.bpm * this.aud.playbackRate))
this.last_ct = this.aud.currentTime;
this.syncBeat(this.aud.currentTime, (this.aud.currentTime - this.last_ct) * (this.bpm*this.aud.playbackRate) / 60);
}
async load(url) {
this.aud.src = url;
this.aud.loop = true;
this.aud.playbackRate = 1;
await new Promise((resolve, reject) => {
this.aud.addEventListener('canplay', event => {
if (this.autostart)
this.play();
resolve();
});
this.aud.addEventListener('error', event => reject());
});
}
/* Start playing */
play() {
this.aud.play();
}
/* Data to let other Peers know when to sync */
getSyncData() {
return {
pos: this.aud.currentTime
}
}
/* To sync video with the song's beat phase */
syncBeat(audio_time, beat_phase) {}
}
class GandalfPlayer extends SynchronizedAudioPlayer {
#PLL_PARAM_TUNE_RATE = 0.1;
#PLL_FREQ_DIVIDE = 1;
#PLL_RATE_BIAS = 1;
constructor(autostart, show_debug = false) {
super(autostart);
this.sync_acc = 1;
this.show_debug = show_debug;
this.phase_hist = [];
this.vid_container = document.createElement('div');
this.vid_container.className = "gandalf-video";
this.vid = document.createElement('video');
this.vid_container.appendChild(this.vid);
this.dbg = new DebugPhaseRenderer();
}
add(elem) {
elem.appendChild(this.vid_container);
if (this.show_debug) {
this.dbg.add(elem);
this.dbg.beginRendering();
}
}
async #load_video(vid_url) {
this.vid.src = vid_url;
this.vid.style.display = "none";
this.vid.loop = true;
this.sync_acc = 1;
return new Promise((resolve, reject) => {
this.vid.addEventListener('canplay', event => resolve());
this.vid.addEventListener('error', event => reject());
});
}
async load(audio_url, vid_url) {
let aud_promise = super.load(audio_url);
let vid_promise = this.#load_video(vid_url);
await aud_promise;
await vid_promise;
}
play() {
super.play();
this.vid.style.removeProperty("display")
this.vid.play();
}
fullscreenToggle() {
if (!document.fullscreenElement)
this.vid_container.requestFullscreen();
else
document.exitFullscreen();
}
//Sync video using a PLL
syncBeat(audio_time, beat_phase) {
let vid_phase = (this.vid.currentTime / this.vid.duration);
let v_phang = vid_phase * 2 * Math.PI;
let b_phang = beat_phase * 2 * Math.PI;
let v_phsq = vid_phase > 0.5;
//Frequency divide
while (beat_phase > 1 / this.#PLL_FREQ_DIVIDE) beat_phase -= 1 / this.#PLL_FREQ_DIVIDE;
let b_phsq = beat_phase > 0.5 / this.#PLL_FREQ_DIVIDE;
let phase_diff = v_phsq ^ b_phsq;
this.dbg.setPhase1(v_phang);
this.dbg.setPhase2(b_phang);
if (Number.isNaN(phase_diff)) phase_diff = 0;
this.phase_hist.push(phase_diff);
if (this.phase_hist.length > 200) this.phase_hist.shift();
if (this.phase_hist.length > 0)
this.sync_acc = this.phase_hist.reduce((a, x) => a+x) / this.phase_hist.length;
let phase_error = 2*(this.sync_acc - 0.5);
this.dbg.setPhaseDiff(phase_error);
//Plotting phase graph
this.dbg.phHist({p: phase_diff, m: this.sync_acc});
//FIXME: Improve this, doesn't seem to work. This magic number seems to be pretty okay for now
//It (phase difference) should approach 0 when this function is called, so we need to change playback rate using feedback
//let x = this.#PLL_RATE_BIAS + phase_error * this.#PLL_PARAM_TUNE_RATE;
//this.vid.playbackRate = Math.max(0.25, Math.min(x, 4));
this.vid.playbackRate = 0.928;
}
}
class DebugPhaseRenderer {
constructor() {
this.ca = document.createElement('canvas');
this.ctx = this.ca.getContext('2d');
this.ca.width = 600;
this.ca.height = 200;
this.phase1 = 0;
this.phase2 = 0;
this.phase_diff = 0;
this.phase_hist = [];
}
add(elem) {
elem.appendChild(this.ca);
}
setPhase1(phase) {
this.phase1 = phase;
}
setPhase2(phase) {
this.phase2 = phase;
}
setPhaseDiff(pd) {
this.phase_diff = pd;
}
phHist(hist_data) {
this.phase_hist.push(hist_data);
}
beginRendering() {
window.requestAnimationFrame(this.render.bind(this));
}
render(ts) {
let px, py;
this.ctx.clearRect(0, 0, this.ca.width, this.ca.height);
while (this.phase_hist.length > this.ca.width-40) this.phase_hist.shift();
this.ctx.strokeStyle = "black";
this.ctx.beginPath();
this.ctx.fillStyle = "red";
px = 40 + Math.cos(this.phase1) * 20;
py = 40 + Math.sin(this.phase1) * 20;
this.ctx.arc(px, py, 5, 0, 2*Math.PI);
this.ctx.fill();
this.ctx.beginPath();
this.ctx.fillStyle = "green";
px = 40 + Math.cos(this.phase2) * 20;
py = 40 + Math.sin(this.phase2) * 20;
this.ctx.arc(px, py, 5, 0, 2*Math.PI);
this.ctx.fill();
this.ctx.strokeRect(40 - 20, 110, 40, 20);
this.ctx.beginPath();
this.ctx.fillStyle = "yellow";
px = 40 + this.phase_diff * 10;
this.ctx.arc(px, 120, 10, 0, 2*Math.PI);
this.ctx.fill();
this.ctx.beginPath();
for (let x = 0; x < this.phase_hist.length; x++) {
let px = 20+x;
let py = 100 - this.phase_hist[x].p * 30;
if (x === 0)
this.ctx.moveTo(px, py);
else
this.ctx.lineTo(px, py);
}
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.strokeStyle = "red";
for (let x = 0; x < this.phase_hist.length; x++) {
let px = 20+x;
let py = 100 - this.phase_hist[x].m * 30;
if (x === 0)
this.ctx.moveTo(px, py);
else
this.ctx.lineTo(px, py);
}
this.ctx.stroke();
window.requestAnimationFrame(this.render.bind(this));
}
}
//https://www.youtube.com/watch?v=t-_VPRCtiUg
//TODO: Generalize this class
//Synchronize a player by crowd averaging over WebRTC P2P
class WRTCSynchronizer {
constructor(player) {
this.player = player;
this.itv = null;
//Local peer connection
this.pcon = new RTCPeerConnection();
//All remote connections that connect to us. Key is the offer SDP
this.remotes = {};
//Just one sync channel for timestamps. We can't share audio as that will kill the bandwidth
this.sync_channel = this.pcon.createDataChannel('sync');
this.#generateLocalOffer().then(offer => this.#letTheWorldKnow(offer));
//Fullscreen only possible with user-event
window.addEventListener('dblclick', () => {
this.player.fullscreenToggle();
});
}
start() {
this.itv = setInterval(this.#synchronizeSend.bind(this), 1000);
}
stop() {
clearTimeout(this.itv);
this.itv = null;
}
#generateLocalOffer() {
return new Promise((resolve, reject) => {
this.pcon.createOffer()
.then(offer => this.pcon.setLocalDescription(offer))
.then(() => resolve(this.pcon.localDescription))
.catch(reject);
});
}
#letTheWorldKnow(localOffer) {
//We can let other peers know about our existence by any means, as long as this offer is sent to them
//console.log(localOffer);
//setTimeout(() =>this.player.play(), 2000);
}
#synchronizeSend() {
//console.log(SOUND_BPM);
//let real_aud_time = this.player.aud.currentTime;
}
//TODO: synchronizeRecv
#synchronizeRecv() { }
}
//Initialize
const gd = new GandalfPlayer(GANDALF_AUTOPLAY, GANDALF_DEBUG_ENABLE);
const sync = new WRTCSynchronizer(gd);
gd.load(SOUND_URL, VIDEO_URL).then(() => sync.start());
window.addEventListener('load', ()=>gd.add(document.body));
</script>
</head>
<body></body>
</html>