Skip to content

fix: prevent channel shift when appending to an empty keep-alive queue#872

Merged
roderickvd merged 1 commit intomasterfrom
fix/queue-frame-alignment-channel-shift
Mar 29, 2026
Merged

fix: prevent channel shift when appending to an empty keep-alive queue#872
roderickvd merged 1 commit intomasterfrom
fix/queue-frame-alignment-channel-shift

Conversation

@roderickvd
Copy link
Copy Markdown
Member

When Player::connect_new calls mixer.add(queue) with an empty queue, UniformSourceIterator::bootstrap() captures channels=1 from the Empty placeholder and creates a Take(1) with a ChannelCountConverter(from=1). If a stereo source is then appended before the audio thread calls next(), that first sample passes through the wrong converter: L is treated as mono and duplicated, permanently shifting the stream by one sample and shifting channels.

Supersedes #870
Fixes #871

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a race where wrapping an empty keep-alive queue in UniformSourceIterator could cache incorrect initial metadata (mono) and apply the wrong channel conversion to the first samples appended shortly after, causing a permanent 1-sample channel shift.

Changes:

  • Make UniformSourceIterator lazily bootstrap its converters on the first next() call (instead of during new()), so it observes up-to-date source metadata.
  • Adjust queue span-length reporting to use self.channels() (which can reflect the next queued source) rather than the current exhausted source’s channel count.
  • Add a regression test ensuring the first appended stereo samples are emitted with correct L/R ordering when added to a mixer after connecting an initially-empty keep-alive queue.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/source/uniform.rs Defers converter initialization until first sample pull using a pending source, preventing early metadata capture from placeholder sources.
src/queue.rs Ensures span-length fallback aligns with the queue’s effective channel count and adds a regression test for correct first-append channel behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@roderickvd roderickvd merged commit 8ca8cd0 into master Mar 29, 2026
7 checks passed
@roderickvd roderickvd deleted the fix/queue-frame-alignment-channel-shift branch March 29, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sample channel shift (L/R swap) when rapidly appending stereo sources to an empty player queue

2 participants