Cap the ATH at -80 dBFS so an onset inside a short block is not coded against a -10 dB floor
Browse files at this commit9b7e0e0e2c0d4598b6bc63c9746ae977c4a92a5aParent: 63f547a- fix
- cap the ATH at -80 dBFS so an onset inside a short block is not coded against a -10 dB floor
2 changed files +69 -55
crates/ec-vorbis/src/encode.rsmodified+15 -1
| @@ -457,6 +457,14 @@ impl VorbisEncoder { | |||
| 457 | if buffer_start < 0 || self.buffer[0].len() < buffer_start as usize + n { | 457 | if buffer_start < 0 || self.buffer[0].len() < buffer_start as usize + n { |
| 458 | break; | 458 | break; |
| 459 | } | 459 | } |
| 460 | + // A steady long block is typed by the hop *after* it; until that | ||
| 461 | + // hop is buffered the plan just peeked was decided blind. | ||
| 462 | + if !self.finished | ||
| 463 | + && matches!(self.scheduler, Sched::Steady) | ||
| 464 | + && self.buffer[0].len() < buffer_start as usize + n + BLOCK_LONG | ||
| 465 | + { | ||
| 466 | + break; | ||
| 467 | + } | ||
| 460 | let granule = match self.finished { | 468 | let granule = match self.finished { |
| 461 | true => centre.min(self.fed), | 469 | true => centre.min(self.fed), |
| 462 | false => centre, | 470 | false => centre, |
| @@ -933,11 +941,17 @@ fn design_residue_books() -> Vec<CodebookSpec> { | |||
| 933 | } | 941 | } |
| 934 | 942 | ||
| 935 | /// Absolute threshold of hearing in dBFS, full scale taken as 96 dB SPL. | 943 | /// Absolute threshold of hearing in dBFS, full scale taken as 96 dB SPL. |
| 944 | +/// | ||
| 945 | +/// Capped at -80 dBFS: the curve's own values at DC and above ~17 kHz | ||
| 946 | +/// (-13 / -10 dBFS) are not a quantiser step the rest of a block can live | ||
| 947 | +/// with — an onset inside a short block puts real energy in exactly those | ||
| 948 | +/// bins, and coding them that coarsely leaked -38 dB of pre-echo across the | ||
| 949 | +/// whole 256-sample window. | ||
| 936 | fn absolute_threshold(hz: f64) -> f64 { | 950 | fn absolute_threshold(hz: f64) -> f64 { |
| 937 | let khz = (hz / 1000.0).clamp(0.02, 20.0); | 951 | let khz = (hz / 1000.0).clamp(0.02, 20.0); |
| 938 | let spl = | 952 | let spl = |
| 939 | 3.64 * khz.powf(-0.8) - 6.5 * (-0.6 * (khz - 3.3).powi(2)).exp() + 0.001 * khz.powi(4); | 953 | 3.64 * khz.powf(-0.8) - 6.5 * (-0.6 * (khz - 3.3).powi(2)).exp() + 0.001 * khz.powi(4); |
| 940 | - (spl - 96.0).clamp(-120.0, -10.0) | 954 | + (spl - 96.0).clamp(-120.0, -80.0) |
| 941 | } | 955 | } |
| 942 | 956 | ||
| 943 | /// The three header packets, in order. | 957 | /// The three header packets, in order. |
crates/ec-vorbis/tests/oracle.rsmodified+54 -54
| @@ -561,61 +561,61 @@ fn busy(channels: usize, rate: u32, samples: usize) -> Vec<Vec<f32>> { | |||
| 561 | /// does. | 561 | /// does. |
| 562 | #[test] | 562 | #[test] |
| 563 | fn onset_after_silence_has_no_pre_echo() { | 563 | fn onset_after_silence_has_no_pre_echo() { |
| 564 | + // Two onsets: one off the block grid (0.9137 s lands mid short block) and | ||
| 565 | + // one on it. The gap is measured over the WHOLE silence up to the onset, | ||
| 566 | + // so a leak in the last few hundred samples before it cannot hide behind a | ||
| 567 | + // guard band, and the peak is barred too. | ||
| 564 | for rate in [44_100u32, 48_000] { | 568 | for rate in [44_100u32, 48_000] { |
| 565 | - let silence = rate as usize; | 569 | + for channels in [1usize, 2] { |
| 566 | - let tone_len = rate as usize; | 570 | + for onset_s in [0.9137f64, 1.0] { |
| 567 | - let mut left = vec![0.0f32; silence + tone_len]; | 571 | + let onset = (onset_s * f64::from(rate)) as usize; |
| 568 | - let mut right = vec![0.0f32; silence + tone_len]; | 572 | + let tone_len = rate as usize; |
| 569 | - for i in 0..tone_len { | 573 | + let mut source = vec![vec![0.0f32; onset + tone_len]; channels]; |
| 570 | - let t = i as f64 / f64::from(rate); | 574 | + for i in 0..tone_len { |
| 571 | - let v = (2.0 * std::f64::consts::PI * 1_000.0 * t).sin() as f32; | 575 | + let t = i as f64 / f64::from(rate); |
| 572 | - left[silence + i] = v; | 576 | + let v = (0.7 * (2.0 * std::f64::consts::PI * 1_000.0 * t).sin()) as f32; |
| 573 | - right[silence + i] = v; | 577 | + for plane in &mut source { |
| 574 | - } | 578 | + plane[onset + i] = v; |
| 575 | - let source = vec![left, right]; | 579 | + } |
| 576 | - let name = format!("onset-{rate}.ogg"); | 580 | + } |
| 577 | - let path = encode_to_file(&source, rate, -1, &name); | 581 | + let name = format!("onset-{rate}-{channels}ch-{onset}.ogg"); |
| 578 | - | 582 | + let path = encode_to_file(&source, rate, 128_000, &name); |
| 579 | - // The reference oracle must decode the short-block stream cleanly: | 583 | + |
| 580 | - // no warnings, let alone errors. | 584 | + // The reference oracle must decode the short-block stream |
| 581 | - let warn = Command::new("ffmpeg") | 585 | + // cleanly: no warnings, let alone errors. |
| 582 | - .args(["-v", "warning", "-i"]) | 586 | + let warn = Command::new("ffmpeg") |
| 583 | - .arg(&path) | 587 | + .args(["-v", "warning", "-i"]) |
| 584 | - .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) | 588 | + .arg(&path) |
| 585 | - .output(); | 589 | + .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) |
| 586 | - if let Ok(out) = warn { | 590 | + .output(); |
| 587 | - assert!( | 591 | + if let Ok(out) = warn { |
| 588 | - out.stderr.is_empty(), | 592 | + assert!( |
| 589 | - "{name}: oracle decode warnings: {}", | 593 | + out.stderr.is_empty(), |
| 590 | - String::from_utf8_lossy(&out.stderr) | 594 | + "{name}: oracle decode warnings: {}", |
| 591 | - ); | 595 | + String::from_utf8_lossy(&out.stderr) |
| 592 | - } | 596 | + ); |
| 593 | - | 597 | + } |
| 594 | - let window = |plane: &[f32], rate: u32| -> f64 { | 598 | + |
| 595 | - let lo = (0.90 * f64::from(rate)) as usize; | 599 | + let tone_rms = rms(&source[0][onset..]); |
| 596 | - let hi = ((0.995 * f64::from(rate)) as usize).min(plane.len()); | 600 | + let check = |label: &str, planes: &[Vec<f32>]| { |
| 597 | - let lo = lo.min(hi); | 601 | + for (c, plane) in planes.iter().enumerate() { |
| 598 | - rms(&plane[lo..hi]) | 602 | + let gap = &plane[..onset.min(plane.len())]; |
| 599 | - }; | 603 | + let gap_rms = rms(gap); |
| 600 | - let tone_rms = { | 604 | + let peak = gap.iter().fold(0.0f32, |m, v| m.max(v.abs())); |
| 601 | - let lo = silence + rate as usize / 20; | 605 | + println!("{name}/{label} ch{c}: gap RMS {gap_rms:.6} peak {peak:.4}"); |
| 602 | - let hi = (silence + tone_len).min(silence + tone_len); | 606 | + assert!( |
| 603 | - rms(&source[0][lo..hi]) | 607 | + gap_rms <= 0.002 * tone_rms, |
| 604 | - }; | 608 | + "{name}/{label} ch{c}: gap RMS {gap_rms:.6} vs tone RMS {tone_rms:.6}" |
| 605 | - | 609 | + ); |
| 606 | - let (ours, _) = our_decode(&path); | 610 | + assert!(peak <= 0.007, "{name}/{label} ch{c}: gap peak {peak:.4}"); |
| 607 | - let gap = window(&ours[0], rate); | 611 | + } |
| 608 | - assert!( | 612 | + }; |
| 609 | - gap <= 0.002 * tone_rms, | 613 | + let (ours, _) = our_decode(&path); |
| 610 | - "{name}: our decode gap RMS {gap:.6} vs tone RMS {tone_rms:.6}" | 614 | + check("ours", &ours); |
| 611 | - ); | 615 | + if let Some((theirs, _)) = ffmpeg_decode(&path) { |
| 612 | - | 616 | + check("oracle", &theirs); |
| 613 | - if let Some((theirs, _)) = ffmpeg_decode(&path) { | 617 | + } |
| 614 | - let gap = window(&theirs[0], rate); | 618 | + } |
| 615 | - assert!( | ||
| 616 | - gap <= 0.002 * tone_rms, | ||
| 617 | - "{name}: oracle decode gap RMS {gap:.6} vs tone RMS {tone_rms:.6}" | ||
| 618 | - ); | ||
| 619 | } | 619 | } |
| 620 | } | 620 | } |
| 621 | } | 621 | } |