-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebaudioclock.html
233 lines (229 loc) · 6.81 KB
/
webaudioclock.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>🌱 let's make a clock</title>
<style>
body {
background-color: #222;
max-width: 512px;
margin: auto;
font-family: serif;
font-size: 1.2em;
color: #edd;
text-align: left;
padding: 20px 8px;
}
@font-face {
font-family: "FontWithASyntaxHighlighter";
src: url("/fonts/FontWithASyntaxHighlighter-Regular.woff2")
format("woff2");
}
pre,
textarea {
font-size: 12px;
font-family: "FontWithASyntaxHighlighter", monospace;
padding: 8px;
border: 0;
outline: none;
overflow: auto;
background-color: #44444490;
width: 100%;
margin: 0px 0;
color: white;
box-sizing: border-box;
}
a {
color: cyan;
font-size: 1em;
}
</style>
</head>
<body>
<h2>🌱 let's make a clock</h2>
<button id="play">play</button>
<button id="stop">stop</button>
<textarea id="code" spellcheck="false" rows="12"></textarea>
<script>
class Clock {
constructor(ac, onTick) {
this.ac = ac;
this.runs = false;
this.onTick = onTick;
return this;
}
timeout(onComplete, startTime, stopTime) {
const constantNode = this.ac.createConstantSource();
constantNode.start(startTime);
constantNode.stop(stopTime);
constantNode.onended = () => onComplete();
}
stop() {
this.runs = false;
}
start(begin = ac.currentTime + 0.01, duration = 0.1) {
if (this.runs) {
return;
}
this.runs = true;
this.tick(begin, duration);
}
tick(begin, duration) {
this.runs = true;
this.onTick(begin);
const end = begin + duration;
this.timeout(
() => {
this.runs && this.tick(end, duration);
},
begin,
end
);
}
}
</script>
<script>
function livecode(input, initialCode, onUpdate, onStop) {
const updateHash = (code) => {
window.location.hash = "#" + btoa(code);
};
window.addEventListener("hashchange", function () {
const urlCode = atob(window.location.hash.slice(1));
if (urlCode) {
input.value = urlCode;
onUpdate(input.value, false);
}
});
// update on ctrl+enter
input.addEventListener("keydown", (e) => {
if ((e.ctrlKey || e.altKey) && e.key === "Enter") {
updateHash(input.value);
onUpdate(input.value, false);
}
if ((e.ctrlKey || e.altKey) && e.code === "Period") {
e.preventDefault();
onStop();
}
});
// read base64 code from url
let urlCode = window.location.hash.slice(1);
if (urlCode) {
urlCode = atob(urlCode);
console.log("loaded code from url!");
}
const code = urlCode || initialCode;
input.value = code;
onUpdate(code, true);
}
</script>
<script>
class Sequencer {
constructor(ac) {
this.tick = 0;
this.seqs = [];
if (ac) {
this.init(ac);
}
}
async loadSamples(map) {
const samples = Object.entries(map).map(async ([name, url]) => {
const res = await fetch(url);
const buf = await res.arrayBuffer();
const audioBuffer = await this.ac.decodeAudioData(buf);
return [name, audioBuffer];
});
return Object.fromEntries(await Promise.all(samples));
}
init(ac, samples) {
this.ac = ac;
this.clock = new Clock(ac, async (a) => {
const t = a + 0.1;
this.seqs.forEach((seq) => this.playStep(seq, t));
this.tick++;
});
this.samplesLoaded = this.loadSamples(samples);
}
start() {
this.clock.start(this.ac.currentTime + 0.01, 0.2);
}
stop() {
this.clock.stop();
}
async playStep(seq, t) {
const step = seq[this.tick % seq.length];
const isRest = step === " ";
if (isRest) {
return;
}
const bufs = await this.samplesLoaded;
if (this.ac.currentTime >= t) {
console.log("too late", this.ac.currentTime - t);
return;
}
if (!bufs[step]) {
console.log(`sound ${step} not loaded`);
return;
}
const src = this.ac.createBufferSource();
src.buffer = bufs[step];
src.connect(this.ac.destination);
src.start(t);
}
}
</script>
<script>
const sequencer = new Sequencer();
const audioReady = new Promise((resolve) => {
document.addEventListener("click", function init() {
const ac = new AudioContext();
document.removeEventListener("click", init);
resolve(ac);
});
});
livecode(
document.querySelector("#code"),
` s
b b b
hh h hh `,
async (code, init) => {
console.log("update", code);
lines = code.split("\n");
sequencer.seqs = lines.map((line) => line.split(""));
await audioReady;
!init && sequencer.start();
},
() => sequencer.stop()
);
document.querySelector("#play").addEventListener("click", async () => {
await audioReady;
sequencer.start();
});
document.querySelector("#stop").addEventListener("click", async () => {
await audioReady;
sequencer.stop();
});
audioReady.then((ac) => {
sequencer.init(ac, {
h: "https://raw.githubusercontent.com/tidalcycles/Dirt-Samples/refs/heads/master/hh/000_hh3closedhh.wav",
b: "https://raw.githubusercontent.com/tidalcycles/Dirt-Samples/refs/heads/master/bd/BT0A0A7.wav",
s: "https://raw.githubusercontent.com/tidalcycles/Dirt-Samples/refs/heads/master/sd/rytm-00-hard.wav",
});
});
</script>
<!---->
<details>
<summary id="loc">show page source</summary>
<pre id="pre"></pre>
</details>
<br />
<a href="/">back to garten.salat</a>
<script type="module">
// render source code
const html = document.querySelector("html").outerHTML;
const loc = html.split("\n").length;
document.querySelector("#pre").textContent = html;
document.querySelector("#loc").textContent = `show source (${loc} loc)`;
</script>
</body>
</html>