Recursive AI

Mute 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.

A mixer bus groups sounds under one control. Use SetBusMute to silence it without resetting its gain.

Terminal
dotnet add package RecursiveAI.OneAudio

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

C#
using OneAudio;
 
const int bus = 1;
var format = new AudioFormat(48000, 1);
var mixer = new AudioMixer(new()
{
    SampleRate = 48000, Channels = 1
});
var clip = new AudioClip(format, [0.5f, 0.5f]);
var block = new float[480];
 
Check(mixer.SetBusGain(bus, 0.4f));
var voice = mixer.Play(new(clip)
{
    Bus = bus, Loop = true
});
if (!voice.IsValid)
    throw new InvalidOperationException("No voice.");
PrintLevel("Before mute");
Check(mixer.SetBusMute(bus, true));
PrintLevel("Muted");
Check(mixer.SetBusMute(bus, false));
PrintLevel("Unmuted");
 
void PrintLevel(string label)
{
    mixer.Render(block); // Let the 10 ms ramp finish.
    mixer.Render(block);
    string level = block[0].ToString("F2",
        System.Globalization.CultureInfo.InvariantCulture);
    Console.WriteLine($"{label}: {level}");
}
 
static void Check(bool queued)
{
    if (!queued)
        throw new InvalidOperationException("Queue full.");
}

The example renders a block to let the default 10 ms ramp finish, then reads one sample from the next block. If a command can’t be queued, the program throws instead of continuing with the previous mix settings.

Unzip the example, then dotnet run.

The snippet loops two generated samples of 0.5 and prints the settled output level before, during, and after mute. No input file or audio device is needed.

Text
Before mute: 0.20
Muted: 0.00
Unmuted: 0.20

Variations

To change what the bus will come back at, insert this line after PrintLevel("Muted"):

C#
Check(mixer.SetBusGain(bus, 0.2f));

The muted output stays silent; the next unmute restores the new gain. The program prints:

Text
Before mute: 0.20
Muted: 0.00
Unmuted: 0.10

The short answer

Call SetBusMute(bus, true) on the numbered mixer bus. To bring that bus back, call SetBusMute(bus, false) on the same mixer and bus. You don’t have to read, cache, or reset the fader value. The mixer keeps mute state separate from the linear gain set by SetBusGain.

What is a bus on a sound mixer?

A bus combines signals so you can change the group as a unit. For example, route several sound effects to one numbered bus using PlayParams.Bus, then mute that bus without touching a separate dialogue bus. Voices use bus 0 by default, so give the groups different bus numbers before you expect their mute buttons to work independently.

A bus can also receive other buses through SetBusOutput. If you mute a parent bus, you silence the sound routed through it too. Buses that still go directly to the master output won’t be muted by that parent. SetBusMute takes a numbered bus, not the MasterBus constant used by the effect APIs. See the mixer routing reference.

What is the difference between mute and unmute?

Mute brings the bus’s effective gain to zero; unmute brings it back to the configured gain. In the example, a sample of 0.5 at 0.4 gain gives 0.20 on either side of the mute. The variation changes the stored gain while the bus is muted, so unmuting returns to 0.10 instead. If your UI has both a fader and a mute toggle, send the fader to SetBusGain and the toggle to SetBusMute.

Neither call is a pause or a stop. The voices keep moving through their audio while the bus is silent, so unmuting a one-shot sound that has already ended won’t replay it. The control calls queue changes; a render pass applies them, then the gain moves over SmoothingMilliseconds (10 ms by default). That’s why the first block after pressing mute can still contain audio during the transition.

All oneaudio recipes