-
Notifications
You must be signed in to change notification settings - Fork 0
/
wf-tnt-delay-02.html
429 lines (387 loc) · 15.7 KB
/
wf-tnt-delay-02.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html>
<script>
function includeHTML() {
var z, i, elmnt, file, xhttp;
/*loop through a collection of all HTML elements:*/
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/*make an HTTP request using the attribute value as the file name:*/
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/*remove the attribute, and call this function once more:*/
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/*exit the function:*/
return;
}
}
};
</script>
<head>
<meta charset="UTF-8">
<title>Waveformer's Playground (work in progress)</title>
<link href="https://fonts.googleapis.com/css?family=Alata|Dosis|Didact|Roboto&display=swap" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="common.css"/>
<link rel="stylesheet" type="text/css" href="navbar.css"/>
<style>
body {
font-family: 'Roboto', sans-serif;
}
input[type=number] {
width: 90%;
margin: 4px 0;
box-sizing: content-box;
text-align: right;
}
table#delayTable {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
vertical-align: middle;
table-layout: fixed;
}
table#delayTable tr {
border: 1px solid black;
border-collapse: collapse;
}
table#delayTable td, table#delayTable th {
border: 1px solid black;
border-collapse: collapse;
padding: 8px;
}
table#paramsTable {
border: 1px solid black;
border-collapse: collapse;
text-align: left;
vertical-align: middle;
table-layout: auto;
}
table#paramsTable th {
border-collapse: collapse;
padding: 4px;
}
table#paramsTable t3 {
border-collapse: collapse;
padding: 4px;
}
table#euclideanParamsTable {
border: 1px solid black;
border-collapse: collapse;
text-align: left;
vertical-align: middle;
table-layout: auto;
}
table#euclideanParamsTable th {
border-collapse: collapse;
padding: 4px;
}
table#euclideanParamsTable t3 {
border-collapse: collapse;
padding: 4px;
}
table.center {
margin-left: auto;
margin-right: auto;
}
h1 {
text-align: center;
}
p.center {
text-align: center;
}
</style>
</head>
<body onload="buildTable()">
<div w3-include-html="navbar.html"></div>
<h1>Waveformer's Playground (work in progress)</h1>
<table id="paramsTable" class="center">
<tr>
<th>
<label for="bpm">BPM ♩</label>
</th>
<td>
<input type="number" id="bpm" value="120" size="10" maxlength="4" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
<tr>
<th>
<label for="numBeats">Number of beats ♩</label>
</tn>
<td>
<input type="number" id="numBeats" value="4" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
<tr>
<th>
<label for="numStepsPerBeat">Steps per beat</label>
</th>
<td>
<input type="number" id="numStepsPerBeat" value="4" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
</table>
<table id="euclideanParamsTable" class="center">
<tr>
<th>
<label for="euclideanOnsets">Onsets</label>
</th>
<td>
<input type="number" id="euclideanOnsets" value="5" size="10" maxlength="4" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
<tr>
<th>
<label for="euclideanSteps">Steps</label>
</tn>
<td>
<input type="number" id="euclideanSteps" value="16" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
<tr>
<th>
<label for="euclideanShift">Shift</label>
</th>
<td>
<input type="number" id="euclideanShift" value="0" onChange="buildTable()" onkeyup="buildTable()"/>
</td>
</tr>
</table>
<br/>
<table id="delayTable" class="center">
</table>
<p class="center">Click on a cell in the Delay (ms) row to add an echo on that step.</p>
<script>
var colors = new Array('#27AE60', '#3998DB', '#F1C40F', '#E74C3C', '#9B59B6', '#795548', '#BDC3C7', '#E91E63');
var grayColors = new Array('#EEEEEE', '#CCCCCC');
var defaultCellColor = '#FFFFFF';
document.body.addEventListener('click', clicked);
var bpm;
var numBeats;
var numStepsPerBeat;
var numSteps;
var timePerStep;
var echo; // array[numSteps], value = -1 means no echo on that step,
// otherwise value = number of the beat that echos on that step
var euclid; // array[numSteps] of type boolean. True if step has an onset. False if step doesn't have an onset.
function pad(num, width, c)
{
c = c || ' ';
num = num + '';
return num.length >= width ? num : new Array(width - num.length + 1).join(c) + num;
}
function buildEuclideanPattern(steps, onsets, shift)
{
var pattern = new Array(steps);
return pattern;
}
function buildTable()
{
var euclideanSteps = document.getElementById("euclideanSteps");
var euclideanOnsets = document.getElementById("euclideanOnsets");
var euclideanShift = document.getElementById("euclideanShift");
euclid = buildEuclideanPattern(euclideanSteps, euclideanOnsets, euclideanShift);
var table = document.getElementById("delayTable");
table.innerHTML="";
bpm = document.getElementById("bpm").value;
numStepsPerBeat = document.getElementById("numStepsPerBeat").value;
numBeats = document.getElementById("numBeats").value;
numSteps = numStepsPerBeat*numBeats;
timePerStep = 1000*60/bpm/numStepsPerBeat;
echo = new Array(numSteps);
for (var s=0; s<numSteps; s++)
{
if (s % numStepsPerBeat == 0)
echo[s] = Math.floor(s / numStepsPerBeat);
else
echo[s] = -1; // no echo on this step
}
// Beats
var row = document.createElement("tr");
table.appendChild(row);
var d = document.createElement("th");
row.appendChild(d);
var t = document.createTextNode("Beat");
d.appendChild(t);
for (var b=0; b<numBeats; b++)
{
var d = document.createElement("th");
d.style.backgroundColor = grayColors[b%grayColors.length];
d.colSpan = numStepsPerBeat;
var t = document.createTextNode(b + 1);
d.appendChild(t);
row.appendChild(d);
}
// Steps
row = document.createElement("tr");
table.appendChild(row);
var d = document.createElement("th");
row.appendChild(d);
var t = document.createTextNode("Step");
d.appendChild(t);
for (var b=0; b<numBeats; b++)
{
for (var s=0; s<numStepsPerBeat; s++)
{
var d = document.createElement("th");
d.style.backgroundColor = grayColors[b%grayColors.length];
var t = document.createTextNode(s+1);
var globalStep = b*numStepsPerBeat+s;
d.appendChild(t);
row.appendChild(d);
}
}
// Echo
row = document.createElement("tr");
table.appendChild(row);
var d = document.createElement("th");
row.appendChild(d);
var t = document.createTextNode("Echo");
d.appendChild(t);
for (var b=0; b<numBeats; b++)
{
for (var s=0; s<numStepsPerBeat; s++)
{
var d = document.createElement("th");
d.style.backgroundColor = grayColors[b%grayColors.length];
var t = document.createTextNode(b*numStepsPerBeat+s);
var globalStep = b*numStepsPerBeat+s;
d.appendChild(t);
row.appendChild(d);
}
}
// Delay
row = document.createElement("tr");
table.appendChild(row);
var d = document.createElement("th");
row.appendChild(d);
var t = document.createTextNode("Delay (ms)");
d.appendChild(t);
for (var b=0; b<numBeats; b++)
{
for (var s=0; s<numStepsPerBeat; s++)
{
var d = document.createElement("td");
d.id = b*numStepsPerBeat+s;
var str = Math.round(timePerStep*(b*numStepsPerBeat+s));
str = pad(str, 4, '0');
var t = document.createTextNode(str);
if (s==0)
{
d.style.backgroundColor = colors[b%colors.length];
d.style.fontWeight = "bold";
}
else
d.style.backgroundColor = defaultCellColor;
d.appendChild(t);
row.appendChild(d);
}
}
}
// Repeats echos for beat 1 for all of the other beats as well
function updateEchoArray()
{
for (var step=0; step<numSteps; step++)
{
if (echo[step] > 0)
echo[step] = -1; // clear all echos that are not echos from the first beat
}
for (var b=1; b<numBeats; b++)
{
for (var step=0; step<numSteps; step++)
{
if (echo[step] == 0)
{
echo[(b*numStepsPerBeat + step) % numSteps] = b;
}
}
}
}
function changeLuminance(hex, luminance)
{
var color = "#";
for (var i=0; i<3; i++)
{
var c = parseInt(hex.substr(i*2+1, 2), 16);
c = Math.round(Math.min(255, Math.max(0, c + (c * luminance)))).toString(16);
color += ("00" + c).substr(c.length);
}
return color;
}
function updateTableColors()
{
for (var step = 0; step < numSteps; step++)
{
var d = document.getElementById(step);
if (echo[step] == -1)
d.style.backgroundColor = defaultCellColor;
else
{
if (step % numStepsPerBeat == 0)
d.style.backgroundColor = colors[echo[step]];
else
d.style.backgroundColor = changeLuminance(colors[echo[step]], 0.1);
}
}
}
function clicked(event)
{
var clickedCell = false;
console.log("Clicked ", event);
for (var b=0; b<numBeats; b++)
{
for (var s=0; s<numStepsPerBeat; s++)
{
var d = document.getElementById(b*numStepsPerBeat+s);
if (d.contains(event.target))
{
clickedCell = true;
console.log("Clicked in cell on beat " + b + ", step " + s);
if (s==0)
{
console.log("Ignore click on first step");
}
else
{
var globalStep = b*numStepsPerBeat+s;
var echoBeat = echo[globalStep];
if (echoBeat == -1) // no previous echo on this step
{
echo[globalStep] = 0; // echo from beat 1
}
else if (echoBeat == 0) // echo from beat 1 on this step
{
echo[globalStep] = -1; // remove echo from beat 1
}
else {
var diff = (numSteps + globalStep - echoBeat * numStepsPerBeat) % numSteps;
echo[diff] = -1; // remove corresponding echo from beat 1
console.log("diff: ", diff);
}
}
}
}
}
if (clickedCell)
{
updateEchoArray();
updateTableColors();
}
}
</script>
<script>
includeHTML();
</script>
</body>
</html>