diff options
| author | Pedro J. Estébanez | 2017-04-06 19:00:19 +0200 |
|---|---|---|
| committer | Pedro J. Estébanez | 2017-04-06 20:11:26 +0200 |
| commit | 0501f3a901ab000b6e02e95ee56369be9513a3bf (patch) | |
| tree | dd61fb455224e8edec1fb8e288c10afde783bfa7 | |
| parent | 12749dd67a4fc172ad69932ad5ac6310a9b92444 (diff) | |
| download | godot-0501f3a901ab000b6e02e95ee56369be9513a3bf.tar.gz godot-0501f3a901ab000b6e02e95ee56369be9513a3bf.tar.zst godot-0501f3a901ab000b6e02e95ee56369be9513a3bf.zip | |
Make spatial AudioServers prefer inactive voices
instead of unconditionally playing on the next voice slot; that will be the fallback if no inactive voice is found
| -rw-r--r-- | servers/spatial_sound/spatial_sound_server_sw.cpp | 15 | ||||
| -rw-r--r-- | servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp | 15 |
2 files changed, 24 insertions, 6 deletions
diff --git a/servers/spatial_sound/spatial_sound_server_sw.cpp b/servers/spatial_sound/spatial_sound_server_sw.cpp index 6a10b175e..b6021d896 100644 --- a/servers/spatial_sound/spatial_sound_server_sw.cpp +++ b/servers/spatial_sound/spatial_sound_server_sw.cpp @@ -398,9 +398,18 @@ SpatialSoundServer::SourceVoiceID SpatialSoundServerSW::source_play_sample(RID p int to_play = 0; if (p_voice == SOURCE_NEXT_VOICE) { - to_play = source->last_voice + 1; - if (to_play >= source->voices.size()) - to_play = 0; + const int num_voices = source->voices.size(); + bool free_found = false; + for (int i = 0; i < num_voices; i++) { + const int candidate = (source->last_voice + 1 + i) % num_voices; + if (!source->voices[candidate].active && !source->voices[candidate].restart) { + free_found = true; + to_play = candidate; + break; + } + } + if (!free_found) + to_play = (source->last_voice + 1) % num_voices; } else to_play = p_voice; diff --git a/servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp b/servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp index 2c6614da8..f9ec253c0 100644 --- a/servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp +++ b/servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp @@ -395,9 +395,18 @@ SpatialSound2DServer::SourceVoiceID SpatialSound2DServerSW::source_play_sample(R int to_play = 0; if (p_voice == SOURCE_NEXT_VOICE) { - to_play = source->last_voice + 1; - if (to_play >= source->voices.size()) - to_play = 0; + const int num_voices = source->voices.size(); + bool free_found = false; + for (int i = 0; i < num_voices; i++) { + const int candidate = (source->last_voice + 1 + i) % num_voices; + if (!source->voices[candidate].active && !source->voices[candidate].restart) { + free_found = true; + to_play = candidate; + break; + } + } + if (!free_found) + to_play = (source->last_voice + 1) % num_voices; } else to_play = p_voice; |
