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

Implement `AudioStreamSynchronized::get_bar_beats()`, fix division by zero
This commit is contained in:
Thaddeus Crews 2024-12-05 14:12:26 -06:00
commit eb5103093c
No known key found for this signature in database
GPG Key ID: 62181B86FE9E5D84
3 changed files with 21 additions and 3 deletions

View File

@ -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();

View File

@ -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()) {

View File

@ -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;