Capturing Unity game’s FMOD output (5.1 / Busses)

Hi,

FMOD has the FMOD_OUTPUTTYPE_WAVWRITER output mode that allows the system to write the final mixer output to a .wav file. Note that using WAVWRITER means that the system will solely output to file; it won’t output to a device. To accomplish this with the Unity Integration, you’ll have to modify FMODUnity.RuntimeManager as follows:

  • Add the following as class members:
byte[] wavPath;
GCHandle wavHandle;
IntPtr wavPtr;
  • Modify the output of the coreSystem object on line 301:
result = coreSystem.setOutput(FMOD.OUTPUTTYPE.WAVWRITER);
  • Replace the studioSystem.initialize() call on line 347 with the following:
using (FMOD.StringHelper.ThreadSafeEncoding encoder = FMOD.StringHelper.GetFreeHelper())
{
    wavPath = encoder.byteFromStringUTF8("C:\\Your\\Path\\Here\\wavFile.wav");
    wavHandle = GCHandle.Alloc(wavPath, GCHandleType.Pinned);
    wavPtr = wavHandle.AddrOfPinnedObject();
    result = studioSystem.initialize(virtualChannels, studioInitFlags, FMOD.INITFLAGS.NORMAL, wavPtr);
}
  • Free the GCHandle object wavHandle in RuntimeManager.OnDestroy() after ReleaseStudioSystem() is called:
private void OnDestroy()
{
    coreSystem.setCallback(null, 0);
    ReleaseStudioSystem();

    wavHandle.Free();
    //...
}

You will also need to make the static class StringHelper in fmod.cs at line 3884 public.

However, this won’t allow you to separately capture audio for different Buses unless you do multiple recordings with the Buses muted/solo’d. To do this, you’ll need to go a little more in-depth - I would suggest modifying our Unity DSP Capture script example to collect the audio data, adding a wav header to the data, and writing it to a .wav file. The DSP Capture example specifically grabs the Master ChannelGroup’s output, but you can modify it to grab any Bus’ core ChannelGroup instead, which you can get with Studio::Bus::getChannelGroup. As for writing to a wav file with the appropriate header, here’s a somewhat old post detailing how to record audio input and write it to a wav file. The post uses in C++, and specifically pertains to writing audio input to file, but you should be able to adapt it to C# and your specific use-case.

If you have any additional questions, feel free to ask.