From 522149da9cf3e1bea23bdc924b0ae016050dd142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 04:17:35 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=87=20test(audio):=20silence=20the=20s?= =?UTF-8?q?uites=20that=20reach=20the=20speaker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `cargo test` played real files out of the real device: measured 0.37 of full scale on the sink monitor, 818k nonzero samples, for as long as the playback suite ran. That is somebody's desk, every run. Both suites now open through a helper that sets the gain to zero. Nothing else changes: the stream is still connected and scheduled, the samples are still decoded, written and handed over, and every assertion here is measured from the device's own position, which the gain does not touch. The one test that walks through audible gains does it on a flushed ring, where there is nothing but the underrun fill to apply them to. Measured after: peak 0, not one nonzero sample, all three suites green. --- crates/engine/tests/ao_output.rs | 35 +++++++++++++++++---- crates/engine/tests/playback.rs | 54 +++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/crates/engine/tests/ao_output.rs b/crates/engine/tests/ao_output.rs index 8338fac..5c002e9 100644 --- a/crates/engine/tests/ao_output.rs +++ b/crates/engine/tests/ao_output.rs @@ -28,6 +28,23 @@ fn tone() -> Vec { samples } +/// Opens the device the way this suite wants it: connected for real, and +/// silent. Everything these tests assert -- that the clock runs, at the right +/// rate, and stops when the stream is paused -- is measured from the device's +/// own position, which the gain does not touch: the frames are still consumed, +/// only the last multiply before the speaker is zero. +/// +/// ponytail: it does mean the suite is deaf, so a plugin that shipped with the +/// gain stuck at zero would pass it. Nothing here could ever hear that -- the +/// checks that can are `scale`'s unit test in engine-audio (unity leaves the +/// bytes alone) and a person with the app open. The upgrade path is recording +/// a null sink and asserting on the samples that come back. +fn open_silent() -> AoSession { + let ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + assert!(ao.set_volume(0.0), "mute rejected"); + ao +} + /// Waits up to `limit` for the clock to report a played position past zero. fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { let start = Instant::now(); @@ -46,7 +63,7 @@ fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { #[ignore = "needs libengine_audio.so and a PipeWire daemon"] fn plays_and_tracks_position() { assert!(AoSession::probe(), "plugin not loadable"); - let mut ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + let mut ao = open_silent(); // The ring holds a second, so two halves go in whole -- enough real audio // to cover the measurement below without starving. @@ -96,14 +113,13 @@ fn plays_and_tracks_position() { #[ignore = "needs libengine_audio.so and a PipeWire daemon"] fn muting_silences_without_stopping_the_clock() { assert!(AoSession::probe(), "plugin not loadable"); - let mut ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + let mut ao = open_silent(); let samples = tone(); assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); let first = wait_for_playback(&ao, Duration::from_secs(2)); - // Mute, then keep it fed: the position must move exactly as it would have. - assert!(ao.set_volume(0.0), "mute rejected"); + // Muted and fed: the position must move exactly as it would have. assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); std::thread::sleep(Duration::from_millis(300)); let muted = ao.position().expect("position went unknown"); @@ -115,8 +131,15 @@ fn muting_silences_without_stopping_the_clock() { "the clock stalled while muted: {advanced} samples in 300 ms" ); - // Every level in range is taken, and the two ends are in range. - for gain in [0.0, 0.5, 1.0] { + // Every level in range is taken, and the two ends are in range. Done on an + // emptied ring and left back at zero, so that the audible gains this walks + // through have nothing but the underrun fill to be applied to: whoever runs + // the suite should not have to hear it prove that 1.0 is a legal number. + assert!(ao.flush(), "flush rejected"); + // A flush lands on the next callback and the ring refuses writes until it + // does, so give it a couple of quanta before asking for anything else. + std::thread::sleep(Duration::from_millis(50)); + for gain in [0.5, 1.0, 0.0] { assert!(ao.set_volume(gain), "gain {gain} rejected"); } // Out of range is refused at the ABI rather than multiplied into the diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index df89873..7b975e7 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -19,6 +19,22 @@ fn asset(name: &str) -> PathBuf { .join(name) } +/// Opens a session the way every test here wants one: silent. The suite plays +/// real files through the real device, so without this a `cargo test` is two +/// minutes of noise out of the speakers of whoever ran it. +/// +/// Muting is the gain knob, not a pause and not a fake device: the stream is +/// still connected, the daemon still schedules it, the samples are still +/// decoded, written, and handed over -- only the last multiply before the +/// device is zero. Every clock, `fed` and `played_out` assertion below is +/// therefore measuring exactly what it measured when this was audible. +fn open(path: impl AsRef) -> PlaybackSession { + let session = PlaybackSession::open(path).expect("open"); + // False for the silent fixtures, which have no device to quieten. + session.set_gain(0.0); + session +} + /// One render tick of a front-end: advance the clock, take whatever frames are /// due. Returns the highest frame index seen so far. fn pump(session: &mut PlaybackSession, last_index: &mut Option) { @@ -52,7 +68,7 @@ fn run_for(session: &mut PlaybackSession, last_index: &mut Option, duration fn plays_at_real_speed(name: &str) { let path = asset(name); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); assert_eq!(session.now(), 0.0, "a fresh session starts at zero"); assert!(!session.is_playing(), "a fresh session starts paused"); let mut last_index = None; @@ -160,7 +176,7 @@ fn threads() -> usize { /// Wall-clock seek: no audio track, so this runs anywhere. #[test] fn seek_repositions_video_and_clock() { - let mut session = PlaybackSession::open(asset("test_baseline.mp4")).expect("open"); + let mut session = open(asset("test_baseline.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -194,7 +210,7 @@ fn seek_repositions_video_and_clock() { /// device is never fed another sample. #[test] fn seek_past_the_end_clamps() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let meta = *session.meta(); let duration = f64::from(meta.frame_count) / meta.frame_rate; @@ -223,7 +239,7 @@ fn seek_past_the_end_clamps() { /// abandoned workers behind, and the last one still wins. #[test] fn rapid_seeks_settle_on_the_last_one() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -253,7 +269,7 @@ fn rapid_seeks_settle_on_the_last_one() { #[test] #[ignore = "needs a running PipeWire daemon"] fn seek_keeps_the_audio_clock() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let meta = *session.meta(); let mut last_index = None; @@ -308,7 +324,7 @@ fn seek_keeps_the_audio_clock() { #[test] fn edits_traverse_cuts() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let whole = f64::from(total) / fps; assert!((session.timeline_duration() - whole).abs() < 1e-9); @@ -406,7 +422,7 @@ fn edits_traverse_cuts() { #[test] fn paste_duplicates_a_clip_at_the_playhead() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let whole = f64::from(total) / fps; @@ -480,7 +496,7 @@ fn paste_duplicates_a_clip_at_the_playhead() { #[test] fn paste_while_playing_does_not_stall() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let mut last_index = None; @@ -518,7 +534,7 @@ fn paste_while_playing_does_not_stall() { #[test] fn paste_at_eos_revives_the_session() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let total = session.meta().frame_count; assert!(session.cut_at(2.0), "cut at 2 s"); @@ -554,7 +570,7 @@ fn paste_at_eos_revives_the_session() { #[test] #[ignore = "needs a running PipeWire daemon"] fn edits_keep_the_audio_clock() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -621,7 +637,7 @@ fn frame_count(path: &Path) -> u32 { #[test] fn import_appends_a_second_source_and_plays_across_the_join() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); - let mut session = PlaybackSession::open(&av).expect("open"); + let mut session = open(&av); let (fps, first) = (session.meta().frame_rate, session.meta().frame_count); let second = frame_count(&av2); assert_eq!((first, second), (150, 120), "fixtures changed"); @@ -707,7 +723,7 @@ fn import_appends_a_second_source_and_plays_across_the_join() { /// changes nothing. #[test] fn import_refuses_what_does_not_match() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let err = session .import(&asset("test_mismatch.mp4")) @@ -730,7 +746,7 @@ fn import_refuses_what_does_not_match() { assert!((session.timeline_duration() - 5.0).abs() < 1e-9); // The mirror: audio into a silent timeline is refused just as loudly. - let mut silent = PlaybackSession::open(asset("test_baseline.mp4")).expect("open"); + let mut silent = open(asset("test_baseline.mp4")); let err = silent .import(&asset("test_av.mp4")) .expect_err("audio into a silent timeline") @@ -742,7 +758,7 @@ fn import_refuses_what_does_not_match() { /// out once and reused, which is what keeps clipboard clips valid forever. #[test] fn importing_one_file_twice_reuses_its_source() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let av2 = asset("test_av2.mp4"); session.import(&av2).expect("first import"); session.import(&av2).expect("second import"); @@ -767,7 +783,7 @@ fn importing_one_file_twice_reuses_its_source() { #[test] fn import_at_eos_resumes_into_the_new_clip() { let av2 = asset("test_av2.mp4"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; session.play(); @@ -796,7 +812,7 @@ fn a_vanished_source_is_skipped() { let scratch = std::env::temp_dir().join(format!("video_editor_vanish_{}.mp4", std::process::id())); std::fs::copy(asset("test_av2.mp4"), &scratch).expect("copy the fixture"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; session.import(&scratch).expect("import the copy"); std::fs::remove_file(&scratch).expect("unlink"); @@ -815,7 +831,7 @@ fn a_vanished_source_is_skipped() { #[test] fn import_during_play_does_not_stall() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); - let mut session = PlaybackSession::open(&av).expect("open"); + let mut session = open(&av); let first = session.meta().frame_count; let mut last_index = None; @@ -845,7 +861,7 @@ fn import_during_play_does_not_stall() { #[test] fn undo_after_import_is_one_step() { let av2 = asset("test_av2.mp4"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); session.import(&av2).expect("import"); assert_eq!(session.clip_spans().len(), 2); assert!((session.timeline_duration() - 9.0).abs() < 1e-9); @@ -881,7 +897,7 @@ fn undo_after_import_is_one_step() { #[test] #[ignore = "needs a running PipeWire daemon"] fn audio_runs_across_a_source_join() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); session.import(&asset("test_av2.mp4")).expect("import"); let first = session.meta().frame_count;