Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M7.1: BS.1770 loudness in Audio Meters (momentary/short-term/integrated LUFS, true peak) fed with contiguous programme audio; matches ffmpeg ebur128

Brandon Thomasde7e84307d9c
diff --git a/Cargo.lock b/Cargo.lock
index 090956989..4eacb0c9b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1359,6 +1359,7 @@ dependencies = [
"filmcraft-h264",
"filmcraft-hevc",
"filmcraft-isobmff",
+ "filmcraft-matroska",
"filmcraft-media",
"filmcraft-prores",
"filmcraft-time",
@@ -1569,6 +1570,7 @@ version = "0.1.0"
dependencies = [
"eframe",
"egui",
+ "filmcraft-audio-dsp",
"filmcraft-color",
"filmcraft-edit",
"filmcraft-engine",
diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml
index 4a45e40a4..540fc7bbb 100644
--- a/crates/ui-egui/Cargo.toml
+++ b/crates/ui-egui/Cargo.toml
@@ -18,6 +18,7 @@ filmcraft-media = { workspace = true }
filmcraft-project = { workspace = true }
filmcraft-edit = { workspace = true }
filmcraft-render = { workspace = true }
+filmcraft-audio-dsp = { workspace = true }
filmcraft-engine = { workspace = true }
filmcraft-gpu = { workspace = true }
egui = { workspace = true }
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index f7b07cc69..6bd56fd39 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -89,6 +89,8 @@ pub struct FilmcraftApp {
deferred: Vec<(ControlRequest, f64)>,
last_ui_time: f64,
pub(crate) synthetic: Vec<egui::Event>,
+ /// BS.1770 loudness of the programme as it plays, and the next sample position to feed.
+ pub(crate) loudness: Option<(filmcraft_audio_dsp::LoudnessMeter, i64)>,
/// Status message last shown and when it first appeared (messages expire after a few seconds).
status_seen: (String, f64),
pending_screenshots: Vec<(u64, Option<String>, Option<[f32; 4]>, Sender<Value>)>,
@@ -176,6 +178,7 @@ impl FilmcraftApp {
deferred: Vec::new(),
last_ui_time: 0.0,
synthetic: Vec::new(),
+ loudness: None,
status_seen: (String::new(), 0.0),
pending_screenshots: Vec::new(),
queued_screenshots: Vec::new(),
diff --git a/crates/ui-egui/src/panels/meters.rs b/crates/ui-egui/src/panels/meters.rs
index 86bb7e782..001231212 100644
--- a/crates/ui-egui/src/panels/meters.rs
+++ b/crates/ui-egui/src/panels/meters.rs
@@ -20,8 +20,47 @@ fn levels(app: &FilmcraftApp) -> [f32; 2] {
[20.0 * p.first().copied().unwrap_or(0.0).max(1e-6).log10(), 20.0 * p.get(1).copied().unwrap_or(0.0).max(1e-6).log10()]
}
+/// Feed the loudness meter with exactly the programme audio played since the last frame
+/// (consecutive, non-overlapping blocks, so gating and integration are correct). Seeks reset it.
+fn feed_loudness(app: &mut FilmcraftApp) {
+ if !app.playback.playing {
+ return;
+ }
+ let Some(seq) = app.session.active_sequence() else { return };
+ let sr = seq.settings.sample_rate as i64;
+ let now = app.session.playhead().to_units_floor(sr);
+ let fresh = match &app.loudness {
+ Some((_, next)) => now < *next || now - *next > sr,
+ None => true,
+ };
+ if fresh {
+ app.loudness = Some((filmcraft_audio_dsp::LoudnessMeter::new(sr as f64, 2), now));
+ return;
+ }
+ let next = app.loudness.as_ref().map_or(now, |l| l.1);
+ if now <= next {
+ return;
+ }
+ let provider = app.session.media.provider(app.session.project.clone(), app.session.services.clone());
+ let buf = filmcraft_render::audio::mix_sequence(&app.session.project, seq, next, (now - next) as usize, &provider);
+ if let Some((m, n)) = app.loudness.as_mut() {
+ let chans: Vec<&[f32]> = buf.channels.iter().take(2).map(Vec::as_slice).collect();
+ if chans.len() == 2 {
+ m.process(&chans);
+ } else if let Some(c) = chans.first() {
+ m.process(&[c, c]);
+ }
+ *n = now;
+ }
+}
+
+fn lufs_text(v: f64) -> String {
+ if v.is_finite() && v > -70.0 { format!("{v:.1}") } else { "—".into() }
+}
+
pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
let t = app.tokens;
+ feed_loudness(app);
let lv = levels(app);
let id = egui::Id::new("meter-state");
let now = ui.input(|i| i.time);
@@ -34,12 +73,38 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
ui.data_mut(|d| d.insert_temp(id, st));
let _ = now;
// Premiere: black meter area, scale 0 … −57 dB in 3 dB steps on the right, "dB" at the foot.
- let area = Rect::from_min_max(pos2(rect.min.x + 8.0, rect.min.y + 8.0), pos2(rect.max.x - 26.0, rect.max.y - 30.0));
- ui.painter().rect_filled(
- Rect::from_min_max(pos2(rect.min.x + 4.0, rect.min.y + 4.0), pos2(rect.max.x - 4.0, rect.max.y - 26.0)),
- 0.0,
- egui::Color32::BLACK,
- );
+ // Loudness readout (BS.1770 / EBU R128) under the bars.
+ let lufs_h = if rect.height() > 260.0 { 64.0 } else { 0.0 };
+ let area = Rect::from_min_max(pos2(rect.min.x + 8.0, rect.min.y + 8.0), pos2(rect.max.x - 26.0, rect.max.y - 30.0 - lufs_h));
+ if lufs_h > 0.0 {
+ let (m, s, i, tp) = app.loudness.as_ref().map_or((f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY), |(l, _)| {
+ (l.momentary(), l.short_term(), l.integrated(), l.true_peak_dbtp())
+ });
+ let lr = Rect::from_min_max(pos2(rect.min.x + 4.0, rect.max.y - lufs_h), pos2(rect.max.x - 4.0, rect.max.y - 4.0));
+ ui.painter().rect_filled(lr, 2.0, egui::Color32::from_rgb(0x14, 0x14, 0x14));
+ let rows = [("M", lufs_text(m)), ("S", lufs_text(s)), ("I", lufs_text(i)), ("TP", lufs_text(tp))];
+ for (k, (label, val)) in rows.iter().enumerate() {
+ let y = lr.min.y + 8.0 + k as f32 * 14.0;
+ ui.painter().text(pos2(lr.min.x + 6.0, y), Align2::LEFT_CENTER, *label, Tokens::ui(9.5), t.text_dim);
+ let hot = *label == "I" || (*label == "TP" && tp > -1.0);
+ let col = if *label == "TP" && tp > -1.0 {
+ egui::Color32::from_rgb(0xe0, 0x50, 0x40)
+ } else if hot {
+ t.hot_text
+ } else {
+ t.text
+ };
+ ui.painter().text(
+ pos2(lr.max.x - 6.0, y),
+ Align2::RIGHT_CENTER,
+ format!("{val} {}", if *label == "TP" { "dBTP" } else { "LUFS" }),
+ Tokens::mono(9.5),
+ col,
+ );
+ }
+ app.auto.add("audioMeters.loudness", lr, &format!("M {} S {} I {} LUFS TP {} dBTP", lufs_text(m), lufs_text(s), lufs_text(i), lufs_text(tp)));
+ }
+ ui.painter().rect_filled(Rect::from_min_max(pos2(rect.min.x + 4.0, rect.min.y + 4.0), pos2(rect.max.x - 4.0, area.max.y + 4.0)), 0.0, egui::Color32::BLACK);
let w = ((area.width() - 4.0) / 2.0).max(3.0);
for c in 0..2 {
let r = Rect::from_min_size(pos2(area.min.x + c as f32 * (w + 4.0), area.min.y), vec2(w, area.height()));
@@ -53,7 +118,7 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
}
ui.painter().text(pos2(rect.max.x - 6.0, area.max.y + 10.0), Align2::RIGHT_CENTER, "dB", Tokens::ui(8.5), t.text_dim);
for c in 0..2 {
- let r = Rect::from_center_size(pos2(area.min.x + c as f32 * (w + 4.0) + w / 2.0, rect.max.y - 14.0), vec2(14.0, 14.0));
+ let r = Rect::from_center_size(pos2(area.min.x + c as f32 * (w + 4.0) + w / 2.0, area.max.y + 16.0), vec2(14.0, 14.0));
ui.painter().text(r.center(), Align2::CENTER_CENTER, "S", Tokens::ui(10.0), t.text_dim);
}
if app.playback.playing {