Where can I find a DSP_CONVOLUTION_REVERB C#-Unity Example?

Hi again,

I want to apply in Unity (on runtime) a FMOD.DSP_CONVOLUTION_REVERB effect to a specific track of a specific event, I’m doing the things this way.

FMODUnity.RuntimeManager.CoreSystem.createDSPByType(FMOD.DSP_TYPE.CONVOLUTIONREVERB, out reverb_FmodDSP);
reverb_FmodDSP.setParameterData((int)FMOD.DSP_CONVOLUTION_REVERB.IR, [index 0 = numchannels][index 1 + = raw 16 bit PCM data]); //I'm struggling here
reverb_FmodDSP.setParameterFloat((int)FMOD.DSP_CONVOLUTION_REVERB.WET, 0.0f);
reverb_FmodDSP.setParameterFloat((int)FMOD.DSP_CONVOLUTION_REVERB.DRY, -80.0f);
reverb_FmodDSP.setActive(true);
myEventInstance.getChannelGroup(out FMOD.ChannelGroup channelGroup_Master);
channelGroup_Master.getGroup(0, out FMOD.ChannelGroup channelGroup_Track);
channelGroup_Track.addDSP(_trackIndex, reverb_FmodDSP);

My idea is implement a wav file from an Asset or an Event direcly from FMOD Studio file,I looked on internet for an idea but I didn’t find it.

Of course, I have read the documentation https://www.fmod.com/docs/2.00/api/core-api-common-dsp-effects.html#fmod_dsp_convolution_reverb but is not too clear for me, sorry.

Thanks in advance FMOD team!

Hi,

I made a function that will read a .wav file from disk and convert it into FMOD friendly IR data:

Conversion Function
public static bool ConvertWavToIR(string wavFilePath, out byte[] wavData)
{
    wavData = null;

    if (!File.Exists(wavFilePath))
    {
        Debug.LogError($"Error: Unable to open WAV file: {wavFilePath}");
        return false;
    }

    try
    {
        using (FileStream fileStream = new FileStream(wavFilePath, FileMode.Open, FileAccess.Read))
        using (BinaryReader reader = new BinaryReader(fileStream))
        {
            byte[] chunkID = reader.ReadBytes(4);
            uint chunkSize = reader.ReadUInt32();
            byte[] format = reader.ReadBytes(4);
            byte[] subchunk1ID = reader.ReadBytes(4);
            uint subchunk1Size = reader.ReadUInt32();
            ushort audioFormat = reader.ReadUInt16();
            ushort numChannels = reader.ReadUInt16();
            uint sampleRate = reader.ReadUInt32();
            uint byteRate = reader.ReadUInt32();
            ushort blockAlign = reader.ReadUInt16();
            ushort bitsPerSample = reader.ReadUInt16();
            byte[] subchunk2ID = reader.ReadBytes(4);
            uint subchunk2Size = reader.ReadUInt32();

            if (System.Text.Encoding.ASCII.GetString(chunkID) != "RIFF" ||
                System.Text.Encoding.ASCII.GetString(format) != "WAVE")
            {
                Debug.LogError("Error: Invalid WAV file format.");
                return false;
            }

            if (audioFormat != 1 || bitsPerSample != 16)
            {
                Debug.LogError("Error: Only PCM 16-bit WAV files are supported.");
                return false;
            }

            wavData = new byte[subchunk2Size + 4];

            wavData[0] = (byte)(numChannels & 0xFF);
            wavData[1] = (byte)((numChannels >> 8) & 0xFF);
            wavData[2] = 0;
            wavData[3] = 0;

            reader.Read(wavData, 4, (int)subchunk2Size);
        }

        Debug.Log("WAV file successfully converted to IR data.");
        return true;
    }
    catch (Exception ex)
    {
        Debug.LogError($"Error: {ex.Message}");
        return false;
    }
}

I am retrieving the IR data and then passing it to my DSP through the setParameterData function:

if (ConvertWavToIR("ExampleFile.wav", out byte[] data))
{
    Debug.Log("Converted data"};
}
FMOD.RESULT result = reverb_FmodDSP.setParameterData((int)FMOD.DSP_CONVOLUTION_REVERB.IR, data);
if (result != FMOD.OK)
{
    Debug.Log($"Failed to set IR data with result {result}");
}

Please let me know if you have any issues!

1 Like

Thank you very much for the example, I’m implementing and testing it right now.

Regards!