Skip to content

Make nested playbacks accessible for synced audio #109381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sebageek
Copy link

@sebageek sebageek commented Aug 7, 2025

Add AudioStreamPlayback.get_playback() to make the internal playbacks of AudioStreamPlaybackSynchronized accessible. This allows us for example to get the playback of a AudioStreamPlaybackInteractive nested inside an AudioStreamPlaybackSynchronized for switching the current clip its playing.

This implements option 1 of godotengine/godot-proposals#12957


Draft of what I was trying out to solve my problem. See godotengine/godot-proposals#12957 for details. I'm not sure if this is the better option of the two.

@@ -262,6 +262,13 @@ int AudioStreamPlaybackSynchronized::mix(AudioFrame *p_buffer, float p_rate_scal
return p_frames;
}

Ref<AudioStreamPlayback> AudioStreamPlaybackSynchronized::get_playback(int p_index) {
ERR_FAIL_INDEX_V_MSG(p_index, stream->stream_count, 0, "Stream index out of bounds");
ERR_FAIL_COND_V_MSG(!playback[p_index].is_valid(), 0, "Stream at index is not valid");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ERR_FAIL_COND_V_MSG(!playback[p_index].is_valid(), 0, "Stream at index is not valid");
ERR_FAIL_COND_V_MSG(playback[p_index].is_null(), Ref<AudioStreamPlayback>(), "Stream at index is not valid.");

Please be explicit about return types, this is pretty rudindant though, did you take this from another method? I don't think this is needed it'll just be noisy and return the same value

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review.
Regarding return types: Specifying Ref<AudioStreamPlayback>() makes more sense. I was trying to get a "null object". Providing 0 actually resulted in the same object being constructed, probably due to implicit conversion? Anyway, your version is definitely the cleaner one.

Regarding is_valid() / is_null(): I looked around in AudioStreamPlaybackSynchronized how playback was being used in code and it seems like every time it's being accessed is_valid() is used to check if the reference is valid, that is why I adopted it as an extra check here as well. My guess is that the playback of a sub-stream can fail/break/stop and then might get freed while others work, resulting in gaps, but... that's only a guess. So if you tell me the check actually is redundant I can take it out of the code. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result should still be checked as it will return a null so using it without a check outside of this will ceash regardless, so id remove it

@@ -262,6 +262,13 @@ int AudioStreamPlaybackSynchronized::mix(AudioFrame *p_buffer, float p_rate_scal
return p_frames;
}

Ref<AudioStreamPlayback> AudioStreamPlaybackSynchronized::get_playback(int p_index) {
ERR_FAIL_INDEX_V_MSG(p_index, stream->stream_count, 0, "Stream index out of bounds");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ERR_FAIL_INDEX_V_MSG(p_index, stream->stream_count, 0, "Stream index out of bounds");
ERR_FAIL_INDEX_V_MSG(p_index, stream->stream_count, Ref<AudioStreamPlayback>(), "Stream index out of bounds.");

Add AudioStreamPlaybackSynchronized.get_playback() to make the internal
playbacks of AudioStreamPlaybackSynchronized accessible. This can be
used to get the playback of an AudioStreamPlaybackInteractive nested
inside an AudioStreamPlaybackSynchronized for switching the current clip
it is playing.

This implements option 1 of godotengine/godot-proposals#12957
@sebageek sebageek force-pushed the make-nested-playbacks-accessible branch from 96269c4 to 80b4cb4 Compare August 7, 2025 22:53
@sebageek sebageek changed the title Make nested playbacks accessible for sync'd audio Make nested playbacks accessible for synced audio Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants