Skip to content

Sound effects

Romain Milbert edited this page Feb 19, 2023 · 2 revisions

Setting an effect to a sound

To use sound effects, you must use the SoundEffect & SoundEffectSlot types; the former will hold an effect's parameters, while the latter is used to apply an effect to any number of Sound objects.

First, create a Sound as you would usually do:

#include <RaZ/Audio/Sound.hpp>

// ...

Raz::Sound& sound = entity.addComponent<Raz::Sound>(Raz::WavFormat::load("sound.wav"));

Next, we will need to use the classes mentioned above. First, let us create a SoundEffect which will apply a reverberation effect to our sound:

#include <RaZ/Audio/SoundEffect.hpp>

// ...

Raz::SoundEffect reverb;

// Creating a ReverberationParams object, which holds all parameters for our effect
Raz::ReverberationParams reverbParams {};
reverbParams.gain = 2.f; // Setting the reverb volume

reverb.load(reverbParams); // Applying the parameters to our sound effect

The SoundEffect is now ready to be used. We can finally create a SoundEffectSlot:

#include <RaZ/Audio/SoundEffectSlot.hpp>

// ...

Raz::SoundEffectSlot effectSlot;
effectSlot.loadEffect(reverb); // Loading the effect in the slot

// Finally, we can link the slot to our sound, which will instantly make it effective
sound.linkSlot(effectSlot);

// You can conversely unlink the slot to remove the effect from the sound
sound.unlinkSlot();

⚠️ Note that loading the effect in a slot internally copies its parameters; should you change those, you must load them again in both the effect & the slot:

reverbParams.decayTime = 3.f;
reverb.load(reverbParams); // Reloading parameters
effectSlot.loadEffect(reverb); // Reapplying the effect to the slot

If the slot is still linked to the sound, the changes will be applied automatically.

Clone this wiki locally