Skip to content

Commit d164443

Browse files
committed
Add ability to mute AudioServer.
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.
1 parent b6b3ec5 commit d164443

File tree

7 files changed

+365
-84
lines changed

7 files changed

+365
-84
lines changed

doc/classes/AudioServer.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@
182182
If [code]true[/code], the bus at index [code]bus_idx[/code] is in solo mode.
183183
</description>
184184
</method>
185+
<method name="is_enabled" qualifiers="const">
186+
<return type="bool" />
187+
<description>
188+
If [code]false[/code], the audio server is disabled/muted.
189+
</description>
190+
</method>
185191
<method name="lock">
186192
<return type="void" />
187193
<description>
@@ -276,6 +282,13 @@
276282
Sets the volume of the bus at index [code]bus_idx[/code] to [code]volume_db[/code].
277283
</description>
278284
</method>
285+
<method name="set_enabled">
286+
<return type="void" />
287+
<argument index="0" name="enabled" type="bool" />
288+
<description>
289+
If [code]true[/code], the audio server is enabled and can output or input audio. Setting this to [code]false[/code] will mute audio input/output and minimize audio CPU usage.
290+
</description>
291+
</method>
279292
<method name="swap_bus_effects">
280293
<return type="void" />
281294
<argument index="0" name="bus_idx" type="int" />

doc/classes/ProjectSettings.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@
301301
<member name="audio/mix_rate.web" type="int" setter="" getter="" default="0">
302302
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).
303303
</member>
304+
<member name="audio/muting/mute_driver" type="bool" setter="" getter="" default="false">
305+
If [code]true[/code], the current audio driver will be disabled, minimizing CPU usage. This will affect both audio output and input.
306+
[b]Note:[/b] The driver can be enabled and disabled at runtime using the [method AudioServer.set_enabled] function.
307+
</member>
308+
<member name="audio/muting/mute_on_focus_loss" type="bool" setter="" getter="" default="false">
309+
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.
310+
</member>
311+
<member name="audio/muting/mute_on_pause" type="bool" setter="" getter="" default="true">
312+
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.
313+
[b]Note:[/b] This only affects platforms where the OS can pause applications (typically Android and iOS). This does not refer to in-game pausing, where you can manually mute audio by calling [method AudioServer.set_enabled].
314+
</member>
315+
<member name="audio/muting/mute_on_silence" type="bool" setter="" getter="" default="false">
316+
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. This may result in temporarily higher audio latency when audio restarts after being muted.
317+
</member>
304318
<member name="audio/output_latency" type="int" setter="" getter="" default="15">
305319
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.
306320
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.

editor/editor_node.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6123,6 +6123,12 @@ EditorNode::EditorNode() {
61236123
EDITOR_DEF("interface/editor/update_vital_only", false);
61246124
#endif
61256125
EDITOR_DEF("interface/editor/localize_settings", true);
6126+
6127+
AudioServer::get_singleton()->set_enabled(!EDITOR_DEF_RST("interface/audio/muting/mute_driver", false));
6128+
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_SILENCE, EDITOR_DEF_RST("interface/audio/muting/mute_on_silence", true));
6129+
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_PAUSED, EDITOR_DEF_RST("interface/audio/muting/mute_on_pause", true));
6130+
AudioDriverManager::set_mute_sensitivity(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, EDITOR_DEF_RST("interface/audio/muting/mute_on_focus_loss", true));
6131+
61266132
EDITOR_DEF_RST("interface/scene_tabs/restore_scenes_on_load", false);
61276133
EDITOR_DEF_RST("interface/scene_tabs/show_thumbnail_on_hover", true);
61286134
EDITOR_DEF_RST("interface/inspector/default_property_name_style", EditorPropertyNameProcessor::STYLE_CAPITALIZED);

scene/main/scene_tree.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "scene/resources/mesh.h"
5050
#include "scene/resources/packed_scene.h"
5151
#include "scene/scene_string_names.h"
52+
#include "servers/audio_server.h"
5253
#include "servers/navigation_server.h"
5354
#include "servers/physics_2d_server.h"
5455
#include "servers/physics_server.h"
@@ -850,6 +851,8 @@ void SceneTree::_notification(int p_notification) {
850851
} break;
851852

852853
case NOTIFICATION_WM_FOCUS_IN: {
854+
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, false);
855+
853856
InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
854857
if (id) {
855858
id->ensure_touch_mouse_raised();
@@ -870,16 +873,25 @@ void SceneTree::_notification(int p_notification) {
870873
get_root()->propagate_notification(p_notification);
871874

872875
} break;
876+
case NOTIFICATION_WM_FOCUS_OUT: {
877+
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_FOCUS_LOSS, true);
878+
get_root()->propagate_notification(p_notification);
879+
} break;
880+
case NOTIFICATION_APP_PAUSED: {
881+
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_PAUSED, true);
882+
get_root()->propagate_notification(p_notification);
883+
} break;
884+
case NOTIFICATION_APP_RESUMED: {
885+
AudioDriverManager::set_mute_flag(AudioDriverManager::MUTE_FLAG_PAUSED, false);
886+
get_root()->propagate_notification(p_notification);
887+
} break;
873888

874889
case NOTIFICATION_OS_MEMORY_WARNING:
875890
case NOTIFICATION_OS_IME_UPDATE:
876891
case NOTIFICATION_WM_MOUSE_ENTER:
877892
case NOTIFICATION_WM_MOUSE_EXIT:
878-
case NOTIFICATION_WM_FOCUS_OUT:
879893
case NOTIFICATION_WM_ABOUT:
880-
case NOTIFICATION_CRASH:
881-
case NOTIFICATION_APP_RESUMED:
882-
case NOTIFICATION_APP_PAUSED: {
894+
case NOTIFICATION_CRASH: {
883895
get_root()->propagate_notification(p_notification);
884896
} break;
885897

servers/audio/audio_driver_dummy.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void AudioDriverDummy::finish() {
9898

9999
if (samples_in) {
100100
memdelete_arr(samples_in);
101+
samples_in = nullptr;
101102
};
102103
};
103104

0 commit comments

Comments
 (0)