Skip to content

Commit

Permalink
Add ability to mute AudioServer.
Browse files Browse the repository at this point in the history
Adds the option to change the audio driver to the Dummy driver and back at runtime, with a set of MuteState flags - Disabled (user control), Silence (period of silence), Focus Loss (when app is not in focus), and Paused (when app is paused).

Control for the flags is added for the editor in EditorSettings, and for the project in ProjectSettings.

Editor defaults to muted (Dummy driver) when there is no audio output, and automatically switches to active on output. This significantly reduces CPU usage.
  • Loading branch information
lawnjelly committed Mar 30, 2023
1 parent 7722461 commit 4a5d495
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 83 deletions.
13 changes: 13 additions & 0 deletions doc/classes/AudioServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@
If [code]true[/code], the bus at index [code]bus_idx[/code] is in solo mode.
</description>
</method>
<method name="is_enabled" qualifiers="const">
<return type="bool" />
<description>
If [code]false[/code], the audio server is disabled / muted.
</description>
</method>
<method name="lock">
<return type="void" />
<description>
Expand Down Expand Up @@ -276,6 +282,13 @@
Sets the volume of the bus at index [code]bus_idx[/code] to [code]volume_db[/code].
</description>
</method>
<method name="set_enabled">
<return type="void" />
<argument index="0" name="enabled" type="bool" />
<description>
Allows disabling / muting the audio server. As well as muting, this will minimize audio CPU usage.
</description>
</method>
<method name="swap_bus_effects">
<return type="void" />
<argument index="0" name="bus_idx" type="int" />
Expand Down
13 changes: 13 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@
<member name="audio/mix_rate.web" type="int" setter="" getter="" default="0">
Safer override for [member audio/mix_rate] in the Web platform. Here [code]0[/code] means "let the browser choose" (since some browsers do not like forcing the mix rate).
</member>
<member name="audio/muting/mute_driver" type="bool" setter="" getter="" default="false">
If [code]true[/code], the current audio driver will be disabled, minimizing CPU usage. This will affect both audio output and input.
[b]Note:[/b] The driver can be enabled and disabled at runtime using the [method AudioServer.set_enabled] function.
</member>
<member name="audio/muting/mute_on_focus_loss" type="bool" setter="" getter="" default="false">
If [code]true[/code], the current audio driver will be automatically disabled when the application loses focus, and re-enabled when the application regains focus.
</member>
<member name="audio/muting/mute_on_pause" type="bool" setter="" getter="" default="true">
If [code]true[/code], the current audio driver will be automatically disabled when the application is paused by the OS, and re-enabled when the application is resumed.
</member>
<member name="audio/muting/mute_on_silence" type="bool" setter="" getter="" default="false">
If [code]true[/code], the current audio driver will be automatically disabled after a period of silence is detected, and re-enabled when audio attempts to play.
</member>
<member name="audio/output_latency" type="int" setter="" getter="" default="15">
Specifies the preferred output latency in milliseconds for audio. Lower values will result in lower audio latency at the cost of increased CPU usage. Low values may result in audible cracking on slower hardware.
Audio output latency may be constrained by the host operating system and audio hardware drivers. If the host can not provide the specified audio output latency then Godot will attempt to use the nearest latency allowed by the host. As such you should always use [method AudioServer.get_output_latency] to determine the actual audio output latency.
Expand Down
6 changes: 6 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6048,6 +6048,12 @@ EditorNode::EditorNode() {
EDITOR_DEF("interface/editor/update_continuously", false);
EDITOR_DEF("interface/editor/update_vital_only", false);
EDITOR_DEF("interface/editor/localize_settings", true);

AudioServer::get_singleton()->set_enabled(!EDITOR_DEF_RST("interface/audio/muting/mute_driver", false));
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_SILENCE, EDITOR_DEF_RST("interface/audio/muting/mute_on_silence", true));
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_PAUSED, EDITOR_DEF_RST("interface/audio/muting/mute_on_pause", true));
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, EDITOR_DEF_RST("interface/audio/muting/mute_on_focus_loss", true));

EDITOR_DEF_RST("interface/scene_tabs/restore_scenes_on_load", false);
EDITOR_DEF_RST("interface/scene_tabs/show_thumbnail_on_hover", true);
EDITOR_DEF_RST("interface/inspector/default_property_name_style", EditorPropertyNameProcessor::STYLE_CAPITALIZED);
Expand Down
20 changes: 16 additions & 4 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "scene/resources/mesh.h"
#include "scene/resources/packed_scene.h"
#include "scene/scene_string_names.h"
#include "servers/audio_server.h"
#include "servers/navigation_server.h"
#include "servers/physics_2d_server.h"
#include "servers/physics_server.h"
Expand Down Expand Up @@ -819,6 +820,8 @@ void SceneTree::_notification(int p_notification) {
} break;

case NOTIFICATION_WM_FOCUS_IN: {
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, false);

InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
if (id) {
id->ensure_touch_mouse_raised();
Expand All @@ -839,16 +842,25 @@ void SceneTree::_notification(int p_notification) {
get_root()->propagate_notification(p_notification);

} break;
case NOTIFICATION_WM_FOCUS_OUT: {
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, true);
get_root()->propagate_notification(p_notification);
} break;
case NOTIFICATION_APP_PAUSED: {
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_PAUSED, true);
get_root()->propagate_notification(p_notification);
} break;
case NOTIFICATION_APP_RESUMED: {
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_PAUSED, false);
get_root()->propagate_notification(p_notification);
} break;

case NOTIFICATION_OS_MEMORY_WARNING:
case NOTIFICATION_OS_IME_UPDATE:
case NOTIFICATION_WM_MOUSE_ENTER:
case NOTIFICATION_WM_MOUSE_EXIT:
case NOTIFICATION_WM_FOCUS_OUT:
case NOTIFICATION_WM_ABOUT:
case NOTIFICATION_CRASH:
case NOTIFICATION_APP_RESUMED:
case NOTIFICATION_APP_PAUSED: {
case NOTIFICATION_CRASH: {
get_root()->propagate_notification(p_notification);
} break;

Expand Down
1 change: 1 addition & 0 deletions servers/audio/audio_driver_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void AudioDriverDummy::finish() {

if (samples_in) {
memdelete_arr(samples_in);
samples_in = nullptr;
};
};

Expand Down
Loading

0 comments on commit 4a5d495

Please sign in to comment.