Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea check measures clipping on each channel: the mono fold read a clean master as clipped

Zachary Guglin6d076868f0a8
diff --git a/apps/check/read/src/lib.rs b/apps/check/read/src/lib.rs
index ac56a8d8..200f594e 100644
--- a/apps/check/read/src/lib.rs
+++ b/apps/check/read/src/lib.rs
@@ -152,7 +152,30 @@ pub fn flash_frames(video: &str, w: usize, h: usize, mut each: impl FnMut(f64, &
/// `ffmpeg -i V -vn -ac 1 -ar RATE -f f32le -` (sound.rs sound): the sound folded to mono at `rate`
pub fn mono(video: &str, rate: u32) -> R<Vec<f32>> {
- Ok(raea_app_media::audio_folded(Path::new(video), rate, true, None, None, false)?.channels.swap_remove(0))
+ if native() {
+ return Ok(raea_app_media::audio_folded(Path::new(video), rate, true, None, None, false)?.channels.swap_remove(0));
+ }
+ ffmpeg_f32(video, 1, rate)
+}
+
+/// `ffmpeg -i V -vn -ac N -ar R -f f32le -`: the sound as N interleaved channels of floats
+fn ffmpeg_f32(video: &str, channels: u32, rate: u32) -> R<Vec<f32>> {
+ let out = Command::new("ffmpeg").args(["-v", "error", "-i", video, "-vn", "-ac", &channels.to_string(), "-ar", &rate.to_string(), "-f", "f32le", "-"]).output().map_err(|e| format!("ffmpeg: {e}"))?;
+ if !out.status.success() {
+ return Err(format!("ffmpeg couldn't read the sound of {video}: {}", String::from_utf8_lossy(&out.stderr).trim()));
+ }
+ Ok(out.stdout.chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect())
+}
+
+/// `ffmpeg -i V -vn -ac 2 -ar R -f f32le -` (defects.rs clipping): each channel at its own level. Clipping is a
+/// channel's: on the mono fold (L + R) * 0.707 a centred voice reads 3 dB hotter than either side, and a master at
+/// -1.6 dBTP read as full scale (11-19, the walkaround's flat tops).
+pub fn channels(video: &str, rate: u32) -> R<Vec<Vec<f32>>> {
+ if native() {
+ return Ok(raea_app_media::audio_folded(Path::new(video), rate, false, None, None, false)?.channels);
+ }
+ let x = ffmpeg_f32(video, 2, rate)?;
+ Ok(vec![x.iter().step_by(2).copied().collect(), x.iter().skip(1).step_by(2).copied().collect()])
}
/// `ffmpeg -i V -vn -af ebur128=metadata=1,ametadata=print:key=lavfi.r128.M:file=- -f null -` (sound.rs sound): the
diff --git a/apps/check/src/defects.rs b/apps/check/src/defects.rs
index 1e699f9d..44f51b64 100644
--- a/apps/check/src/defects.rs
+++ b/apps/check/src/defects.rs
@@ -11,8 +11,12 @@ pub(crate) const FULL: f32 = 0.999;
pub(crate) const FLAT_LEVEL: f32 = 0.05;
pub(crate) const CLIP_RUNS: usize = 3;
-pub(crate) fn clipped(x: &[f32]) -> Vec<(f64, String)> {
- let mut per: Vec<usize> = vec![0; x.len() / SR + 1];
+pub(crate) fn clipped(channels: &[Vec<f32>]) -> Vec<(f64, String)> {
+ let len = channels.iter().map(Vec::len).max().unwrap_or(0);
+ let mut per: Vec<usize> = vec![0; len / SR + 1];
+ // each channel's runs, a second's count the most any channel had: clipping is a channel's
+ for x in channels {
+ let mut own: Vec<usize> = vec![0; len / SR + 1];
let mut i = 0;
while i < x.len() {
let v = x[i];
@@ -22,18 +26,22 @@ pub(crate) fn clipped(x: &[f32]) -> Vec<(f64, String)> {
j += 1;
}
if j - i >= 3 {
- per[i / SR] += 1;
+ own[i / SR] += 1;
}
} else if v.abs() >= FLAT_LEVEL {
while j < x.len() && x[j].to_bits() == v.to_bits() {
j += 1;
}
if j - i >= 4 {
- per[i / SR] += 1;
+ own[i / SR] += 1;
}
}
i = j;
}
+ for (p, o) in per.iter_mut().zip(own) {
+ *p = (*p).max(o);
+ }
+ }
let mut out = Vec::new();
let mut s = 0;
while s < per.len() {
@@ -145,8 +153,8 @@ pub(crate) fn moments(rule: &'static str, p: u8, found: Vec<(f64, String)>) -> i
}
/// The sound's defects on the decoded mix (sound.rs decodes it once)
-pub(crate) fn sound_defects(x: &[f32], issues: &mut Vec<Issue>, passed: &mut Vec<String>) {
- for (rule, found, ok) in [("clipping", clipped(x), "clipping: no flat-topped runs"), ("dropout", dropouts(x), "dropout: the sound never drops to silence and back")] {
+pub(crate) fn sound_defects(x: &[f32], channels: &[Vec<f32>], issues: &mut Vec<Issue>, passed: &mut Vec<String>) {
+ for (rule, found, ok) in [("clipping", clipped(channels), "clipping: no flat-topped runs"), ("dropout", dropouts(x), "dropout: the sound never drops to silence and back")] {
if found.is_empty() {
passed.push(ok.into());
}
diff --git a/apps/check/src/sound.rs b/apps/check/src/sound.rs
index 83d4ecf2..6c02d71c 100644
--- a/apps/check/src/sound.rs
+++ b/apps/check/src/sound.rs
@@ -162,17 +162,9 @@ pub(crate) fn bed_up(rms: &[f64], speak: &[bool]) -> Vec<(f64, String)> {
/// The sound's rules on an export: dead stops always; with the voice's words, a stretch with no voice over it and a bed
/// too close under it
pub(crate) fn sound(video: &str, words: Option<&[Word]>, issues: &mut Vec<Issue>, passed: &mut Vec<String>) -> R<()> {
- let x = if crate::read::native() {
- crate::read::mono(video, SR as u32)?
- } else {
- let out = Command::new("ffmpeg").args(["-v", "error", "-i", video, "-vn", "-ac", "1", "-ar", &SR.to_string(), "-f", "f32le", "-"]).output().map_err(|e| format!("ffmpeg: {e}"))?;
- if !out.status.success() {
- return Err(format!("ffmpeg couldn't read the sound of {video}: {}", String::from_utf8_lossy(&out.stderr).trim()));
- }
- out.stdout.chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect()
- };
+ let x = crate::read::mono(video, SR as u32)?;
let (rms, bands) = levels(&x);
- sound_defects(&x, issues, passed);
+ sound_defects(&x, &crate::read::channels(video, SR as u32)?, issues, passed);
let found = |list: Vec<(f64, String)>, p: u8| crate::defects::moments("sound", p, list);
let stops = dead_stops(&rms, &bands);
if stops.is_empty() {
diff --git a/apps/check/src/tests.rs b/apps/check/src/tests.rs
index 9782530c..144fb1f9 100644
--- a/apps/check/src/tests.rs
+++ b/apps/check/src/tests.rs
@@ -356,15 +356,23 @@ fn a_card_sharing_one_word_with_another_line_isnt_held_to_it() {
fn clipping_is_found_and_a_loud_clean_tone_isnt() {
// a clean tone near full scale, then the same tone clamped (a limiter's flat tops), then pushed past full scale
let loud = tone(3.0, &[(0.0, 3.0, -1.0)]);
- assert!(clipped(&loud).is_empty(), "{:?}", clipped(&loud));
+ let one = |x: &[f32]| clipped(&[x.to_vec()]);
+ assert!(one(&loud).is_empty(), "{:?}", one(&loud));
let clamped: Vec<f32> = loud.iter().map(|v| (v * 10.0).clamp(-0.3, 0.3)).collect();
- assert_eq!(clipped(&clamped).len(), 1);
+ assert_eq!(one(&clamped).len(), 1);
let over: Vec<f32> = loud.iter().map(|v| (v * 2.0).clamp(-1.0, 1.0)).collect();
- assert_eq!(clipped(&over).len(), 1);
+ assert_eq!(one(&over).len(), 1);
// one transient hitting full scale once isn't clipping
let mut once = tone(3.0, &[(0.0, 3.0, -20.0)]);
once[SR] = 1.0;
- assert!(clipped(&once).is_empty());
+ assert!(one(&once).is_empty());
+ // clipping is a channel's: one side clipped is found; a centred voice at -1.6 dBFS a side is clean, though the mono
+ // fold (L + R) * 0.707 takes it past full scale (11-19: the walkaround's master read as clipping)
+ assert_eq!(clipped(&[loud.clone(), over.clone()]).len(), 1);
+ let centred: Vec<f32> = loud.iter().map(|v| v * 0.92).collect();
+ assert!(clipped(&[centred.clone(), centred.clone()]).is_empty());
+ let folded: Vec<f32> = centred.iter().map(|v| (v + v) * 0.7071).collect();
+ assert_eq!(one(&folded).len(), 1);
}
#[test]