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 a project setting to mute audio on focus loss #76987

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented May 12, 2023

This is disabled by default to keep expectations from the current Godot version intact.

However, most games should consider enabling this option by default as it's common behavior on most PC games (while providing a way for users to disable it).

Tested on Linux, it works as expected including when using popups (which spawn separate windows). However, pressing Alt + F9 to minimize the window (custom KDE shortcut) will not send a focus loss notification, so audio won't be muted in this case. This is an issue with DisplayServer, as minimizing by clicking the - button in the top-right corner sends a focus loss notification as expected.

Window may not be the best location for this code, but I'm not sure what would be a more suited location.

Testing project: test_mute_on_focus_loss.zip

TODO

  • Add an editor setting to control this behavior on the editor itself (right now, it uses the project setting's value).

This is disabled by default to keep expectations from the
current Godot version intact.

However, most games should consider enabling this option by default
as it's common behavior on most PC games (while providing a way for
users to disable it).
@Calinou Calinou requested review from a team as code owners May 12, 2023 04:06
@Calinou Calinou added this to the 4.x milestone May 12, 2023
Comment on lines +658 to +661
// mute_on_focus_loss_is_tweening = true;
// Ref<Tween> tween = get_tree()->create_tween();
// AudioServer::get_singleton()->AudioServer::set_bus_mute(0, false);
// tween->tween_method(callable_mp(this, &Window::_smooth_audio_mute_callback), -80, mute_on_focus_loss_previous_db, 0.5);
Copy link
Member Author

@Calinou Calinou May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to smoothly fade the transition and left a WIP as commented code. While this code works with a single window, I couldn't figure out a way to have the tweens not "fight" each other when both are triggered at the same time (due to popups creating "focus in" notifications when the main window gets a "focus out").

Checking only for window_id == DisplayServer::MAIN_WINDOW_ID mutes audio as soon as you open a popup, which shouldn't occur.

@RandomShaper
Copy link
Member

This is disabled by default to keep expectations from the current Godot version intact.

I guess we can afford in 4.1, making it clear in the release notes, can't we?

@Calinou
Copy link
Member Author

Calinou commented May 12, 2023

I guess we can afford in 4.1, making it clear in the release notes, can't we?

Another option is to enable it only in projects created with Godot 4.1 or later, using the approach described in godotengine/godot-proposals#4834.

@RandomShaper
Copy link
Member

I guess we can afford in 4.1, making it clear in the release notes, can't we?

Another option is to enable it only in projects created with Godot 4.1 or later, using the approach described in godotengine/godot-proposals#4834.

That makes sense. It wouldn't be the first time we tweak the default project settings to roll out a different default without affecting existing projects.

@akien-mga
Copy link
Member

akien-mga commented May 12, 2023

I'm not sure this should be default, not for compatibility reasons, but because I'm not sure this is actually something that most games use.

I don't recall seeing this much when playing, at least with games in windowed mode, where changing focus can be common and you typically still hear the game audio when doing so (games might sometimes auto-pause when losing focus, but still play the pause menu music).

I don't have hard data on that, but I think this needs querying the community more widely than the handful of upvotes on the current proposal.

Tested a few games I have installed locally:

Mutes on focus loss:

  • A Short Hike
  • Battle Chasers: Nightwar
  • This War of Mine

Doesn't mute on focus loss:

  • Barony
  • Children of Morta
  • Darkest Dungeon
  • Don't Starve
  • Hollow Knight
  • Mindustry
  • Northgard

For the few I tested, the games which do mute on focus loss don't actually mute - they pause. Any processing is stopped, and the audio isn't just silenced, but will restart where it stopped. Game logic / menu animations are paused too, and even loading levels tends to be paused.

Needs more data, but it seems to me that this only does part of the work and isn't actually implementing something standard in games.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a project setting to mute audio when the project window is unfocused
3 participants