From e44ad82a9f9322a3a373dc50dfff45bd5aa5f1e9 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 4 Nov 2026 16:09:12 +0300 Subject: [PATCH] test: exercise moving mass sources through offline acoustics capture Co-Authored-By: Codex --- crates/horizon-client/src/audio.rs | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index f97453e30..1cf24ea7a 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -2399,6 +2399,108 @@ mod tests { a.room_now().unwrap_or(crate::acoustics::Room::OPEN) } + /// Optional offline acceptance through Audio's production acoustics/capture path. + /// Uses synthetic samples and geometry, never an output device or player resources. + /// HORIZON_ACOUSTICS_OUT=/scratch cargo test -p horizon-client --lib + /// audio::tests::export_moving_mass_raid -- --ignored --nocapture + #[test] + #[ignore] + fn export_moving_mass_raid() { + use crate::acoustics::{Changes, Geometry, room}; + let run = || { + let mut audio = Audio::silent(Path::new("__synthetic_acoustic_fixture_no_assets__")); + audio.set_acoustics(true); + audio.film_begin(); + let (geometry, listener) = room::tests::scene("gorge"); + let geometry: Arc = Arc::new(geometry); + audio.set_listener(listener, 0.0, 0.0); + { + let mut m = audio.backend.mixer.lock().unwrap(); + for id in 1..=100u64 { + let mut c = voice_channel(tone(110.0 + id as f32 * 7.0, 24_000), + [0.0, 66.5, 20.0], true, 0.01, 0.0, 0.0); + c.gain = 0.025; + c.linear = Some(500.0); + m.channels.insert(id, c); + } + } + let mut output = Vec::new(); + let mut times = Vec::new(); + let mut max_sources = 0; + let mut max_cells = 0; + let mut max_echo_cells = 0; + for tick in 0..120 { + // Move sources across acoustic cache cells; rotate loud one-shots. + { + let mut m = audio.backend.mixer.lock().unwrap(); + for id in 1..=100u64 { + m.channels.get_mut(&id).unwrap().at = [ + -20.0 + (id % 10) as f32 * 4.0, + 66.5, + 20.0 + (id / 10) as f32 * 2.0 - tick as f32 * 0.2, + ]; + } + if tick % 20 == 0 { + for id in 101..=116u64 { + let mut c = channel(click(9_600, 96), Some(300.0), + [-10.0 + (id % 8) as f32 * 3.0, 66.5, 28.0], false); + c.gain = 0.6; + m.channels.insert(id, c); + } + } + } + // A camera cut, a changed wall, and a new world epoch. + if tick == 40 { audio.set_listener([4.5, 66.5, 24.5], 90.0, 0.0); } + let changes = Changes { + epoch: if tick < 80 { 1 } else { 2 }, + overflow: tick == 60, + ..Default::default() + }; + let a = audio.acoustics.as_ref().unwrap(); + let (cells, echo_cells) = (a.stats.cells, a.stats.echo_cells); + let start = std::time::Instant::now(); + audio.tick_acoustics(Some(&geometry), &changes); + let data = audio.film_mix(2400, 48_000); + times.push(start.elapsed().as_secs_f64() * 1000.0); + let a = audio.acoustics.as_ref().unwrap(); + max_sources = max_sources.max(a.stats.sources); + max_cells = max_cells.max(a.stats.cells - cells); + max_echo_cells = max_echo_cells.max(a.stats.echo_cells - echo_cells); + assert!(data.iter().all(|v| v.is_finite() && v.abs() <= 1.00001)); + output.extend(data); + } + assert!(max_sources >= 100, "fixture must actually process the mass of sources"); + let a = audio.acoustics.as_ref().unwrap(); + assert!(a.stats.echo_traces > 0 && a.stats.sends > 0); + // Reset on capture end must leave no old voices or reverb tail in a new capture. + audio.film_end(); + audio.film_begin(); + assert!(audio.film_mix(4800, 48_000).iter().all(|&v| v == 0.0)); + audio.film_end(); + times.sort_by(f64::total_cmp); + let report = serde_json::json!({ + "synthetic": true, "output_device": false, "ticks": 120, + "rate": 48000, "channels": 2, "seconds": 6, + "max_sources": max_sources, "max_direct_cells_per_tick": max_cells, + "max_echo_cells_per_tick": max_echo_cells, + "step_plus_mix_ms_p50": times[60], "step_plus_mix_ms_p95": times[114], + "step_plus_mix_ms_max": times[119], "audio_ms_per_tick": 50, + }); + (output, report) + }; + let (first, report) = run(); + let (second, _) = run(); + assert_eq!(first, second, "offline capture must be sample deterministic"); + assert!(rms(&first) > 0.0001, "a silent fixture proves no working mix"); + println!("mass-raid-acoustics {report}"); + if let Some(dir) = std::env::var_os("HORIZON_ACOUSTICS_OUT") { + let dir = Path::new(&dir); + let raw: Vec = first.iter().flat_map(|v| ((v * 32767.0) as i16).to_le_bytes()).collect(); + std::fs::write(dir.join("mass-raid.raw"), raw).unwrap(); + std::fs::write(dir.join("mass-raid.json"), report.to_string()).unwrap(); + } + } + /// 100 looping voices (a quarter sending to the reverb, all four slots ringing) against none: /// `cargo test -p horizon-client --lib --release audio::tests::bench_reverb -- --ignored --nocapture`. #[test]