diff --git a/Source/Engine/Audio/AudioClip.cpp b/Source/Engine/Audio/AudioClip.cpp index 0204dd222..0abcc5479 100644 --- a/Source/Engine/Audio/AudioClip.cpp +++ b/Source/Engine/Audio/AudioClip.cpp @@ -32,7 +32,7 @@ bool AudioClip::StreamingTask::Run() { const auto idx = queue[i]; uint32& bufferId = clip->Buffers[idx]; - if (bufferId == AUDIO_BUFFER_ID_INVALID) + if (bufferId == 0) { bufferId = AudioBackend::Buffer::Create(); } @@ -40,7 +40,7 @@ bool AudioClip::StreamingTask::Run() { // Release unused data AudioBackend::Buffer::Delete(bufferId); - bufferId = AUDIO_BUFFER_ID_INVALID; + bufferId = 0; } } @@ -267,7 +267,7 @@ Task* AudioClip::CreateStreamingTask(int32 residency) for (int32 i = 0; i < StreamingQueue.Count(); i++) { const int32 idx = StreamingQueue[i]; - if (Buffers[idx] == AUDIO_BUFFER_ID_INVALID) + if (Buffers[idx] == 0) { const auto task = (Task*)RequestChunkDataAsync(idx); if (task) @@ -383,8 +383,8 @@ Asset::LoadResult AudioClip::load() void AudioClip::unload(bool isReloading) { bool hasAnyBuffer = false; - for (const AUDIO_BUFFER_ID_TYPE bufferId : Buffers) - hasAnyBuffer |= bufferId != AUDIO_BUFFER_ID_INVALID; + for (const uint32 bufferId : Buffers) + hasAnyBuffer |= bufferId != 0; // Stop any audio sources that are using this clip right now // TODO: find better way to collect audio sources using audio clip and impl it for AudioStreamingHandler too @@ -399,9 +399,9 @@ void AudioClip::unload(bool isReloading) StreamingQueue.Clear(); if (hasAnyBuffer && AudioBackend::Instance) { - for (AUDIO_BUFFER_ID_TYPE bufferId : Buffers) + for (uint32 bufferId : Buffers) { - if (bufferId != AUDIO_BUFFER_ID_INVALID) + if (bufferId != 0) AudioBackend::Buffer::Delete(bufferId); } } @@ -414,7 +414,7 @@ bool AudioClip::WriteBuffer(int32 chunkIndex) { // Ignore if buffer is not created const uint32 bufferId = Buffers[chunkIndex]; - if (bufferId == AUDIO_BUFFER_ID_INVALID) + if (bufferId == 0) return false; // Ensure audio backend exists diff --git a/Source/Engine/Audio/AudioClip.h b/Source/Engine/Audio/AudioClip.h index eee1557ca..f5061ed6f 100644 --- a/Source/Engine/Audio/AudioClip.h +++ b/Source/Engine/Audio/AudioClip.h @@ -88,7 +88,7 @@ public: /// /// The audio backend buffers (internal ids) collection used by this audio clip. /// - Array> Buffers; + Array> Buffers; /// /// The streaming cache. Contains indices of chunks to stream. If empty no streaming required. Managed by AudioStreamingHandler and used by the Audio streaming tasks. diff --git a/Source/Engine/Audio/AudioSource.cpp b/Source/Engine/Audio/AudioSource.cpp index c990332a0..dbe57e150 100644 --- a/Source/Engine/Audio/AudioSource.cpp +++ b/Source/Engine/Audio/AudioSource.cpp @@ -32,7 +32,7 @@ void AudioSource::SetVolume(float value) if (Math::NearEqual(_volume, value)) return; _volume = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::VolumeChanged(this); } @@ -42,7 +42,7 @@ void AudioSource::SetPitch(float value) if (Math::NearEqual(_pitch, value)) return; _pitch = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::PitchChanged(this); } @@ -52,7 +52,7 @@ void AudioSource::SetPan(float value) if (Math::NearEqual(_pan, value)) return; _pan = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::PanChanged(this); } @@ -63,7 +63,7 @@ void AudioSource::SetIsLooping(bool value) _loop = value; // When streaming we handle looping manually by the proper buffers submission - if (SourceIDs.HasItems() && !UseStreaming()) + if (SourceID && !UseStreaming()) AudioBackend::Source::IsLoopingChanged(this); } @@ -83,7 +83,7 @@ void AudioSource::SetMinDistance(float value) if (Math::NearEqual(_minDistance, value)) return; _minDistance = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::SpatialSetupChanged(this); } @@ -93,7 +93,7 @@ void AudioSource::SetAttenuation(float value) if (Math::NearEqual(_attenuation, value)) return; _attenuation = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::SpatialSetupChanged(this); } @@ -103,7 +103,7 @@ void AudioSource::SetDopplerFactor(float value) if (Math::NearEqual(_dopplerFactor, value)) return; _dopplerFactor = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::SpatialSetupChanged(this); } @@ -112,7 +112,7 @@ void AudioSource::SetAllowSpatialization(bool value) if (_allowSpatialization == value) return; _allowSpatialization = value; - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::SpatialSetupChanged(this); } @@ -152,7 +152,7 @@ void AudioSource::Play() RequestStreamingBuffersUpdate(); } } - else if (SourceIDs.HasItems()) + else if (SourceID) { // Play it right away SetNonStreamingBuffer(); @@ -187,14 +187,13 @@ void AudioSource::Stop() _state = States::Stopped; _isActuallyPlayingSth = false; _streamingFirstChunk = 0; - - if (SourceIDs.HasItems()) + if (SourceID) AudioBackend::Source::Stop(this); } float AudioSource::GetTime() const { - if (_state == States::Stopped || SourceIDs.IsEmpty() || !Clip->IsLoaded()) + if (_state == States::Stopped || SourceID == 0 || !Clip->IsLoaded()) return 0.0f; float time = AudioBackend::Source::GetCurrentBufferTime(this); @@ -265,10 +264,10 @@ void AudioSource::Cleanup() _savedTime = GetTime(); Stop(); - if (SourceIDs.HasItems()) + if (SourceID) { AudioBackend::Source::Cleanup(this); - SourceIDs.Clear(); + SourceID = 0; } } @@ -283,7 +282,7 @@ void AudioSource::OnClipLoaded() AudioBackend::Source::ClipLoaded(this); // Start playing if source was waiting for the clip to load - if (SourceIDs.HasItems() && _state == States::Playing && !_isActuallyPlayingSth) + if (SourceID && _state == States::Playing && !_isActuallyPlayingSth) { if (Clip->IsStreamable()) { @@ -329,7 +328,7 @@ void AudioSource::SetNonStreamingBuffer() void AudioSource::PlayInternal() { - if (_clipChanged && SourceIDs.HasItems()) + if (_clipChanged && SourceID != 0) { // If clip was changed between source setup (OnEnable) and actual playback start then ensure to flush any runtime properties with the audio backend _clipChanged = false; @@ -420,13 +419,13 @@ void AudioSource::Update() } // Skip other update logic if it's not valid streamable source - if (!UseStreaming() || SourceIDs.IsEmpty()) + if (!UseStreaming() || SourceID == 0) return; auto clip = Clip.Get(); clip->Locker.Lock(); // Handle streaming buffers queue submit (ensure that clip has loaded the first chunk with audio data) - if (_needToUpdateStreamingBuffers && clip->Buffers[_streamingFirstChunk] != AUDIO_BUFFER_ID_INVALID) + if (_needToUpdateStreamingBuffers && clip->Buffers[_streamingFirstChunk] != 0) { // Get buffers in a queue count int32 numQueuedBuffers; @@ -434,11 +433,11 @@ void AudioSource::Update() // Queue missing buffers uint32 bufferId; - if (numQueuedBuffers < 1 && (bufferId = clip->Buffers[_streamingFirstChunk]) != AUDIO_BUFFER_ID_INVALID) + if (numQueuedBuffers < 1 && (bufferId = clip->Buffers[_streamingFirstChunk]) != 0) { AudioBackend::Source::QueueBuffer(this, bufferId); } - if (numQueuedBuffers < 2 && _streamingFirstChunk + 1 < clip->Buffers.Count() && (bufferId = clip->Buffers[_streamingFirstChunk + 1]) != AUDIO_BUFFER_ID_INVALID) + if (numQueuedBuffers < 2 && _streamingFirstChunk + 1 < clip->Buffers.Count() && (bufferId = clip->Buffers[_streamingFirstChunk + 1]) != 0) { AudioBackend::Source::QueueBuffer(this, bufferId); } @@ -535,7 +534,7 @@ void AudioSource::OnTransformChanged() _box = BoundingBox(_transform.Translation); _sphere = BoundingSphere(_transform.Translation, 0.0f); - if (IsActiveInHierarchy() && SourceIDs.HasItems()) + if (IsActiveInHierarchy() && SourceID) { AudioBackend::Source::TransformChanged(this); } diff --git a/Source/Engine/Audio/AudioSource.h b/Source/Engine/Audio/AudioSource.h index 7e3c17d80..f94e04fb9 100644 --- a/Source/Engine/Audio/AudioSource.h +++ b/Source/Engine/Audio/AudioSource.h @@ -8,7 +8,7 @@ #include "Config.h" /// -/// Represents a source for emitting audio. Audio can be played spatially (gun shot), or normally (music). Each audio source must have an AudioClip to play - back, and it can also have a position in the case of spatial(3D) audio. +/// Represents a source for emitting audio. Audio can be played spatially (gun shot), or normally (music). Each audio source must have an AudioClip to play - back, and it can also have a position in the case of spatial (3D) audio. /// /// /// Whether or not an audio source is spatial is controlled by the assigned AudioClip.The volume and the pitch of a spatial audio source is controlled by its position and the AudioListener's position/direction/velocity. @@ -19,6 +19,7 @@ class FLAXENGINE_API AudioSource : public Actor DECLARE_SCENE_OBJECT(AudioSource); friend class AudioStreamingHandler; friend class AudioClip; + public: /// /// Valid states in which AudioSource can be in. @@ -66,9 +67,9 @@ private: public: /// - /// The internal IDs of this audio source used by the audio backend (unique ID per context/listener). + /// The internal ID of this audio source used by the audio backend. Empty if 0. /// - Array> SourceIDs; + uint32 SourceID = 0; /// /// The audio clip asset used as a source of the sound. @@ -260,7 +261,7 @@ public: API_PROPERTY() void SetTime(float time); /// - /// Returns true if the sound source is three dimensional (volume and pitch varies based on listener distance and velocity). + /// Returns true if the sound source is three-dimensional (volume and pitch varies based on listener distance and velocity). /// API_PROPERTY() bool Is3D() const; diff --git a/Source/Engine/Audio/Config.h b/Source/Engine/Audio/Config.h index 287e25d2d..e824b8e0f 100644 --- a/Source/Engine/Audio/Config.h +++ b/Source/Engine/Audio/Config.h @@ -5,16 +5,7 @@ #include "Engine/Core/Config.h" // The maximum amount of listeners used at once -#define AUDIO_MAX_LISTENERS 8 +#define AUDIO_MAX_LISTENERS 1 // The maximum amount of audio emitter buffers #define AUDIO_MAX_SOURCE_BUFFERS (ASSET_FILE_DATA_CHUNKS) - -// The type of the audio source IDs used to identify it (per listener) -#define AUDIO_SOURCE_ID_TYPE uint32 - -// The type of the audio buffer IDs used to identify it -#define AUDIO_BUFFER_ID_TYPE uint32 - -// The buffer ID that is invalid (unused) -#define AUDIO_BUFFER_ID_INVALID 0 diff --git a/Source/Engine/Audio/OpenAL/AudioBackendOAL.cpp b/Source/Engine/Audio/OpenAL/AudioBackendOAL.cpp index ea22143fd..86850e0c4 100644 --- a/Source/Engine/Audio/OpenAL/AudioBackendOAL.cpp +++ b/Source/Engine/Audio/OpenAL/AudioBackendOAL.cpp @@ -6,6 +6,7 @@ #include "Engine/Platform/StringUtils.h" #include "Engine/Core/Log.h" #include "Engine/Tools/AudioTool/AudioTool.h" +#include "Engine/Engine/Units.h" #include "Engine/Profiler/ProfilerCPU.h" #include "Engine/Audio/Audio.h" #include "Engine/Audio/AudioListener.h" @@ -19,12 +20,9 @@ #include #include -#define ALC_MULTIPLE_LISTENERS 0 - -#define FLAX_COORD_SCALE 0.01f // units are meters -#define FLAX_DST_TO_OAL(x) x * FLAX_COORD_SCALE -#define FLAX_POS_TO_OAL(vec) ((ALfloat)vec.X * -FLAX_COORD_SCALE), ((ALfloat)vec.Y * FLAX_COORD_SCALE), ((ALfloat)vec.Z * FLAX_COORD_SCALE) -#define FLAX_VEL_TO_OAL(vec) ((ALfloat)vec.X * -(FLAX_COORD_SCALE*FLAX_COORD_SCALE)), ((ALfloat)vec.Y * (FLAX_COORD_SCALE*FLAX_COORD_SCALE)), ((ALfloat)vec.Z * (FLAX_COORD_SCALE*FLAX_COORD_SCALE)) +#define FLAX_DST_TO_OAL(x) x * UNITS_TO_METERS_SCALE +#define FLAX_POS_TO_OAL(vec) ((ALfloat)vec.X * -UNITS_TO_METERS_SCALE), ((ALfloat)vec.Y * UNITS_TO_METERS_SCALE), ((ALfloat)vec.Z * UNITS_TO_METERS_SCALE) +#define FLAX_VEL_TO_OAL(vec) ((ALfloat)vec.X * -(UNITS_TO_METERS_SCALE*UNITS_TO_METERS_SCALE)), ((ALfloat)vec.Y * (UNITS_TO_METERS_SCALE*UNITS_TO_METERS_SCALE)), ((ALfloat)vec.Z * (UNITS_TO_METERS_SCALE*UNITS_TO_METERS_SCALE)) #if BUILD_RELEASE #define ALC_CHECK_ERROR(method) #else @@ -39,75 +37,30 @@ } #endif -#if ALC_MULTIPLE_LISTENERS -#define ALC_FOR_EACH_CONTEXT() \ - for (int32 i = 0; i < Contexts.Count(); i++) - { \ - if (Contexts.Count() > 1) \ - alcMakeContextCurrent(Contexts[i]); -#define ALC_GET_DEFAULT_CONTEXT() \ - if (Contexts.Count() > 1) \ - alcMakeContextCurrent(Contexts[0]); -#define ALC_GET_LISTENER_CONTEXT(listener) \ - if (Contexts.Count() > 1) \ - alcMakeContextCurrent(ALC::GetContext(listener))); -#else -#define ALC_FOR_EACH_CONTEXT() { int32 i = 0; -#define ALC_GET_DEFAULT_CONTEXT() -#define ALC_GET_LISTENER_CONTEXT(listener) -#endif - namespace ALC { ALCdevice* Device = nullptr; - Array> Contexts; + ALCcontext* Context = nullptr; AudioBackend::FeatureFlags Features = AudioBackend::FeatureFlags::None; bool IsExtensionSupported(const char* extension) { if (Device == nullptr) return false; - const int32 length = StringUtils::Length(extension); if ((length > 2) && (StringUtils::Compare(extension, "ALC", 3) == 0)) return alcIsExtensionPresent(Device, extension) != AL_FALSE; return alIsExtensionPresent(extension) != AL_FALSE; } - ALCcontext* GetContext(const class AudioListener* listener) + void ClearContext() { -#if ALC_MULTIPLE_LISTENERS - const auto& listeners = Audio::Listeners; - if (listeners.HasItems()) + if (Context) { - ASSERT(listeners.Count() == Contexts.Count()); - - const int32 numContexts = Contexts.Count(); - ALC_FOR_EACH_CONTEXT() - { - if (listeners[i] == listener) - return Contexts[i]; - } + alcMakeContextCurrent(nullptr); + alcDestroyContext(Context); + Context = nullptr; } - ASSERT(Contexts.HasItems()); -#else - ASSERT(Contexts.Count() == 1); -#endif - return Contexts[0]; - } - - FORCE_INLINE const Array>& GetContexts() - { - return Contexts; - } - - void ClearContexts() - { - alcMakeContextCurrent(nullptr); - - for (ALCcontext* context : Contexts) - alcDestroyContext(context); - Contexts.Clear(); } namespace Listener @@ -126,58 +79,51 @@ namespace ALC { void Rebuild(AudioSource* source) { - ASSERT(source->SourceIDs.IsEmpty()); + ASSERT(source->SourceID == 0); const bool is3D = source->Is3D(); const bool loop = source->GetIsLooping() && !source->UseStreaming(); - ALC_FOR_EACH_CONTEXT() - uint32 sourceID = 0; - alGenSources(1, &sourceID); + uint32 sourceID = 0; + alGenSources(1, &sourceID); + source->SourceID = sourceID; - source->SourceIDs.Add(sourceID); - } - - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - alSourcef(sourceID, AL_GAIN, source->GetVolume()); - alSourcef(sourceID, AL_PITCH, source->GetPitch()); - alSourcef(sourceID, AL_SEC_OFFSET, 0.0f); - alSourcei(sourceID, AL_LOOPING, loop); - alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); - alSourcei(sourceID, AL_BUFFER, 0); - if (is3D) - { + alSourcef(sourceID, AL_GAIN, source->GetVolume()); + alSourcef(sourceID, AL_PITCH, source->GetPitch()); + alSourcef(sourceID, AL_SEC_OFFSET, 0.0f); + alSourcei(sourceID, AL_LOOPING, loop); + alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); + alSourcei(sourceID, AL_BUFFER, 0); + if (is3D) + { #ifdef AL_SOFT_source_spatialize - alSourcei(sourceID, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); -#endif - alSourcef(sourceID, AL_ROLLOFF_FACTOR, source->GetAttenuation()); - alSourcef(sourceID, AL_DOPPLER_FACTOR, source->GetDopplerFactor()); - alSourcef(sourceID, AL_REFERENCE_DISTANCE, FLAX_DST_TO_OAL(source->GetMinDistance())); - alSource3f(sourceID, AL_POSITION, FLAX_POS_TO_OAL(source->GetPosition())); - alSource3f(sourceID, AL_VELOCITY, FLAX_VEL_TO_OAL(source->GetVelocity())); - } - else - { - alSourcef(sourceID, AL_ROLLOFF_FACTOR, 0.0f); - alSourcef(sourceID, AL_DOPPLER_FACTOR, 1.0f); - alSourcef(sourceID, AL_REFERENCE_DISTANCE, 0.0f); - alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f); - alSource3f(sourceID, AL_VELOCITY, 0.0f, 0.0f, 0.0f); - } -#ifdef AL_EXT_STEREO_ANGLES - const float panAngle = source->GetPan() * PI_HALF; - const ALfloat panAngles[2] = { (ALfloat)(PI / 6.0 - panAngle), (ALfloat)(-PI / 6.0 - panAngle) }; // Angles are specified counter-clockwise in radians - alSourcefv(sourceID, AL_STEREO_ANGLES, panAngles); + alSourcei(sourceID, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); #endif + alSourcef(sourceID, AL_ROLLOFF_FACTOR, source->GetAttenuation()); + alSourcef(sourceID, AL_DOPPLER_FACTOR, source->GetDopplerFactor()); + alSourcef(sourceID, AL_REFERENCE_DISTANCE, FLAX_DST_TO_OAL(source->GetMinDistance())); + alSource3f(sourceID, AL_POSITION, FLAX_POS_TO_OAL(source->GetPosition())); + alSource3f(sourceID, AL_VELOCITY, FLAX_VEL_TO_OAL(source->GetVelocity())); } + else + { + alSourcef(sourceID, AL_ROLLOFF_FACTOR, 0.0f); + alSourcef(sourceID, AL_DOPPLER_FACTOR, 1.0f); + alSourcef(sourceID, AL_REFERENCE_DISTANCE, 0.0f); + alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f); + alSource3f(sourceID, AL_VELOCITY, 0.0f, 0.0f, 0.0f); + } +#ifdef AL_EXT_STEREO_ANGLES + const float panAngle = source->GetPan() * PI_HALF; + const ALfloat panAngles[2] = { (ALfloat)(PI / 6.0 - panAngle), (ALfloat)(-PI / 6.0 - panAngle) }; // Angles are specified counter-clockwise in radians + alSourcefv(sourceID, AL_STEREO_ANGLES, panAngles); +#endif // Restore state after Cleanup source->Restore(); } } - void RebuildContexts(bool isChangingDevice) + void RebuildContext(bool isChangingDevice) { LOG(Info, "Rebuilding audio contexts"); @@ -187,7 +133,7 @@ namespace ALC source->Cleanup(); } - ClearContexts(); + ClearContext(); if (Device == nullptr) return; @@ -200,23 +146,8 @@ namespace ALC attrList = attrsHrtf; } -#if ALC_MULTIPLE_LISTENERS - const int32 numListeners = Audio::Listeners.Count(); - const int32 numContexts = numListeners > 1 ? numListeners : 1; - Contexts.Resize(numContexts); - - ALC_FOR_EACH_CONTEXT() - ALCcontext* context = alcCreateContext(Device, attrList); - Contexts[i] = context; - } -#else - Contexts.Resize(1); - Contexts[0] = alcCreateContext(Device, attrList); -#endif - - // If only one context is available keep it active as an optimization. - // Audio listeners and sources will avoid excessive context switching in such case. - alcMakeContextCurrent(Contexts[0]); + Context = alcCreateContext(Device, attrList); + alcMakeContextCurrent(Context); for (AudioListener* listener : Audio::Listeners) Listener::Rebuild(listener); @@ -315,33 +246,22 @@ const Char* GetOpenALErrorString(int error) void AudioBackendOAL::Listener_OnAdd(AudioListener* listener) { -#if ALC_MULTIPLE_LISTENERS - ALC::RebuildContexts(false); -#else AudioBackend::Listener::TransformChanged(listener); alListenerf(AL_GAIN, Audio::GetVolume()); -#endif } void AudioBackendOAL::Listener_OnRemove(AudioListener* listener) { -#if ALC_MULTIPLE_LISTENERS - ALC::RebuildContexts(false); -#endif } void AudioBackendOAL::Listener_VelocityChanged(AudioListener* listener) { - ALC_GET_LISTENER_CONTEXT(listener) - const Float3 velocity = listener->GetVelocity(); alListener3f(AL_VELOCITY, FLAX_VEL_TO_OAL(velocity)); } void AudioBackendOAL::Listener_TransformChanged(AudioListener* listener) { - ALC_GET_LISTENER_CONTEXT(listener) - const Float3 position = listener->GetPosition(); const Quaternion orientation = listener->GetOrientation(); const Float3 flipX(-1, 1, 1); @@ -359,7 +279,7 @@ void AudioBackendOAL::Listener_TransformChanged(AudioListener* listener) void AudioBackendOAL::Listener_ReinitializeAll() { - ALC::RebuildContexts(false); + ALC::RebuildContext(false); } void AudioBackendOAL::Source_OnAdd(AudioSource* source) @@ -376,36 +296,28 @@ void AudioBackendOAL::Source_VelocityChanged(AudioSource* source) { if (!source->Is3D()) return; - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSource3f(sourceID, AL_VELOCITY, FLAX_VEL_TO_OAL(source->GetVelocity())); - } + const uint32 sourceID = source->SourceID; + alSource3f(sourceID, AL_VELOCITY, FLAX_VEL_TO_OAL(source->GetVelocity())); } void AudioBackendOAL::Source_TransformChanged(AudioSource* source) { if (!source->Is3D()) return; - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSource3f(sourceID, AL_POSITION, FLAX_POS_TO_OAL(source->GetPosition())); - } + const uint32 sourceID = source->SourceID; + alSource3f(sourceID, AL_POSITION, FLAX_POS_TO_OAL(source->GetPosition())); } void AudioBackendOAL::Source_VolumeChanged(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcef(sourceID, AL_GAIN, source->GetVolume()); - } + const uint32 sourceID = source->SourceID; + alSourcef(sourceID, AL_GAIN, source->GetVolume()); } void AudioBackendOAL::Source_PitchChanged(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcef(sourceID, AL_PITCH, source->GetPitch()); - } + const uint32 sourceID = source->SourceID; + alSourcef(sourceID, AL_PITCH, source->GetPitch()); } void AudioBackendOAL::Source_PanChanged(AudioSource* source) @@ -413,131 +325,106 @@ void AudioBackendOAL::Source_PanChanged(AudioSource* source) #ifdef AL_EXT_STEREO_ANGLES const float panAngle = source->GetPan() * PI_HALF; const ALfloat panAngles[2] = { (ALfloat)(PI / 6.0 - panAngle), (ALfloat)(-PI / 6.0 - panAngle) }; // Angles are specified counter-clockwise in radians - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcefv(sourceID, AL_STEREO_ANGLES, panAngles); - } + const uint32 sourceID = source->SourceID; + alSourcefv(sourceID, AL_STEREO_ANGLES, panAngles); #endif } void AudioBackendOAL::Source_IsLoopingChanged(AudioSource* source) { const bool loop = source->GetIsLooping() && !source->UseStreaming(); - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcei(sourceID, AL_LOOPING, loop); - } + const uint32 sourceID = source->SourceID; + alSourcei(sourceID, AL_LOOPING, loop); } void AudioBackendOAL::Source_SpatialSetupChanged(AudioSource* source) { const bool is3D = source->Is3D(); - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); - if (is3D) - { + const uint32 sourceID = source->SourceID; + alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); + if (is3D) + { #ifdef AL_SOFT_source_spatialize - alSourcei(sourceID, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); + alSourcei(sourceID, AL_SOURCE_SPATIALIZE_SOFT, AL_TRUE); #endif - alSourcef(sourceID, AL_ROLLOFF_FACTOR, source->GetAttenuation()); - alSourcef(sourceID, AL_DOPPLER_FACTOR, source->GetDopplerFactor()); - alSourcef(sourceID, AL_REFERENCE_DISTANCE, FLAX_DST_TO_OAL(source->GetMinDistance())); - } - else - { - alSourcef(sourceID, AL_ROLLOFF_FACTOR, 0.0f); - alSourcef(sourceID, AL_DOPPLER_FACTOR, 1.0f); - alSourcef(sourceID, AL_REFERENCE_DISTANCE, 0.0f); - } + alSourcef(sourceID, AL_ROLLOFF_FACTOR, source->GetAttenuation()); + alSourcef(sourceID, AL_DOPPLER_FACTOR, source->GetDopplerFactor()); + alSourcef(sourceID, AL_REFERENCE_DISTANCE, FLAX_DST_TO_OAL(source->GetMinDistance())); + } + else + { + alSourcef(sourceID, AL_ROLLOFF_FACTOR, 0.0f); + alSourcef(sourceID, AL_DOPPLER_FACTOR, 1.0f); + alSourcef(sourceID, AL_REFERENCE_DISTANCE, 0.0f); } } void AudioBackendOAL::Source_ClipLoaded(AudioSource* source) { - if (source->SourceIDs.Count() < ALC::Contexts.Count()) + if (source->SourceID == 0) return; const auto clip = source->Clip.Get(); const bool is3D = source->Is3D(); const bool loop = source->GetIsLooping() && !clip->IsStreamable(); - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); - alSourcei(sourceID, AL_LOOPING, loop); - } + const uint32 sourceID = source->SourceID; + alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D); + alSourcei(sourceID, AL_LOOPING, loop); } void AudioBackendOAL::Source_Cleanup(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - alSourcei(sourceID, AL_BUFFER, 0); - ALC_CHECK_ERROR(alSourcei); - alDeleteSources(1, &sourceID); - ALC_CHECK_ERROR(alDeleteSources); - } + const uint32 sourceID = source->SourceID; + alSourcei(sourceID, AL_BUFFER, 0); + ALC_CHECK_ERROR(alSourcei); + alDeleteSources(1, &sourceID); + ALC_CHECK_ERROR(alDeleteSources); } void AudioBackendOAL::Source_Play(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - // Play - alSourcePlay(sourceID); - ALC_CHECK_ERROR(alSourcePlay); - } + const uint32 sourceID = source->SourceID; + alSourcePlay(sourceID); + ALC_CHECK_ERROR(alSourcePlay); } void AudioBackendOAL::Source_Pause(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - // Pause - alSourcePause(sourceID); - ALC_CHECK_ERROR(alSourcePause); - } + const uint32 sourceID = source->SourceID; + alSourcePause(sourceID); + ALC_CHECK_ERROR(alSourcePause); } void AudioBackendOAL::Source_Stop(AudioSource* source) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; + const uint32 sourceID = source->SourceID; - // Stop and rewind - alSourceRewind(sourceID); - ALC_CHECK_ERROR(alSourceRewind); - alSourcef(sourceID, AL_SEC_OFFSET, 0.0f); + // Stop and rewind + alSourceRewind(sourceID); + ALC_CHECK_ERROR(alSourceRewind); + alSourcef(sourceID, AL_SEC_OFFSET, 0.0f); - // Unset streaming buffers - alSourcei(sourceID, AL_BUFFER, 0); - ALC_CHECK_ERROR(alSourcei); - } + // Unset streaming buffers + alSourcei(sourceID, AL_BUFFER, 0); + ALC_CHECK_ERROR(alSourcei); } void AudioBackendOAL::Source_SetCurrentBufferTime(AudioSource* source, float value) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - alSourcef(sourceID, AL_SEC_OFFSET, value); - } + const uint32 sourceID = source->SourceID; + alSourcef(sourceID, AL_SEC_OFFSET, value); } float AudioBackendOAL::Source_GetCurrentBufferTime(const AudioSource* source) { - ALC_GET_DEFAULT_CONTEXT() - #if 0 float time; - alGetSourcef(source->SourceIDs[0], AL_SEC_OFFSET, &time); + alGetSourcef(source->SourceID, AL_SEC_OFFSET, &time); #else ASSERT(source->Clip && source->Clip->IsLoaded()); const AudioDataInfo& clipInfo = source->Clip->AudioHeader.Info; ALint samplesPlayed; - alGetSourcei(source->SourceIDs[0], AL_SAMPLE_OFFSET, &samplesPlayed); + alGetSourcei(source->SourceID, AL_SAMPLE_OFFSET, &samplesPlayed); const uint32 totalSamples = clipInfo.NumSamples / clipInfo.NumChannels; const float time = (samplesPlayed % totalSamples) / static_cast(Math::Max(1U, clipInfo.SampleRate)); #endif @@ -548,56 +435,44 @@ float AudioBackendOAL::Source_GetCurrentBufferTime(const AudioSource* source) void AudioBackendOAL::Source_SetNonStreamingBuffer(AudioSource* source) { const uint32 bufferId = source->Clip->Buffers[0]; - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - alSourcei(sourceID, AL_BUFFER, bufferId); - ALC_CHECK_ERROR(alSourcei); - } + const uint32 sourceID = source->SourceID; + alSourcei(sourceID, AL_BUFFER, bufferId); + ALC_CHECK_ERROR(alSourcei); } void AudioBackendOAL::Source_GetProcessedBuffersCount(AudioSource* source, int32& processedBuffersCount) { - ALC_GET_DEFAULT_CONTEXT() - // Check the first context only - const uint32 sourceID = source->SourceIDs[0]; + const uint32 sourceID = source->SourceID; alGetSourcei(sourceID, AL_BUFFERS_PROCESSED, &processedBuffersCount); ALC_CHECK_ERROR(alGetSourcei); } void AudioBackendOAL::Source_GetQueuedBuffersCount(AudioSource* source, int32& queuedBuffersCount) { - ALC_GET_DEFAULT_CONTEXT() - // Check the first context only - const uint32 sourceID = source->SourceIDs[0]; + const uint32 sourceID = source->SourceID; alGetSourcei(sourceID, AL_BUFFERS_QUEUED, &queuedBuffersCount); ALC_CHECK_ERROR(alGetSourcei); } void AudioBackendOAL::Source_QueueBuffer(AudioSource* source, uint32 bufferId) { - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; + const uint32 sourceID = source->SourceID; - // Queue new buffer - alSourceQueueBuffers(sourceID, 1, &bufferId); - ALC_CHECK_ERROR(alSourceQueueBuffers); - } + // Queue new buffer + alSourceQueueBuffers(sourceID, 1, &bufferId); + ALC_CHECK_ERROR(alSourceQueueBuffers); } void AudioBackendOAL::Source_DequeueProcessedBuffers(AudioSource* source) { ALuint buffers[AUDIO_MAX_SOURCE_BUFFERS]; - ALC_FOR_EACH_CONTEXT() - const uint32 sourceID = source->SourceIDs[i]; - - int32 numProcessedBuffers; - alGetSourcei(sourceID, AL_BUFFERS_PROCESSED, &numProcessedBuffers); - alSourceUnqueueBuffers(sourceID, numProcessedBuffers, buffers); - ALC_CHECK_ERROR(alSourceUnqueueBuffers); - } + const uint32 sourceID = source->SourceID; + int32 numProcessedBuffers; + alGetSourcei(sourceID, AL_BUFFERS_PROCESSED, &numProcessedBuffers); + alSourceUnqueueBuffers(sourceID, numProcessedBuffers, buffers); + ALC_CHECK_ERROR(alSourceUnqueueBuffers); } uint32 AudioBackendOAL::Buffer_Create() @@ -729,7 +604,7 @@ void AudioBackendOAL::Base_OnActiveDeviceChanged() // Cleanup for (AudioSource* source : Audio::Sources) source->Cleanup(); - ALC::ClearContexts(); + ALC::ClearContext(); if (ALC::Device != nullptr) { alcCloseDevice(ALC::Device); @@ -746,7 +621,7 @@ void AudioBackendOAL::Base_OnActiveDeviceChanged() } // Setup - ALC::RebuildContexts(true); + ALC::RebuildContext(true); } void AudioBackendOAL::Base_SetDopplerFactor(float value) @@ -756,9 +631,7 @@ void AudioBackendOAL::Base_SetDopplerFactor(float value) void AudioBackendOAL::Base_SetVolume(float value) { - ALC_FOR_EACH_CONTEXT() - alListenerf(AL_GAIN, value); - } + alListenerf(AL_GAIN, value); } bool AudioBackendOAL::Base_Init() @@ -862,7 +735,7 @@ bool AudioBackendOAL::Base_Init() int32 clampedIndex = Math::Clamp(activeDeviceIndex, -1, Audio::Devices.Count() - 1); if (clampedIndex == Audio::GetActiveDeviceIndex()) { - ALC::RebuildContexts(true); + ALC::RebuildContext(true); } Audio::SetActiveDeviceIndex(activeDeviceIndex); #ifdef AL_SOFT_source_spatialize diff --git a/Source/Engine/Audio/XAudio2/AudioBackendXAudio2.cpp b/Source/Engine/Audio/XAudio2/AudioBackendXAudio2.cpp index 7c16f05ca..37fab4f1d 100644 --- a/Source/Engine/Audio/XAudio2/AudioBackendXAudio2.cpp +++ b/Source/Engine/Audio/XAudio2/AudioBackendXAudio2.cpp @@ -218,9 +218,9 @@ namespace XAudio2 Source* GetSource(const AudioSource* source) { - if (source->SourceIDs.Count() == 0) + if (source->SourceID == 0) return nullptr; - const AUDIO_SOURCE_ID_TYPE sourceId = source->SourceIDs[0]; + const uint32 sourceId = source->SourceID; // 0 is invalid ID so shift them return &Sources[sourceId - 1]; } @@ -333,7 +333,7 @@ void AudioBackendXAudio2::Source_OnAdd(AudioSource* source) // Get first free source XAudio2::Source* aSource = nullptr; - AUDIO_SOURCE_ID_TYPE sourceID; + uint32 sourceID; for (int32 i = 0; i < XAudio2::Sources.Count(); i++) { if (XAudio2::Sources[i].IsFree()) @@ -377,8 +377,7 @@ void AudioBackendXAudio2::Source_OnAdd(AudioSource* source) if (FAILED(hr)) return; - sourceID++; // 0 is invalid ID so shift them - source->SourceIDs.Add(sourceID); + source->SourceID = sourceID + 1; // 0 is invalid ID so shift them // Prepare source state aSource->Callback.Source = source; diff --git a/Source/Engine/Engine/Units.h b/Source/Engine/Engine/Units.h index 6ddd7273e..1b1c0a55a 100644 --- a/Source/Engine/Engine/Units.h +++ b/Source/Engine/Engine/Units.h @@ -3,3 +3,5 @@ #pragma once #define METERS_TO_UNITS(meters) (meters * 100.0f) +#define UNITS_TO_METERS(units) (units * 0.01f) +#define UNITS_TO_METERS_SCALE 0.01f diff --git a/Source/Engine/Video/MF/VideoBackendMF.cpp b/Source/Engine/Video/MF/VideoBackendMF.cpp index 84715db45..6396c7379 100644 --- a/Source/Engine/Video/MF/VideoBackendMF.cpp +++ b/Source/Engine/Video/MF/VideoBackendMF.cpp @@ -254,16 +254,15 @@ namespace bufferStride = bufferCurrentLength; } - Span bufferSpan(bufferData, bufferStride); if (isVideo) { // Send pixels to the texture - player.UpdateVideoFrame(bufferSpan, frameTime, franeDuration); + player.UpdateVideoFrame(Span(bufferData, bufferStride), frameTime, franeDuration); } else if (isAudio) { // Send PCM data - player.UpdateAudioBuffer(bufferSpan, frameTime, franeDuration); + player.UpdateAudioBuffer(Span(bufferData, bufferStride), frameTime, franeDuration); } // Unlock sample buffer memory diff --git a/Source/Engine/Video/Types.h b/Source/Engine/Video/Types.h index ee03b0238..40ccb8e0f 100644 --- a/Source/Engine/Video/Types.h +++ b/Source/Engine/Video/Types.h @@ -2,11 +2,11 @@ #pragma once +#include "Engine/Core/Core.h" #include "Engine/Core/Types/BaseTypes.h" #include "Engine/Core/Types/TimeSpan.h" #include "Engine/Core/Types/DataContainer.h" #include "Engine/Audio/Types.h" -#include "Engine/Audio/Config.h" #include "Engine/Graphics/PixelFormat.h" class Video; @@ -35,8 +35,8 @@ struct VideoBackendPlayer TimeSpan AudioBufferTime, AudioBufferDuration; AudioDataInfo AudioInfo; BytesContainer VideoFrameMemory; - AUDIO_BUFFER_ID_TYPE AudioBuffer; - AUDIO_SOURCE_ID_TYPE AudioSource; + uint32 AudioBuffer; + uint32 AudioSource; class GPUUploadVideoFrameTask* UploadVideoFrameTask; uintptr BackendState[8]; diff --git a/Source/Engine/Video/VideoPlayer.h b/Source/Engine/Video/VideoPlayer.h index dd5fccd28..75416e36b 100644 --- a/Source/Engine/Video/VideoPlayer.h +++ b/Source/Engine/Video/VideoPlayer.h @@ -2,6 +2,7 @@ #pragma once +#include "Engine/Core/Math/Vector2.h" #include "Engine/Level/Actor.h" #include "Engine/Content/AssetReference.h" #include "Types.h"