mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-11 17:51:04 +00:00
Add sound volume when unfocused setting (#14083)
This adds a new setting to set sound volume multiplier when Minetest window is unfocused/inactive (sound_volume_unfocused, located in Settings > Graphics and Audio > Audio > Volume when unfocused). If the window is not focused, the sound volume will be multiplied by sound_volume_unfocused setting. The sound volume will be set back to sound_volume again when the window is focused.
This commit is contained in:
parent
321bcf5c44
commit
55fafb7d25
6 changed files with 38 additions and 14 deletions
|
@ -3209,19 +3209,7 @@ void Game::updateSound(f32 dtime)
|
|||
camera->getDirection(),
|
||||
camera->getCameraNode()->getUpVector());
|
||||
|
||||
bool mute_sound = g_settings->getBool("mute_sound");
|
||||
if (mute_sound) {
|
||||
sound_manager->setListenerGain(0.0f);
|
||||
} else {
|
||||
// Check if volume is in the proper range, else fix it.
|
||||
float old_volume = g_settings->getFloat("sound_volume");
|
||||
float new_volume = rangelim(old_volume, 0.0f, 1.0f);
|
||||
sound_manager->setListenerGain(new_volume);
|
||||
|
||||
if (old_volume != new_volume) {
|
||||
g_settings->setFloat("sound_volume", new_volume);
|
||||
}
|
||||
}
|
||||
sound_volume_control(sound_manager.get(), device->isWindowActive());
|
||||
|
||||
// Tell the sound maker whether to make footstep sounds
|
||||
soundmaker->makes_footstep_sound = player->makes_footstep_sound;
|
||||
|
|
|
@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "filesys.h"
|
||||
#include "log.h"
|
||||
#include "porting.h"
|
||||
#include "settings.h"
|
||||
#include "util/numeric.h"
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
@ -95,3 +96,26 @@ void ISoundManager::freeId(sound_handle_t id, u32 num_owners)
|
|||
else
|
||||
it->second -= num_owners;
|
||||
}
|
||||
|
||||
void sound_volume_control(ISoundManager *sound_mgr, bool is_window_active)
|
||||
{
|
||||
bool mute_sound = g_settings->getBool("mute_sound");
|
||||
if (mute_sound) {
|
||||
sound_mgr->setListenerGain(0.0f);
|
||||
} else {
|
||||
// Check if volume is in the proper range, else fix it.
|
||||
float old_volume = g_settings->getFloat("sound_volume");
|
||||
float new_volume = rangelim(old_volume, 0.0f, 1.0f);
|
||||
|
||||
if (old_volume != new_volume) {
|
||||
g_settings->setFloat("sound_volume", new_volume);
|
||||
}
|
||||
|
||||
if (!is_window_active) {
|
||||
new_volume *= g_settings->getFloat("sound_volume_unfocused");
|
||||
new_volume = rangelim(new_volume, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
sound_mgr->setListenerGain(new_volume);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -184,3 +184,9 @@ public:
|
|||
void fadeSound(sound_handle_t sound, f32 step, f32 target_gain) override {}
|
||||
void updateSoundPosVel(sound_handle_t sound, const v3f &pos, const v3f &vel) override {}
|
||||
};
|
||||
|
||||
/**
|
||||
* A helper function to control sound volume based on some values: sound volume
|
||||
* settings, mute sound setting, and window activity.
|
||||
*/
|
||||
void sound_volume_control(ISoundManager *sound_mgr, bool is_window_active);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue