Repository navigation
fix(audio): initialize spatial parameters before sample playback - #334
Merged
joeykchen merged 2 commits intoSep 12, 2026
Merged
Conversation
Initialize spatial gains, bus routing and pitch before starting 2D and 3D samples. Share pending playback handling for pause, seek, cancellation and polyphony, and cover startup and lifecycle boundaries with regression tests. Validated with a Web build, Safari audio capture and a macOS editor build. Native audio tests remain for manual validation.
There was a problem hiding this comment.
Review: fix/positional-sample-startup
This refactor is well-conceived and cleanly executed. Sample playbacks for AudioStreamPlayer2D/3D are now queued and started during NOTIFICATION_INTERNAL_PHYSICS_PROCESS with up-to-date spatial parameters (bus/volume/pitch), replacing the ad-hoc setplay/setplayback members with a shared pending_playbacks queue in AudioStreamPlayerInternal. Notable strengths:
- Reentrancy is handled deliberately: the pending list is snapshotted before iterating and the index is re-validated after
start_playback_stream. - The
AudioStreamPlayback↔AudioSamplePlaybackreference cycle is broken viaset_sample_playback(nullptr)on the clear/evict paths, with direct test coverage. - Skipping pending voices in
process()prevents never-started voices from being misread as "finished". - The shared 1D
AudioStreamPlayernever populates the pending queue, so the modified internal remains backward-compatible for it (verified). - The deterministic
SampleDrivertest fixture directly asserts start offset/bus/pitch/volume — good coverage of the core goal.
Findings below, ordered by priority. Nothing here is blocking; the inline items are worth a look before merge.
Additional notes (no reliable inline anchor):
- 3D
_update_panningmulti-listener routing (scene/3d/audio_stream_player_3d.cpp:452):output_bus_volumes = bus_volumes;is reassigned inside the per-listener loop, so with multiple 3D listeners the pending sample's initial route uses only the last listener's volumes. This mirrors the last-wins behavior already applied to started voices, so it's likely acceptable, but the multi-listener case is untested. - Per-frame
HashMapallocation (scene/3d/audio_stream_player_3d.cpp:321-329):_update_panningnow builds and returns aHashMap(withStringNamehashing) every physics frame for every active 3D player, even in steady state when nothing is pending and the returned map is discarded. Bounded work, but an avoidable per-frame allocation on the audio hot path — consider building the map only whenhas_pending_playback()is true. - Doc polish (
doc/classes/AudioStreamPlayer2D.xml/AudioStreamPlayer3D.xml,get_playback_position): now returns the queued start position while a sample is pending; theplay/playingdocs were correctly updated, butget_playback_positionstill reads only "Returns the position in the [AudioStream]."
Revalidate pending voices after driver callbacks and stop samples cancelled during startup. Collect initial 3D bus volumes only for pending playback, preserving routing and seek behavior. Add regression coverage for reentrant startup, bus updates and polyphonic seek, and clarify queued playback positions. Formatting, C++ syntax and XML checks passed; native tests have not been run.
songweijun1203
approved these changes
Sep 8, 2026
go-wyvern
approved these changes
Sep 9, 2026
nighca
approved these changes
Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Short positional samples could finish before the first spatial volume update, producing no audible output on Web. Queue 2D/3D playback requests and start samples after their gains, bus routing, and pitch are initialized.
Share pending playback handling for pause, seek, stop, and polyphony limits. Preserve complete 3D bus routing and initialize pitch from the current configuration when no valid listener is available or the source is out of range.
Validation: