Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sound volume when unfocused setting #14083

Merged
merged 10 commits into from
Dec 10, 2023
3 changes: 3 additions & 0 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@ bloom_radius (Bloom Radius) float 1 0.1 8
# Requires the sound system to be enabled.
sound_volume (Volume) float 0.8 0.0 1.0

# Volume multiplier when the window is unfocused.
sound_volume_unfocused (Volume when unfocused) float 0.3 0.0 1.0

# Whether to mute sounds. You can unmute sounds at any time, unless the
# sound system is disabled (enable_sound=false).
# In-game, you can toggle the mute state with the mute key or by using the
Expand Down
14 changes: 1 addition & 13 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_control_by_window(sound_manager.get(), device);

// Tell the sound maker whether to make footstep sounds
soundmaker->makes_footstep_sound = player->makes_footstep_sound;
Expand Down
25 changes: 24 additions & 1 deletion src/client/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include "log.h"
#include "porting.h"
#include "util/numeric.h"
#include <algorithm>
#include <string>
#include <vector>
#include "settings.h"
#include "util/numeric.h"

std::vector<std::string> SoundFallbackPathProvider::
getLocalFallbackPathsForSoundname(const std::string &name)
Expand Down Expand Up @@ -95,3 +96,25 @@ void ISoundManager::freeId(sound_handle_t id, u32 num_owners)
else
it->second -= num_owners;
}

void sound_control_by_window(ISoundManager *sound_mgr, irr::IrrlichtDevice *device) {
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 (!device->isWindowActive()) {
new_volume *= g_settings->getFloat("sound_volume_unfocused");
new_volume = rangelim(new_volume, 0.0f, 1.0f);
}

sound_mgr->setListenerGain(new_volume);
}
}
4 changes: 3 additions & 1 deletion src/client/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "irr_v3d.h"
#include "irrlichttypes_extrabloated.h"
Desour marked this conversation as resolved.
Show resolved Hide resolved
#include <limits>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -184,3 +184,5 @@ class DummySoundManager final : public ISoundManager
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 {}
};

void sound_control_by_window(ISoundManager *sound_mgr, irr::IrrlichtDevice *device);
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void set_default_settings()
settings->setDefault("address", "");
settings->setDefault("enable_sound", "true");
settings->setDefault("sound_volume", "0.8");
settings->setDefault("sound_volume_unfocused", "0.3");
settings->setDefault("mute_sound", "false");
settings->setDefault("enable_mesh_cache", "false");
settings->setDefault("mesh_generation_interval", "0");
Expand Down
4 changes: 3 additions & 1 deletion src/gui/guiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <ICameraSceneNode.h>
#include "client/renderingengine.h"
#include "scripting_mainmenu.h"
#include "util/numeric.h"
#include "config.h"
#include "version.h"
#include "porting.h"
Expand Down Expand Up @@ -296,6 +295,7 @@ void GUIEngine::run()
driver->endScene();

IrrlichtDevice *device = m_rendering_engine->get_raw_device();

u32 frametime_min = 1000 / (device->isWindowFocused()
? g_settings->getFloat("fps_max")
: g_settings->getFloat("fps_max_unfocused"));
Expand All @@ -310,6 +310,8 @@ void GUIEngine::run()

m_script->step();

sound_control_by_window(m_sound_manager.get(), device);

m_sound_manager->step(dtime);

#ifdef __ANDROID__
Expand Down