Skip to content

Commit de3521e

Browse files
committed
Updated based on feedback from audio team meeting.
Removed the override flag, changed speaker_mode to allow taking a 'Default' value (to correspond to using the audio driver speaker_mode). Moved the setting to audio/general. Removed the channel remap settings from project settings, and made configuring them done through exposed methods on AudioServer (get/set_channel_remap).
1 parent 0559209 commit de3521e

File tree

4 files changed

+108
-72
lines changed

4 files changed

+108
-72
lines changed

doc/classes/AudioServer.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@
118118
[b]Note:[/b] The returned value is equivalent to the result of [method @GlobalScope.db_to_linear] on the result of [method get_bus_volume_db].
119119
</description>
120120
</method>
121+
<method name="get_channel_remap" qualifiers="const">
122+
<return type="int" />
123+
<param index="0" name="output_channel" type="int" />
124+
<description>
125+
Returns the mapping for the stereo channel index at [param output_channel]. Channel index values correspond to the following stereo channels:
126+
- [code]0[/code]: Front left and right
127+
- [code]1[/code]: Center and LFE (bass)
128+
- [code]2[/code]: Back left and right
129+
- [code]3[/code]: Side left and right
130+
- [code]-1[/code]: Muted
131+
The default is for the [param output_channel] to map to the same corresponding internal stereo channel.
132+
</description>
133+
</method>
121134
<method name="get_driver_name" qualifiers="const">
122135
<return type="String" />
123136
<description>
@@ -322,6 +335,19 @@
322335
[b]Note:[/b] Using this method is equivalent to calling [method set_bus_volume_db] with the result of [method @GlobalScope.linear_to_db] on a value.
323336
</description>
324337
</method>
338+
<method name="set_channel_remap">
339+
<return type="void" />
340+
<param index="0" name="output_channel" type="int" />
341+
<param index="1" name="source_channel" type="int" />
342+
<description>
343+
Given a stereo channel index [param output_channel], configures the [AudioServer] to map it to the internal channel [param source_channel]. Channel index values correspond to the following stereo channels:
344+
- [code]0[/code]: Front left and right
345+
- [code]1[/code]: Center and LFE (bass)
346+
- [code]2[/code]: Back left and right
347+
- [code]3[/code]: Side left and right
348+
Setting an output channel to an invalid source channel (e.g. [code]-1[/code]) will mute output from that channel. If the current [method get_speaker_mode] does not use a particular channel, mapping that source channel to an output channel will mute the output channel.
349+
</description>
350+
</method>
325351
<method name="set_enable_tagging_used_audio_streams">
326352
<return type="void" />
327353
<param index="0" name="enable" type="bool" />

doc/classes/ProjectSettings.xml

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -391,29 +391,6 @@
391391
<member name="audio/buses/channel_disable_time" type="float" setter="" getter="" default="2.0">
392392
Audio buses will disable automatically when sound goes below a given dB threshold for a given time. This saves CPU as effects assigned to that bus will no longer do any processing.
393393
</member>
394-
<member name="audio/buses/channel_override/override_channels" type="bool" setter="" getter="" default="false">
395-
If [code]true[/code], the number of audio channels processed by Godot is set via the Channel Override [code]speaker_mode[/code] instead of the speaker mode of the current audio device.
396-
[b]Note:[/b] This setting is intended for debugging, testing, and troubleshooting, and should not be enabled on production builds.
397-
</member>
398-
<member name="audio/buses/channel_override/remap_channel_0" type="int" setter="" getter="" default="0">
399-
Audio channel remapping index for the front left/right speakers (stereo channel 0). This causes the front speakers to play audio from the given stereo channel. Useful for testing different audio device configurations without needing specific hardware.
400-
[b]Note:[/b] Channel remapping is only enabled when [code]override_channels[/code] is true.
401-
</member>
402-
<member name="audio/buses/channel_override/remap_channel_1" type="int" setter="" getter="" default="1">
403-
Audio channel remapping index for the center speaker and LFE channel (stereo channel 1). This causes the center speaker (and LFE channel) to play audio from the given stereo channel. Useful for testing different audio device configurations without needing specific hardware.
404-
[b]Note:[/b] Channel remapping is only enabled when [code]override_channels[/code] is true.
405-
</member>
406-
<member name="audio/buses/channel_override/remap_channel_2" type="int" setter="" getter="" default="2">
407-
Audio channel remapping index for the rear left/right speakers (stereo channel 2). This causes the rear speakers to play audio from the given stereo channel. Useful for testing different audio device configurations without needing specific hardware.
408-
[b]Note:[/b] Channel remapping is only enabled when [code]override_channels[/code] is true.
409-
</member>
410-
<member name="audio/buses/channel_override/remap_channel_3" type="int" setter="" getter="" default="3">
411-
Audio channel remapping index for the side left/right speakers (stereo channel 3). This causes the side speakers to play audio from the given stereo channel. Useful for testing different audio device configurations without needing specific hardware.
412-
[b]Note:[/b] Channel remapping is only enabled when [code]override_channels[/code] is true.
413-
</member>
414-
<member name="audio/buses/channel_override/speaker_mode" type="int" setter="" getter="" default="0">
415-
The speaker mode used by Godot when [code]override_channels[/code] is true. Additional channels are muted (e.g. rear speaker channels when set to Stereo speaker mode 0).
416-
</member>
417394
<member name="audio/buses/default_bus_layout" type="String" setter="" getter="" default="&quot;res://default_bus_layout.tres&quot;">
418395
Default [AudioBusLayout] resource file to use in the project, unless overridden by the scene.
419396
</member>
@@ -468,6 +445,10 @@
468445
<member name="audio/general/ios/session_category" type="int" setter="" getter="" default="0">
469446
Sets the [url=https://developer.apple.com/documentation/avfaudio/avaudiosessioncategory]AVAudioSessionCategory[/url] on iOS. Use the [code]Playback[/code] category to get sound output, even if the phone is in silent mode.
470447
</member>
448+
<member name="audio/general/speaker_mode" type="int" setter="" getter="" default="-1">
449+
The speaker mode used internally by Godot. [code]Default[/code] will use the audio drivers speaker mode, while other values will override the internal speaker mode, and thus the number of audio channels, used by the [AudioServer].
450+
When the internal speaker mode differs from the drivers speaker mode, audio channels will be muted, ignored, or mapped to the audio drivers channels.
451+
</member>
471452
<member name="audio/general/text_to_speech" type="bool" setter="" getter="" default="false">
472453
If [code]true[/code], text-to-speech support is enabled on startup, otherwise it is enabled first time TTS method is used, see [method DisplayServer.tts_get_voices] and [method DisplayServer.tts_speak].
473454
[b]Note:[/b] Enabling TTS can cause addition idle CPU usage and interfere with the sleep mode, so consider disabling it if TTS is not used.

servers/audio_server.cpp

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
264264
#endif
265265

266266
if (channel_count != get_channel_count()) {
267-
// Amount of channels changed due to a output_device change, or override_channels/speaker_mode change
267+
// Amount of channels changed due to a output_device change, or speaker_mode change
268268
// reinitialize the buses channels and buffers
269269
init_channels_and_buffers();
270270
}
@@ -293,7 +293,7 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
293293
// The destination start for data will be the same in all cases.
294294
int32_t *dest = &p_buffer[from_buf * (cs * 2) + (k * 2)];
295295

296-
int rk = override_channels ? override_remap_channel[k] : k;
296+
int rk = channel_remap[k];
297297

298298
#ifdef DEBUG_ENABLED
299299
if (!debug_mute && rk < mcs && master->channels[rk].active) {
@@ -1506,36 +1506,38 @@ int AudioServer::get_driver_channel_count() const {
15061506
ERR_FAIL_V(1);
15071507
}
15081508

1509-
void AudioServer::_project_settings_changed() {
1510-
override_channels = GLOBAL_GET("audio/buses/channel_override/override_channels");
1511-
override_speaker_mode = (SpeakerMode) int(GLOBAL_GET("audio/buses/channel_override/speaker_mode"));
1512-
1513-
_get_configured_channel_remap();
1509+
void AudioServer::init_channel_remap() {
1510+
for (int i = 0; i < MAX_CHANNELS_PER_BUS; i++) {
1511+
channel_remap[i] = i;
1512+
}
15141513
}
15151514

1516-
void AudioServer::_get_configured_channel_remap() {
1517-
bool remap_changed = false;
1518-
for (int i = 0; i < MAX_CHANNELS_PER_BUS; i++) {
1519-
const String channel_key = vformat("audio/buses/channel_override/remap_channel_%d", i);
1520-
int channel = int(GLOBAL_GET(channel_key));
1521-
if (channel < 0 || channel > MAX_CHANNELS_PER_BUS) {
1522-
if (override_channels) {
1523-
WARN_PRINT(vformat("Invalid %s set to source channel %d. Muting channel.",
1524-
channel_key, channel));
1525-
}
1526-
// Source channels outside valid range will get muted.
1527-
remap_changed = remap_changed || override_remap_channel[i] != MAX_CHANNELS_PER_BUS;
1528-
override_remap_channel[i] = MAX_CHANNELS_PER_BUS;
1529-
} else {
1530-
remap_changed = remap_changed || override_remap_channel[i] != channel;
1531-
override_remap_channel[i] = channel;
1532-
}
1515+
void AudioServer::set_channel_remap(int p_output_channel, int p_source_channel) {
1516+
ERR_FAIL_INDEX_MSG(p_output_channel, MAX_CHANNELS_PER_BUS, "Invalid output channel, out of range");
1517+
bool remap_changed;
1518+
1519+
lock();
1520+
if (p_source_channel < 0 || p_source_channel > MAX_CHANNELS_PER_BUS) {
1521+
remap_changed = channel_remap[p_output_channel] != MAX_CHANNELS_PER_BUS;
1522+
1523+
// Source channels outside valid range will get muted.
1524+
channel_remap[p_output_channel] = MAX_CHANNELS_PER_BUS;
1525+
} else {
1526+
remap_changed = channel_remap[p_output_channel] != p_source_channel;
1527+
channel_remap[p_output_channel] = p_source_channel;
15331528
}
1534-
if (remap_changed && override_channels) {
1535-
print_verbose(vformat("AudioServer: override channel remap: [%d, %d, %d, %d]", override_remap_channel[0], override_remap_channel[1], override_remap_channel[2], override_remap_channel[3]))
1529+
unlock();
1530+
1531+
if (remap_changed) {
1532+
print_verbose(vformat("AudioServer: channel remap changed: output channel [%d] -> source channel [%d]", p_output_channel, channel_remap[p_output_channel]))
15361533
}
15371534
}
15381535

1536+
int AudioServer::get_channel_remap(int p_output_channel) const {
1537+
ERR_FAIL_INDEX_V_MSG(p_output_channel, MAX_CHANNELS_PER_BUS, -1, "Invalid output channel, out of range");
1538+
return channel_remap[p_output_channel] == MAX_CHANNELS_PER_BUS ? -1 : channel_remap[p_output_channel];
1539+
}
1540+
15391541
void AudioServer::notify_listener_changed() {
15401542
for (CallbackItem *ci : listener_changed_callback_list) {
15411543
ci->callback(ci->userdata);
@@ -1545,11 +1547,7 @@ void AudioServer::notify_listener_changed() {
15451547
void AudioServer::init_channels_and_buffers() {
15461548
channel_count = get_channel_count();
15471549

1548-
if (override_channels) {
1549-
print_verbose(vformat("AudioServer: initializing %d override stereo channel(s), detected %d device stereo channel(s)", channel_count, get_driver_channel_count()));
1550-
} else {
1551-
print_verbose(vformat("AudioServer: initializing %d stereo channel(s)", channel_count));
1552-
}
1550+
print_verbose(vformat("AudioServer: Speaker mode set to %s, initializing %d stereo channel(s)", _get_speaker_mode_name(), channel_count));
15531551

15541552
temp_buffer.resize(channel_count);
15551553
mix_buffer.resize(buffer_size + LOOKAHEAD_BUFFER_SIZE);
@@ -1568,17 +1566,11 @@ void AudioServer::init_channels_and_buffers() {
15681566
}
15691567

15701568
void AudioServer::init() {
1571-
channel_disable_threshold_db = GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_threshold_db", PROPERTY_HINT_RANGE, "-80,0,0.1,suffix:dB"), -60.0);
1569+
channel_disable_threshold_db = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_threshold_db", PROPERTY_HINT_RANGE, "-80,0,0.1,suffix:dB"), -60.0);
15721570
channel_disable_frames = float(GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 2.0)) * get_mix_rate();
15731571

1574-
override_channels = GLOBAL_DEF("audio/buses/channel_override/override_channels", false);
1575-
override_speaker_mode = (SpeakerMode) int(GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/buses/channel_override/speaker_mode", PROPERTY_HINT_ENUM, "Stereo,Surround 3.1,Surround 5.1,Surround 7.1"), SpeakerMode::SPEAKER_MODE_STEREO));
1576-
1577-
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/buses/channel_override/remap_channel_0", PROPERTY_HINT_ENUM, "Front Left/Right,Center/LFE,Rear Left/Right,Side Left/Right,Muted"), 0);
1578-
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/buses/channel_override/remap_channel_1", PROPERTY_HINT_ENUM, "Front Left/Right,Center/LFE,Rear Left/Right,Side Left/Right,Muted"), 1);
1579-
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/buses/channel_override/remap_channel_2", PROPERTY_HINT_ENUM, "Front Left/Right,Center/LFE,Rear Left/Right,Side Left/Right,Muted"), 2);
1580-
GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/buses/channel_override/remap_channel_3", PROPERTY_HINT_ENUM, "Front Left/Right,Center/LFE,Rear Left/Right,Side Left/Right,Muted"), 3);
1581-
_get_configured_channel_remap();
1572+
speaker_mode_config = GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/speaker_mode", PROPERTY_HINT_ENUM, "Default:-1,Stereo:0,Surround 3.1:1,Surround 5.1:2,Surround 7.1:3"), -1);
1573+
init_channel_remap();
15821574

15831575
// TODO: Buffer size is hardcoded for now. This would be really nice to have as a project setting because currently it limits audio latency to an absolute minimum of 11ms with default mix rate, but there's some additional work required to make that happen. See TODOs in `_mix_step_for_channel`.
15841576
// When this becomes a project setting, it should be specified in milliseconds rather than raw sample count, because 512 samples at 192khz is shorter than it is at 48khz, for example.
@@ -1601,7 +1593,14 @@ void AudioServer::init() {
16011593

16021594
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/video/video_delay_compensation_ms", PROPERTY_HINT_RANGE, "-1000,1000,1,suffix:ms"), 0);
16031595

1604-
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &AudioServer::_project_settings_changed));
1596+
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &AudioServer::project_settings_changed));
1597+
}
1598+
1599+
void AudioServer::project_settings_changed() {
1600+
lock();
1601+
channel_disable_threshold_db = GLOBAL_GET("audio/buses/channel_disable_threshold_db");
1602+
speaker_mode_config = GLOBAL_GET("audio/general/speaker_mode");
1603+
unlock();
16051604
}
16061605

16071606
void AudioServer::update() {
@@ -1724,10 +1723,33 @@ void AudioServer::unlock() {
17241723
}
17251724

17261725
AudioServer::SpeakerMode AudioServer::get_speaker_mode() const {
1727-
if (override_channels) {
1728-
return override_speaker_mode;
1726+
switch (speaker_mode_config) {
1727+
case 0:
1728+
return SPEAKER_MODE_STEREO;
1729+
case 1:
1730+
return SPEAKER_SURROUND_31;
1731+
case 2:
1732+
return SPEAKER_SURROUND_51;
1733+
case 3:
1734+
return SPEAKER_SURROUND_71;
1735+
default:
1736+
return get_driver_speaker_mode();
1737+
}
1738+
}
1739+
1740+
String AudioServer::_get_speaker_mode_name() const {
1741+
switch (speaker_mode_config) {
1742+
case 0:
1743+
return "Stereo";
1744+
case 1:
1745+
return "Surround 3.1";
1746+
case 2:
1747+
return "Surround 5.1";
1748+
case 3:
1749+
return "Surround 7.1";
1750+
default:
1751+
return "Default";
17291752
}
1730-
return get_driver_speaker_mode();
17311753
}
17321754

17331755
float AudioServer::get_mix_rate() const {
@@ -2062,6 +2084,9 @@ void AudioServer::_bind_methods() {
20622084
ClassDB::bind_method(D_METHOD("set_bus_effect_enabled", "bus_idx", "effect_idx", "enabled"), &AudioServer::set_bus_effect_enabled);
20632085
ClassDB::bind_method(D_METHOD("is_bus_effect_enabled", "bus_idx", "effect_idx"), &AudioServer::is_bus_effect_enabled);
20642086

2087+
ClassDB::bind_method(D_METHOD("set_channel_remap", "output_channel", "source_channel"), &AudioServer::set_channel_remap);
2088+
ClassDB::bind_method(D_METHOD("get_channel_remap", "output_channel"), &AudioServer::get_channel_remap);
2089+
20652090
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
20662091
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
20672092

servers/audio_server.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ class AudioServer : public Object {
224224
int channel_count = 0;
225225
int to_mix = 0;
226226

227-
bool override_channels = false;
228-
SpeakerMode override_speaker_mode = SPEAKER_MODE_STEREO;
229-
int override_remap_channel[MAX_CHANNELS_PER_BUS];
227+
int speaker_mode_config = -1;
228+
int channel_remap[MAX_CHANNELS_PER_BUS];
230229

231230
float playback_speed_scale = 1.0f;
232231

@@ -326,10 +325,12 @@ class AudioServer : public Object {
326325

327326
void _update_bus_effects(int p_bus);
328327

329-
void _get_configured_channel_remap();
328+
String _get_speaker_mode_name() const;
330329

331330
static AudioServer *singleton;
332331

332+
void init_channel_remap();
333+
333334
void init_channels_and_buffers();
334335

335336
void _mix_step();
@@ -381,7 +382,7 @@ class AudioServer : public Object {
381382
bool get_debug_mute() const;
382383
#endif // DEBUG_ENABLED
383384

384-
void _project_settings_changed();
385+
void project_settings_changed();
385386

386387
void set_bus_count(int p_count);
387388
int get_bus_count() const;
@@ -432,6 +433,9 @@ class AudioServer : public Object {
432433

433434
bool is_bus_channel_active(int p_bus, int p_channel) const;
434435

436+
void set_channel_remap(int p_output_channel, int p_source_channel);
437+
int get_channel_remap(int p_output_channel) const;
438+
435439
void set_playback_speed_scale(float p_scale);
436440
float get_playback_speed_scale() const;
437441

0 commit comments

Comments
 (0)