forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

clip_gain.rs167 lines, 5.4 KiBHistoryRawDownload
1//! The per-clip gain where it has to be true: in the samples playback feeds the
2//! device and in the samples an export writes. Both come off the same opener
3//! with the same per-segment envelope ([`engine::Project::audio_fades_from`]),
4//! so a clip at -6 dB must mix at half amplitude through that one door.
5//!
6//! The fixture is the 440 Hz / 880 Hz stereo tone in `test_av.mp4`; the assertion
7//! is a ratio of RMS levels, never a level, so the fixture's own loudness drops
8//! out. No picture is decoded.
9
10use std::path::PathBuf;
11
12use engine::GainKey;
13use engine::project::{Lane, LaneKind, Source, Speed};
14use engine::scale::FitPolicy;
15use engine::{AudioSession, Clip, Project};
16
17const RATE: u32 = 44_100;
18const FPS: f64 = 30.0;
19
20fn asset(name: &str) -> PathBuf {
21 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
22 .join("../../assets")
23 .join(name)
24}
25
26fn clip(frames: u32, gain_db: f32) -> Clip {
27 Clip {
28 speed_keys: Vec::new(),
29 reversed: false,
30 stabilise: None, deinterlace: Default::default(), mark: Default::default(),
31 angle: Default::default(),
32 fade_in: 0,
33 fade_out: 0,
34 transition_out: 0,
35 transition_kind: Default::default(),
36 visualizer: 0,
37 viz_paint: 0,
38 gain_db,
39 gain_keys: Vec::new(),
40 pan: 0,
41 pitch: 0.0,
42 transform_keys: Vec::new(),
43 opacity_keys: Vec::new(),
44 color_keys: Vec::new(),
45 effects: Vec::new(),
46 start: 0,
47 in_frame: 0,
48 out_frame: frames,
49 source: 0,
50 link: None,
51 eq: None,
52 color: None,
53 fit: FitPolicy::default(),
54 speed: Speed::NORMAL,
55 transform: None,
56 }
57}
58
59/// One video clip and one audio clip of `gain_db`, both from `test_av.mp4`.
60fn project(gain_db: f32) -> Project {
61 let frames = 90;
62 Project::from_parts(
63 vec![Source::new(asset("test_av.mp4"), 0)],
64 vec![
65 (LaneKind::Video, vec![clip(frames, 0.0)]),
66 (LaneKind::Audio, vec![clip(frames, gain_db)]),
67 ],
68 Vec::new(),
69 Vec::new(),
70 Vec::new(),
71 )
72 .expect("a one-source project")
73}
74
75/// What the device is fed, drained to the end: the opener `PlaybackSession::seek`
76/// and the export both call, with the same segments, eqs, speeds and fades.
77fn play(project: &Project) -> Vec<f32> {
78 let sources = project.audio_sources();
79 let segs = project.audio_segments_from(0, FPS);
80 let eqs = project.audio_eqs_from(0, FPS);
81 let speeds = project.audio_speeds_from(0, FPS);
82 let fades = project.audio_fades_from(0, FPS);
83 let (meta, chunks) = AudioSession::open_mixed_streams_master_at_fade(
84 &sources,
85 &segs,
86 &eqs,
87 &speeds,
88 &fades,
89 FPS,
90 &project.audio_gains(),
91 &project.audio_ducks(),
92 &project.audio_voices(),
93 project.limiter(),
94 None,
95 )
96 .expect("open the timeline's audio")
97 .expect("the timeline has audio");
98 assert_eq!((meta.sample_rate, meta.channels), (RATE, 2));
99 chunks.into_iter().flat_map(|c| c.samples).collect()
100}
101
102/// Root mean square of `[from, to)` seconds of the interleaved stereo stream.
103fn rms(samples: &[f32], from: f64, to: f64) -> f64 {
104 let at = |secs: f64| (secs * f64::from(RATE)) as usize * 2;
105 let window = &samples[at(from).min(samples.len())..at(to).min(samples.len())];
106 assert!(!window.is_empty(), "no samples in [{from}, {to})");
107 (window
108 .iter()
109 .map(|s| f64::from(*s) * f64::from(*s))
110 .sum::<f64>()
111 / window.len() as f64)
112 .sqrt()
113}
114
115#[test]
116fn a_clip_at_minus_six_db_mixes_at_half_amplitude() {
117 let unity = rms(&play(&project(0.0)), 0.2, 2.8);
118 let quiet = rms(&play(&project(-6.0)), 0.2, 2.8);
119 let ratio = quiet / unity;
120 let want = 10f64.powf(-6.0 / 20.0);
121 assert!(
122 (ratio - want).abs() < 0.01,
123 "-6 dB should mix at {want:.4} of unity, got {ratio:.4}"
124 );
125}
126
127#[test]
128fn a_gained_clip_on_a_video_lane_is_refused_and_the_audio_is_untouched() {
129 let mut p = project(0.0);
130 assert!(
131 !p.set_clip_gain(Lane::V1, 0, -6.0),
132 "a video clip takes no gain"
133 );
134 assert!(p.set_clip_gain(Lane::new(LaneKind::Audio, 0), 0, -6.0));
135 assert_eq!(p.clip_gain(Lane::V1, 0), Some(0.0));
136}
137
138/// The same tone through a volume envelope that falls from 0 dB at frame 0 to
139/// -20 dB at frame 30 (1 s at 30 fps). At frame 15 (0.5 s) it is halfway, -10 dB;
140/// at frame 45 (1.5 s) it is past the last point and holds at -20 dB. Each
141/// level is measured as a ratio over a 40 ms window against the same window of
142/// the unshaped clip, so the tone's own level drops out.
143#[test]
144fn a_gain_envelope_falls_from_unity_to_minus_twenty_over_thirty_frames() {
145 let base = play(&project(0.0));
146 let mut shaped_project = project(0.0);
147 assert!(shaped_project.set_gain_keys(
148 Lane::new(LaneKind::Audio, 0),
149 0,
150 vec![GainKey::new(0, 0.0), GainKey::new(30, -20.0)],
151 ));
152 let shaped = play(&shaped_project);
153 let db_at = |secs: f64| {
154 let ratio = rms(&shaped, secs - 0.02, secs + 0.02) / rms(&base, secs - 0.02, secs + 0.02);
155 20.0 * ratio.log10()
156 };
157 let mid = db_at(0.5);
158 assert!(
159 (mid + 10.0).abs() < 1.0,
160 "frame 15 should sit near -10 dB, got {mid:.2}"
161 );
162 let held = db_at(1.5);
163 assert!(
164 (held + 20.0).abs() < 0.5,
165 "frame 45 should hold at -20 dB, got {held:.2}"
166 );
167}