/ proof-of-concept / soundbox
Sign in
proof-of-concept / soundbox
soundbox / index.html
416 lines · 19.6 KBBlameHistoryRaw
1<!doctype html>
2<html lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta name="viewport" content="width=device-width, initial-scale=1" />
6 <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><rect width=%22100%22 height=%22100%22 fill=%22%23f2f2f2%22/><rect x=%2212%22 y=%2254%22 width=%2250%22 height=%2228%22 rx=%224%22 fill=%22%23a07040%22/><circle cx=%2237%22 cy=%2268%22 r=%226%22 fill=%22%23f2f2f2%22/><path d=%22M12 44 Q 37 30 62 44%22 stroke=%22%23b40426%22 stroke-width=%224%22 fill=%22none%22/><circle cx=%2278%22 cy=%2266%22 r=%2214%22 stroke=%22%233b4cc0%22 stroke-width=%224%22 fill=%22none%22/><path d=%22M70 30 L86 30%22 stroke=%22%233b4cc0%22 stroke-width=%224%22/><path d=%22M78 30 L78 52%22 stroke=%22%233b4cc0%22 stroke-width=%224%22/></svg>" />
7 <title>soundbox — build instruments out of wave equations</title>
8 <style>
9 :root {
10 --bg: #ffffff;
11 --ink: #1f2328;
12 --ink-2: #57606a;
13 --line: #d0d7de;
14 --accent: #0969da;
15 --panel-bg: #f4f6f8;
16 --tok-com: #6e7781;
17 --tok-str: #0a3069;
18 --tok-num: #0550ae;
19 --tok-kw: #cf222e;
20 --tok-ext: #8250df;
21 color-scheme: light dark;
22 }
23 @media (prefers-color-scheme: dark) {
24 :root {
25 --bg: #14171a;
26 --ink: #e6e9ec;
27 --ink-2: #9aa4af;
28 --line: #333b44;
29 --accent: #58a6ff;
30 --panel-bg: #14161c;
31 --tok-com: #8b949e;
32 --tok-str: #a5d6ff;
33 --tok-num: #79c0ff;
34 --tok-kw: #ff7b72;
35 --tok-ext: #d2a8ff;
36 }
37 }
38 body {
39 margin: 0;
40 background: var(--bg);
41 color: var(--ink);
42 font: 15px/1.5 system-ui, -apple-system, sans-serif;
43 }
44 main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 48px; }
45 h1 { font-size: 20px; margin: 0 0 2px; }
46 .sub { color: var(--ink-2); margin: 0 0 10px; font-size: 13px; }
47 .sub a { color: var(--accent); }
48 .controls {
49 display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center;
50 padding: 5px 0;
51 }
52 .controls label { color: var(--ink-2); font-size: 13px; white-space: nowrap; }
53 select, input[type="number"], button {
54 font: inherit; font-size: 13px;
55 color: var(--ink); background: var(--bg);
56 border: 1px solid var(--line); border-radius: 6px;
57 padding: 4px 8px;
58 }
59 input[type="range"] { width: 8em; vertical-align: middle; accent-color: var(--accent); }
60 button { cursor: pointer; }
61 button:hover { border-color: var(--accent); }
62 button:disabled { cursor: default; opacity: 0.5; }
63 button.primary { border-color: var(--accent); color: var(--accent); font-weight: 600; min-width: 5.5em; }
64 .sliders {
65 display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
66 gap: 2px 16px; padding: 4px 0;
67 }
68 .slider { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
69 .slider > span:first-child { flex: 0 0 9.5em; text-align: right; overflow: hidden; text-overflow: ellipsis; }
70 .slider > output { flex: 0 0 4.5em; font-variant-numeric: tabular-nums; color: var(--ink); }
71 .group-title {
72 font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em;
73 color: var(--ink-2); margin: 10px 0 0;
74 }
75 progress { width: 10em; height: 0.9em; }
76 #scenehint code {
77 font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
78 background: var(--panel-bg); border: 1px solid var(--line);
79 border-radius: 4px; padding: 1px 6px; user-select: all;
80 }
81 #scenehint button { font-size: 11px; padding: 1px 8px; margin-left: 4px; }
83 /* tabs */
84 .tabs { display: flex; gap: 2px; margin-top: 10px; border-bottom: 1px solid var(--line); }
85 .tabs button {
86 border: 1px solid transparent; border-bottom: none; border-radius: 6px 6px 0 0;
87 background: none; color: var(--ink-2); padding: 6px 14px; font-size: 14px;
88 }
89 .tabs button.active {
90 border-color: var(--line); background: var(--panel-bg); color: var(--ink); font-weight: 600;
91 }
92 .tabpanel { display: none; padding: 8px 0 4px; }
93 .tabpanel.active { display: block; }
95 /* preset cards */
96 .presets { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; padding: 6px 0; }
97 .preset {
98 text-align: left; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
99 background: var(--bg); cursor: pointer; font: inherit; color: var(--ink);
100 }
101 .preset b { display: block; font-size: 14px; }
102 .preset span { font-size: 12px; color: var(--ink-2); display: block; margin-top: 2px; }
103 .preset.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
105 /* object cards */
106 .objcard {
107 border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px 8px; margin-top: 8px;
108 }
109 .objcard-head { display: flex; align-items: center; gap: 10px; }
110 .objcard-head b { font-size: 14px; }
111 .objcard-head .swatch { width: 10px; height: 10px; border-radius: 5px; display: inline-block; }
112 .objcard-head span { flex: 1; }
113 .objcard-head button { padding: 1px 8px; font-size: 12px; }
115 #stringbox {
116 margin-top: 12px; border: 1px solid var(--line); border-radius: 8px;
117 background: var(--panel-bg); position: relative;
118 }
119 #objplot { width: 100%; height: 92px; display: block; }
120 #objlabel {
121 position: absolute; top: 4px; left: 8px; font-size: 11px;
122 color: var(--ink-2); font-variant-numeric: tabular-nums;
123 }
124 #objpick { position: absolute; top: 2px; right: 6px; font-size: 11px; padding: 1px 4px; }
125 #stage {
126 display: flex; gap: 14px; margin-top: 12px; align-items: stretch;
127 border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
128 }
129 .canvas-box {
130 flex: 1; aspect-ratio: 16 / 10; max-height: 70vh; position: relative;
131 background: #0b0e12;
132 }
133 #view { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
134 #overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
135 .colorbar {
136 display: flex; flex-direction: column; align-items: center; justify-content: center;
137 gap: 4px; padding: 8px 4px; background: var(--panel-bg);
138 width: 60px; flex: none; box-sizing: border-box;
139 }
140 .colorbar canvas { border: 1px solid var(--line); border-radius: 2px; }
141 .colorbar-label { font-size: 11px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
142 .readout { font-variant-numeric: tabular-nums; color: var(--ink); }
143 .warn { color: #b35900; font-weight: 600; }
144 .stats { margin-top: 8px; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
145 .stats b { color: var(--ink); font-weight: 600; }
146 #err {
147 color: #b35900; white-space: pre-wrap; font-size: 13px;
148 font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
149 }
150 #specbox {
151 margin-top: 10px; border: 1px solid var(--line); border-radius: 8px;
152 background: var(--panel-bg); position: relative;
153 }
154 #spectrum { width: 100%; height: 170px; display: block; }
155 #speclegend {
156 position: absolute; top: 4px; right: 8px; font-size: 11px; color: var(--ink-2);
157 display: flex; gap: 10px; flex-wrap: wrap;
158 }
159 #speclegend i { font-style: normal; }
160 .editor {
161 margin-top: 12px; border: 1px solid var(--line); border-radius: 8px;
162 overflow: hidden;
163 }
164 .editor-head {
165 display: flex; gap: 10px; align-items: center; justify-content: space-between;
166 padding: 6px 10px; font-size: 12px; color: var(--ink-2);
167 background: var(--panel-bg); border-bottom: 1px solid var(--line);
168 }
169 .editor-head button { padding: 2px 10px; font-size: 12px; }
170 /* The fixed height lives on the row, not on either child: sizing the row
171 makes both children stretch to one shared pixel height regardless of
172 either's font size. */
173 .editor-body { display: flex; align-items: stretch; height: 32em; }
174 .editor-code { position: relative; flex: 1 1 62%; min-width: 0; }
175 /* The overlay and the textarea must agree on every metric that affects
176 where a character lands. Keep these two rules together. */
177 .editor-code > pre,
178 .editor-code > textarea {
179 margin: 0; padding: 10px 12px; border: 0;
180 box-sizing: border-box; width: 100%; height: 100%;
181 font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
182 tab-size: 2;
183 white-space: pre; overflow-wrap: normal;
184 }
185 #highlight {
186 position: absolute; inset: 0; overflow: hidden;
187 pointer-events: none; background: var(--bg); color: var(--ink);
188 }
189 #source {
190 position: relative; z-index: 1; display: block;
191 resize: none; overflow: auto;
192 background: transparent; color: transparent; caret-color: var(--ink);
193 }
194 #source:focus { outline: none; }
195 /* Transparent text means the selection must be see-through, or selected
196 code would be invisible. */
197 #source::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }
198 #compiled {
199 flex: 1 1 38%; min-width: 0; margin: 0; padding: 10px 12px; overflow: auto;
200 border-left: 1px solid var(--line); background: var(--panel-bg);
201 font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
202 color: var(--ink-2); white-space: pre;
203 }
204 @media (max-width: 860px) {
205 .editor-body { flex-direction: column; height: auto; }
206 .editor-code { flex: none; height: 26em; }
207 #compiled { border-left: 0; border-top: 1px solid var(--line); max-height: 12em; }
208 }
209 .tok-com { color: var(--tok-com); }
210 .tok-str { color: var(--tok-str); }
211 .tok-num { color: var(--tok-num); }
212 .tok-kw { color: var(--tok-kw); font-weight: 600; }
213 .tok-ext { color: var(--tok-ext); }
214 </style>
215 </head>
216 <body>
217 <main>
218 <h1>soundbox</h1>
219 <p class="sub">
220 A small studio for instruments made of wave equations. Compose strings,
221 bars, membranes and a resonating body into a scene; the app writes the
222 coupled PDEs as MATLAB, <a href="https://numbl.org">numbl</a> compiles
223 them to WebGPU compute shaders in your browser, and a microphone in the
224 simulated air records what you then listen to. Play with a preset,
225 build your own in the Build tab, or open the Lab to read (and edit) the
226 equations and see the spectrum against where the partials should land.
227 </p>
228 <p class="sub" id="domaininfo"></p>
230 <div class="controls" id="transport">
231 <button id="excite" class="primary">Excite</button>
232 <button id="runpause" class="primary">Run</button>
233 <label title="Timesteps taken between display frames while watching live. Below 1×, one step is taken every few frames.">speed
234 <select id="spf">
235 <option value="0.25">¼×</option>
236 <option value="0.5">½×</option>
237 <option value="1">1×</option>
238 <option value="4">4×</option>
239 <option value="16" selected>16×</option>
240 <option value="64">64×</option>
241 <option value="256">256×</option>
242 </select>
243 </label>
244 <span style="flex:1"></span>
245 <label>render
246 <select id="renderdur">
247 <option value="1">1 s</option>
248 <option value="2" selected>2 s</option>
249 <option value="3">3 s</option>
250 <option value="5">5 s</option>
251 <option value="10">10 s</option>
252 <option value="20">20 s</option>
253 </select>
254 </label>
255 <button id="renderclip" title="Excite, then run the solver flat out with no display until this much audio is recorded, then play it">Render clip</button>
256 <button id="fastrender" title="Record the room's impulse responses once (about the cost of half a second of ordinary rendering), then synthesize the clip from every object's closed-form modes. Same numbers as the solver; after the first recording, every knob re-renders in about a second. Body, object size or placement, grid and mic edits re-record the room. Needs the generated model .m.">Fast render</button>
257 <progress id="renderprog" value="0" max="1" hidden></progress>
258 <button id="listen" title="Play what the microphone has recorded since the last excite, in real time at its real pitch">Listen</button>
259 <button id="download" title="Save the recording as a 48 kHz .wav">Save .wav</button>
260 <button id="savescene" title="Download this scene — objects, body, every knob, the mic, and any hand-edited .m — as a .json that loads back here or renders on another machine with `npm run render`">Save scene</button>
261 <button id="loadscene" title="Load a scene .json saved from this page">Load scene</button>
262 <input type="file" id="loadfile" accept=".json,application/json" hidden />
263 <span class="stats" id="recinfo"></span>
264 </div>
265 <p class="sub" id="scenehint" hidden></p>
267 <div class="tabs">
268 <button id="tab-play" class="active">Play</button>
269 <button id="tab-build">Build</button>
270 <button id="tab-lab">Lab</button>
271 </div>
273 <section id="panel-play" class="tabpanel active">
274 <div class="presets" id="presets"></div>
275 <p class="group-title">tuning — free to move, even while it plays</p>
276 <div class="sliders" id="musicalparams"></div>
277 <p class="group-title">shape — the instrument itself</p>
278 <p class="sub" style="margin-top:0" title="Sizes recompile the solver (a brief pause); body shape re-evaluates the scene in place. Either way the geometry changed, so the next Fast render re-records its room.">
279 Sizes recompile; body shape applies in place. Both change the geometry,
280 so the next Fast render re-records the room. The full set of shape and
281 placement controls is in the Build tab.
282 </p>
283 <div class="sliders" id="shapeparams"></div>
284 </section>
286 <section id="panel-build" class="tabpanel">
287 <div class="controls">
288 <span class="group-title" style="margin:0">objects</span>
289 <button id="add-string">+ string</button>
290 <button id="add-bar">+ bar</button>
291 <button id="add-membrane">+ membrane</button>
292 <span style="flex:1"></span>
293 <label title="Air grid points along the long axis; across and up get half each. Changing it recompiles.">air grid
294 <select id="gridsize">
295 <option value="64">64 (draft)</option>
296 <option value="96" selected>96</option>
297 <option value="128">128</option>
298 <option value="160">160</option>
299 <option value="192">192</option>
300 <option value="256">256 (slow)</option>
301 </select>
302 </label>
303 </div>
304 <div id="objects"></div>
305 <div class="controls" style="margin-top:10px">
306 <span class="group-title" style="margin:0">body</span>
307 <select id="bodytype">
308 <option value="none">open air</option>
309 <option value="box">box</option>
310 <option value="shell">drum shell</option>
311 </select>
312 <span class="sub" id="bodyblurb" style="margin:0"></span>
313 </div>
314 <div class="sliders" id="bodyparams"></div>
315 <p class="group-title">microphone</p>
316 <div class="sliders" id="micparams"></div>
317 </section>
319 <section id="panel-lab" class="tabpanel">
320 <div id="specbox">
321 <canvas id="spectrum"></canvas>
322 <div id="speclegend"></div>
323 </div>
324 <p class="sub" id="specinfo" style="margin:4px 0 0">
325 The measured spectrum of the recording (analyzed after Listen or
326 Render clip), against each object's predicted partials: solid marks
327 are the discrete scheme's own modes, dashed the continuous
328 equations'. The gap between them is the numerical dispersion — on a
329 coarse bar grid you can hear it.
330 </p>
331 <div class="editor">
332 <div class="editor-head">
333 <span>
334 <select id="editor-file" aria-label="file to edit">
335 <option value="model">model (.m) — the solvers, compiled to WebGPU</option>
336 <option value="scene">scene (.m) — bodies and coupling, CPU-evaluated</option>
337 </select>
338 <span id="editor-title"></span>
339 </span>
340 <span>
341 <button id="recompile" type="button">Run edits</button>
342 <button id="revert" type="button" title="Throw away hand edits and regenerate this file from the scene the Build tab describes">Regenerate</button>
343 </span>
344 </div>
345 <div class="editor-body">
346 <div class="editor-code">
347 <pre id="highlight" aria-hidden="true"></pre>
348 <textarea
349 id="source"
350 spellcheck="false"
351 autocomplete="off"
352 autocapitalize="off"
353 aria-label="source (MATLAB)"
354 ></textarea>
355 </div>
356 <pre id="compiled"></pre>
357 </div>
358 </div>
359 </section>
361 <div class="controls">
362 <label>colormap
363 <select id="colormap"></select>
364 </label>
365 <label title="Pressure the colour scale saturates at. Auto follows the field.">scale
366 <select id="scalemode">
367 <option value="auto" selected>auto</option>
368 <option value="fixed">hold</option>
369 </select>
370 </label>
371 <label>opacity
372 <input type="range" id="opacity" min="0.2" max="6" step="0.1" value="2" />
373 </label>
374 <label>contrast
375 <input type="range" id="contrast" min="0.5" max="3" step="0.1" value="1.6" />
376 </label>
377 <label title="Ray-march samples per pixel ray">quality
378 <select id="quality">
379 <option value="96">96</option>
380 <option value="192" selected>192</option>
381 <option value="320">320</option>
382 </select>
383 </label>
384 <label title="Cut the picture open: everything on the far side of this y-plane is hidden">clip
385 <input type="range" id="clipy" min="-0.5" max="0.5" step="0.01" value="0.5" />
386 </label>
387 <label title="How many times larger than life displacements are drawn in the 3D view">motion ×
388 <input type="range" id="exaggerate" min="1" max="100" step="1" value="25" />
389 </label>
390 <label><input type="checkbox" id="showfield" checked /> sound</label>
391 <label><input type="checkbox" id="showbody" checked /> body</label>
392 <label><input type="checkbox" id="showobjects" checked /> objects</label>
393 <label><input type="checkbox" id="showwire" checked /> outline</label>
394 <label><input type="checkbox" id="showmic" checked /> mic</label>
395 </div>
397 <div id="stage">
398 <div class="canvas-box">
399 <canvas id="view"></canvas>
400 <canvas id="overlay"></canvas>
401 </div>
402 <div id="colorbar"></div>
403 </div>
405 <div id="stringbox">
406 <canvas id="objplot"></canvas>
407 <span id="objlabel"></span>
408 <select id="objpick"></select>
409 </div>
411 <p class="stats" id="stats"></p>
412 <p id="err"></p>
413 </main>
414 <script type="module" src="/src/main.ts"></script>
415 </body>
416</html>
↑↓ move↵openescclose