| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | use std::f64::consts::PI; |
| 38 | use std::path::Path; |
| 39 | |
| 40 | use crate::AudioSession; |
| 41 | |
| 42 | |
| 43 | const ABS_GATE_LUFS: f64 = -70.0; |
| 44 | |
| 45 | const REL_GATE_LU: f64 = -10.0; |
| 46 | |
| 47 | const LRA_REL_GATE_LU: f64 = -20.0; |
| 48 | |
| 49 | const CHUNKS_PER_SEC: usize = 10; |
| 50 | |
| 51 | const MOMENTARY_CHUNKS: usize = 4; |
| 52 | |
| 53 | const SHORT_TERM_CHUNKS: usize = 30; |
| 54 | |
| 55 | const LRA_HOP_CHUNKS: usize = 5; |
| 56 | |
| 57 | pub(crate) const TP_TAPS: usize = 32; |
| 58 | |
| 59 | const TP_PHASES: usize = 8; |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | pub(crate) const TP_COEF: [[f64; TP_TAPS]; TP_PHASES] = [ |
| 80 | [-9.113131439975678e-06, 4.0076035670222974e-05, -0.00010621819573270595, 0.0002284040759670764, -0.0004338708206289006, 0.0007569269598663376, -0.001239982208738999, 0.0019355921973831004, -0.0029109007764240528, 0.004257373975001418, -0.006112418887724569, 0.00870982957472023, -0.012509950835799514, 0.018598441767977404, -0.030321702331554387, 0.06538702643260477, 0.9935370754684095, -0.057482007155269126, 0.02827349906597848, -0.017640066565440395, 0.011942377142413332, -0.008332275252542716, 0.005846125446777624, -0.0040647034070906305, 0.002770883608971783, -0.0018349008372697962, 0.0011691692091189859, -0.0007087508284246482, 0.00040253288581122037, -0.00020919986159042688, 9.537201341962132e-05, -3.46447644203612e-05], |
| 81 | [-3.311694388264495e-05, 0.00013113986569634532, -0.0003358453978417591, 0.0007088167627000853, -0.0013300206219824767, 0.0022998489712809, -0.0037422781404128427, 0.005811272430550032, -0.008704737233512524, 0.012695107173726997, -0.018197543923624927, 0.02593058478547953, -0.037337762954957596, 0.05591630108406594, -0.09294865839644116, 0.21555037786454243, 0.9426947295402057, -0.145858673869235, 0.07531945801178211, -0.047702981628914426, 0.032480746980827084, -0.022701723557568016, 0.015920900371157783, -0.011048146610773036, 0.007507937684653195, -0.004950613758014254, 0.0031369765747049742, -0.0018880054283359473, 0.0010620801545960844, -0.0005445803041891528, 0.0002430517421209236, -8.464122840432894e-05], |
| 82 | [-6.169787761900706e-05, 0.00022418908603070837, -0.000556455526893554, 0.0011539493624898778, -0.0021399892134944926, 0.003668916980414196, -0.005931212321661181, 0.0091640417587589, -0.013674291087551773, 0.019889092089511885, -0.02846897809283543, 0.04057719426108761, -0.05859942936943879, 0.08848148300534162, -0.1503840991546257, 0.3823053610670679, 0.8457322859905341, -0.1965949653427006, 0.10574314941958163, -0.06786563501345873, 0.04644402518288106, -0.03250685839030642, 0.02278221067211325, -0.015776461984025044, 0.0106861423046505, -0.007015178439754547, 0.004419776898118025, -0.002640349206419353, 0.0014706075069126929, -0.0007434479010369788, 0.00032434279095271655, -0.00010771945462504619], |
| 83 | [-8.888904218233107e-05, 0.00030047873971013594, -0.0007247222673508771, 0.0014781413284720366, -0.0027105491893057556, 0.004608923212425362, -0.00740395560020146, 0.011383759722552785, -0.016923835860553745, 0.02455287281769743, -0.03510086512343416, 0.05005558258074104, -0.07253382289668456, 0.11053946484280222, -0.1927372791631574, 0.552445740092612, 0.7115968219479728, -0.210662107454357, 0.11729533434259606, -0.07616916079430923, 0.052359451330621726, -0.036687218160952884, 0.0256897361931744, -0.0177499783487386, 0.011981956177112868, -0.007829952650949394, 0.004903989286442165, -0.0029071517994024823, 0.0016025392331753556, -0.0007981591599705676, 0.00033979186197556166, -0.00010693619853382159], |
| 84 | [-0.00010693619853382162, 0.0003397918619755617, -0.0007981591599705678, 0.0016025392331753558, -0.002907151799402483, 0.004903989286442166, -0.007829952650949396, 0.011981956177112871, -0.017749978348738604, 0.025689736193174405, -0.03668721816095289, 0.05235945133062174, -0.07616916079430926, 0.11729533434259608, -0.21066210745435704, 0.711596821947973, 0.5524457400926122, -0.19273727916315744, 0.11053946484280223, -0.07253382289668457, 0.05005558258074105, -0.035100865123434175, 0.024552872817697434, -0.016923835860553748, 0.011383759722552787, -0.007403955600201462, 0.004608923212425363, -0.002710549189305756, 0.001478141328472037, -0.0007247222673508772, 0.00030047873971013604, -8.88890421823311e-05], |
| 85 | [-0.00010771945462504618, 0.0003243427909527165, -0.0007434479010369787, 0.0014706075069126927, -0.0026403492064193527, 0.004419776898118025, -0.007015178439754547, 0.010686142304650498, -0.01577646198402504, 0.02278221067211325, -0.03250685839030642, 0.04644402518288106, -0.06786563501345873, 0.10574314941958161, -0.19659496534270057, 0.845732285990534, 0.38230536106706786, -0.15038409915462567, 0.0884814830053416, -0.05859942936943878, 0.0405771942610876, -0.028468978092835426, 0.01988909208951188, -0.013674291087551773, 0.009164041758758899, -0.00593121232166118, 0.0036689169804141955, -0.002139989213494492, 0.0011539493624898776, -0.0005564555268935539, 0.00022418908603070835, -6.169787761900705e-05], |
| 86 | [-8.464122840432894e-05, 0.0002430517421209236, -0.0005445803041891528, 0.0010620801545960844, -0.0018880054283359473, 0.0031369765747049742, -0.004950613758014254, 0.007507937684653195, -0.011048146610773036, 0.015920900371157783, -0.022701723557568016, 0.032480746980827084, -0.047702981628914426, 0.07531945801178211, -0.145858673869235, 0.9426947295402057, 0.21555037786454243, -0.09294865839644116, 0.05591630108406594, -0.037337762954957596, 0.02593058478547953, -0.018197543923624927, 0.012695107173726997, -0.008704737233512524, 0.005811272430550032, -0.0037422781404128427, 0.0022998489712809, -0.0013300206219824767, 0.0007088167627000853, -0.0003358453978417591, 0.00013113986569634532, -3.311694388264495e-05], |
| 87 | [-3.464476442036121e-05, 9.537201341962134e-05, -0.00020919986159042688, 0.0004025328858112204, -0.0007087508284246483, 0.0011691692091189859, -0.0018349008372697964, 0.0027708836089717836, -0.004064703407090631, 0.005846125446777624, -0.008332275252542718, 0.011942377142413333, -0.0176400665654404, 0.028273499065978484, -0.05748200715526913, 0.9935370754684096, 0.06538702643260477, -0.03032170233155439, 0.018598441767977408, -0.012509950835799515, 0.00870982957472023, -0.00611241888772457, 0.004257373975001418, -0.0029109007764240528, 0.0019355921973831006, -0.001239982208738999, 0.0007569269598663377, -0.00043387082062890065, 0.00022840407596707644, -0.00010621819573270597, 4.007603567022298e-05, -9.113131439975678e-06], |
| 88 | ]; |
| 89 | |
| 90 | |
| 91 | |
| 92 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 93 | pub struct Loudness { |
| 94 | |
| 95 | pub integrated_lufs: f64, |
| 96 | |
| 97 | pub range_lu: f64, |
| 98 | |
| 99 | pub true_peak_dbtp: f64, |
| 100 | |
| 101 | pub sample_peak_dbfs: f64, |
| 102 | |
| 103 | pub momentary_max_lufs: f64, |
| 104 | |
| 105 | pub short_term_max_lufs: f64, |
| 106 | |
| 107 | pub analysed_secs: f64, |
| 108 | |
| 109 | |
| 110 | |
| 111 | pub lra_samples: usize, |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | #[derive(Debug, Clone, Copy)] |
| 117 | struct Biquad { |
| 118 | b: [f64; 3], |
| 119 | a: [f64; 2], |
| 120 | x: [f64; 2], |
| 121 | y: [f64; 2], |
| 122 | } |
| 123 | |
| 124 | impl Biquad { |
| 125 | fn new(b: [f64; 3], a: [f64; 2]) -> Self { |
| 126 | Self { |
| 127 | b, |
| 128 | a, |
| 129 | x: [0.0; 2], |
| 130 | y: [0.0; 2], |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn run(&mut self, x: f64) -> f64 { |
| 135 | let y = self.b[0] * x + self.b[1] * self.x[0] + self.b[2] * self.x[1] |
| 136 | - self.a[0] * self.y[0] |
| 137 | - self.a[1] * self.y[1]; |
| 138 | self.x = [x, self.x[0]]; |
| 139 | self.y = [y, self.y[0]]; |
| 140 | y |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | fn k_weighting(rate: f64) -> (Biquad, Biquad) { |
| 149 | let f0 = 1_681.974_450_955_533; |
| 150 | let g = 3.999_843_853_973_347; |
| 151 | let q = 0.707_175_236_955_419_6; |
| 152 | let k = (PI * f0 / rate).tan(); |
| 153 | let vh = 10f64.powf(g / 20.0); |
| 154 | let vb = vh.powf(0.499_666_774_154_541_4); |
| 155 | let a0 = 1.0 + k / q + k * k; |
| 156 | let shelf = Biquad::new( |
| 157 | [ |
| 158 | (vh + vb * k / q + k * k) / a0, |
| 159 | 2.0 * (k * k - vh) / a0, |
| 160 | (vh - vb * k / q + k * k) / a0, |
| 161 | ], |
| 162 | [2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0], |
| 163 | ); |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | let f0 = 38.135_470_876_024_44; |
| 170 | let q = 0.500_327_037_323_877_3; |
| 171 | let k = (PI * f0 / rate).tan(); |
| 172 | let a0 = 1.0 + k / q + k * k; |
| 173 | let hipass = Biquad::new( |
| 174 | [1.0, -2.0, 1.0], |
| 175 | [2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0], |
| 176 | ); |
| 177 | (shelf, hipass) |
| 178 | } |
| 179 | |
| 180 | |
| 181 | fn channel_weight(index: usize, channels: usize) -> f64 { |
| 182 | if channels < 6 { |
| 183 | return 1.0; |
| 184 | } |
| 185 | match index { |
| 186 | 3 => 0.0, |
| 187 | 4 | 5 => 1.41, |
| 188 | _ => 1.0, |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | #[derive(Debug, Clone)] |
| 195 | pub(crate) struct TruePeak { |
| 196 | hist: [f64; 2 * TP_TAPS], |
| 197 | pos: usize, |
| 198 | } |
| 199 | |
| 200 | impl TruePeak { |
| 201 | pub(crate) fn new() -> Self { |
| 202 | Self { |
| 203 | hist: [0.0; 2 * TP_TAPS], |
| 204 | pos: 0, |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | pub(crate) fn push(&mut self, x: f64) -> f64 { |
| 215 | self.pos = (self.pos + TP_TAPS - 1) % TP_TAPS; |
| 216 | self.hist[self.pos] = x; |
| 217 | self.hist[self.pos + TP_TAPS] = x; |
| 218 | let w = &self.hist[self.pos..self.pos + TP_TAPS]; |
| 219 | let mut peak = 0.0f64; |
| 220 | for row in &TP_COEF { |
| 221 | let y: f64 = w.iter().zip(row.iter()).map(|(s, c)| s * c).sum(); |
| 222 | peak = peak.max(y.abs()); |
| 223 | } |
| 224 | peak |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | #[derive(Debug, Clone)] |
| 231 | pub struct Meter { |
| 232 | channels: usize, |
| 233 | weights: Vec<f64>, |
| 234 | shelf: Vec<Biquad>, |
| 235 | hipass: Vec<Biquad>, |
| 236 | chunk_frames: usize, |
| 237 | frames_in_chunk: usize, |
| 238 | chunk_acc: f64, |
| 239 | |
| 240 | chunks: Vec<f64>, |
| 241 | sample_peak: f64, |
| 242 | tp: Vec<TruePeak>, |
| 243 | tp_peak: f64, |
| 244 | } |
| 245 | |
| 246 | impl Meter { |
| 247 | |
| 248 | pub fn new(sample_rate: u32, channels: u16) -> Self { |
| 249 | let channels = usize::from(channels).max(1); |
| 250 | let (shelf, hipass) = k_weighting(f64::from(sample_rate.max(1))); |
| 251 | Self { |
| 252 | channels, |
| 253 | weights: (0..channels).map(|c| channel_weight(c, channels)).collect(), |
| 254 | shelf: vec![shelf; channels], |
| 255 | hipass: vec![hipass; channels], |
| 256 | chunk_frames: (sample_rate as usize / CHUNKS_PER_SEC).max(1), |
| 257 | frames_in_chunk: 0, |
| 258 | chunk_acc: 0.0, |
| 259 | chunks: Vec::new(), |
| 260 | sample_peak: 0.0, |
| 261 | tp: vec![TruePeak::new(); channels], |
| 262 | tp_peak: 0.0, |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | |
| 268 | pub fn push(&mut self, interleaved: &[f32]) { |
| 269 | for frame in interleaved.chunks_exact(self.channels) { |
| 270 | let mut acc = 0.0; |
| 271 | for (c, &s) in frame.iter().enumerate() { |
| 272 | let x = f64::from(s); |
| 273 | self.sample_peak = self.sample_peak.max(x.abs()); |
| 274 | let y = self.hipass[c].run(self.shelf[c].run(x)); |
| 275 | acc += self.weights[c] * y * y; |
| 276 | let tp = self.tp[c].push(x); |
| 277 | self.tp_peak = self.tp_peak.max(tp); |
| 278 | } |
| 279 | self.chunk_acc += acc; |
| 280 | self.frames_in_chunk += 1; |
| 281 | if self.frames_in_chunk == self.chunk_frames { |
| 282 | self.chunks.push(self.chunk_acc / self.chunk_frames as f64); |
| 283 | self.chunk_acc = 0.0; |
| 284 | self.frames_in_chunk = 0; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | pub fn finish(&self) -> Loudness { |
| 293 | let chunks = &self.chunks; |
| 294 | |
| 295 | |
| 296 | let tp_peak = self.tp_peak; |
| 297 | |
| 298 | let momentary: Vec<f64> = (MOMENTARY_CHUNKS - 1..chunks.len()) |
| 299 | .map(|i| window_mean(chunks, i, MOMENTARY_CHUNKS)) |
| 300 | .collect(); |
| 301 | let short_term: Vec<f64> = (SHORT_TERM_CHUNKS - 1..chunks.len()) |
| 302 | .map(|i| window_mean(chunks, i, SHORT_TERM_CHUNKS)) |
| 303 | .collect(); |
| 304 | |
| 305 | let integrated = gated_blocks(&momentary, REL_GATE_LU); |
| 306 | let integrated_lufs = if integrated.is_empty() { |
| 307 | f64::NEG_INFINITY |
| 308 | } else { |
| 309 | lufs(mean(&integrated)) |
| 310 | }; |
| 311 | |
| 312 | |
| 313 | |
| 314 | let lra_blocks: Vec<f64> = short_term.iter().step_by(LRA_HOP_CHUNKS).copied().collect(); |
| 315 | let mut lra_values: Vec<f64> = gated_blocks(&lra_blocks, LRA_REL_GATE_LU) |
| 316 | .into_iter() |
| 317 | .map(lufs) |
| 318 | .collect(); |
| 319 | lra_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); |
| 320 | let lra_samples = lra_values.len(); |
| 321 | let range_lu = if lra_samples < 2 { |
| 322 | 0.0 |
| 323 | } else { |
| 324 | percentile(&lra_values, 0.95) - percentile(&lra_values, 0.10) |
| 325 | }; |
| 326 | |
| 327 | Loudness { |
| 328 | integrated_lufs, |
| 329 | range_lu, |
| 330 | true_peak_dbtp: db(tp_peak), |
| 331 | sample_peak_dbfs: db(self.sample_peak), |
| 332 | momentary_max_lufs: momentary |
| 333 | .iter() |
| 334 | .copied() |
| 335 | .map(lufs) |
| 336 | .fold(f64::NEG_INFINITY, f64::max), |
| 337 | short_term_max_lufs: short_term |
| 338 | .iter() |
| 339 | .copied() |
| 340 | .map(lufs) |
| 341 | .fold(f64::NEG_INFINITY, f64::max), |
| 342 | analysed_secs: chunks.len() as f64 / CHUNKS_PER_SEC as f64, |
| 343 | lra_samples, |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | fn window_mean(chunks: &[f64], end: usize, n: usize) -> f64 { |
| 350 | chunks[end + 1 - n..=end].iter().sum::<f64>() / n as f64 |
| 351 | } |
| 352 | |
| 353 | fn mean(xs: &[f64]) -> f64 { |
| 354 | xs.iter().sum::<f64>() / xs.len() as f64 |
| 355 | } |
| 356 | |
| 357 | |
| 358 | |
| 359 | fn gated_blocks(blocks: &[f64], rel_lu: f64) -> Vec<f64> { |
| 360 | let above_abs: Vec<f64> = blocks |
| 361 | .iter() |
| 362 | .copied() |
| 363 | .filter(|&p| lufs(p) > ABS_GATE_LUFS) |
| 364 | .collect(); |
| 365 | if above_abs.is_empty() { |
| 366 | return Vec::new(); |
| 367 | } |
| 368 | let relative = lufs(mean(&above_abs)) + rel_lu; |
| 369 | above_abs |
| 370 | .into_iter() |
| 371 | .filter(|&p| lufs(p) > relative) |
| 372 | .collect() |
| 373 | } |
| 374 | |
| 375 | |
| 376 | fn percentile(sorted: &[f64], p: f64) -> f64 { |
| 377 | let idx = ((sorted.len() - 1) as f64 * p).round() as usize; |
| 378 | sorted[idx.min(sorted.len() - 1)] |
| 379 | } |
| 380 | |
| 381 | |
| 382 | fn lufs(power: f64) -> f64 { |
| 383 | if power > 0.0 { |
| 384 | -0.691 + 10.0 * power.log10() |
| 385 | } else { |
| 386 | f64::NEG_INFINITY |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
| 391 | fn db(amplitude: f64) -> f64 { |
| 392 | if amplitude > 0.0 { |
| 393 | 20.0 * amplitude.log10() |
| 394 | } else { |
| 395 | f64::NEG_INFINITY |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | pub fn measure_file( |
| 404 | path: &Path, |
| 405 | stream: usize, |
| 406 | range: Option<(f64, f64)>, |
| 407 | ) -> crate::Result<Option<Loudness>> { |
| 408 | let (from, to) = range.unwrap_or((0.0, f64::INFINITY)); |
| 409 | let sources = [(path.to_path_buf(), stream)]; |
| 410 | let Some((meta, rx)) = |
| 411 | AudioSession::open_multi_streams(&sources, &[(Some(0), from.max(0.0), to)])? |
| 412 | else { |
| 413 | return Ok(None); |
| 414 | }; |
| 415 | let mut meter = Meter::new(meta.sample_rate, meta.channels); |
| 416 | for chunk in rx { |
| 417 | meter.push(&chunk.samples); |
| 418 | } |
| 419 | Ok(Some(meter.finish())) |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | pub fn gain_to_target(measured: &Loudness, target_lufs: f64, tp_ceiling: f64) -> f64 { |
| 427 | if !measured.integrated_lufs.is_finite() { |
| 428 | return 0.0; |
| 429 | } |
| 430 | let mut gain = target_lufs - measured.integrated_lufs; |
| 431 | if measured.true_peak_dbtp.is_finite() { |
| 432 | gain = gain.min(tp_ceiling - measured.true_peak_dbtp); |
| 433 | } |
| 434 | gain |
| 435 | } |
| 436 | |
| 437 | #[cfg(test)] |
| 438 | mod tests { |
| 439 | use std::path::PathBuf; |
| 440 | |
| 441 | use super::{Loudness, Meter, TP_COEF, gain_to_target, measure_file}; |
| 442 | |
| 443 | |
| 444 | |
| 445 | fn sine( |
| 446 | rate: u32, |
| 447 | secs: f64, |
| 448 | amp: f64, |
| 449 | freq: f64, |
| 450 | phase: f64, |
| 451 | channels: usize, |
| 452 | live: &[usize], |
| 453 | ) -> Vec<f32> { |
| 454 | let frames = (f64::from(rate) * secs) as usize; |
| 455 | let mut out = Vec::with_capacity(frames * channels); |
| 456 | for n in 0..frames { |
| 457 | let v = amp |
| 458 | * (2.0 * std::f64::consts::PI * freq * n as f64 / f64::from(rate) + phase).sin(); |
| 459 | for c in 0..channels { |
| 460 | out.push(if live.contains(&c) { v as f32 } else { 0.0 }); |
| 461 | } |
| 462 | } |
| 463 | out |
| 464 | } |
| 465 | |
| 466 | fn measure(samples: &[f32], rate: u32, channels: u16) -> Loudness { |
| 467 | let mut m = Meter::new(rate, channels); |
| 468 | |
| 469 | |
| 470 | for piece in samples.chunks(4097 * usize::from(channels)) { |
| 471 | m.push(piece); |
| 472 | } |
| 473 | m.finish() |
| 474 | } |
| 475 | |
| 476 | |
| 477 | |
| 478 | #[test] |
| 479 | fn one_channel_tone_at_minus_20_dbfs_reads_minus_23() { |
| 480 | let amp = 0.1; |
| 481 | let s = sine(48_000, 10.0, amp, 1000.0, 0.0, 2, &[0]); |
| 482 | let l = measure(&s, 48_000, 2); |
| 483 | assert!((l.integrated_lufs - -23.0).abs() < 0.02, "{l:?}"); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | #[test] |
| 490 | fn two_channel_tone_at_minus_20_dbfs_reads_minus_20() { |
| 491 | let s = sine(48_000, 10.0, 0.1, 1000.0, 0.0, 2, &[0, 1]); |
| 492 | let l = measure(&s, 48_000, 2); |
| 493 | assert!((l.integrated_lufs - -20.0).abs() < 0.1, "{l:?}"); |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn same_reading_at_44_1_khz() { |
| 498 | let s = sine(44_100, 10.0, 0.1, 1000.0, 0.0, 2, &[0]); |
| 499 | let l = measure(&s, 44_100, 2); |
| 500 | assert!((l.integrated_lufs - -23.0).abs() < 0.02, "{l:?}"); |
| 501 | } |
| 502 | |
| 503 | #[test] |
| 504 | fn lra_needs_two_two_hz_samples_and_says_how_many_it_had() { |
| 505 | |
| 506 | let three = measure(&sine(48_000, 3.0, 0.1, 440.0, 0.0, 1, &[0]), 48_000, 1); |
| 507 | assert_eq!(three.lra_samples, 1); |
| 508 | assert_eq!(three.range_lu, 0.0); |
| 509 | assert!((three.analysed_secs - 3.0).abs() < 1e-9, "{}", three.analysed_secs); |
| 510 | let four = measure(&sine(48_000, 4.0, 0.1, 440.0, 0.0, 1, &[0]), 48_000, 1); |
| 511 | assert!(four.lra_samples >= 2, "{}", four.lra_samples); |
| 512 | } |
| 513 | |
| 514 | #[test] |
| 515 | fn silence_is_negative_infinity_and_has_no_range() { |
| 516 | let s = vec![0.0f32; 48_000 * 2 * 5]; |
| 517 | let l = measure(&s, 48_000, 2); |
| 518 | assert_eq!(l.integrated_lufs, f64::NEG_INFINITY); |
| 519 | assert_eq!(l.true_peak_dbtp, f64::NEG_INFINITY); |
| 520 | assert_eq!(l.sample_peak_dbfs, f64::NEG_INFINITY); |
| 521 | assert_eq!(l.momentary_max_lufs, f64::NEG_INFINITY); |
| 522 | assert_eq!(l.range_lu, 0.0); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | #[test] |
| 530 | fn gating_ignores_trailing_silence() { |
| 531 | let tone = sine(48_000, 10.0, 0.1, 1000.0, 0.0, 2, &[0]); |
| 532 | let alone = measure(&tone, 48_000, 2); |
| 533 | let mut padded = tone.clone(); |
| 534 | padded.extend(std::iter::repeat_n(0.0f32, 48_000 * 2 * 10)); |
| 535 | let gated = measure(&padded, 48_000, 2); |
| 536 | assert!( |
| 537 | (gated.integrated_lufs - alone.integrated_lufs).abs() < 0.1, |
| 538 | "alone {alone:?} padded {gated:?}" |
| 539 | ); |
| 540 | } |
| 541 | |
| 542 | #[test] |
| 543 | fn full_scale_sine_true_peak_is_about_zero_dbtp() { |
| 544 | let s = sine(48_000, 1.0, 1.0, 1000.0, 0.0, 1, &[0]); |
| 545 | let l = measure(&s, 48_000, 1); |
| 546 | assert!(l.true_peak_dbtp.abs() < 0.1, "{l:?}"); |
| 547 | assert!(l.sample_peak_dbfs.abs() < 0.1, "{l:?}"); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | #[test] |
| 553 | fn intersample_peak_is_found_above_sample_peak() { |
| 554 | let s = sine( |
| 555 | 48_000, |
| 556 | 1.0, |
| 557 | 1.0, |
| 558 | 12_000.0, |
| 559 | std::f64::consts::FRAC_PI_4, |
| 560 | 1, |
| 561 | &[0], |
| 562 | ); |
| 563 | let l = measure(&s, 48_000, 1); |
| 564 | assert!((l.sample_peak_dbfs - -3.01).abs() < 0.05, "{l:?}"); |
| 565 | assert!(l.true_peak_dbtp.abs() < 0.2, "{l:?}"); |
| 566 | assert!(l.true_peak_dbtp > l.sample_peak_dbfs + 2.5, "{l:?}"); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | #[test] |
| 574 | fn fs_over_4_sine_at_45_degrees_reads_about_3_dbtp() { |
| 575 | let s = sine( |
| 576 | 48_000, |
| 577 | 1.0, |
| 578 | std::f64::consts::SQRT_2, |
| 579 | 12_000.0, |
| 580 | std::f64::consts::FRAC_PI_4, |
| 581 | 1, |
| 582 | &[0], |
| 583 | ); |
| 584 | let l = measure(&s, 48_000, 1); |
| 585 | assert!(l.sample_peak_dbfs.abs() < 0.05, "{l:?}"); |
| 586 | assert!((l.true_peak_dbtp - 3.0).abs() < 0.2, "{l:?}"); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | #[test] |
| 593 | fn every_interpolator_phase_has_unit_dc_gain() { |
| 594 | for (p, row) in TP_COEF.iter().enumerate() { |
| 595 | let sum: f64 = row.iter().sum(); |
| 596 | assert!((sum - 1.0).abs() < 1e-9, "phase {p} sums to {sum}"); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
| 602 | #[test] |
| 603 | fn a_997_hz_sine_at_minus_6_dbfs_reads_minus_6_dbtp() { |
| 604 | let amp = 10f64.powf(-6.0 / 20.0); |
| 605 | let s = sine(48_000, 1.0, amp, 997.0, 0.3, 1, &[0]); |
| 606 | let l = measure(&s, 48_000, 1); |
| 607 | assert!((l.true_peak_dbtp - -6.0).abs() < 0.1, "{l:?}"); |
| 608 | } |
| 609 | |
| 610 | #[test] |
| 611 | fn measure_file_reads_the_test_tone() { |
| 612 | let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/test_tone.wav"); |
| 613 | let l = measure_file(&path, 0, None) |
| 614 | .expect("decode") |
| 615 | .expect("has audio"); |
| 616 | assert!(l.integrated_lufs.is_finite(), "{l:?}"); |
| 617 | assert!((-60.0..0.0).contains(&l.integrated_lufs), "{l:?}"); |
| 618 | assert!( |
| 619 | l.true_peak_dbtp.is_finite() && l.true_peak_dbtp <= 0.5, |
| 620 | "{l:?}" |
| 621 | ); |
| 622 | assert!(l.sample_peak_dbfs <= l.true_peak_dbtp + 0.5, "{l:?}"); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | |
| 627 | #[test] |
| 628 | fn k_weighting_at_48_khz_is_the_bs1770_table() { |
| 629 | let (shelf, hipass) = super::k_weighting(48_000.0); |
| 630 | let close = |got: &[f64], want: &[f64]| { |
| 631 | got.iter() |
| 632 | .zip(want) |
| 633 | .all(|(g, w)| (g - w).abs() < 1e-10) |
| 634 | }; |
| 635 | assert!(close( |
| 636 | &shelf.b, |
| 637 | &[1.535_124_859_586_97, -2.691_696_189_406_38, 1.198_392_810_852_85] |
| 638 | )); |
| 639 | assert!(close(&shelf.a, &[-1.690_659_293_182_41, 0.732_480_774_215_85])); |
| 640 | assert!(close(&hipass.b, &[1.0, -2.0, 1.0])); |
| 641 | assert!(close(&hipass.a, &[-1.990_047_454_833_98, 0.990_072_250_366_21])); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | #[test] |
| 648 | fn one_channel_997_hz_at_minus_20_dbfs_matches_ffmpeg() { |
| 649 | |
| 650 | let s = sine(48_000, 10.0, 0.1, 997.0, 0.0, 1, &[0]); |
| 651 | let l = measure(&s, 48_000, 1); |
| 652 | assert!((l.integrated_lufs - -23.016).abs() < 0.01, "{l:?}"); |
| 653 | } |
| 654 | |
| 655 | #[test] |
| 656 | fn stereo_997_hz_at_minus_23_dbfs_matches_ffmpeg() { |
| 657 | |
| 658 | let amp = 10f64.powf(-23.0 / 20.0); |
| 659 | let s = sine(48_000, 10.0, amp, 997.0, 0.0, 2, &[0, 1]); |
| 660 | let l = measure(&s, 48_000, 2); |
| 661 | assert!((l.integrated_lufs - -23.006).abs() < 0.01, "{l:?}"); |
| 662 | } |
| 663 | |
| 664 | #[test] |
| 665 | fn gain_is_limited_by_true_peak_ceiling() { |
| 666 | let m = Loudness { |
| 667 | integrated_lufs: -23.0, |
| 668 | range_lu: 0.0, |
| 669 | true_peak_dbtp: -3.0, |
| 670 | sample_peak_dbfs: -4.0, |
| 671 | momentary_max_lufs: -20.0, |
| 672 | short_term_max_lufs: -21.0, |
| 673 | analysed_secs: 10.0, |
| 674 | lra_samples: 0, |
| 675 | }; |
| 676 | |
| 677 | assert!((gain_to_target(&m, -16.0, -1.0) - 2.0).abs() < 1e-9); |
| 678 | |
| 679 | assert!((gain_to_target(&m, -26.0, -1.0) - -3.0).abs() < 1e-9); |
| 680 | let silent = Loudness { |
| 681 | integrated_lufs: f64::NEG_INFINITY, |
| 682 | true_peak_dbtp: f64::NEG_INFINITY, |
| 683 | ..m |
| 684 | }; |
| 685 | assert_eq!(gain_to_target(&silent, -16.0, -1.0), 0.0); |
| 686 | } |
| 687 | } |