From 04137abcdfdc14446fac1050bc6acb848c5c5388 Mon Sep 17 00:00:00 2001 From: Kolfering Date: Sat, 21 Sep 2024 23:21:36 +0200 Subject: [PATCH] Adapt existing audio code to make music over dialogs properly work --- Source_Files/Misc/preferences.cpp | 22 +++++++++++++++++-- Source_Files/Sound/MusicPlayer.cpp | 2 +- Source_Files/Sound/MusicPlayer.h | 1 - Source_Files/Sound/OpenALManager.cpp | 22 ++++++++++++++++--- Source_Files/Sound/OpenALManager.h | 8 +++++-- Source_Files/Sound/SoundManager.cpp | 33 ++++------------------------ Source_Files/Sound/SoundManager.h | 1 - 7 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Source_Files/Misc/preferences.cpp b/Source_Files/Misc/preferences.cpp index 4042e2b93..01a4a0d57 100644 --- a/Source_Files/Misc/preferences.cpp +++ b/Source_Files/Misc/preferences.cpp @@ -1560,7 +1560,11 @@ class w_volume_slider : public w_percentage_slider { void item_selected(void) { - SoundManager::instance()->TestVolume((selection - 20) * 2, _snd_adjust_volume); + if (SoundManager::instance()->IsActive()) + { + OpenALManager::Get()->SetMasterVolume(SoundManager::From_db((selection - 20) * 2)); + SoundManager::instance()->PlaySound(_snd_adjust_volume, 0, NONE); + } } }; @@ -1570,6 +1574,14 @@ class w_music_slider : public w_slider { init_formatted_value(); } + void item_selected(void) + { + if (OpenALManager::Get()) + { + OpenALManager::Get()->SetMusicVolume(SoundManager::From_db((selection - 20) * 2)); + } + } + virtual std::string formatted_value() { std::ostringstream ss; ss << (selection * 200 / (num_items - 1)) << "%"; @@ -1709,11 +1721,17 @@ static void sound_dialog(void *arg) } if (changed) { -// set_sound_manager_parameters(sound_preferences); + bool is_music_playing = Music::instance()->Playing(Music::MusicSlot::Intro); SoundManager::instance()->SetParameters(*sound_preferences); write_preferences(); + if (is_music_playing) Music::instance()->RestartIntroMusic(); } } + else if (OpenALManager::Get()) + { + OpenALManager::Get()->SetMasterVolume(SoundManager::From_db(sound_preferences->volume_db)); + OpenALManager::Get()->SetMusicVolume(SoundManager::From_db(sound_preferences->music_db)); + } } diff --git a/Source_Files/Sound/MusicPlayer.cpp b/Source_Files/Sound/MusicPlayer.cpp index 1601a24de..1d4f6f210 100644 --- a/Source_Files/Sound/MusicPlayer.cpp +++ b/Source_Files/Sound/MusicPlayer.cpp @@ -16,7 +16,7 @@ int MusicPlayer::GetNextData(uint8* data, int length) { } SetupALResult MusicPlayer::SetUpALSourceIdle() { - float default_music_volume = default_volume * OpenALManager::Get()->GetMasterVolume(); + float default_music_volume = OpenALManager::Get()->GetMusicVolume() * OpenALManager::Get()->GetMasterVolume(); alSourcef(audio_source->source_id, AL_MAX_GAIN, default_music_volume); alSourcef(audio_source->source_id, AL_GAIN, default_music_volume * parameters.Get().volume); return SetupALResult(alGetError() == AL_NO_ERROR, true); diff --git a/Source_Files/Sound/MusicPlayer.h b/Source_Files/Sound/MusicPlayer.h index a916cfe7d..227e877c5 100644 --- a/Source_Files/Sound/MusicPlayer.h +++ b/Source_Files/Sound/MusicPlayer.h @@ -11,7 +11,6 @@ struct MusicParameters { class MusicPlayer : public AudioPlayer { public: MusicPlayer(std::shared_ptr decoder, MusicParameters parameters); //Must not be used outside OpenALManager (public for make_shared) - static void SetDefaultVolume(float volume) { default_volume = volume; } //Since we can only change global music volume in settings, we don't have to care about AL sync here float GetPriority() const override { return 5; } //Doesn't really matter, just be above maximum volume (1) to be prioritized over sounds void UpdateParameters(MusicParameters musicParameters) { parameters.Store(musicParameters); } MusicParameters GetParameters() const { return parameters.Get(); } diff --git a/Source_Files/Sound/OpenALManager.cpp b/Source_Files/Sound/OpenALManager.cpp index ea6238cb3..f6dea6cc9 100644 --- a/Source_Files/Sound/OpenALManager.cpp +++ b/Source_Files/Sound/OpenALManager.cpp @@ -107,9 +107,24 @@ void OpenALManager::SetMasterVolume(float volume) { ResyncPlayers(); } -void OpenALManager::ResyncPlayers() { +void OpenALManager::SetMusicVolume(float volume) { + volume = std::min(2.f, std::max(volume, 0.f)); + if (music_volume == volume) return; + music_volume = volume; + ResyncPlayers(true); +} + +void OpenALManager::ResyncPlayers(bool music_players_only) { SDL_LockAudio(); - for (auto& player : audio_players_queue) player->is_sync_with_al_parameters = false; + + for (auto& player : audio_players_queue) + { + if (!music_players_only || std::dynamic_pointer_cast(player) != nullptr) + { + player->is_sync_with_al_parameters = false; + } + } + SDL_UnlockAudio(); } @@ -424,7 +439,8 @@ int OpenALManager::GetBestOpenALSupportedFormat() { void OpenALManager::UpdateParameters(const AudioParameters& parameters) { audio_parameters = parameters; - master_volume = parameters.volume; + SetMasterVolume(parameters.master_volume); + SetMusicVolume(parameters.music_volume); } void OpenALManager::Shutdown() { diff --git a/Source_Files/Sound/OpenALManager.h b/Source_Files/Sound/OpenALManager.h index 4f13b4cf6..37683d32e 100644 --- a/Source_Files/Sound/OpenALManager.h +++ b/Source_Files/Sound/OpenALManager.h @@ -42,7 +42,8 @@ struct AudioParameters { bool balance_rewind; bool hrtf; bool sounds_3d; - float volume; + float master_volume; + float music_volume; }; class OpenALManager { @@ -61,7 +62,9 @@ class OpenALManager { void UpdateListener(world_location3d listener) { listener_location.Set(listener); } const world_location3d& GetListener() const { return listener_location.Get(); } void SetMasterVolume(float volume); + void SetMusicVolume(float volume); float GetMasterVolume() const { return master_volume.load(); } + float GetMusicVolume() const { return music_volume.load(); } void ToggleDeviceMode(bool recording_device); int GetFrequency() const { return audio_parameters.rate; } uint32_t GetElapsedPauseTime() const { return elapsed_pause_time; } @@ -79,6 +82,7 @@ class OpenALManager { OpenALManager(const AudioParameters& parameters); ~OpenALManager(); std::atomic master_volume; + std::atomic music_volume; bool process_audio_active = false; bool paused_audio = false; uint32_t elapsed_pause_time = 0; @@ -91,7 +95,7 @@ class OpenALManager { bool OpenDevice(); bool CloseDevice(); void ProcessAudioQueue(); - void ResyncPlayers(); + void ResyncPlayers(bool music_players_only = false); bool is_using_recording_device = false; std::queue> sources_pool; std::deque> audio_players_queue; //for audio thread only diff --git a/Source_Files/Sound/SoundManager.cpp b/Source_Files/Sound/SoundManager.cpp index 48f8a9e93..63c43fdff 100644 --- a/Source_Files/Sound/SoundManager.cpp +++ b/Source_Files/Sound/SoundManager.cpp @@ -159,7 +159,6 @@ void SoundManager::Initialize(const Parameters& new_parameters) initialized = true; active = false; SetParameters(new_parameters); - SetStatus(true); } } @@ -167,22 +166,10 @@ void SoundManager::SetParameters(const Parameters& parameters) { if (!initialized) return; - bool initial_state = active; - - // If it was initially on, turn off the sound manager - if (initial_state) - SetStatus(false); - - // We need to get rid of the sounds we have in memory - UnloadAllSounds(); - // Stuff in our new parameters this->parameters = parameters; this->parameters.Verify(); - - // If it was initially on, turn the sound manager back on - if (initial_state) - SetStatus(true); + SetStatus(true); } void SoundManager::Shutdown() @@ -251,18 +238,6 @@ bool SoundManager::AdjustVolumeDown(short sound_index) return false; } -void SoundManager::TestVolume(float db, short sound_index) -{ - if (active) - { - OpenALManager::Get()->SetMasterVolume(From_db(db)); - auto sound = PlaySound(sound_index, 0, NONE); - while (sound && sound->IsActive()) - yield(); - OpenALManager::Get()->SetMasterVolume(From_db(parameters.volume_db)); - } -} - bool SoundManager::LoadSound(short sound_index) { if (!active) return false; @@ -748,7 +723,7 @@ SoundManager::SoundManager() : active(false), initialized(false), sounds(new Sou void SoundManager::SetStatus(bool active) { - if (!initialized || active == this->active) return; + if (!initialized) return; if (active) { @@ -781,14 +756,14 @@ void SoundManager::SetStatus(bool active) !(parameters.flags & _lower_restart_delay), static_cast(parameters.flags & _hrtf_flag), static_cast(parameters.flags & _3d_sounds_flag), - From_db(parameters.volume_db) + From_db(parameters.volume_db), + From_db(parameters.music_db) }; bool success = OpenALManager::Init(audio_parameters); if (!success) return; - MusicPlayer::SetDefaultVolume(From_db(parameters.music_db, true)); OpenALManager::Get()->Start(); } else diff --git a/Source_Files/Sound/SoundManager.h b/Source_Files/Sound/SoundManager.h index c404b5be3..45f028bb7 100644 --- a/Source_Files/Sound/SoundManager.h +++ b/Source_Files/Sound/SoundManager.h @@ -60,7 +60,6 @@ class SoundManager bool AdjustVolumeUp(short sound_index = NONE); bool AdjustVolumeDown(short sound_index = NONE); - void TestVolume(float db, short sound_index); bool LoadSound(short sound); void LoadSounds(short *sounds, short count);