Skip to content

Commit eb51030

Browse files
committed
Merge pull request #99327 from colinator27/sync-bar-beats
Implement `AudioStreamSynchronized::get_bar_beats()`, fix division by zero
2 parents 4b91e98 + 0a4dd37 commit eb51030

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

modules/interactive_music/audio_stream_interactive.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,14 @@ void AudioStreamPlaybackInteractive::_queue(int p_to_clip_index, bool p_is_auto_
691691
src_fade_wait = beat_sec - remainder;
692692
} break;
693693
case AudioStreamInteractive::TRANSITION_FROM_TIME_NEXT_BAR: {
694-
float bar_sec = beat_sec * from_state.stream->get_bar_beats();
695-
float remainder = Math::fmod(current_pos, bar_sec);
696-
src_fade_wait = bar_sec - remainder;
694+
if (from_state.stream->get_bar_beats() > 0) {
695+
float bar_sec = beat_sec * from_state.stream->get_bar_beats();
696+
float remainder = Math::fmod(current_pos, bar_sec);
697+
src_fade_wait = bar_sec - remainder;
698+
} else {
699+
// Stream does not have a number of beats per bar - avoid NaN, and play immediately.
700+
src_fade_wait = 0;
701+
}
697702
} break;
698703
case AudioStreamInteractive::TRANSITION_FROM_TIME_END: {
699704
float end = from_state.stream->get_beat_count() > 0 ? float(from_state.stream->get_beat_count() * beat_sec) : from_state.stream->get_length();

modules/interactive_music/audio_stream_synchronized.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ int AudioStreamSynchronized::get_beat_count() const {
9999
return max_beats;
100100
}
101101

102+
int AudioStreamSynchronized::get_bar_beats() const {
103+
for (int i = 0; i < stream_count; i++) {
104+
if (audio_streams[i].is_valid()) {
105+
int bar_beats = audio_streams[i]->get_bar_beats();
106+
if (bar_beats != 0) {
107+
return bar_beats;
108+
}
109+
}
110+
}
111+
return 0;
112+
}
113+
102114
bool AudioStreamSynchronized::has_loop() const {
103115
for (int i = 0; i < stream_count; i++) {
104116
if (audio_streams[i].is_valid()) {

modules/interactive_music/audio_stream_synchronized.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class AudioStreamSynchronized : public AudioStream {
5454
public:
5555
virtual double get_bpm() const override;
5656
virtual int get_beat_count() const override;
57+
virtual int get_bar_beats() const override;
5758
virtual bool has_loop() const override;
5859
void set_stream_count(int p_count);
5960
int get_stream_count() const;

0 commit comments

Comments
 (0)