-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·126 lines (105 loc) · 2.68 KB
/
index.html
File metadata and controls
executable file
·126 lines (105 loc) · 2.68 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Example usage of clocksync.js</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="libs/buffer-loader.js"></script>
<script src="libs/raf.js"></script>
<script src="clocksync.js"></script>
<script>
</script>
<style>
html, body {
font-family: Helvetica, sans serif;
font-size: 2em;
height: 100%;
background: #eee;
}
</style>
</head>
<body>
<div id="clock"></a>
<script>
var $clock = $('#clock');
var context = new webkitAudioContext();
var bufferLoader;
var bufferList;
function initsounds() {
bufferLoader = new BufferLoader(
context,
[
'sounds/Kick.mp3',
'sounds/beep.mp3'
],
function (b) {
bufferList = b;
}
);
bufferLoader.load();
};
var beep = function (time) {
if (!bufferList) {
return;
}
var source1 = context.createBufferSource();
source1.buffer = bufferList[1];
source1.connect(context.destination);
source1.noteOn(time);
};
var lastPlay = -1;
// scheduling based on server time more stable on mobile (context time stops when app looses focus?)
var scheduleSound = function (serverTime) {
nowMs = serverTime * .001;
ctxTime = context.currentTime;
nextSecond = Math.ceil(nowMs);
if (nextSecond > lastPlay) {
lastPlay = nextSecond
msToNextS = nextSecond - nowMs;
var time = ctxTime + msToNextS;
beep(time);
}
};
// schedule based on audiocontext time
/*
var scheduleSound = function (serverTime) {
currentTime = context.currentTime;
if (lastPlay === -1) {
// schedule first play based on server time
nowMs = serverTime * .001;
nextSecond = Math.ceil(nowMs);
msToNextS = nextSecond - nowMs;
nextTime = currentTime + msToNextS;
}
else {
nextTime = lastPlay + 1;
}
if (currentTime > lastPlay) {
lastPlay = nextTime;
beep(nextTime);
}
};
*/
var runLoop = function () {
window.requestAnimationFrame(runLoop);
var time = clocksync.time();
scheduleSound(time);
$clock.text(time);
}
$('body').on('touchstart', function () {
context.createBufferSource().noteOn(0);
//lastPlay = -1;
});
initsounds();
// setup a sync method using ajax
clocksync.syncMethod = function () {
$.post('/sync', clocksync.data(), clocksync.syncResponse, 'text');
}
// start synching
clocksync.sync(function (now, delta) {
console.log('obtained synched time:', now, ' local delta: ', delta);
window.requestAnimationFrame(runLoop);
});
</script>
</body>
</html>