Skip to main content

Crate broadcast_loudness

Crate broadcast_loudness 

Source
Expand description

§Runnable examples

Run with cargo run -p broadcast-loudness --example <name>.

§measure_loudness

//! Measure loudness of a stereo 1 kHz sine wave at -23 LUFS.
//!
//! This example demonstrates the basic loudness measurement API:
//! create a meter, push planar f32 samples, and query results.

use broadcast_loudness::{ChannelLayout, LoudnessMeter};

fn main() {
    let sample_rate = 48_000u32;
    let layout = ChannelLayout::Stereo;
    let duration = 5.0; // 5 seconds
    let n = (duration * sample_rate as f64) as usize;

    // Generate a stereo 1 kHz sine wave at -23 LUFS (per-channel peak)
    let amplitude = 10.0f64.powf(-23.0 / 20.0) as f32;
    let mut left = vec![0.0f32; n];
    let mut right = vec![0.0f32; n];
    for i in 0..n {
        let t = i as f64 / sample_rate as f64;
        let val = (amplitude as f64 * (2.0 * std::f64::consts::PI * 1000.0 * t).sin()) as f32;
        left[i] = val;
        right[i] = val;
    }

    // Feed to the meter (interleaved API — one slice per channel)
    let mut meter = LoudnessMeter::new(sample_rate, layout).unwrap();
    meter.push_interleaved_f32(&left, &right).unwrap();
    meter.finish();

    println!("Integrated loudness: {:.1} LUFS", meter.integrated_lufs());
    println!(
        "Max momentary:       {:.1} LUFS",
        meter.max_momentary_lufs()
    );
    println!(
        "Max short-term:      {:.1} LUFS",
        meter.max_short_term_lufs()
    );
    println!("Loudness range:      {:.1} LU", meter.loudness_range());
    println!("Duration:            {:.1} s", meter.duration_seconds());
}

§measure_true_peak

//! Measure true-peak level of a stereo signal.
//!
//! Demonstrates the `TruePeakMeter` with a pure 1 kHz sine wave at -6 dBFS.

use broadcast_loudness::TruePeakMeter;

fn main() {
    let sample_rate = 48_000.0;
    let duration = 0.5; // 500 ms
    let n = (duration * sample_rate) as usize;
    let freq = 1000.0;

    // Create one meter per channel (true-peak is per-channel)
    let mut left_meter = TruePeakMeter::new();
    let mut right_meter = TruePeakMeter::new();

    // Generate stereo 1 kHz sine at -6 dBFS
    let amplitude = 10.0f64.powf(-6.0 / 20.0); // ~0.5
    for i in 0..n {
        let t = i as f64 / sample_rate;
        let val = amplitude * (2.0 * std::f64::consts::PI * freq * t).sin();
        left_meter.push_f64(val).unwrap();
        right_meter.push_f64(val).unwrap();
    }

    let left_tp = left_meter.finish();
    let right_tp = right_meter.finish();
    let max_tp = left_tp.max(right_tp);

    println!("Left  true-peak:  {:.1} dBTP", left_tp);
    println!("Right true-peak:  {:.1} dBTP", right_tp);
    println!("Max   true-peak:  {:.1} dBTP", max_tp);
    // Expected: approximately -6.0 dBTP for a -6 dBFS sine
    println!("(Expected ~-6.0 dBTP for a -6 dBFS sine wave)");
}

Modules§

kfilter
K-weighting IIR filter coefficients + state (ITU-R BS.1770-5 §Annex 1).

Structs§

LoudnessMeter
EBU R 128 / ITU‑R BS.1770‑5 loudness meter.
TruePeakMeter
True‑peak meter for a single channel.

Enums§

ChannelLayout
Channel layout defining the set of channels to measure and their BS.1770-5 per-channel weighting coefficients G_i.
Error
Loudness measurement error.

Type Aliases§

Result
Result type for loudness operations.