-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
248 lines (209 loc) · 7.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Cycling with Euclid - Generative music using JavaScript</title>
</head>
<body>
<style>
:root {
background-color: rgb(10, 10, 10);
}
body {
margin-inline: 1em;
margin-block: 1.8em;
}
@media (width > 500px) {
body {
margin-inline: 2em;
}
}
p {
margin-block: 3em;
}
a {
color: peru;
opacity: 0.8;
}
a:hover {
opacity: 1;
}
.container {
margin-block: 3em;
}
.beats {
height: 100px;
margin-block-start: 1em;
margin-block-end: 1.5em;
display: flex;
gap: 18px;
}
@media (width < 400px) {
.beats {
gap: min((100% - 12 * 10px) / 11, 18px);
}
}
.beats > div {
width: 10px;
border: 1px solid tomato;
box-sizing: border-box;
}
.beats > div.oob {
display: none;
}
.beats > div.on {
background-color: tomato;
}
</style>
<div class="container">
<div class="beats">
<div id="b11"></div>
<div id="b12"></div>
<div id="b13"></div>
<div id="b14"></div>
<div id="b15"></div>
<div id="b16"></div>
<div id="b17"></div>
<div id="b18"></div>
</div>
<div class="beats">
<div id="b21"></div>
<div id="b22"></div>
<div id="b23"></div>
<div id="b24"></div>
<div id="b25" class="oob"></div>
<div id="b26" class="oob"></div>
<div id="b27" class="oob"></div>
<div id="b28" class="oob"></div>
<div id="b29" class="oob"></div>
<div id="b210" class="oob"></div>
<div id="b211" class="oob"></div>
<div id="b212" class="oob"></div>
</div>
<div class="beats">
<div id="b31"></div>
<div id="b32"></div>
<div id="b33"></div>
<div id="b34"></div>
<div id="b35"></div>
<div id="b36"></div>
<div id="b37"></div>
<div id="b38"></div>
</div>
</div>
<button onclick="toggle()">Play</button>
<p>
<small>
<a href="https://github.com/mnvr/gm1k">Source code on GitHub</a>
</small>
</p>
<script>
// https://github.com/mnvr/mrmr.io/tree/main/pages/mj/euclid/er.js
const E = (k, n) => {
let s = Array(n)
.fill(0)
.map((_, i) => (i < k ? [1] : [0]));
let d = n - k;
n = Math.max(k, d);
k = Math.min(k, d);
let z = d;
while (z > 0 || k > 1) {
for (let i = 0; i < k; i++)
s[i].push(...s[s.length - 1 - i]);
s.splice(-k);
z = z - k;
d = n - k;
n = Math.max(k, d);
k = Math.min(k, d);
}
return s.flat();
};
/* Unlike *410*, this song doesn't continue playing in the
background in Safari when you switch tabs since Safari throttles
setInterval and co */
let context; /* Audio context */
let intervalID; /* The setInterval ID, set if we're playing */
let k = 3,
n = 4; /* Current E(k, n) for the middle beat */
let p = 0; /* Phase */
const n8 = 8;
const seq38 = E(3, 8);
const seq78 = E(7, 8);
const beep = (
duration,
attack = 0.001,
release = 0.1,
frequency = 440
) => {
const ctx = context;
const osc = new OscillatorNode(ctx, { frequency });
const env = new GainNode(ctx);
const t = ctx.currentTime;
env.gain.setValueCurveAtTime([0, 1], t, attack);
env.gain.setTargetAtTime(0, t + attack + duration, release / 5);
const mix = new GainNode(ctx, { gain: 0.1 });
osc.connect(env).connect(mix).connect(ctx.destination);
osc.start();
osc.stop(t + attack + duration + release);
};
function toggle() {
const button = document.getElementsByTagName("button")[0];
if (intervalID) {
clearInterval(intervalID);
intervalID = undefined;
button.innerText = "Play";
} else {
button.innerText = "Pause";
play();
}
}
function play() {
context = new AudioContext();
context.resume();
intervalID = setInterval(tick, 1000 / 7);
// The button interaction feels better without this initial tick
// tick();
}
function tick() {
document
.querySelectorAll(".on")
.forEach((e) => e.classList.remove("on"));
const seq = E(k, n);
const p8 = p % n8;
if (seq38[p8]) {
classList(`#b1${p8 + 1}`).add("on");
beep(0.01, 0.001, 0.1, 660);
}
if (seq[p]) {
classList(`#b2${p + 1}`).add("on");
beep(0.01);
beep(0.005, 0.001, 0.02, 660);
}
if (seq78[p8]) {
classList(`#b3${p8 + 1}`).add("on");
beep(0.1, 0.001, 0.1, 110);
}
p = p + 1;
if (p == n) {
p = 0;
k = k + 1;
if (k == n) {
k = n < 5 ? 1 : 3;
n = n + 1;
if (n == 13) {
n = 4;
for (let i = 5; i < 13; i++)
classList(`#b2${i}`).add("oob");
} else {
classList(`#b2${n}`).remove("oob");
}
}
}
}
function classList(selector) {
return document.querySelector(selector).classList;
}
</script>
</body>
</html>