Need help building a multiplayer VOIP system

Hello everyone!
So the game I’m working on is finally adding multiplayer features and now I need to build a push-to-talk VOIP system.

I am an intermediate Studio user and have some coding experience, however I am not any good at Core API coding and need some help.

  1. First and foremost, do I really need to run VOIP through FMOD? If there’s any way it can be run through native Unity Audio without performance issues, then I’ll just ask developers to find and install some third-party VOIP solution.

  2. I’ve done my research through forums and FMOD scripting examples, so am currently trying to build a simplest VOIP prototype. And at this point I am stopped with my lack of experience with Core API coding.

So I know the best way to do this would be by using a dedicated VOIP event in a Studio with progammer sound instrument added, and then setting up a callback.

Firstly I get my hardware info and set up basic things

FMODUnity.RuntimeManager.CoreSystem.getRecordDriverInfo(RecordingDeviceIndex, out RecordingDeviceName, 50, out MicGUID, out SampleRate, out FMODSpeakerMode, out NumOfChannels, out driverState);

    FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
    exinfo.cbsize = Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));
    exinfo.numchannels = NumOfChannels;
    exinfo.format = FMOD.SOUND_FORMAT.PCMFLOAT;
    exinfo.defaultfrequency = SampleRate;
    exinfo.length = (uint)(exinfo.defaultfrequency * sizeof(float) * exinfo.numchannels);
    FMOD.MODE recordMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER;

Then under Input.GetKeyDown method I’m creating an instance of my VOIP event and set a callback for it:

VOIPCallback = new FMOD.Studio.EVENT_CALLBACK(VOIPEventCallback);
VOIPEvent = FMODUnity.RuntimeManager.CreateInstance(“event:/VOIP”);
VOIPEvent.setCallback(VOIPCallback);
VOIPEvent.start();
VOIPEvent.release();

And then I need to record a sound and play it trough a programmer instrument in VOIP event using a callback. I’ve created the event and now am trying to get at least my mic to sound through it and this is where I’m stuck at the moment.

static FMOD.RESULT VOIPEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
{

    switch (type)
    {
        case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
            {

                FMOD.Sound VOIPSound;
                FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
                FMOD.MODE recordMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER;
                var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound("VOIP", recordMode, ref exinfo, out VOIPSound);


                if (soundResult == FMOD.RESULT.OK)
                {
                    //get properties
                    FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES properties = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
                    properties.sound = VOIPSound.handle;
                    Marshal.StructureToPtr(properties, parameterPtr, false);
                    FMODUnity.RuntimeManager.CoreSystem.recordStart(RecordingDeviceIndex, sound, true);
                }
            }
            break;
    }

    return FMOD.RESULT.OK;
}

Sure it’s not working and, honestly, I have no idea what I’m doing here, and even somehow re-declare some variables since it gives me errors otherwise.

Could anyone please help me with the right way to do this ? For now I need a simple push-to-talk single-user voice system run through FMOD Stuido, but this callback thing is a bit out of my skill range at the moment.

VOIP does not necessarily need to be run through FMOD, you can use a 3rd party package such as Normcore or Vivox. Such packages would however rely on Unity’s internal audio system, so you would need to re-enable that in the Unity settings, which would also mean you couldn’t ship on PS5 or Xbox.

There have been some good discussions on this topic previously, I recommend you look through this thread: Using FMOD with voice chat utilizing OnAudioFilterRead(), which has lots of code samples and various resolutions to issues encountered along the way.