Cleanup HRTF support code #963

This commit is contained in:
Wojtek Figat
2023-04-19 13:12:27 +02:00
parent 1b278f5c56
commit 1e1c296300
5 changed files with 29 additions and 35 deletions

View File

@@ -278,6 +278,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=heightfield/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=heightfield/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Heightmap/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Heightmap/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=HLSL/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=HLSL/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=HRTF/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Inlines/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Inlines/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Inscattering/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Inscattering/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Interpolants/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Interpolants/@EntryIndexedValue">True</s:Boolean>

View File

@@ -57,7 +57,7 @@ namespace
float Volume = 1.0f; float Volume = 1.0f;
int32 ActiveDeviceIndex = -1; int32 ActiveDeviceIndex = -1;
bool MuteOnFocusLoss = true; bool MuteOnFocusLoss = true;
bool UseHRTFWhenAvailable = true; bool EnableHRTF = true;
} }
class AudioService : public EngineService class AudioService : public EngineService
@@ -95,7 +95,7 @@ void AudioSettings::Apply()
if (AudioBackend::Instance != nullptr) if (AudioBackend::Instance != nullptr)
{ {
Audio::SetDopplerFactor(DopplerFactor); Audio::SetDopplerFactor(DopplerFactor);
Audio::SetUseHRTFWhenAvailable(UseHRTFWhenAvailable); Audio::SetEnableHRTF(EnableHRTF);
} }
} }
@@ -143,14 +143,16 @@ void Audio::SetDopplerFactor(float value)
AudioBackend::SetDopplerFactor(value); AudioBackend::SetDopplerFactor(value);
} }
bool Audio::GetUseHRTFWhenAvailable() bool Audio::GetEnableHRTF()
{ {
return UseHRTFWhenAvailable; return EnableHRTF;
} }
void Audio::SetUseHRTFWhenAvailable(bool value) void Audio::SetEnableHRTF(bool value)
{ {
UseHRTFWhenAvailable = value; if (EnableHRTF == value)
return;
EnableHRTF = value;
AudioBackend::Listener::ReinitializeAll(); AudioBackend::Listener::ReinitializeAll();
} }

View File

@@ -18,7 +18,6 @@ API_CLASS(Static) class FLAXENGINE_API Audio
friend class AudioClip; friend class AudioClip;
public: public:
/// <summary> /// <summary>
/// The audio listeners collection registered by the service. /// The audio listeners collection registered by the service.
/// </summary> /// </summary>
@@ -45,7 +44,6 @@ public:
API_EVENT() static Action ActiveDeviceChanged; API_EVENT() static Action ActiveDeviceChanged;
public: public:
/// <summary> /// <summary>
/// Gets the active device. /// Gets the active device.
/// </summary> /// </summary>
@@ -65,7 +63,6 @@ public:
API_PROPERTY() static void SetActiveDeviceIndex(int32 index); API_PROPERTY() static void SetActiveDeviceIndex(int32 index);
public: public:
/// <summary> /// <summary>
/// Gets the master volume applied to all the audio sources (normalized to range 0-1). /// Gets the master volume applied to all the audio sources (normalized to range 0-1).
/// </summary> /// </summary>
@@ -91,18 +88,17 @@ public:
API_PROPERTY() static void SetDopplerFactor(float value); API_PROPERTY() static void SetDopplerFactor(float value);
/// <summary> /// <summary>
/// Gets the preference to use HRTF audio when available. Default is true. /// Gets the preference to use HRTF audio (when available on platform). Default is true.
/// </summary> /// </summary>
API_PROPERTY() static bool GetUseHRTFWhenAvailable(); API_PROPERTY() static bool GetEnableHRTF();
/// <summary> /// <summary>
/// Sets the preference to use HRTF audio when available. Default is true. /// Sets the preference to use HRTF audio (when available on platform). Default is true.
/// </summary> /// </summary>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
API_PROPERTY() static void SetUseHRTFWhenAvailable(bool value); API_PROPERTY() static void SetEnableHRTF(bool value);
public: public:
static void OnAddListener(AudioListener* listener); static void OnAddListener(AudioListener* listener);
static void OnRemoveListener(AudioListener* listener); static void OnRemoveListener(AudioListener* listener);

View File

