forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

silent_first_source.rs153 lines, 5.7 KiBHistoryRawDownload
1//! A silent leading source must not silence the timeline for good.
2//!
3//! The reported sequence: (1) a video with no audio track is added to the
4//! timeline, (2) it is removed, (3) another file -- one that *does* have audio
5//! -- is added. The engine used to pin the session to its first source: the
6//! device was opened (or, here, not opened) from that one file and never
7//! re-armed, and the import door refused the newcomer outright ("the file has
8//! audio, the timeline is silent"). So the second file's picture showed but its
9//! audio was silent -- or the file could not join at all.
10//!
11//! Three seams held that pin, and each is covered here:
12//! - the import gate (`audio_matches_probed` / `first_audio_of`) refused a file
13//! with sound onto a timeline whose leading source has none;
14//! - the session never opened a device when such a file did join
15//! (`PlaybackSession::arm_audio`);
16//! - playback itself re-picked the first non-image source and returned
17//! `Ok(None)` -- silence -- however many later segments named a source with
18//! audio (`AudioSession::open_multi_streams_speed_at_fade`).
19//!
20//! ```text
21//! cargo test -p engine --test silent_first_source -- --nocapture
22//! ```
23
24use std::path::{Path, PathBuf};
25use std::thread::sleep;
26use std::time::{Duration, Instant};
27
28use engine::{AudioSession, PlaybackSession};
29
30fn asset(name: &str) -> PathBuf {
31 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
32 .join("../../assets")
33 .join(name)
34}
35
36/// The real phone clip the report named, when it is on this machine: mono-ish
37/// ambient at about -36 dBFS, so the "non-silent" bar is its own peak, not a
38/// loud fixture's. Skipped (and the run says so) when the file is absent.
39fn real_clip() -> Option<PathBuf> {
40 let path = PathBuf::from("/home/tahinli/Downloads/real_clip.mp4");
41 path.is_file().then_some(path)
42}
43
44fn peak(session: &PlaybackSession) -> f32 {
45 session
46 .audio_tap()
47 .map(|(s, _)| s.iter().fold(0.0f32, |m, s| m.max(s.abs())))
48 .unwrap_or(0.0)
49}
50
51fn pump(session: &mut PlaybackSession, secs: f64) -> f32 {
52 let deadline = Instant::now() + Duration::from_secs_f64(secs);
53 let mut best = 0.0f32;
54 while Instant::now() < deadline {
55 session.tick();
56 best = best.max(peak(session));
57 sleep(Duration::from_millis(8));
58 }
59 best
60}
61
62fn remove_all_clips(session: &mut PlaybackSession) {
63 for lane in session.lanes() {
64 let mut i = session.lane_clips(lane).len();
65 while i > 0 {
66 i -= 1;
67 session.delete_clip(lane, i);
68 }
69 }
70}
71
72/// The app's own import door: the gate taken off the live session, the probe
73/// read off it (what the worker does), then the registration.
74fn app_import(session: &mut PlaybackSession, b: &Path) -> engine::Result<usize> {
75 let gate = session.import_gate();
76 let probe = PlaybackSession::probe_import(gate, b)?;
77 session.import_probed(b, probe)
78}
79
80/// The reported add/remove/add, end to end at the session level: a silent first
81/// file, removed from the timeline, then a file with sound imported into the
82/// *same* session and placed -- its audio must decode non-silent.
83fn add_remove_add(a: &Path, b: &Path, loud: f32) {
84 let mut session = PlaybackSession::open_library(a).expect("open_library A");
85 session.set_gain(0.0);
86
87 // (1) A joins the timeline. Silent by nature: no track to open a device on.
88 let end = session.timeline_duration();
89 assert!(
90 session.place_stream_at(end, a, 0, None).expect("place A"),
91 "A lands"
92 );
93 session.play();
94 let a_peak = pump(&mut session, 1.5);
95 assert!(a_peak < 0.01, "A must be silent, got {a_peak}");
96 session.pause();
97 session.seek(0.0);
98
99 // (2) A comes off the timeline; the library keeps it.
100 remove_all_clips(&mut session);
101
102 // (3) B joins the library through the app's own door, then the timeline.
103 app_import(&mut session, b).expect("B must be admitted onto a silent timeline");
104 assert!(
105 session.audio_disabled_reason().is_none(),
106 "the device was not armed for B: {:?}",
107 session.audio_disabled_reason()
108 );
109 let end = session.timeline_duration();
110 assert!(
111 session.place_stream_at(end, b, 0, None).expect("place B"),
112 "B lands"
113 );
114 session.play();
115 let b_peak = pump(&mut session, 2.5);
116 assert!(
117 b_peak > loud,
118 "B's audio is silent after the sequence: peak {b_peak}"
119 );
120}
121
122#[test]
123fn a_second_file_with_sound_is_heard_after_a_silent_first_file() {
124 add_remove_add(&asset("test_baseline.mp4"), &asset("test_av2.mp4"), 0.05);
125}
126
127/// The same sequence with the report's own file as the second import.
128#[test]
129fn the_reports_second_file_is_heard_too() {
130 match real_clip() {
131 Some(real) => add_remove_add(&asset("test_baseline.mp4"), &real, 0.02),
132 None => eprintln!("SKIPPED: {} is not on this machine", "/home/tahinli/Downloads/real_clip.mp4"),
133 }
134}
135
136/// The playback seam on its own: a segment naming the second source must decode
137/// that source's samples even though the first source has no track. Pre-fix
138/// this returned `Ok(None)` -- the whole timeline silent.
139#[test]
140fn playback_walks_past_a_trackless_leading_source() {
141 let sources = vec![(asset("test_baseline.mp4"), 0usize), (asset("test_av2.mp4"), 0usize)];
142 let segs = vec![(Some(1usize), 0.0f64, f64::INFINITY)];
143 let (_, rx) = AudioSession::open_multi_streams(&sources, &segs)
144 .expect("open the second source's audio")
145 .expect("the second source has a track");
146 let (mut n, mut peak) = (0usize, 0.0f32);
147 for chunk in rx {
148 n += chunk.samples.len();
149 peak = chunk.samples.iter().fold(peak, |m, s| m.max(s.abs()));
150 }
151 assert!(n > 0, "nothing decoded");
152 assert!(peak > 0.05, "the second source's audio is silent: peak {peak}");
153}