Recursive AI

Measure LUFS on an audio mixer bus in C#

oneaudio is a free, open-source audio library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

Measure perceived loudness (LUFS) for voice and music separately.

Terminal
dotnet add package RecursiveAI.OneAudio

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneAudio;
 
static void ReadLufs(AudioMixer mixer)
{
    for (int bus = 0; bus < 2; bus++)
    {
        if (!mixer.TryGetBusLoudness(
            bus, out var level))
            continue;
        double lufs = level.ShortTermLufs;
        if (double.IsFinite(lufs))
            Console.WriteLine(
                FormattableString.Invariant(
                    $"Bus {bus}: {lufs:F1} LUFS"));
    }
}

Set MaxBusMeters = 2, route voice to bus 0 and music to bus 1, and call EnableBusMeter for each. Call ReadLufs on the control thread while audio renders. With the example’s two tones:

Text
Bus 0: -17.7 LUFS
Bus 1: -29.9 LUFS

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The full program

The example uses a 440 Hz tone in place of speech and a quieter 220 Hz tone in place of music. It generates both in C#, so you don’t need audio files or a sound device. A worker renders blocks of 4,800 mono samples at 48 kHz. The main thread polls both meters until they’ve measured at least three seconds of audio.

C#
using OneAudio;
 
const int rate = 48_000, voice = 0, music = 1;
var mixer = new AudioMixer(new()
{
    SampleRate = rate, Channels = 1,
    MaxBuses = 2, MaxBusMeters = 2
});
if (!mixer.EnableBusMeter(voice) ||
    !mixer.EnableBusMeter(music))
    throw new InvalidOperationException("Mixer queue full.");
 
if (!mixer.Play(new(Tone(440, 0.2f)) { Bus = voice }).IsValid ||
    !mixer.Play(new(Tone(220, 0.05f)) { Bus = music }).IsValid)
    throw new InvalidOperationException("Could not start both clips.");
 
// A worker stands in for the audio callback; no sound device is needed.
var block = new float[rate / 10];
using var stop = new CancellationTokenSource();
var render = new Thread(() =>
{
    for (int pass = 0; pass < 40 && !stop.IsCancellationRequested; pass++)
    {
        mixer.Render(block);
        if (stop.Token.WaitHandle.WaitOne(100)) break;
    }
});
render.Start();
try
{
    var clock = System.Diagnostics.Stopwatch.StartNew();
    while (true)
    {
        if (mixer.TryGetBusLoudness(voice, out var speech) &&
            mixer.TryGetBusLoudness(music, out var bed) &&
            speech.FramesMeasured >= 3 * rate &&
            bed.FramesMeasured >= 3 * rate)
        {
            Console.WriteLine(FormattableString.Invariant(
                $"Voice: {speech.ShortTermLufs:F1} LUFS (short-term)"));
            Console.WriteLine(FormattableString.Invariant(
                $"Music: {bed.ShortTermLufs:F1} LUFS (short-term)"));
            break;
        }
        if (clock.Elapsed > TimeSpan.FromSeconds(10))
            throw new TimeoutException("No three-second meter readings.");
        Thread.Sleep(25);
    }
}
finally
{
    stop.Cancel();
    render.Join();
}
 
static AudioClip Tone(int hz, float amplitude)
{
    var samples = new float[4 * rate];
    for (int i = 0; i < samples.Length; i++)
        samples[i] = amplitude * MathF.Sin(MathF.Tau * hz * i / rate);
    return new AudioClip(new(rate, 1), samples);
}
Text
Voice: -17.7 LUFS (short-term)
Music: -29.9 LUFS (short-term)

If EnableBusMeter throws InvalidOperationException, there’s no meter slot left; if it returns false, the command queue is full.

Variations

Meter the final mix too. Set MaxBusMeters = 3 when constructing the mixer and enable the master meter before rendering:

C#
if (!mixer.EnableBusMeter(AudioMixer.MasterBus))
    throw new InvalidOperationException("Mixer queue full.");

Read it with TryGetBusLoudness(AudioMixer.MasterBus, out var master) on the control thread. The master meter reads after the limiter, if you use one.

Start a fresh measurement for the next track. Reset an enabled meter without giving up its slot:

C#
if (!mixer.ResetBusMeter(music))
    throw new InvalidOperationException("Mixer queue full.");

Route audio through mixer buses · Duck music under voice · Measure loudness of an audio file


The short answer

Reserve a meter for each bus you want to measure. Call EnableBusMeter with the same bus index you pass to Play, then poll TryGetBusLoudness while audio renders. Each snapshot contains readings from one completed render pass. The two reads are independent: the voice and music snapshots aren’t guaranteed to come from the same pass.

What are LUFS used for in an audio mix?

LUFS stands for loudness units relative to full scale. The meter weights frequencies before measuring level, so two signals with the same peak can read differently in LUFS. That makes a bus meter useful for comparing sounds that have different dynamics, such as steady music and speech with pauses. True peak, reported separately as TruePeakDbtp, answers a different question: how high the signal peaks, including estimated peaks between samples. See EBU Tech 3341 for the meter definitions.

The bus meter reads after filters, reverb and bus gain, but before the bus is ducked into its parent. If you lower the music bus gain, the meter sees the change. If voice ducks music, the music meter alone won’t show how much gets through to the parent. Read the parent or master to see the resulting mix, rather than adding the individual LUFS numbers.

What’s the difference between momentary, short-term and integrated LUFS?

  • MomentaryLufs looks at the last 400 ms. Use it for a display that reacts quickly as the voice comes and goes.
  • ShortTermLufs looks at the last 3 seconds. Use it to compare voice and music without chasing every syllable. That’s why the example waits for 144,000 frames at 48 kHz.
  • IntegratedLufs measures gated loudness since you enabled or last reset the meter. Use it to summarize a take or a section. It isn’t an arithmetic average of the displayed LUFS readings; quiet blocks can be excluded by the gate.

A successful read means the bus has a meter, not that the reading is ready. Just after enabling or resetting, FramesMeasured can be zero and the level can be negative infinity. A measured but silent bus can also read as negative infinity, so display an empty level rather than a numeric zero in either case. Keep calling Render on the audio thread; polling never advances the meter by itself.

All oneaudio recipes