forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

pan.rs135 lines, 4.4 KiBHistoryRawDownload
1//! Clip and lane pan 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::project::{Lane, LaneKind, Source, Speed};
13use engine::scale::FitPolicy;
14use engine::{AudioSession, Clip, Project};
15
16const RATE: u32 = 44_100;
17const FPS: f64 = 30.0;
18
19fn asset(name: &str) -> PathBuf {
20 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
21 .join("../../assets")
22 .join(name)
23}
24
25fn clip(frames: u32, gain_db: f32) -> Clip {
26 Clip {
27 speed_keys: Vec::new(),
28 reversed: false,
29 stabilise: None, deinterlace: Default::default(), mark: Default::default(),
30 angle: Default::default(),
31 fade_in: 0,
32 fade_out: 0,
33 transition_out: 0,
34 transition_kind: Default::default(),
35 visualizer: 0,
36 viz_paint: 0,
37 gain_db,
38 gain_keys: Vec::new(),
39 pan: 0,
40 pitch: 0.0,
41 transform_keys: Vec::new(),
42 opacity_keys: Vec::new(),
43 color_keys: Vec::new(),
44 effects: Vec::new(),
45 start: 0,
46 in_frame: 0,
47 out_frame: frames,
48 source: 0,
49 link: None,
50 eq: None,
51 color: None,
52 fit: FitPolicy::default(),
53 speed: Speed::NORMAL,
54 transform: None,
55 }
56}
57
58/// One video clip and one audio clip of `gain_db`, both from `test_av.mp4`.
59fn project(gain_db: f32) -> Project {
60 let frames = 90;
61 Project::from_parts(
62 vec![Source::new(asset("test_av.mp4"), 0)],
63 vec![
64 (LaneKind::Video, vec![clip(frames, 0.0)]),
65 (LaneKind::Audio, vec![clip(frames, gain_db)]),
66 ],
67 Vec::new(),
68 Vec::new(),
69 Vec::new(),
70 )
71 .expect("a one-source project")
72}
73
74/// What the device is fed, drained to the end: the opener `PlaybackSession::seek`
75/// and the export both call, with the same segments, eqs, speeds and fades.
76fn play(project: &Project) -> Vec<f32> {
77 let sources = project.audio_sources();
78 let segs = project.audio_segments_from(0, FPS);
79 let eqs = project.audio_eqs_from(0, FPS);
80 let speeds = project.audio_speeds_from(0, FPS);
81 let fades = project.audio_fades_from(0, FPS);
82 let (meta, chunks) = AudioSession::open_mixed_streams_master_at_fade(
83 &sources,
84 &segs,
85 &eqs,
86 &speeds,
87 &fades,
88 FPS,
89 &project.audio_gains(),
90 &project.audio_ducks(),
91 &project.audio_voices(),
92 project.limiter(),
93 None,
94 )
95 .expect("open the timeline's audio")
96 .expect("the timeline has audio");
97 assert_eq!((meta.sample_rate, meta.channels), (RATE, 2));
98 chunks.into_iter().flat_map(|c| c.samples).collect()
99}
100
101fn chan_rms(samples: &[f32], ch: usize) -> f64 {
102 let v: Vec<f64> = samples.chunks(2).skip(8820).map(|f| f64::from(f[ch])).collect();
103 (v.iter().map(|x| x * x).sum::<f64>() / v.len() as f64).sqrt()
104}
105
106#[test]
107fn a_clip_panned_hard_right_silences_the_left_and_centre_is_untouched() {
108 let a1 = Lane::new(LaneKind::Audio, 0);
109 let centre = play(&project(0.0));
110 let (l0, r0) = (chan_rms(&centre, 0), chan_rms(&centre, 1));
111 assert!(l0 > 0.01 && r0 > 0.01);
112 let mut p = project(0.0);
113 assert!(p.set_clip_pan(a1, 0, 1.0));
114 assert!(!p.set_clip_pan(a1, 0, 1.0), "already set");
115 assert!(!p.set_clip_pan(Lane::V1, 0, 0.5), "video takes no pan");
116 let hard = play(&p);
117 assert!(chan_rms(&hard, 0) < 1e-6, "left is gone");
118 assert!((chan_rms(&hard, 1) / r0 - 1.0).abs() < 1e-3, "right is as it was");
119 p.undo();
120 assert_eq!(p.clip_pan(a1, 0), Some(0.0));
121}
122
123#[test]
124fn a_lane_pan_stacks_on_the_clip_pan() {
125 let a1 = Lane::new(LaneKind::Audio, 0);
126 let mut p = project(0.0);
127 assert!(p.set_lane_pan(a1, -1.0));
128 assert!(!p.set_lane_pan(Lane::V1, 0.3));
129 let played = play(&p);
130 assert!(chan_rms(&played, 1) < 1e-6, "right is gone");
131 assert!(chan_rms(&played, 0) > 0.01);
132 assert!(p.set_clip_pan(a1, 0, 0.5));
133 let (l, r) = engine::project::pan_gains(-0.5, 1.0);
134 assert!(l == 0.0 && r < 0.71 && r > 0.70, "{l} {r}");
135}