// Instrument geometry as a cell-type mask (see CELL_* in sim.ts), with presets and a paint brush. import { CELL_FLUID, CELL_MOUTH, CELL_WALL } from "./sim.ts"; export type Preset = "empty" | "jet" | "edge-tone" | "stopped-pipe"; // Each preset has its own grid size (cells) and default listener position. The stopped pipe gets a // grid cropped close around it, which makes it about twice as fast as the 512 x 256 grid. export const PRESETS: { id: Preset; label: string; nx: number; ny: number; listener: [number, number] }[] = [ { id: "stopped-pipe", label: "Stopped pipe", nx: 448, ny: 128, listener: [96, 88] }, { id: "edge-tone", label: "Edge tone", nx: 512, ny: 256, listener: [110, 200] }, { id: "jet", label: "Jet", nx: 512, ny: 256, listener: [110, 200] }, { id: "empty", label: "Empty", nx: 512, ny: 256, listener: [110, 200] }, ]; export class Geometry { readonly cells: Uint32Array; constructor(readonly nx: number, readonly ny: number) { this.cells = new Uint32Array(nx * ny); } set(x: number, y: number, t: number) { if (x >= 0 && x < this.nx && y >= 0 && y < this.ny) this.cells[y * this.nx + x] = t; } fillRect(x0: number, y0: number, x1: number, y1: number, t: number) { for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) this.set(x, y, t); } // Round brush of radius r (cells) centered at (cx, cy). paint(cx: number, cy: number, r: number, t: number) { const R = Math.ceil(r); for (let dy = -R; dy <= R; dy++) for (let dx = -R; dx <= R; dx++) if (dx * dx + dy * dy <= r * r) this.set(Math.round(cx + dx), Math.round(cy + dy), t); } // Paint along a segment so fast drags leave no gaps. paintLine(x0: number, y0: number, x1: number, y1: number, r: number, t: number) { const n = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0) / Math.max(0.5, r / 2))); for (let k = 0; k <= n; k++) this.paint(x0 + ((x1 - x0) * k) / n, y0 + ((y1 - y0) * k) / n, r, t); } load(preset: Preset) { this.cells.fill(CELL_FLUID); if (preset === "empty") return; if (preset === "stopped-pipe") { this.stoppedPipe(); return; } // A windway (channel) blowing in +x, its jet centered on row cy. // The channel is 10 cells high (rows cy-4 .. cy+5); its axis is one cell above the grid's // mid-line (ny-1)/2, so the setup is not mirror-symmetric and the jet can break symmetry. const cy = Math.floor(this.ny / 2); const h = 10; const x0 = 60; // back of the windway const x1 = 150; // exit (flue) const wall = 6; const lo = cy - h / 2 + 1; // lowest open row const hi = lo + h - 1; // highest open row this.fillRect(x0, lo - wall, x1, lo - 1, CELL_WALL); this.fillRect(x0, hi + 1, x1, hi + wall, CELL_WALL); this.fillRect(x0 - 4, lo - wall, x0 - 1, hi + wall, CELL_WALL); this.fillRect(x0, lo, x0 + 1, hi, CELL_MOUTH + 0); if (preset === "edge-tone") { // A wedge with its tip d cells past the flue, half a cell off the jet axis, opening at +/- 15 degrees. const d = 30; const tipX = x1 + d; const len = 90; const slope = Math.tan((15 * Math.PI) / 180); const axis = (lo + hi) / 2; for (let x = tipX; x <= tipX + len; x++) { const half = (x - tipX) * slope; for (let y = Math.ceil(axis - half); y <= Math.floor(axis + half); y++) this.set(x, y, CELL_WALL); } this.set(tipX, Math.round(axis), CELL_WALL); } } // A stopped flue pipe (like a gedackt organ pipe or a recorder head with a closed foot). // A windway of height h ends at the window; the jet crosses the window (length w) and meets // the labium, the beveled upstream end of the pipe's upper wall. The bore (b cells wide) runs // len cells from the labium to a closed end. Sounding pitch is about c / (4 * (len + end corrections)): // ~250 Hz at dx = 1 mm. With w = 24 the jet sustains the fundamental at 20 m/s; at 30 m/s it // jumps to the third harmonic, and with w = 16 it does not sustain at 20 m/s. // The solid parts start at cell (32, 24), just inside a 16-cell sponge. private stoppedPipe() { const b = 20; const h = 5; const w = 24; const len = 300; const wall = 4; const xBack = 36; // inlet at the back of the windway const xw = xBack + 50; // windway exit const xLab = xw + w; const xEnd = xLab + len; const yb0 = 24 + wall; // lowest bore row const tip = yb0 + b; // row of the labium's lower surface; the bore's top row is tip - 1 const jlo = tip - Math.floor(h / 2); // windway rows jlo .. jhi, centered on the labium tip const jhi = jlo + h - 1; this.fillRect(xBack - 4, yb0 - wall, xEnd + wall, yb0 - 1, CELL_WALL); // lower pipe wall this.fillRect(xBack - 4, yb0, xw - 1, jlo - 1, CELL_WALL); // block below the windway this.fillRect(xBack - 4, jhi + 1, xw - 1, jhi + 6, CELL_WALL); // cap above the windway this.fillRect(xBack - 4, jlo, xBack - 1, jhi, CELL_WALL); this.fillRect(xBack, jlo, xBack + 1, jhi, CELL_MOUTH + 0); const slope = Math.tan((20 * Math.PI) / 180); // labium bevel on the outer side for (let x = xLab; x <= xEnd + wall; x++) { const top = Math.min(tip + wall - 1, tip + Math.floor((x - xLab) * slope)); for (let y = tip; y <= top; y++) this.set(x, y, CELL_WALL); } this.fillRect(xEnd + 1, yb0 - wall, xEnd + wall, tip + wall - 1, CELL_WALL); // stopped end } }