forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

clip_envelope.rs183 lines, 6.2 KiBHistoryRawDownload
1//! The audio clip envelope's geometry (`ui/bench_stance/clip_envelope.rs`):
2//! where each key lands on a clip body, the dB-to-height curve, and what counts
3//! as an envelope to draw at all. Pure numbers, so no window is needed.
4
5use crate::ui::bench_stance::clip_envelope::{
6 BOTTOM_DB, TOP_DB, envelope, has_envelope, height_of, key_x,
7};
8use engine::GainKey;
9
10fn k(at: u32, db: f32) -> GainKey {
11 GainKey::new(at, db)
12}
13
14fn close(a: f32, b: f32) -> bool {
15 (a - b).abs() < 1e-4
16}
17
18/// The fader-style curve pins its ends and holds outside the range.
19#[test]
20fn height_of_pins_the_ends_and_clamps() {
21 assert!(close(height_of(TOP_DB), 1.));
22 assert!(close(height_of(BOTTOM_DB), 0.));
23 assert!(
24 close(height_of(40.), 1.),
25 "above the top is held at the top"
26 );
27 assert!(
28 close(height_of(-100.), 0.),
29 "below the floor is held at the floor"
30 );
31}
32
33/// Louder is always higher, so a dip reads as a dip.
34#[test]
35fn height_of_is_monotonic() {
36 let mut prev = height_of(BOTTOM_DB);
37 let mut db = BOTTOM_DB;
38 while db <= TOP_DB {
39 let h = height_of(db);
40 assert!(h >= prev, "height fell at {db} dB");
41 prev = h;
42 db += 0.5;
43 }
44}
45
46/// Unity sits well up the body, and the -6 and -18 dips are clearly apart.
47#[test]
48fn unity_is_high_and_the_dips_are_visible() {
49 let zero = height_of(0.);
50 assert!((0.6..0.75).contains(&zero), "0 dB at {zero}");
51 let six = height_of(-6.);
52 let eighteen = height_of(-18.);
53 assert!(zero - six > 0.1, "-6 dB dip too small: {}", zero - six);
54 assert!(six - eighteen > 0.15, "-18 dB dip too small");
55}
56
57/// Only a clip with keys or a non-zero gain gets an envelope.
58#[test]
59fn has_envelope_needs_keys_or_gain() {
60 assert!(!has_envelope(&[], 0.));
61 assert!(has_envelope(&[], -6.));
62 assert!(has_envelope(&[k(0, 0.)], 0.), "a key at unity still counts");
63}
64
65/// A key's x: its source frame relative to the in point, divided by speed, times
66/// the pixels per timeline frame. Before the in point it is negative.
67#[test]
68fn key_x_follows_in_point_and_speed() {
69 assert!(close(key_x(150, 100, 1., 2.), 100.));
70 assert!(
71 close(key_x(150, 100, 2., 2.), 50.),
72 "2x plays twice as fast"
73 );
74 assert!(close(key_x(100, 100, 1., 2.), 0.));
75 assert!(
76 key_x(90, 100, 1., 2.) < 0.,
77 "before the in point is left of the edge"
78 );
79}
80
81/// A clip with no keys is a flat line at its own gain, end to end.
82#[test]
83fn clip_gain_alone_is_a_flat_line() {
84 let env = envelope(&[], -6., 0, 1., 1., 30.);
85 assert!(env.dots.is_empty());
86 assert!(!env.line.is_empty());
87 assert!(close(env.line.first().unwrap().0, 0.));
88 assert!(close(env.line.last().unwrap().0, 30.));
89 for &(_, h) in &env.line {
90 assert!(close(h, height_of(-6.)));
91 }
92}
93
94/// Between two keys the dB is linear in source frames, so a sample halfway
95/// between them carries the halfway dB, and each key is a dot at its own x.
96#[test]
97fn the_line_is_linear_in_db_between_keys() {
98 let keys = [k(0, 0.), k(30, -12.)];
99 let env = envelope(&keys, 0., 0, 1., 1., 60.);
100 // x = 15 is on the sampling grid, halfway between the keys: -6 dB.
101 let mid = env
102 .line
103 .iter()
104 .find(|(x, _)| close(*x, 15.))
105 .expect("grid point at 15");
106 assert!(close(mid.1, height_of(-6.)), "midpoint height {}", mid.1);
107 // x = 30 is the second key, exact.
108 let at_key = env
109 .line
110 .iter()
111 .find(|(x, _)| close(*x, 30.))
112 .expect("key x is a sample");
113 assert!(close(at_key.1, height_of(-12.)));
114 assert_eq!(env.dots.len(), 2);
115 assert!(close(env.dots[1].0, 30.) && close(env.dots[1].1, height_of(-12.)));
116}
117
118/// Clip gain is added to the keyed envelope, on the line and on the dots alike.
119#[test]
120fn clip_gain_offsets_keys() {
121 let env = envelope(&[k(0, 0.)], -6., 0, 1., 1., 20.);
122 assert!(close(env.line[0].1, height_of(-6.)));
123 assert!(close(env.dots[0].1, height_of(-6.)));
124}
125
126/// Outside the first and last key the envelope holds its end value.
127#[test]
128fn the_envelope_holds_beyond_its_ends() {
129 let env = envelope(&[k(50, -12.)], 0., 0, 1., 1., 100.);
130 assert_eq!(env.dots.len(), 1, "the one key is inside the body");
131 for &(_, h) in &env.line {
132 assert!(close(h, height_of(-12.)));
133 }
134}
135
136/// Keys off the body are not dots, but the line still runs across it, following
137/// the envelope between the keys either side of the body.
138#[test]
139fn keys_outside_the_body_are_not_dots() {
140 let env = envelope(&[k(10, -24.), k(200, -3.)], 0., 100, 1., 1., 50.);
141 assert!(env.dots.is_empty(), "both keys are off the body");
142 // Source 100 and 150 sit between the keys at 10 and 200 (-24 and -3 dB).
143 let at = |src: f32| -24. + 21. * (src - 10.) / 190.;
144 assert!(close(env.line[0].1, height_of(at(100.))));
145 assert!(close(env.line.last().unwrap().1, height_of(at(150.))));
146}
147
148/// A trimmed-in clip: the first kept key sits on the left edge, a later one
149/// lands at its own offset, and speed scales that offset.
150#[test]
151fn trim_and_speed_move_the_dots() {
152 let keys = [k(100, 0.), k(200, -24.)];
153 let env = envelope(&keys, 0., 100, 1., 1., 100.);
154 assert!(close(env.dots[0].0, 0.));
155 assert!(close(env.dots[1].0, 100.));
156
157 let fast = envelope(&keys, 0., 100, 2., 1., 100.);
158 assert!(close(fast.dots[1].0, 50.), "at 2x the same key is half way");
159}
160
161/// Keys on the body and at both edges are all samples, sorted, inside the
162/// body, with no duplicates -- the polyline a painter can walk in order.
163#[test]
164fn line_is_sorted_inside_the_body() {
165 let keys = [k(0, 0.), k(7, -3.), k(40, -20.), k(90, 6.)];
166 let env = envelope(&keys, 0., 0, 1., 1., 45.);
167 let xs: Vec<f32> = env.line.iter().map(|p| p.0).collect();
168 assert!(xs.windows(2).all(|w| w[0] < w[1]), "sorted and unique");
169 assert!(close(xs[0], 0.) && close(*xs.last().unwrap(), 45.));
170 assert!(env.dots.iter().all(|(x, _)| (0. ..=45.).contains(x)));
171}
172
173/// A zero-width body, or a scale that is not usable, has nothing to draw.
174#[test]
175fn degenerate_geometry_is_empty() {
176 assert!(envelope(&[k(0, 0.)], 0., 0, 1., 1., 0.).line.is_empty());
177 assert!(envelope(&[k(0, 0.)], 0., 0, 1., 0., 10.).line.is_empty());
178 assert!(
179 envelope(&[k(0, 0.)], 0., 0, 1., f32::NAN, 10.)
180 .line
181 .is_empty()
182 );
183}