2
0
Эх сурвалжийг харах

Merge pull request #8776 from RandomShaper/fix-sample-priority-2.1

Fix priority in sample players (2.1)
Rémi Verschelde 8 жил өмнө
parent
commit
21bf3778d5

+ 17 - 5
scene/audio/sample_player.cpp

@@ -221,18 +221,30 @@ SamplePlayer::VoiceID SamplePlayer::play(const String &p_name, bool unique) {
 	last_check++;
 
 	const int num_voices = voices.size();
-	bool found = false;
+	bool free_found = false;
+	int lowest_priority_voice = -1;
+	int lowest_priority = 0x7FFFFFFF;
 	for (int i = 0; i < num_voices; i++) {
 		const int candidate = (last_id + 1 + i) % num_voices;
-		if (voices[candidate].priority <= priority) {
-			found = true;
+		const Voice &v = voices[candidate];
+		if (!(v.active && AudioServer::get_singleton()->voice_is_active(v.voice))) {
+			free_found = true;
 			last_id = candidate;
 			break;
 		}
+		if (v.priority <= lowest_priority) {
+			lowest_priority = v.priority;
+			lowest_priority_voice = candidate;
+		}
 	}
 
-	if (!found)
-		return INVALID_VOICE_ID;
+	if (!free_found) {
+		if (lowest_priority > priority) {
+			return INVALID_VOICE_ID;
+		} else {
+			last_id = lowest_priority_voice;
+		}
+	}
 
 	Voice &v = voices[last_id];
 	v.clear();

+ 2 - 2
servers/spatial_sound/spatial_sound_server_sw.cpp

@@ -402,7 +402,7 @@ 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_voice = -1;
 		int lowest_priority = 0x7FFFFFFF;
 		for (int i = 0; i < num_voices; i++) {
 			const int candidate = (source->last_voice + 1 + i) % num_voices;
@@ -418,7 +418,7 @@ SpatialSoundServer::SourceVoiceID SpatialSoundServerSW::source_play_sample(RID p
 			}
 		}
 		if (!free_found)
-			to_play = (source->last_voice + 1) % num_voices;
+			to_play = lowest_priority_voice;
 	} else
 		to_play = p_voice;
 

+ 2 - 2
servers/spatial_sound_2d/spatial_sound_2d_server_sw.cpp

@@ -399,7 +399,7 @@ SpatialSound2DServer::SourceVoiceID SpatialSound2DServerSW::source_play_sample(R
 	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_voice = -1;
 		int lowest_priority = 0x7FFFFFFF;
 		for (int i = 0; i < num_voices; i++) {
 			const int candidate = (source->last_voice + 1 + i) % num_voices;
@@ -415,7 +415,7 @@ SpatialSound2DServer::SourceVoiceID SpatialSound2DServerSW::source_play_sample(R
 			}
 		}
 		if (!free_found)
-			to_play = (source->last_voice + 1) % num_voices;
+			to_play = lowest_priority_voice;
 	} else
 		to_play = p_voice;