Skip to content

fix(cpn): avoid clipped start of sound preview (builds on #7516) - #23

Closed
pfeerick wants to merge 6 commits into
mainfrom
fix/cpn-audio-preview-coldstart
Closed

pfeerick wants to merge 6 commits into
mainfrom
fix/cpn-audio-preview-coldstart

Conversation

@pfeerick

Copy link
Copy Markdown
Owner

Builds on top of upstream PR EdgeTX#7516 (fixes EdgeTX#7354 — silent audio preview on macOS by creating a fresh QAudioOutput per playback instead of reusing a shared one).

That fix trades the "stale shared output blocks playback" bug for a possible new one: a freshly constructed QAudioOutput isn't guaranteed to be immediately ready when play() is called, which could clip the very start of the previewed sound (a "cold start" glitch), especially on backends like macOS/AVFoundation where attaching to the audio session isn't necessarily synchronous.

This commit adds a playWhenAvailable() helper that polls QMediaPlayer::isAvailable() for up to ~200ms (10 x 20ms) before calling play(), falling back to playing immediately/after the last attempt regardless.

For testing: this is meant to validate whether isAvailable() is sufficient to fix the clipped-start symptom before proposing anything upstream. If it doesn't fully resolve it, the next things to try would be waiting on QMediaPlayer::mediaStatusChanged reaching LoadedMedia/BufferedMedia instead, since isAvailable() only reports whether the platform backend exists, not whether the audio pipeline has actually finished attaching.

rickard-von-essen and others added 6 commits August 27, 2026 19:58
… macOS

Shared QAudioOutput was created once and reused across QMediaPlayer
instances. On macOS/AVFoundation, deleteLater() leaves the old player
alive long enough that the new player cannot claim the same audio output,
so nothing plays. Fix by creating QAudioOutput as a child of each new
QMediaPlayer so it is always fresh and auto-deleted with the player.

Also fixes a memory leak: the old audioOutput had no parent and was
never deleted.

Fixes EdgeTX#7354

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…utput readiness

PR EdgeTX#7516 fixes silent playback on macOS by creating a fresh QAudioOutput
per playback instead of reusing one across QMediaPlayer instances. That
removes the stale-output issue, but the newly created output may not be
immediately ready when play() is called, which can clip the very start
of the sound. Poll QMediaPlayer::isAvailable() briefly before starting
playback to give the backend a chance to finish attaching.
isAvailable() only reports whether a platform media backend exists at
all, not whether this specific source's playback pipeline (and its
audio output) has actually finished attaching, so polling it is
unlikely to catch the same readiness gap responsible for a clipped
start of the previewed sound.

Wait for QMediaPlayer::mediaStatus() to reach LoadedMedia before
calling play() instead, which reflects source-specific readiness. Kept
as a separate commit (rather than amending) so CI results for both
approaches remain comparable side by side.
…d preview

Neither polling isAvailable() nor waiting for LoadedMedia fixed the
clipped start of playback on macOS (~0.5s of audio missing). New data
point: playing a track again quickly after the first play produces the
full, unclipped audio. That points to a device/route warm-up cost on
macOS (CoreAudio/AVFoundation) rather than a Qt-level readiness race:
the audio hardware route takes time to open when it has been idle, but
reopening shortly after a previous playback reuses the still-warm
route.

