Selaa lähdekoodia

Merge pull request #99327 from colinator27/sync-bar-beats

Implement `AudioStreamSynchronized::get_bar_beats()`, fix division by zero
Thaddeus Crews 8 kuukautta sitten
vanhempi
commit
eb5103093c

+ 8 - 3
modules/interactive_music/audio_stream_interactive.cpp

@@ -691,9 +691,14 @@ void AudioStreamPlaybackInteractive::_queue(int p_to_clip_index, bool p_is_auto_
 				src_fade_wait = beat_sec - remainder;
 			} break;
 			case AudioStreamInteractive::TRANSITION_FROM_TIME_NEXT_BAR: {
-				float bar_sec = beat_sec * from_state.stream->get_bar_beats();
-				float remainder = Math::fmod(current_pos, bar_sec);
-				src_fade_wait = bar_sec - remainder;
+				if (from_state.stream->get_bar_beats() > 0) {
+					float bar_sec = beat_sec * from_state.stream->get_bar_beats();
+					float remainder = Math::fmod(current_pos, bar_sec);
+					src_fade_wait = bar_sec - remainder;
+				} else {
+					// Stream does not have a number of beats per bar - avoid NaN, and play immediately.
+					src_fade_wait = 0;
+				}
 			} break;
 			case AudioStreamInteractive::TRANSITION_FROM_TIME_END: {
 				float end = from_state.stream->get_beat_count() > 0 ? float(from_state.stream->get_beat_count() * beat_sec) : from_state.stream->get_length();

+ 12 - 0
modules/interactive_music/audio_stream_synchronized.cpp

@@ -99,6 +99,18 @@ int AudioStreamSynchronized::get_beat_count() const {
 	return max_beats;
 }
 
+int AudioStreamSynchronized::get_bar_beats() const {
+	for (int i = 0; i < stream_count; i++) {
+		if (audio_streams[i].is_valid()) {
+			int bar_beats = audio_streams[i]->get_bar_beats();
+			if (bar_beats != 0) {
+				return bar_beats;
+			}
+		}
+	}
+	return 0;
+}
+
 bool AudioStreamSynchronized::has_loop() const {
 	for (int i = 0; i < stream_count; i++) {
 		if (audio_streams[i].is_valid()) {

+ 1 - 0
modules/interactive_music/audio_stream_synchronized.h

@@ -54,6 +54,7 @@ private:
 public:
 	virtual double get_bpm() const override;
 	virtual int get_beat_count() const override;
+	virtual int get_bar_beats() const override;
 	virtual bool has_loop() const override;
 	void set_stream_count(int p_count);
 	int get_stream_count() const;