Steam Audio tryout

This commit is contained in:
Timur Nizamov
2025-11-21 22:37:24 +02:00
parent ab737e016c
commit b25bde77fc
279 changed files with 18620 additions and 63 deletions

View File

@@ -0,0 +1,128 @@
//
// Copyright 2017-2023 Valve Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if STEAMAUDIO_ENABLED
using System;
using System.Reflection;
using UnityEngine;
namespace SteamAudio
{
public sealed class FMODStudioAudioEngineSource : AudioEngineSource
{
bool mFoundDSP = false;
FMODUnity.StudioEventEmitter mEventEmitter = null;
FMOD.Studio.EventInstance mEventInstance;
FMOD.DSP mDSP;
SteamAudioSource mSteamAudioSource = null;
int mHandle = -1;
const int kSimulationOutputsParamIndex = 33;
public override void Initialize(GameObject gameObject)
{
FindDSP(gameObject);
mSteamAudioSource = gameObject.GetComponent<SteamAudioSource>();
if (mSteamAudioSource)
{
mHandle = FMODStudioAPI.iplFMODAddSource(mSteamAudioSource.GetSource().Get());
}
}
public override void Destroy()
{
mFoundDSP = false;
if (mSteamAudioSource)
{
FMODStudioAPI.iplFMODRemoveSource(mHandle);
}
}
public override void UpdateParameters(SteamAudioSource source)
{
CheckForChangedEventInstance();
FindDSP(source.gameObject);
if (!mFoundDSP)
return;
mDSP.setParameterInt(kSimulationOutputsParamIndex, mHandle);
}
void CheckForChangedEventInstance()
{
if (mEventEmitter != null)
{
var eventInstance = mEventEmitter.EventInstance;
if (!eventInstance.Equals(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
// when FindDSP is called next.
mFoundDSP = false;
}
}
else
{
// We haven't yet seen a valid event emitter component, so make sure we look for one when
// FindDSP is called.
mFoundDSP = false;
}
}
void FindDSP(GameObject gameObject)
{
if (mFoundDSP)
return;
mEventEmitter = gameObject.GetComponent<FMODUnity.StudioEventEmitter>();
if (mEventEmitter == null)
return;
mEventInstance = mEventEmitter.EventInstance;
if (!mEventInstance.isValid())
return;
FMOD.ChannelGroup channelGroup;
mEventInstance.getChannelGroup(out channelGroup);
int numDSPs;
channelGroup.getNumDSPs(out numDSPs);
for (var i = 0; i < numDSPs; ++i)
{
channelGroup.getDSP(i, out mDSP);
var dspName = "";
var dspVersion = 0u;
var dspNumChannels = 0;
var dspConfigWidth = 0;
var dspConfigHeight = 0;
mDSP.getInfo(out dspName, out dspVersion, out dspNumChannels, out dspConfigWidth, out dspConfigHeight);
if (dspName == "Steam Audio Spatializer")
{
mFoundDSP = true;
return;
}
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c36706fa5b924b94c8895e3051bdee14
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
//
// Copyright 2017-2023 Valve Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if STEAMAUDIO_ENABLED
using System;
using System.Reflection;
using UnityEngine;
namespace SteamAudio
{
public sealed class FMODStudioAudioEngineState : AudioEngineState
{
public override void Initialize(IntPtr context, IntPtr defaultHRTF, SimulationSettings simulationSettings, PerspectiveCorrection correction)
{
FMODStudioAPI.iplFMODInitialize(context);
FMODStudioAPI.iplFMODSetHRTF(defaultHRTF);
FMODStudioAPI.iplFMODSetSimulationSettings(simulationSettings);
}
public override void Destroy()
{
FMODStudioAPI.iplFMODTerminate();
}
public override void SetHRTF(IntPtr hrtf)
{
FMODStudioAPI.iplFMODSetHRTF(hrtf);
}
public override void SetReverbSource(Source reverbSource)
{
FMODStudioAPI.iplFMODSetReverbSource(reverbSource.Get());
}
public override void SetHRTFDisabled(bool disabled)
{
base.SetHRTFDisabled(disabled);
FMODStudioAPI.iplFMODSetHRTFDisabled(disabled);
}
}
public sealed class FMODStudioAudioEngineStateHelpers : AudioEngineStateHelpers
{
public override Transform GetListenerTransform()
{
var fmodStudioListener = (MonoBehaviour) GameObject.FindObjectOfType<FMODUnity.StudioListener>();
return (fmodStudioListener != null) ? fmodStudioListener.transform : null;
}
public override AudioSettings GetAudioSettings()
{
var audioSettings = new AudioSettings { };
int samplingRate = 0;
FMOD.SPEAKERMODE speakerMode = FMOD.SPEAKERMODE.DEFAULT;
int numRawSpeakers = 0;
FMODUnity.RuntimeManager.CoreSystem.getSoftwareFormat(out samplingRate, out speakerMode, out numRawSpeakers);
uint frameSize = 0u;
int numBuffers = 0;
FMODUnity.RuntimeManager.CoreSystem.getDSPBufferSize(out frameSize, out numBuffers);
audioSettings.samplingRate = samplingRate;
audioSettings.frameSize = (int) frameSize;
return audioSettings;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f8e2075eab110ad4b88d26e434723035
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
{
"name": "SteamAudioFMODStudio",
"rootNamespace": "",
"references": [
"GUID:558d81aa619cc114fbdab8165212bc9d",
"GUID:0c752da273b17c547ae705acf0f2adf2"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f94a722c562013b4bb87a97a6a58fca0
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,83 @@
//
// Copyright 2017-2023 Valve Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Runtime.InteropServices;
using UnityEngine;
namespace SteamAudio
{
public static class FMODStudioAPI
{
// FMOD STUDIO PLUGIN
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODInitialize(IntPtr context);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODSetHRTF(IntPtr hrtf);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODSetSimulationSettings(SimulationSettings simulationSettings);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODSetReverbSource(IntPtr reverbSource);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODTerminate();
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern int iplFMODAddSource(IntPtr source);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODRemoveSource(int handle);
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
[DllImport("__Internal")]
#else
[DllImport("phonon_fmod")]
#endif
public static extern void iplFMODSetHRTFDisabled(bool disabled);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 910b4010d4f5f4c388938e56f9715f37
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: