Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unity] Improve perfomance in FMOD + Steam Audio #387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public sealed class FMODStudioAudioEngineSource : AudioEngineSource
SteamAudioSource mSteamAudioSource = null;
int mHandle = -1;

static readonly object[] Array1 = new object[1];
static readonly object[] Array2 = new object[2];
static readonly object[] Array5 = new object[5];

static readonly object BoxedZero = (int)0;
static readonly object[] BoxedNumbers = new object[] { BoxedZero, (int)1, (int)2 };
static readonly object BoxedSimulationOutputsParamIndex = kSimulationOutputsParamIndex;

static Type FMOD_DSP;
static Type FMOD_ChannelGroup;
static Type FMOD_Studio_EventInstance;
Expand Down Expand Up @@ -74,16 +82,18 @@ public override void UpdateParameters(SteamAudioSource source)
if (!mFoundDSP)
return;

var index = kSimulationOutputsParamIndex;
FMOD_DSP_setParameterInt.Invoke(mDSP, new object[] { index++, mHandle });
var args = Array2;
args[1] = mHandle;
args[0] = BoxedSimulationOutputsParamIndex;
FMOD_DSP_setParameterInt.Invoke(mDSP, args);
}

void CheckForChangedEventInstance()
{
if (mEventEmitter != null)
{
var eventInstance = FMODUnity_StudioEventEmitter_EventInstance.GetValue(mEventEmitter, null);
if (!eventInstance.Equals(mEventInstance))
if (eventInstance != mEventInstance)
{
// The event instance is different from the one we last used, which most likely means the
// event-related objects were destroyed and re-created. Make sure we look for the DSP instance
Expand Down Expand Up @@ -114,19 +124,23 @@ void FindDSP(GameObject gameObject)
if (!((bool)FMOD_Studio_EventInstance_isValid.Invoke(mEventInstance, null)))
return;

var channelGroup = Activator.CreateInstance(FMOD_ChannelGroup);
object channelGroup = null;

var getChannelGroupArgs = new object[] { channelGroup };
var getChannelGroupArgs = Array1;
getChannelGroupArgs[0] = channelGroup;
FMOD_Studio_EventInstance_getChannelGroup.Invoke(mEventInstance, getChannelGroupArgs);
channelGroup = getChannelGroupArgs[0];

var getNumDSPsArgs = new object[] { 0 };
var getNumDSPsArgs = Array1;
getNumDSPsArgs[0] = BoxedZero;
FMOD_ChannelGroup_getNumDSPs.Invoke(channelGroup, getNumDSPsArgs);
int numDSPs = (int)getNumDSPsArgs[0];

for (var i = 0; i < numDSPs; ++i)
{
var getDSPArgs = new object[] { i, mDSP };
var getDSPArgs = Array2;
getDSPArgs[1] = mDSP;
getDSPArgs[0] = i < BoxedNumbers.Length ? BoxedNumbers[i] : i;
FMOD_ChannelGroup_getDSP.Invoke(channelGroup, getDSPArgs);
mDSP = getDSPArgs[1];

Expand All @@ -136,7 +150,12 @@ void FindDSP(GameObject gameObject)
var dspConfigWidth = 0;
var dspConfigHeight = 0;

var getInfoArgs = new object[] { dspName, dspVersion, dspNumChannels, dspConfigWidth, dspConfigHeight };
var getInfoArgs = Array5;
getInfoArgs[4] = dspConfigHeight;
getInfoArgs[3] = dspConfigWidth;
getInfoArgs[2] = dspNumChannels;
getInfoArgs[1] = dspVersion;
getInfoArgs[0] = dspName;
FMOD_DSP_getInfo.Invoke(mDSP, getInfoArgs);
dspName = (string)getInfoArgs[0];

Expand Down