/ simulated_instruments / aerophone
Sign in
simulated_instruments / aerophone
aerophone / src / geometry.ts
113 lines · 5.3 KBCodeBlameHistory
b8aa175aerophone: 2D wind instruments with lattice Boltzmann on WebGPUJeremy Magland 1// Instrument geometry as a cell-type mask (see CELL_* in sim.ts), with presets and a paint brush.
2import { CELL_FLUID, CELL_MOUTH, CELL_WALL } from "./sim.ts";
4export type Preset = "empty" | "jet" | "edge-tone" | "stopped-pipe";
6// Each preset has its own grid size (cells) and default listener position. The stopped pipe gets a
7// grid cropped close around it, which makes it about twice as fast as the 512 x 256 grid.
8export const PRESETS: { id: Preset; label: string; nx: number; ny: number; listener: [number, number] }[] = [
9 { id: "stopped-pipe", label: "Stopped pipe", nx: 448, ny: 128, listener: [96, 88] },
10 { id: "edge-tone", label: "Edge tone", nx: 512, ny: 256, listener: [110, 200] },
11 { id: "jet", label: "Jet", nx: 512, ny: 256, listener: [110, 200] },
12 { id: "empty", label: "Empty", nx: 512, ny: 256, listener: [110, 200] },
13];
15export class Geometry {
16 readonly cells: Uint32Array<ArrayBuffer>;
18 constructor(readonly nx: number, readonly ny: number) {
19 this.cells = new Uint32Array(nx * ny);
20 }
22 set(x: number, y: number, t: number) {
23 if (x >= 0 && x < this.nx && y >= 0 && y < this.ny) this.cells[y * this.nx + x] = t;
24 }
26 fillRect(x0: number, y0: number, x1: number, y1: number, t: number) {
27 for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) this.set(x, y, t);
28 }
30 // Round brush of radius r (cells) centered at (cx, cy).
31 paint(cx: number, cy: number, r: number, t: number) {
32 const R = Math.ceil(r);
33 for (let dy = -R; dy <= R; dy++)
34 for (let dx = -R; dx <= R; dx++)
35 if (dx * dx + dy * dy <= r * r) this.set(Math.round(cx + dx), Math.round(cy + dy), t);
36 }
38 // Paint along a segment so fast drags leave no gaps.
39 paintLine(x0: number, y0: number, x1: number, y1: number, r: number, t: number) {
40 const n = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0) / Math.max(0.5, r / 2)));
41 for (let k = 0; k <= n; k++) this.paint(x0 + ((x1 - x0) * k) / n, y0 + ((y1 - y0) * k) / n, r, t);
42 }
44 load(preset: Preset) {
45 this.cells.fill(CELL_FLUID);
46 if (preset === "empty") return;
47 if (preset === "stopped-pipe") {
48 this.stoppedPipe();
49 return;
50 }
51 // A windway (channel) blowing in +x, its jet centered on row cy.
52 // The channel is 10 cells high (rows cy-4 .. cy+5); its axis is one cell above the grid's
53 // mid-line (ny-1)/2, so the setup is not mirror-symmetric and the jet can break symmetry.
54 const cy = Math.floor(this.ny / 2);
55 const h = 10;
56 const x0 = 60; // back of the windway
57 const x1 = 150; // exit (flue)
58 const wall = 6;
59 const lo = cy - h / 2 + 1; // lowest open row
60 const hi = lo + h - 1; // highest open row
61 this.fillRect(x0, lo - wall, x1, lo - 1, CELL_WALL);
62 this.fillRect(x0, hi + 1, x1, hi + wall, CELL_WALL);
63 this.fillRect(x0 - 4, lo - wall, x0 - 1, hi + wall, CELL_WALL);
64 this.fillRect(x0, lo, x0 + 1, hi, CELL_MOUTH + 0);
65 if (preset === "edge-tone") {
66 // A wedge with its tip d cells past the flue, half a cell off the jet axis, opening at +/- 15 degrees.
67 const d = 30;
68 const tipX = x1 + d;
69 const len = 90;
70 const slope = Math.tan((15 * Math.PI) / 180);
71 const axis = (lo + hi) / 2;
72 for (let x = tipX; x <= tipX + len; x++) {
73 const half = (x - tipX) * slope;
74 for (let y = Math.ceil(axis - half); y <= Math.floor(axis + half); y++) this.set(x, y, CELL_WALL);
75 }
76 this.set(tipX, Math.round(axis), CELL_WALL);
77 }
78 }
80 // A stopped flue pipe (like a gedackt organ pipe or a recorder head with a closed foot).
81 // A windway of height h ends at the window; the jet crosses the window (length w) and meets
82 // the labium, the beveled upstream end of the pipe's upper wall. The bore (b cells wide) runs
83 // len cells from the labium to a closed end. Sounding pitch is about c / (4 * (len + end corrections)):
84 // ~250 Hz at dx = 1 mm. With w = 24 the jet sustains the fundamental at 20 m/s; at 30 m/s it
85 // jumps to the third harmonic, and with w = 16 it does not sustain at 20 m/s.
86 // The solid parts start at cell (32, 24), just inside a 16-cell sponge.
87 private stoppedPipe() {
88 const b = 20;
89 const h = 5;
90 const w = 24;
91 const len = 300;
92 const wall = 4;
93 const xBack = 36; // inlet at the back of the windway
94 const xw = xBack + 50; // windway exit
95 const xLab = xw + w;
96 const xEnd = xLab + len;
97 const yb0 = 24 + wall; // lowest bore row
98 const tip = yb0 + b; // row of the labium's lower surface; the bore's top row is tip - 1
99 const jlo = tip - Math.floor(h / 2); // windway rows jlo .. jhi, centered on the labium tip
100 const jhi = jlo + h - 1;
101 this.fillRect(xBack - 4, yb0 - wall, xEnd + wall, yb0 - 1, CELL_WALL); // lower pipe wall
102 this.fillRect(xBack - 4, yb0, xw - 1, jlo - 1, CELL_WALL); // block below the windway
103 this.fillRect(xBack - 4, jhi + 1, xw - 1, jhi + 6, CELL_WALL); // cap above the windway
104 this.fillRect(xBack - 4, jlo, xBack - 1, jhi, CELL_WALL);
105 this.fillRect(xBack, jlo, xBack + 1, jhi, CELL_MOUTH + 0);
106 const slope = Math.tan((20 * Math.PI) / 180); // labium bevel on the outer side
107 for (let x = xLab; x <= xEnd + wall; x++) {
108 const top = Math.min(tip + wall - 1, tip + Math.floor((x - xLab) * slope));
109 for (let y = tip; y <= top; y++) this.set(x, y, CELL_WALL);
110 }
111 this.fillRect(xEnd + 1, yb0 - wall, xEnd + wall, tip + wall - 1, CELL_WALL); // stopped end
112 }
↑↓ move↵openescclose