Recreating a new QMediaPlayer + QAudioOutput on every playSound() call
(per EdgeTX#7516) forces that route to be closed and reopened every single
time, so every preview after any idle gap pays the opening cost and
loses its lead-in. Instead, create the QMediaPlayer and its
QAudioOutput once, in the constructor, and reuse them for every
playSound() call (just swapping the source and calling play() again).
This keeps the underlying audio route continuously open for the life
of the panel, so only the very first playback (if any) should pay the
warm-up cost, not every one of them.

Since there is now only ever one QMediaPlayer instance, the signal
connections move to the constructor (made once) instead of being
redone per playback, and stopSound() no longer needs to
disconnect/deleteLater/recreate anything.

This also sidesteps the original EdgeTX#7354 failure mode a different way:
that bug came from a *new* QMediaPlayer racing to claim a *shared*
QAudioOutput while the *previous* QMediaPlayer was still alive pending
deleteLater(). With a single long-lived player/output pair there is
never a second instance to race against.
…ay cost

The persistent QMediaPlayer/QAudioOutput (previous commit) means only
the very first preview after opening the panel should pay the audio
route warm-up cost, but that first click would still clip. Since the
panel takes some time to build its UI after construction, pay that
cost during construction instead: do a silent (volume 0), throwaway
play of a bundled sound immediately, then stop it and restore the
volume shortly after. By the time the user reaches the tab and clicks
play, the audio route should already be warm.

The playbackStateChanged/errorOccurred handlers are guarded against
this warm-up so it doesn't affect the play-button UI state or trigger
the "unable to play" error dialog.
New data: with the persistent QMediaPlayer/QAudioOutput from the
previous commit, playback still clips after only a couple of seconds
idle between plays. That rules out "keep the C++ objects alive" as a
fix on its own -- the underlying macOS audio route appears to go cold
within a couple of seconds regardless of whether the Qt objects are
recreated or reused, and a one-time warm-up when the panel opens
(previous commit) is very unlikely to survive that long either.

This is a deliberately blunt experiment to isolate the variable: delay
every real play() call (not just the first) by a fixed 700ms after
setSource(), using a request-id token so a stale delayed play() from a
superseded or stopped request doesn't fire. If this alone eliminates
the clipping regardless of how much idle time preceded it, that tells
us it's purely an elapsed-time cost and not something tied to the
specific decode pipeline/content, which would point toward either
living with the added latency or (more likely) injecting real silence
into the audio content itself so the loss lands on the silence instead
of on the real audio.
@pfeerick

Copy link
Copy Markdown
Owner Author

Closing this experimental branch.

Summary of findings while testing on macOS:

  • fix(cpn): recreate QAudioOutput per playback to fix silent preview on macOS EdgeTX/edgetx#7516 correctly fixes the original Companion 2.12.0 - On macOS, in Special Functions, the audio track 'preview' button has no effect EdgeTX/edgetx#7354 bug (audio preview producing no sound at all), by giving each playback its own QAudioOutput instead of reusing one across QMediaPlayer instances.
  • After that fix, a residual issue remained: the first ~0.5s of the previewed sound would sometimes be clipped/missing.
  • Tried and ruled out as fixes: polling QMediaPlayer::isAvailable() (f95a2f5), waiting for mediaStatus() == LoadedMedia (e408af1), keeping a single persistent QMediaPlayer/QAudioOutput for the panel's lifetime (5d4170d), a silent throwaway warm-up play on panel construction (bde930a), and a fixed 700ms delay before every play() call (d24e95d) -- none of these reliably fixed it.
  • Root cause identified: this only reproduces over Bluetooth audio output (confirmed with AirPods) and disappears when using the Mac's internal speaker, or when other audio is already playing in the background (which keeps the Bluetooth link active). It's Bluetooth link wake-up latency -- the AirPods drop the audio link to idle within a couple of seconds of silence, and reconnecting/resyncing the link before it can carry audio takes long enough to clip the very start of playback.
  • This is OS/Bluetooth-stack behavior outside of what Companion can reliably control from Qt's multimedia API, without adding either a perceptible upfront delay (embedding a silent lead-in) or a continuous background "keep-alive" tone while the panel is open (extra battery drain / persistent audio indicator) -- both real trade-offs, not free fixes.

Known issue, not pursuing further here.

@pfeerick pfeerick closed this Aug 28, 2026
@pfeerick
pfeerick deleted the fix/cpn-audio-preview-coldstart branch October 24, 2026 06:14
pfeerick added a commit that referenced this pull request Nov 21, 2026
Reverts d20e431. EdgeTX/rs-dfu#23 adds synchronization to the Windows system libs in rs_dfu-config.cmake, so the Companion-side workaround is no longer needed, and leaving it out lets a green Windows link verify #23.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Companion 2.12.0 - On macOS, in Special Functions, the audio track 'preview' button has no effect

2 participants