Skip to content

Audio output: rate matched FIFO reading with a reserve, whole blocks - #3215

Open
VasylSamoilov wants to merge 1 commit into
f4exb:masterfrom
VasylSamoilov:audio-output-fix-pr
Open

VasylSamoilov wants to merge 1 commit into
f4exb:masterfrom
VasylSamoilov:audio-output-fix-pr

Conversation

@VasylSamoilov

Copy link
Copy Markdown
Contributor

Fixes #3214. Found while investigating #3182.

What the fix is

Each audio FIFO feeding an output device is read through an asynchronous sample rate converter with adaptive ratio control, one per FIFO:

  • Interpolator: polyphase fractional-delay FIR, Kaiser windowed sinc (beta 9, cutoff 0.45 fs), 32 taps, 256 phases, linear interpolation between phases.
  • Ratio: a least-squares line fit of frames received against time (60 s, exponential forgetting, used once 10 s are covered) gives the producer rate against the nominal output rate; a proportional correction holds a 50 ms reserve in the FIFO (+-50 % band); the ratio change is slew limited.
  • It is the same principle as interpolation-based timing recovery in modems, driven by the FIFO level instead of a symbol timing error.
  • Start: a FIFO starts when it holds the reserve, the sink buffer and its largest arrival, or when a producer that only fills to a level (self-throttled) has stopped adding for 500 ms with at least the reserve.
  • Mixing: the device offers the minimum available over the started FIFOs; a FIFO that is late is waited for while the sink still has audio, then stalled alone (counted).
  • The sink gets whole blocks or nothing; bytesAvailable reports real audio only; readyRead is emitted when audio arrives after starving. No zeros are put into the sink.
  • Locks: the FIFO list in the output device, and a recursive mutex over the audio device manager maps (used from the channels' threads, the GUI and the rate reports; calls into the devices under it only add or remove a FIFO or push a message).
  • A status line per device every 10 s: reserve min..max, clock difference (ppm), stalls.

Per channel:

  • Broadcast FM demod: audio written in 20 ms blocks, FIFO not cleared on a short write, output rate report applied when it changes.
  • Radio paging demod: 20 ms blocks, FIFO resized only on a real rate change, silence after the voice so its end is played, output rate report applied.
  • Voice message player: FIFO resized only on a real rate change, silence after the end for the output's latency and 100 ms so the end is played before the output can stop, stops when only silence is left, FIFO label.
  • Radio paging mod: live voice starts once 100 ms of it waits and waits again after a dry-out, so uneven arrival from the audio input does not cut it.
  • Audio input device: reports the sample rate in use to the manager.

Related: #3212 (timer paced devices, separate PR) fixes the file input running off its rate, which fed the FIFOs too fast or too slow.

Benefits for users

  • No start gap or click when a channel starts or a voice message plays.
  • No slow drift of the delay over long listening: the ratio follows the producer against the sound card clock. A live SDR measured +37 ppm against the sound card, about 130 ms per hour that builds up or runs dry without it; the model covers up to +-7500 ppm.
  • Several channels with different rates and timing share one sound card without one starving the others: a late reader is waited for, then stalled alone.
  • One resampler per FIFO, so any channel rate plays on any card rate.
  • The same code path on Qt5 and Qt6 and on every OS backend: it only relies on the device pulling whole blocks.
  • A per-device status line every 10 s (reserve, clock ppm, stalls) to diagnose user reports.

Evidence

Test Before After, Qt5 After, Qt 6.11.3
Broadcast FM starts master: gaps 10.7-32 ms in 6/8 starts; with the #3203 demod changes before this fix: 139-245 ms, 8/8 8/8 clean 8/8 clean
Voice message player master: 21.3 ms gap at 0.2 s in 5/5 plays, the last 220 ms of the file not played 5/5 clean, full file to the last sample 5/5 clean, full file to the last sample
NFM, AM, USB, WFM (1 kHz tone, played once) no gaps; audio from 1.05-1.23 s no gaps, same level (USB 0.2 dB lower at full scale); audio 0.10-0.15 s later (reserve, first 341 ms block) not measured
60 s steady gaps 0 gaps 0 gaps
902 s steady (file input, looped) not measured 0 gaps, 0 stalls not measured
One channel running, one idle, both creation orders not measured 0 gaps 0 gaps, 0 stalls
Live SDR clock against the sound card clock +37 ppm (measured) long live run on this build not done not measured
Windows build (user test) stutter not applicable Broadcast FM audio much better (by ear)

Model: the reader and mixer simulated against the Qt5 and Qt6 sink behaviours, chunk sizes, clock offsets and pull jitter: 1045 of 1062 cases pass; the 17 failures are all a -7500 ppm, 480-frame chunk case that real devices do not produce.

Unit tests: radio paging TX stream 95/95, radio paging, message table 49/49.

This PR branch alone (without #3212), both Qt5 and Qt 6.11.3: Broadcast FM starts 8/8 without gaps, one channel running and one idle in both creation orders 0 gaps 0 stalls; here the file input runs about +8000 ppm off its rate (#3212) and the reader follows it with the reserve held.

With the audio device manager mutex, both Qt5 and Qt 6.11.3: Broadcast FM starts 8/8 without gaps (audio from 0.94-1.02 s), one channel running and one idle in both creation orders 0 gaps 0 stalls, 60 s steady 0 gaps 0 stalls, voice message player 5/5 complete (the last 160 ms were lost on Qt5 before the end padding followed the output's latency).

How tested

  • Recording of a null sink while the plugin runs, then: gaps inside the programme, a sweep test (source time against wall time around each gap), start and end of the played audio.
  • Qt5 from the distribution and Qt 6.11.3 built locally, same tree.
  • File input with a test signal for repeatable runs; a live SDR for the clock difference.

Next steps (not in this PR)

  • Same 20 ms block writing and rate handling for the other demods and mods: most write 341 ms bursts and ignore the output rate report.
  • Modulator microphone path (NFM, AM, SSB, WFM, FreeDV, M17): stale audio replayed on underrun, new audio dropped near full, the microphone FIFO not added back after an input device change (TX Audio source fails after 1 TX cycle #1731, SSB Modulator input source issue #2336, Audio Popping #2695).
  • FIFOs never unregistered: DATV, AudioInput, AudioOutput (use after free, [audiofifo] Use after free in AudioFifo::write #2059).
  • Audio device manager: the default device rate stored under the real device name but read under the default name; an input device named from the output list; a message queue removed from the wrong map.
  • FIFO cleared on an unchanged rate: DRM, SELCAL, DAB, AM, NFM, VOR, VORMC, SSB, WDSP.
  • Wrong rate assumptions: DATV fixed 48 kHz, M17 rate/8000 capped at 6, FreeDV integer rate/8000.

@codacy-production

codacy-production Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 105 complexity · 111 duplication

Metric Results
Complexity 105
Duplication 111

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Fixes f4exb#3214.

Each FIFO feeding an output device is read through an asynchronous
sample rate converter with adaptive ratio control: a polyphase
fractional-delay FIR (Kaiser windowed sinc, 32 taps, 256 phases), the
ratio from a least-squares line fit of frames received against time
plus a proportional correction holding a 50 ms reserve, slew limited.
A FIFO starts when it holds the reserve, the sink buffer and its
largest arrival, or when a self-throttled producer has settled. The
device offers the minimum available over the started FIFOs; a late
FIFO is waited for, then stalled alone. The sink gets whole blocks or
nothing and readyRead after starving; no zeros are made up. The FIFO
list and the audio device manager maps are locked. A status line per
device every 10 s: reserve, clock difference, stalls.

Broadcast FM and radio paging demods write 20 ms blocks and apply the
output rate report; the paging demod adds silence after the voice and
the voice message player silence for the output's latency after the
end, so the end is played before the output can stop; the paging mod
starts live voice once 100 ms waits; the audio input device reports
its rate.
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.

Audio output: gaps at start, clicks, drift and underruns from the FIFO reading

1 participant