Page cover

Audio Effect

Introduction

Audio effects can enhance the immersion and fun of your game. BroAudio supports all of Unity's built-in audio effects and allows for dynamic parameter adjustments during gameplay.

There are two ways to apply audio effects:

  • BroAudio.SetEffect(BroAudioType)

  • IAudioPlayer.AddXXXEffect()

Set effects for a specific type

This applies an audio mixer effect to all audio of a specified BroAudioType. It's ideal when you want to apply a single effect to an entire category of sounds, such as changing the sound of all ambient tracks or sound effects simultaneously.

How To Use

Create an 'Effect' data struct

To trigger an audio effect at runtime, you need to create an Effect struct with the parameter values via the static factory methods. This data will be passed as a parameter to the SetEffect method.

// Create high pass filter and low pass filter effect
Effect highPassFilter = Effect.HighPass(highPassFrequency, fadeTime);
Effect lowPassFilter = Effect.LowPass(lowPassFrequency, fadeTime);

//Create a custom effect
Effect reverbEffect = Effect.Custom("exposedParameter", value, fadeTime);
//The custom effect and "exposedParameter" must be created and available in BroAudioMixer.

BroAudio.SetEffect(Effect, BroAudioType)

Common use cases and API examples:

When the player is underwater

Effect lowPassFilter = Effect.LowPass(freq, fadeTime);
BroAudio.SetEffect(lowPassFilter, BroAudioType.SFX | BroAudioType.Ambience); 

// Or write it in one line!
BroAudio.SetEffect(Effect.LowPass(freq, fadeTime)); 

// If the BroAudioType is not specified, it will apply to all.

Add more effect

Bro Audio only offers two initial effects, but you can add more! However, since BroAudio has already set up a comprehensive audio mixer with numerous Exposed Parameters, editing it directly through Unity's Audio Mixer Window can be challenging.

Therefore, it's highly recommended to use the Audio Effect Editor to add more effect. It lets you focus on the Effect track and only displays the Exposed Parameters of the Audio Effects.

Add Effects to Individual Audio Players

This applies audio filter effects to a specific audio instance, offering more granular control.

How To Use

Use the chaining methods on the IAudioPlayer instance returned by BroAudio.Play() methods. For example:

// Play a sound and apply a low-pass filter to it.
BroAudio.Play(sound)
    .AddLowPassEffect(lowpass => lowpass.cutoffFrequency = 1000f);
    
// You can also add multiple effects
// If the onSet parameter doesn't fulfill, the effect use the Unity's default value
BroAudio.Play(anotherSound)
        .AddLowPassEffect()
        .AddReverbEffect(reverb => reverb.reverbPreset = AudioReverbPreset.Room);

Available Effects

You can chain any of the following effect methods to an IAudioPlayer:

Each of these methods accepts an onSet action to configure the effect's parameters.

Removing Effects

You can also remove effects from an audio player using the corresponding Remove...Effect() methods:

var myPlayer = BroAudio.Play("mySound")
                                .AddLowPassEffect();
                                
// ... later on
myPlayer.RemoveLowPassEffect();

Last updated