Skip to content

Commit

Permalink
Merge pull request godotengine#99327 from colinator27/sync-bar-beats
Browse files Browse the repository at this point in the history
Implement `AudioStreamSynchronized::get_bar_beats()`, fix division by zero
  • Loading branch information
Repiteo committed Dec 5, 2024
2 parents 4b91e98 + 0a4dd37 commit eb51030
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/interactive_music/audio_stream_interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions modules/interactive_music/audio_stream_synchronized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions modules/interactive_music/audio_stream_synchronized.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eb51030

Please sign in to comment.