Recursive AI

Duck background music under speech in C#

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

Use SetDucking(music, speech) to turn music down during speech and bring it back in the gaps.

Terminal
dotnet add package RecursiveAI.OneAudio

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

C#
using OneAudio;
 
const int rate = 48000, music = 0, speech = 1;
var mixer = new AudioMixer(new()
{
    Channels = 1, SampleRate = rate, MaxBuses = 2
});
if (!mixer.SetDucking(music, speech))
    throw new InvalidOperationException("Queue full."); // Mixer command queue is full.
 
var bed = mixer.Play(new(Audio.Load("music.wav"))
{
    Bus = music
});
var voice = mixer.Play(new(Audio.Load("speech.wav"))
{
    Bus = speech, StartAtFrame = rate
});
if (!bed.IsValid || !voice.IsValid)
    throw new InvalidOperationException("No voice.");
 
var output = new float[5 * rate];
mixer.Render(output);
using (var wav = new WavEncoder(
    File.Create("ducked.wav"), new(rate, 1)))
    wav.Write(output);
Console.WriteLine("Saved ducked.wav");

Unzip the example, then dotnet run.

The zip includes music.wav (a generated chord) and speech.wav (a synthetic voice saying “The music gets quieter whenever I speak”). The snippet starts speech one second into the mix and saves five seconds to ducked.wav without needing an audio device.

Text
Saved ducked.wav

The full program

This version pans music left and speech right so it can measure the music by itself. It then averages both channels into the mono ducked.wav. Each printed level is a root mean square (RMS) sample amplitude over a quarter-second window, not a loudness reading. If SetDucking returns false, the queue is full and the link hasn’t changed.

C#
using OneAudio;
 
const int rate = 48000, music = 0, speech = 1;
var mixer = new AudioMixer(new()
{
    SampleRate = rate, Channels = 2, MaxBuses = 2
});
if (!mixer.SetDucking(music, speech))
    throw new InvalidOperationException("Command queue full.");
 
// Separate channels let us measure music without counting the speech.
var bed = mixer.Play(new(Audio.Load("music.wav")) { Bus = music, Pan = -1 });
var voice = mixer.Play(new(Audio.Load("speech.wav"))
{
    Bus = speech, Pan = 1, StartAtFrame = rate
});
if (!bed.IsValid || !voice.IsValid) throw new InvalidOperationException("No voice.");
 
var stereo = new float[5 * rate * 2];
mixer.Render(stereo);
Console.WriteLine($"Music RMS before speech: {Level(0.25)}");
Console.WriteLine($"Music RMS during speech: {Level(1.50)}");
Console.WriteLine($"Music RMS after speech:  {Level(4.50)}");
 
// Put both signals in one channel for the file you listen to.
var mono = new float[5 * rate];
for (int i = 0; i < mono.Length; i++)
    mono[i] = (stereo[2 * i] + stereo[2 * i + 1]) * 0.5f;
using (var wav = new WavEncoder(File.Create("ducked.wav"), new(rate, 1)))
    wav.Write(mono);
Console.WriteLine("Saved ducked.wav");
 
string Level(double start)
{
    int first = (int)(start * rate);
    int count = rate / 4;
    double sum = 0;
    for (int i = first; i < first + count; i++)
        sum += stereo[2 * i] * stereo[2 * i];
    return FormattableString.Invariant($"{Math.Sqrt(sum / count):F3}");
}
Text
Music RMS before speech: 0.078
Music RMS during speech: 0.028
Music RMS after speech:  0.077
Saved ducked.wav

Variations

For less reduction and a shorter recovery, replace the SetDucking check with:

C#
if (!mixer.SetDucking(music, speech, new DuckingSettings
{
    ReductionDb = 6,
    ReleaseMs = 150
}))
    throw new InvalidOperationException("Command queue full.");

The same full program prints:

Text
Music RMS before speech: 0.078
Music RMS during speech: 0.047
Music RMS after speech:  0.078
Saved ducked.wav

The short answer

Give speech and music their own mixer buses, then call SetDucking(music, speech). The first argument is the bus to turn down; the second is the bus to listen to. Render both together. The music level follows the speech without you scheduling fades at every sentence. The speech itself isn’t turned down by this link.

What does audio ducking mean?

It means lowering one sound in response to another. The speech bus is the sidechain: it supplies the level that controls the reduction, while the music bus gets the reduction. Both are still heard in the mix. This works on separate sources: it doesn’t extract dialogue or separate the music out of a WAV in which they’re already mixed together.

The detector watches the speech bus after its gain, mute, and effects. If you mute that bus, it stops triggering the duck as the detector settles. Without custom settings, oneaudio compares the detected level with a threshold of −30 dBFS (decibels relative to digital full scale) and moves toward 12 dB of reduction when the detector is above it. Very quiet speech may never cross the threshold. See the mixer reference for the ducking curve.

Should you use audio ducking?

Use it when speech needs room over a music bed but you want the music to come back in the gaps. Listen for words lost at the start of phrases and for music surging between syllables. A shorter AttackMs makes the music get out of the way sooner; a longer ReleaseMs makes the music return more gradually. These settings are time constants, not deadlines: a 250 ms release (the default) moves most of the way back only after several such intervals. If a deeper duck sounds distracting, try the 6 dB variation or turn the music bed down before ducking it.

How do I turn off audio ducking?

Call ClearDucking(music) on the target bus and check that it returns true. The gain multiplier returns toward 1 with the release setting the link had; this doesn’t reset the bus fader. Each target has only one detector source, so a new SetDucking call on the same target replaces the old link. Use distinct source and target buses: a self-link or a link that creates a routing loop throws ArgumentException.

C#
if (!mixer.ClearDucking(music))
    throw new InvalidOperationException("Command queue full.");
All oneaudio recipes