@@ -10,9 +10,9 @@
/// </summary> /// </summary>
API_CLASS(sealed, Namespace="FlaxEditor.Content.Settings") class FLAXENGINE_API AudioSettings : public SettingsBase API_CLASS(sealed, Namespace="FlaxEditor.Content.Settings") class FLAXENGINE_API AudioSettings : public SettingsBase
{ {
DECLARE_SCRIPTING_TYPE_MINIMAL(AudioSettings); DECLARE_SCRIPTING_TYPE_MINIMAL(AudioSettings);
public:
public:
/// <summary> /// <summary>
/// If checked, audio playback will be disabled in build game. Can be used if game uses custom audio playback engine. /// If checked, audio playback will be disabled in build game. Can be used if game uses custom audio playback engine.
/// </summary> /// </summary>
@@ -32,15 +32,13 @@ public:
bool MuteOnFocusLoss = true; bool MuteOnFocusLoss = true;
/// <summary> /// <summary>
/// Enables/disables HRTF audio for in-engine processing of 3d audio. /// Enables or disables HRTF audio for in-engine processing of 3D audio (if supported by platform).
/// If enabled, the user should be using two-channel/headphones audio output and have all other surround virtualization disabled (Atmos, DTS:X, vendor specific, etc.) /// If enabled, the user should be using two-channel/headphones audio output and have all other surround virtualization disabled (Atmos, DTS:X, vendor specific, etc.)
/// Note: this is currently only available with the OpenAL audio backend.
/// </summary> /// </summary>
API_FIELD(Attributes="EditorOrder(300), DefaultValue(true), EditorDisplay(\"Spatial Audio\")") API_FIELD(Attributes="EditorOrder(300), DefaultValue(true), EditorDisplay(\"Spatial Audio\")")
bool UseHRTFWhenAvailable = true; bool EnableHRTF = true;
public: public:
/// <summary> /// <summary>
/// Gets the instance of the settings asset (default value if missing). Object returned by this method is always loaded with valid data to use. /// Gets the instance of the settings asset (default value if missing). Object returned by this method is always loaded with valid data to use.
/// </summary> /// </summary>
@@ -54,6 +52,6 @@ public:
DESERIALIZE(DisableAudio); DESERIALIZE(DisableAudio);
DESERIALIZE(DopplerFactor); DESERIALIZE(DopplerFactor);
DESERIALIZE(MuteOnFocusLoss); DESERIALIZE(MuteOnFocusLoss);
DESERIALIZE(UseHRTFWhenAvailable); DESERIALIZE(EnableHRTF);
} }
}; };

View File

@@ -156,7 +156,7 @@ namespace ALC
void RebuildContexts(bool isChangingDevice) void RebuildContexts(bool isChangingDevice)
{ {
LOG(Info, "Audio: Rebuilding audio contexts"); LOG(Info, "Rebuilding audio contexts");
if (!isChangingDevice) if (!isChangingDevice)
{ {
@@ -169,29 +169,26 @@ namespace ALC
if (Device == nullptr) if (Device == nullptr)
return; return;
ALCint attrsHrtf[] = { ALC_HRTF_SOFT, ALC_TRUE };
const ALCint* attrList = nullptr;
if (Audio::GetEnableHRTF())
{
LOG(Info, "Enabling OpenAL HRTF");
attrList = attrsHrtf;
}
#if ALC_MULTIPLE_LISTENERS #if ALC_MULTIPLE_LISTENERS
const int32 numListeners = Audio::Listeners.Count(); const int32 numListeners = Audio::Listeners.Count();
const int32 numContexts = numListeners > 1 ? numListeners : 1; const int32 numContexts = numListeners > 1 ? numListeners : 1;
Contexts.Resize(numContexts); Contexts.Resize(numContexts);
ALC_FOR_EACH_CONTEXT() ALC_FOR_EACH_CONTEXT()
ALCcontext* context = alcCreateContext(Device, nullptr); ALCcontext* context = alcCreateContext(Device, attrList);
Contexts[i] = context; Contexts[i] = context;
} }
#else #else
Contexts.Resize(1); Contexts.Resize(1);
Contexts[0] = alcCreateContext(Device, attrList);
if (Audio::GetUseHRTFWhenAvailable())
{
LOG(Info, "Audio: Enabling OpenAL HRTF");
ALCint attrs[] = { ALC_HRTF_SOFT, ALC_TRUE };
Contexts[0] = alcCreateContext(Device, attrs);
}
else
{
Contexts[0] = alcCreateContext(Device, nullptr);
}
#endif #endif
// If only one context is available keep it active as an optimization. // If only one context is available keep it active as an optimization.