-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
256 lines (226 loc) · 6.67 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Polyrhythm Builder</title>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script src="https://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js"></script>
<style>
body {
margin: 0;
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
p {
font-family: sans-serif;
}
#controls {
width: 50vw;
display: flex;
justify-content: space-around;
flex-direction: column;
margin-bottom: 32px;
}
@media only screen and (max-width: 600px) {
#controls {
width: 98vw;
}
}
.control-group {
display: flex;
justify-content: space-around;
margin-bottom: 16px;
/*overflow-x: scroll;*/
}
#grid {
display: grid;
width: calc(25vw / 2);
height: 25vw;
grid-template-columns: repeat(1, 1fr);
grid-template-rows: repeat(2, 1fr);
}
.cell {
border: 1px solid #ccc;
display: flex;
justify-content: center;
align-items: center;
}
.note {
background-color: #ff4d4d;
width: 80%;
height: 80%;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="controls">
<div class="control-group">
<button id="start">Start/Stop</button>
</div>
<div class="control-group">
<p>Polyrhythm</p>
</div>
<div class="control-group" id="rhythm-inputs">
<input type="number" class="rhythm" min="1" value="1" oninput="updatePoly()">
<p>:</p>
<input type="number" class="rhythm" min="1" value="1" oninput="updatePoly()">
</div>
<div class="control-group">
<button id="rhythm-add">Add</button>
<button id="rhythm-delete">Remove</button>
</div>
<div class="control-group">
<p>BPM</p>
<input type="number" id="bpm" name="bpm" min="1" value="60">
</div>
<div class="control-group">
<p>Bottomost number gets the beat</p>
</div>
</div>
<div id="grid">
<div class="cell"><div class="note"></div></div>
<div class="cell"><div class="note"></div></div>
<div class="cell"><div class="note"></div></div>
</div>
<script type="text/javascript">
// Utility
function findGCD(a, b) {
return a ? findGCD(b % a, a) : b;
}
function findLCM(a, b) {
return a * b / findGCD(a, b);
}
var unlocked = false; // Unlocked audio buffer
var running = false;
var lcm = 1;
// This here is magic
timeWorker = new Worker("metronomeworker.js");
timeWorker.onmessage = function(e) {
if (e.data == "tick") {
console.log("ticked");
scheduler();
}
}
// Sound stuff
var audioContext = new AudioContext();
var nextNoteTime = 0.0;
var noteLength = 0.05;
var notesInQueue = [];
var currentNote = 0;
var scheduleAheadTime = 0.1;
// Start the metronome
$('#start').click(function(){
if (!unlocked) {
var buffer = audioContext.createBuffer(1, 1, 22050);
var node = audioContext.createBufferSource();
node.buffer= buffer;
node.start(0);
unlocked = true;
}
if (!running) {
nextNoteTime = audioContext.currentTime;
timeWorker.postMessage("start");
running = true;
} else {
timeWorker.postMessage("stop");
running = false;
}
});
// Next note
function nextNote() {
var secondsPerBeat = 60.0 / $('#bpm').val() * $($('.rhythm')[$('.rhythm').length - 1]).val();
nextNoteTime += secondsPerBeat / lcm;
currentNote++;
if (currentNote == lcm)
currentNote = 0;
}
function scheduleNote(beatNumber, time) {
notesInQueue.push({note: beatNumber, time: time});
var frequency = 0;
for (var i = 0; i < $('.rhythm').length; i++) {
if (beatNumber % $($('.rhythm')[i]).val() == 0) {
if (frequency == 0)
frequency = 660 + 10 * i;
frequency += 400;
}
}
var osc = audioContext.createOscillator();
osc.connect(audioContext.destination);
osc.frequency.value = frequency;
osc.start(time);
osc.stop(time + noteLength);
$('.cell').css({'background-color': 'transparent'});
for (var r = 0; r < $('.rhythm').length; r++) {
for (var cell = lcm * r; cell < lcm * (r + 1); cell++) {
if ((cell % lcm) == beatNumber) {
$($('.cell')[cell]).css({'background-color': '#ababab'});
}
}
}
}
function scheduler() {
while (nextNoteTime < audioContext.currentTime + scheduleAheadTime) {
scheduleNote(currentNote, nextNoteTime);
nextNote();
}
}
// Visual stuff
function updatePoly() {
var rhythms = [];1
for (var r = 0; r < $('.rhythm').length; r++) {
rhythms.push($($('.rhythm')[r]).val());
}
// TODO: Actually find the lcm
lcm = rhythms.reduce(findLCM);
console.log(lcm);
// Set dimensions
$('#grid').css({
'grid-template-columns': 'repeat(' + lcm + ', 1fr)',
'grid-template-rows': 'repeat(' + rhythms.length + ', 1fr)',
'width': 'calc(calc(25vw / ' + rhythms.length + ') * ' + lcm + ')'
});
if ($('#grid').width() > $(window).width()) {
$('#grid').css({'width': '50vw', 'height': 'calc(calc(50vw / ' + lcm + ') * ' + rhythms.length + ')'});
}
else {
$('#grid').css({'height': '25vw'});
}
// Fill up grid
$('#grid').empty();
for (var i = 0; i < lcm * rhythms.length; i++) {
$('#grid').append('<div class="cell"></div>')
}
// Loop through every rhythm
for (var r = 0; r < rhythms.length; r++) {
// Loop through the cells
for (var cell = lcm * r; cell < lcm * (r + 1); cell++) {
if ((cell % lcm) % rhythms[rhythms.length - 1 - r] == 0) {
$($('.cell')[cell]).append('<div class="note"></div>');
}
}
}
}
// Add rhythms
$('#rhythm-add').click(function(){
$('#rhythm-inputs').append('<p>:</p><input type="number" class="rhythm" min="1" value="1" oninput="updatePoly()">');
updatePoly();
});
$('#rhythm-delete').click(function(){
if ($('.rhythm').length > 1) {
$('#rhythm-inputs').children().last().remove();
$('#rhythm-inputs').children().last().remove();
updatePoly();
}
});
</script>
</body>
</html>