1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-01 16:38:31 +00:00

Add priority to samples in a library

This commit is contained in:
Pedro J. Estébanez
2017-04-06 19:00:19 +02:00
parent 3916d964de
commit 9f8f8efa67
14 changed files with 116 additions and 17 deletions

View File

@@ -97,6 +97,7 @@ SpatialSoundServerSW::Source::Voice::Voice() {
active = false;
restart = false;
priority = 0;
pitch_scale = 1.0;
volume_scale = 0.0;
voice_rid = AudioServer::get_singleton()->voice_create();
@@ -390,7 +391,7 @@ void SpatialSoundServerSW::source_set_audio_stream(RID p_source, AudioServer::Au
} //null to unset
SpatialSoundServer::SourceVoiceID SpatialSoundServerSW::source_play_sample(RID p_source, RID p_sample, int p_mix_rate, int p_voice) {
SpatialSoundServer::SourceVoiceID SpatialSoundServerSW::source_play_sample(RID p_source, RID p_sample, int p_mix_rate, int p_voice, int p_priority) {
Source *source = source_owner.get(p_source);
ERR_FAIL_COND_V(!source, SOURCE_INVALID_VOICE);
@@ -400,23 +401,33 @@ SpatialSoundServer::SourceVoiceID SpatialSoundServerSW::source_play_sample(RID p
if (p_voice == SOURCE_NEXT_VOICE) {
const int num_voices = source->voices.size();
bool free_found = false;
int lowest_priority_voice = 0;
int lowest_priority = 0x7FFFFFFF;
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) {
const Source::Voice &v = source->voices[candidate];
if (!v.active && !v.restart) {
free_found = true;
to_play = candidate;
break;
}
if (v.priority < lowest_priority) {
lowest_priority = v.priority;
lowest_priority_voice = candidate;
}
}
if (!free_found)
to_play = (source->last_voice + 1) % num_voices;
} else
to_play = p_voice;
ERR_FAIL_INDEX_V(to_play, source->voices.size(), SOURCE_INVALID_VOICE);
if ((source->voices[to_play].active || source->voices[to_play].restart) && source->voices[to_play].priority > p_priority)
return SOURCE_INVALID_VOICE;
source->voices[to_play].restart = true;
source->voices[to_play].priority = p_priority;
source->voices[to_play].sample_rid = p_sample;
source->voices[to_play].sample_mix_rate = p_mix_rate;
source->voices[to_play].pitch_scale = 1;