Unity: Pick a random sound from a clip using FMOD

At the moment I am working on a project where I have the chance to challenge myself in different roles, from AI programmer to audio programmer, and this is awesome! Having to deal with the audio part, during these days I am learning how FMOD works.

What is FMOD?

FMOD is a software that allows to easily add advanced audio features without re-inventing the wheel. It also includes FMOD Studio that is a “DAW-like” interface that enables the audio processing outside the engine, making easy to invoke a specific sound in the engine without huge programming efforts.

I won’t tell you about basic setup and configuration, because you can find it on their fantastic documentation, and in a few clicks you should be ready to go. I will share with you a specific use case that I was trying to achieve. I just started using this tool so those might not be the optimal solutions, so I am glad if you show me a better way to achieve the same result.

This is the scenario: I downloaded from Soundly an impact sound, that has five different impacts into the same clip. What I wanted is that each time an object collides, one of these five impact sounds is randomly picked from the audio clip.

Method 1: labeled parameter

This is a less optimal solution, but it’s interesting to learn how you can set a parameter from a C# script. First of all create a User: Labeled parameter, that has five different values (one for each sound).

Then, create a different region for each of the sounds, with a marker called End after the last clip.

After this, create as many transitions as the sounds to play, all at the beginning. Each transition has a condition that triggers it, and depends on the value of the Sound parameter set before.

There is only one thing that is left to do: after each region, it keeps playing all the regions after it. So you need another transition to the End marker, making the sound end. Be careful that this transition must be put in the end of the region, not in the start of the following region!

This is the final result:

Save the project, add the event to a bank and build the solution (press F7).

Now let’s switch to Unity!

In Unity the only thing we have to handle is to pick a random value from A to E. In order to do this, I created an array with some configured values, and on collision I trigger the sound performing the call to the FMOD running instance. The script is something like the following:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class NoiseOnCollision : MonoBehaviour
{
    #region PRIVATE PARAMETERS

    [SerializeField] private string[] regions;

    private FMOD.Studio.EventInstance _fmodInstance;

    #endregion

    #region PRIVATE METHODS

    private void OnCollisionEnter(Collision collision)
    {
        _fmodInstance = FMODUnity.RuntimeManager.CreateInstance("event:/SmallImpact");

        string region = regions[Random.Range(0, regions.Length)];
        _fmodInstance.setParameterByNameWithLabel("Sound", region);

        _fmodInstance.start();
        _fmodInstance.release();
    }

    #endregion
}

A little note. This is just for testing purpose so it’s just fine, but in a real world scenario you should not set the parameter by name. You should do by ID, instead. In fact, considering the FMOD documentation, accessing a parameter by name is slower than accessing by ID.

Method 2: randomisation

This is the best method in my opinion, because it does not involve randomic generation into Unity, but it involves random generation inside FMOD itself.

The scenario is the same as the one shown above, but you can safely remove the Sound parameter.

Then, click on each transition and set the probability of 20% each. I set it to 20% because they are 5, you should use a correct percent depending on the amount of sounds you are playing. Each transition should have the probability set as shown in the below image (remember to remove the trigger conditions):

You can also add a randomisation to the volume FMOD plays the sound, each time it plays it. Holding ALT key and adjusting the volume knob of the track, FMOD will add a green area. This is the range into it will pick a random volume next time you play the sound. You can use this trick even on pitch, and other knobs shown inside FMOD. It’s cool, isn’t it?

The Unity script is now easier, because we have taken care of the parameters exchange between Unity and FMOD. It will looks like the following:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class NoiseOnCollision : MonoBehaviour
{
    #region PRIVATE PARAMETERS

    private FMOD.Studio.EventInstance _fmodInstance;

    #endregion

    #region PRIVATE METHODS

    private void OnCollisionEnter(Collision collision)
    {
        _fmodInstance = FMODUnity.RuntimeManager.CreateInstance("event:/SmallImpact");

        _fmodInstance.start();
        _fmodInstance.release();
    }

    #endregion
}

Conclusions

This is not a huge use case and maybe it is not much useful (you can create a random sound even without FMOD!). But I just started playing around with FMOD and I wanted to share with you a simple scenario that can help you figure out all the basic logic behind this tool, and how to use it with C# scripts inside Unity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Built with WordPress.com.

Up ↑

%d bloggers like this: