From ffa3a8931543244c414a6b53c3a264a6b4125716 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Mon, 22 Dec 2025 22:19:01 +0100 Subject: [PATCH] refactor(audio): Simplify volume related code in AudioManager and MilesAudioManager --- Core/GameEngine/Include/Common/GameAudio.h | 4 ++-- .../Source/Common/Audio/AudioEventRTS.cpp | 24 ++++++++----------- .../Source/Common/Audio/GameAudio.cpp | 18 +++++++------- .../MilesAudioDevice/MilesAudioManager.cpp | 18 +++++++++----- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Core/GameEngine/Include/Common/GameAudio.h b/Core/GameEngine/Include/Common/GameAudio.h index c88b9303ac..de19bb7fcc 100644 --- a/Core/GameEngine/Include/Common/GameAudio.h +++ b/Core/GameEngine/Include/Common/GameAudio.h @@ -105,7 +105,7 @@ enum local player affiliation, etc. (The entire list of checks is contained in shouldPlayLocally()). In addition, the world and unit audio are never allowed to exceed their footprint, as specified - in the audio settings INI file. In order to accomodate this, the audio uses an audio cache. The + in the audio settings INI file. In order to accommodate this, the audio uses an audio cache. The audio cache will attempt to load a sample, assuming there is enough room. If there is not enough room, then it goes through and finds any samples that are lower priority, and kills them until enough room is present for the sample. If it cannot free enough room, nothing happens to the @@ -190,7 +190,7 @@ class AudioManager : public SubsystemInterface virtual void closeDevice( void ) = 0; virtual void *getDevice( void ) = 0; - // Debice Dependent notification functions + // Device Dependent notification functions virtual void notifyOfAudioCompletion( UnsignedInt audioCompleted, UnsignedInt flags ) = 0; // Device Dependent enumerate providers functions. It is okay for there to be only 1 provider (Miles provides a maximum of 64. diff --git a/Core/GameEngine/Source/Common/Audio/AudioEventRTS.cpp b/Core/GameEngine/Source/Common/Audio/AudioEventRTS.cpp index eda40c71c7..c15c26a570 100644 --- a/Core/GameEngine/Source/Common/Audio/AudioEventRTS.cpp +++ b/Core/GameEngine/Source/Common/Audio/AudioEventRTS.cpp @@ -724,14 +724,13 @@ void AudioEventRTS::setVolume( Real vol ) //------------------------------------------------------------------------------------------------- const Coord3D *AudioEventRTS::getCurrentPosition( void ) { - if (m_ownerType == OT_Positional) + switch (m_ownerType) { + case OT_Positional: return &m_positionOfAudio; - } - else if (m_ownerType == OT_Object) - { - Object *obj = TheGameLogic->findObjectByID(m_objectID); - if (obj) + + case OT_Object: + if (Object *obj = TheGameLogic->findObjectByID(m_objectID)) { m_positionOfAudio.set( obj->getPosition() ); } @@ -740,11 +739,9 @@ const Coord3D *AudioEventRTS::getCurrentPosition( void ) m_ownerType = OT_Dead; } return &m_positionOfAudio; - } - else if (m_ownerType == OT_Drawable) - { - Drawable *draw = TheGameClient->findDrawableByID(m_drawableID); - if( draw ) + + case OT_Drawable: + if (Drawable *draw = TheGameClient->findDrawableByID(m_drawableID)) { m_positionOfAudio.set( draw->getPosition() ); } @@ -753,9 +750,8 @@ const Coord3D *AudioEventRTS::getCurrentPosition( void ) m_ownerType = OT_Dead; } return &m_positionOfAudio; - } - else if( m_ownerType == OT_Dead ) - { + + case OT_Dead: return &m_positionOfAudio; } diff --git a/Core/GameEngine/Source/Common/Audio/GameAudio.cpp b/Core/GameEngine/Source/Common/Audio/GameAudio.cpp index 8db82783b3..23c944fd95 100644 --- a/Core/GameEngine/Source/Common/Audio/GameAudio.cpp +++ b/Core/GameEngine/Source/Common/Audio/GameAudio.cpp @@ -419,7 +419,12 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd) } } - switch (eventToAdd->getAudioEventInfo()->m_soundType) + const AudioType soundType = eventToAdd->getAudioEventInfo()->m_soundType; + + // Check if audio type is on + // TheSuperHackers @info Zero audio volume is not a fail condition, because music, speech and sounds + // still need to be in flight in case the user raises the volume on runtime after the audio was already triggered. + switch (soundType) { case AT_Music: if (!isOn(AudioAffect_Music)) @@ -430,16 +435,14 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd) return AHSV_NoSound; break; case AT_Streaming: + // if we're currently playing uninterruptable speech, then disallow the addition of this sample + if (getDisallowSpeech()) + return AHSV_NoSound; if (!isOn(AudioAffect_Speech)) return AHSV_NoSound; break; } - // if we're currently playing uninterruptable speech, then disallow the addition of this sample - if (getDisallowSpeech() && eventToAdd->getAudioEventInfo()->m_soundType == AT_Streaming) { - return AHSV_NoSound; - } - if (!eventToAdd->getUninterruptable()) { if (!shouldPlayLocally(eventToAdd)) { return AHSV_NotForLocal; @@ -469,8 +472,7 @@ AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd) return AHSV_Muted; } - AudioType type = eventToAdd->getAudioEventInfo()->m_soundType; - if (type == AT_Music) + if (soundType == AT_Music) { m_music->addAudioEvent(audioEvent); } diff --git a/Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp index 973669ed6b..5a47735ca7 100644 --- a/Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp +++ b/Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp @@ -2677,25 +2677,29 @@ Bool MilesAudioManager::isOnScreen( const Coord3D *pos ) const //------------------------------------------------------------------------------------------------- Real MilesAudioManager::getEffectiveVolume(AudioEventRTS *event) const { - Real volume = 1.0f; - volume *= (event->getVolume() * event->getVolumeShift()); - if (event->getAudioEventInfo()->m_soundType == AT_Music) + Real volume = event->getVolume() * event->getVolumeShift(); + + switch (event->getAudioEventInfo()->m_soundType) + { + case AT_Music: { volume *= m_musicVolume; + break; } - else if (event->getAudioEventInfo()->m_soundType == AT_Streaming) + case AT_Streaming: { volume *= m_speechVolume; + break; } - else + case AT_SoundEffect: { if (event->isPositionalAudio()) { volume *= m_sound3DVolume; - Coord3D distance = m_listenerPosition; const Coord3D *pos = event->getCurrentPosition(); if (pos) { + Coord3D distance = m_listenerPosition; distance.sub(pos); Real objMinDistance; Real objMaxDistance; @@ -2730,6 +2734,8 @@ Real MilesAudioManager::getEffectiveVolume(AudioEventRTS *event) const { volume *= m_soundVolume; } + break; + } } return volume;