From 0a4dd371b7a2b8d6a832e7207906b88e9649fc08 Mon Sep 17 00:00:00 2001 From: colinator27 <17358554+colinator27@users.noreply.github.com> Date: Sat, 16 Nov 2024 11:41:35 -0500 Subject: [PATCH] Implement get_bar_beats() for AudioStreamSynchronized, fix division by zero --- .../interactive_music/audio_stream_interactive.cpp | 11 ++++++++--- .../interactive_music/audio_stream_synchronized.cpp | 12 ++++++++++++ .../interactive_music/audio_stream_synchronized.h | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/interactive_music/audio_stream_interactive.cpp b/modules/interactive_music/audio_stream_interactive.cpp index 8656be988d98..995fa15015d2 100644 --- a/modules/interactive_music/audio_stream_interactive.cpp +++ b/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(); diff --git a/modules/interactive_music/audio_stream_synchronized.cpp b/modules/interactive_music/audio_stream_synchronized.cpp index e38a57ba753a..ad67e778eec2 100644 --- a/modules/interactive_music/audio_stream_synchronized.cpp +++ b/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()) { diff --git a/modules/interactive_music/audio_stream_synchronized.h b/modules/interactive_music/audio_stream_synchronized.h index a2d8c5540443..83798a13bd5b 100644 --- a/modules/interactive_music/audio_stream_synchronized.h +++ b/modules/interactive_music/audio_stream_synchronized.h @@ -54,6 +54,7 @@ class AudioStreamSynchronized : public AudioStream { 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;