Skip to content

Commit

Permalink
Adapt existing audio code to make music over dialogs properly work
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolfering committed Sep 21, 2024
1 parent 00f5772 commit 04137ab
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 39 deletions.
22 changes: 20 additions & 2 deletions Source_Files/Misc/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};

Expand All @@ -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)) << "%";
Expand Down Expand Up @@ -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));
}
}


Expand Down
2 changes: 1 addition & 1 deletion Source_Files/Sound/MusicPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion Source_Files/Sound/MusicPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct MusicParameters {
class MusicPlayer : public AudioPlayer {
public:
MusicPlayer(std::shared_ptr<StreamDecoder> 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(); }
Expand Down
22 changes: 19 additions & 3 deletions Source_Files/Sound/OpenALManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MusicPlayer>(player) != nullptr)
{
player->is_sync_with_al_parameters = false;
}
}

SDL_UnlockAudio();
}

Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions Source_Files/Sound/OpenALManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct AudioParameters {
bool balance_rewind;
bool hrtf;
bool sounds_3d;
float volume;
float master_volume;
float music_volume;
};

class OpenALManager {
Expand All @@ -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; }
Expand All @@ -79,6 +82,7 @@ class OpenALManager {
OpenALManager(const AudioParameters& parameters);
~OpenALManager();
std::atomic<float> master_volume;
std::atomic<float> music_volume;
bool process_audio_active = false;
bool paused_audio = false;
uint32_t elapsed_pause_time = 0;
Expand All @@ -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<std::unique_ptr<AudioPlayer::AudioSource>> sources_pool;
std::deque<std::shared_ptr<AudioPlayer>> audio_players_queue; //for audio thread only
Expand Down
33 changes: 4 additions & 29 deletions Source_Files/Sound/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,17 @@ void SoundManager::Initialize(const Parameters& new_parameters)
initialized = true;
active = false;
SetParameters(new_parameters);
SetStatus(true);
}
}

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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -781,14 +756,14 @@ void SoundManager::SetStatus(bool active)
!(parameters.flags & _lower_restart_delay),
static_cast<bool>(parameters.flags & _hrtf_flag),
static_cast<bool>(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
Expand Down
1 change: 0 additions & 1 deletion Source_Files/Sound/SoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 04137ab

Please sign in to comment.