| src | |
| .gitignore | |
| index.html | |
| package-lock.json | |
| package.json | |
| README.md | |
| tsconfig.json | |
| vite.config.ts |
aerophone#
2D wind instruments simulated as compressible air flow in the browser. A lattice Boltzmann solver (D2Q9) on WebGPU carries both the flow in the instrument and the sound it radiates into free space; a listener probe records pressure, and the recording is played back at real speed. The simulation runs slower than real time (see Speed).
npm install
npm run dev
npm run check # tsc
Status#
Steps 1-3: free-space sound; walls, a blown mouth and a paint editor; a listener with record and playback. Presets: Stopped pipe (the default; a flue pipe with a closed end that sounds a steady, nearly pure tone, on a 448 × 128 grid cropped around it), Jet (a 10-cell windway blowing into open space) and Edge tone (the same jet hitting a 30-degree wedge 30 cells downstream), both on 512 × 256, as is Empty. Each preset sets its grid size and listener position. Tools: Pulse (click for a pressure pulse), Wall, Erase, Mouth (with a blowing direction), Listener (click or drag the white ring). Views: Combined (acoustic pressure with vorticity overlaid), Acoustic, Pressure, Vorticity, Speed. Blow ramps the mouth to the set speed, and Stop blowing back to zero, along a raised cosine lasting the Attack time (default 40 ms of simulated time). The ramp is evaluated every step on the GPU (a step counter ping-pongs between two words, selected by the bind group's parity), so it does not jump at frame boundaries; changing Speed while blowing also ramps.
Record, Play, Download WAV: the listener records its density fluctuation every step (on the GPU, up to 2^22 steps, about 7 s of sound at dx = 1 mm). On stop, the recording is resampled to 48 kHz (windowed sinc, cutoff 0.45 of the lower rate), high-passed at 20 Hz, and shown as a waveform and a spectrum (log frequency, 80 dB range, strongest peak marked with its note). Playback and the WAV are normalized to 0.9 peak; the level in Pa is reported. Levels are 2D levels (cylindrical spreading, 1/sqrt(r)), not comparable to 3D.
Planned: pitch versus pipe length, open pipes.
Model#
- D2Q9, regularized BGK collision (non-equilibrium part projected onto the stress tensor) with an optional Smagorinsky eddy viscosity (C = 0.1 by default), pull streaming, one fused kernel per step. Stored shifted,
h_q = f_q - w_q, so small acoustic fluctuations keep float32 precision. - Lattice units dx = dt = 1, sound speed c_s = 1/sqrt(3). Physical scale: dx (mm) is a UI setting; dt = c_s dx / 343 m/s (1.68 µs at dx = 1 mm). A speed U is U c_s / 343 cells per step, independent of dx. Pressure p' = rho0 c^2 (rho - 1).
- Viscosity nu = (tau - 1/2)/3 in lattice units. Default tau = 0.503: about 6e-4 m²/s at dx = 1 mm, 40 times air; Re of the 10 mm windway at 20 m/s is about 340.
- Walls: halfway bounce-back. Mouth: a wall moving at the blowing speed (Ladd's moving-wall bounce-back), which pushes air in like a piston; the speed is the windway's mean speed (the centerline is about 1.5 times that).
- Edges: a 16-cell sponge relaxes toward rest density and a running-mean velocity (rate 2e-4 per step, about 8 ms at dx = 1 mm), with strength 0.3 (d/W)^2. Beyond the edge, populations are copied from the edge cell. Damping the velocity fully absorbs sound best but holds the jet in like a soft wall; damping toward the running mean lets the steady outflow leave while still absorbing sound.
Checks (512 × 256)#
- Wave speed: pulse peak moves 0.575 cells/step along x and along the diagonal (c_s = 0.577).
- Absorption: 700 steps after the pulse reaches the sponge, interior energy is 3e-5 of the initial value (2D waves also leave a trailing wake, so this bounds reflection from above). Damping toward rest density only, without velocity, left 6e-3. On a 448 × 128 grid, 600 steps after a central pulse, a 16-cell sponge left 1.7e-5 of the energy, a 24-cell one 1.8e-5 and a 40-cell one 3.9e-5, so the sponge is now 16 cells. With the jet preset, mean interior pressure and outflow at the right edge after 60 ms were the same for 16 and 40 cells (1.4 and 1.1 Pa; 0.094 and 0.090).
- Cropped grids for the stopped pipe (40 ms attack, 20 m/s, listener 10 cells past the windway exit, 40 to 60 above the labium): 246 Hz and at least 99% harmonic on 512 × 256 (sponge 40), 464 × 160 and 464 × 128 (sponge 24), and 448 × 128 (sponge 16, now the preset). At 448 × 112 the tone wavered more (5 ms envelope varying 12%), and at 464 × 112 with a 24-cell sponge the pipe barely sounded: the top sponge was then 36 cells above the labium.
- Jet (20 m/s, tau 0.503): rolls up into vortices about 150 cells past the flue; 2D eddies then fill the domain.
- Edge tone (20 m/s): the jet oscillates at about 350 Hz at the wedge tip; 0.4 U_centerline / d gives 400 Hz.
- Stable to 60 m/s at tau 0.503, and at tau 0.501; peak Mach passes 0.3 above about 40 m/s, where lattice Boltzmann loses accuracy.
- In the Pressure view, the hydrodynamic pressure of vortex cores (hundreds of Pa) swamps the sound. The Acoustic view subtracts a per-cell running mean of the pressure (time constant 2 ms, kept in
moments.w); it removes the slow mean-pressure shifts, but in the jet's wake the eddies' own pressure still dominates, since it varies at similar frequencies. - Listener: a pulse 115 cells away arrives at step 197 (expected 200).
- Edge tone recording (20 m/s, listener above the windway, 0.3 s): a tone at 281 Hz, in intermittent bursts, under louder low-frequency rumble (strongest at 35 Hz) from the turbulent wake in this small domain. 0.3 s of sound took about 22 s to compute.
- Stopped pipe (5-cell windway, 24-cell window, 20-cell bore, 300 cells from labium to closed end, 20 m/s, listener above the window): a steady 249 Hz tone. Over the last 0.24 s of a 0.48 s recording, 99.8% of the power above 20 Hz lies within 3% of a harmonic, the 5 ms RMS envelope varies by 6%, and the third harmonic is 29 dB below the fundamental (even harmonics more than 50 dB below, as expected for a closed pipe). About 20 Pa outside the window, about 300 Pa inside near the closed end. c/4L gives 286 Hz; 249 Hz means an effective length of 344 mm, so the mouth adds about 44 mm.
- Clicks at blow start and stop: the mouth is a piston, and a sudden speed change of U radiates a pulse of order rho c U (8 kPa in the windway at 20 m/s). With the old ramp (2 ms, updated once per frame, so two 10 m/s jumps at 1000 steps per frame) the clicks peaked at 550 Pa at the listener above the stopped pipe, 30 dB above the 17 Pa rms tone. With the per-step raised-cosine ramp, the peak of the processed recording (20 Hz high-pass) divided by the tone's rms was 28 dB at 0 ms attack, 9 dB at 10 ms, 11 dB at 20 ms and 5 dB at 40 ms, where the loudest moment is the tone itself (a sine alone is 3 dB). A higher high-pass cutoff (50 or 80 Hz) helped less than a slower attack.
- What sustains the pipe: with a 16-cell window at 20 m/s the startup transient rings the pipe and then decays; aiming the jet 1-2 cells into the pipe or steepening the labium bevel did not help; a 24-cell window sustains it; at 30 m/s the 16-cell window jumps to the third harmonic (727 Hz). Raising tau to 0.51 stops the edge tone oscillating altogether. A listener downstream of the window, in the shed vortices, hears mostly their pressure (39% harmonic at (250, 200)).
Speed#
Measure with aerophone.bench() in the browser console (pause first): it runs 30,000 steps without drawing and reports MLUPS and how many times slower than real time the simulation runs at the current dx.
On the laptop's Iris Xe, about 1350 MLUPS (run-to-run variation about 10%, likely thermal). At dx = 1 mm the stopped pipe (448 × 128) runs 25 times slower than real time, and the 512 × 256 presets 55 times slower. Before these changes the app ran the stopped pipe on 512 × 256 at about 85 times slower than real time. The gains:
- Smaller grid for the stopped pipe, with a 16-cell sponge: 2.3 times.
- Display moments written every 16th step and on the step that is drawn, instead of every step (the acoustic view's running mean is updated every 16th step with the equivalent rate): about 13%.
- Frame loop: each frame submits a batch only while fewer than two are queued, so the GPU never waits for the next animation frame. The app previously reached about 880 MLUPS while the benchmark reached about 1300; it now matches the benchmark.
- No effect: the workgroup shape (16 × 16, 64 × 1, 128 × 1, 256 × 1, 32 × 8 and 64 × 4 within 5%) and turning off the Smagorinsky term.
Storing populations as 16-bit floats might gain about 1.5 times more but could add audible rounding noise to the quiet acoustic field; it is not tried. For watching waves, a few steps per frame is right; producing audio wants the maximum.