FmodAudio icon indicating copy to clipboard operation
FmodAudio copied to clipboard

FmodSystem Acts Strange With Certain Channel Counts

Open 11clock opened this issue 3 years ago • 3 comments

Below are a couple of snippets in my code.

    internal SfxSystem()
    {
        _fmodSystem = Fmod.CreateSystem();
        _fmodSystem.Init(MaxChannels);
    }
            Channel? channel = _fmodSystem.PlaySound(sfx.Sound);
            if (channel is null) throw new Exception("Sound channel wasn't properly created");
            if (randomPitch) channel.Pitch = _rng.Range(0.9f, 1.1f);

When I play the same sound repeatedly, what I hear is different depending on the MaxChannels value. If it's 16, I hear the sound effect playing repeatedly just fine. If it's 64, the sound will play normally at first, but it will eventually stop overlapping with itself and only play when the previous sound is done playing. When I set MaxChannels to 128, the sound plays fine for a few seconds and then cuts out completely, leaving me with total silence.

11clock avatar Aug 26 '22 06:08 11clock

I have tested further and found that the issues at 64 and 128 channels occur after 64 iterations of the sound have played. No audio issues at 63 channels and below.

11clock avatar Aug 26 '22 07:08 11clock

Nevermind about 63 and below being fine. My sound effects are randomly cutting out or not playing, and it seems to get worse with higher channel counts.

11clock avatar Aug 26 '22 07:08 11clock

Here is the full wrapper class I made where I am encountering the above-described issues.

public class SfxSystem
{
    private const int MaxChannels = 63;
    
    private FmodSystem _fmodSystem;
    private Rng _rng = new();
    
    internal SfxSystem()
    {
        _fmodSystem = Fmod.CreateSystem();
        _fmodSystem.Init(MaxChannels);
    }

    public void Play(Sfx sfx, bool randomPitch = false)
    {
        Channel? channel = _fmodSystem.PlaySound(sfx.Sound);
        if (channel is null) throw new Exception("Sound channel wasn't properly created");
        if (randomPitch) channel.Pitch = _rng.Range(0.9f, 1.1f);
    }

    public void Play(string sfxName, bool randomPitch = false) => Play(Lib.Sfx[sfxName], randomPitch);

    internal Sound CreateSound(string assetName)
    {
        var soundPath = $"{ContentPaths.Root}/{ContentPaths.Sfx}/{assetName}";
        if (!File.Exists(soundPath)) throw new Exception($"Invalid path for sound file: {soundPath}");
        return _fmodSystem.CreateSound(soundPath, Mode.CreateSample | Mode.Loop_Off);
    }
}

11clock avatar Aug 26 '22 07:08 11clock

Ok, so, one thing to note about Fmod, is that how many channels it can actually play at a time depends on how many channels the underlying hardware/API allows. Anything above that is updated virtually until such time as a real/hardware channel becomes available.

Also, sorry for the really late reply.

sunkin351 avatar Feb 07 '23 21:02 sunkin351

You can query how many real channels are available via FmodSystem.SoftwareChannels. This API reports on my system to be 64 channels.

sunkin351 avatar Feb 07 '23 21:02 sunkin351

As for the other issue, where all sound cuts out entirely, that might be something you will want to report to the Fmod team directly.

sunkin351 avatar Feb 07 '23 22:02 sunkin351