Skip to content

CLI mode, native capture backends with accurate timestamps, offset calibration - #22

Merged
cboulay merged 4 commits into
masterfrom
cli-mode
Nov 26, 2026
Merged

cboulay merged 4 commits into
masterfrom
cli-mode

Conversation

@cboulay

@cboulay cboulay commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • AudioCaptureCLI: headless mode that starts streaming at once and stops on SIGINT/SIGTERM. That covers what [Enhancement] RCS functionality #20 asked for, start/stop from scripts, without adding remote control. It reads GUI .cfg files, and flags override them. Where a native backend exists it builds without Qt.

  • Pluggable capture layer (capture.h, runtime registry). New native backends:

    Platform Backend
    Linux PipeWire (default) and ALSA (--api ALSA, exclusive)
    macOS CoreAudio
    Windows WASAPI

    QtMultimedia stays as the fallback. Each native backend stamps a buffer with the driver's capture time instead of its arrival time.

  • CaptureSession (was reader.*, now Qt-free): a watchdog restarts a backend that fails or stops delivering audio, keeping the same outlet (Stream hangs unexpectedly #12).

  • Per-device latency correction: --ts-offset-us / timestamp_offset_us, the same names App-MeasurementComputing uses. The value is recorded in the stream metadata.

  • Measuring that offset: scripts/measure_offset.py (XDF analysis) and scripts/pi_edge_markers.py (Raspberry Pi GPIO + LSL markers), plus a README section. The reference can be an MCC DAQ or a Raspberry Pi.

  • Fixes restoring the saved device and sample format from the config file (Sample Format not parsed/recognized when loading configuration #19).

Timestamp accuracy

Square-wave edges were fed into the audio input. Each table compares the backends on the same hardware and input.

Linux (Realtek ALC897 line-in; reference: PTP-synchronized GPIO edge times; 30 min runs):

Capture Bias SD Backwards timestamp steps
QtMultimedia (before) 2.1 ms 953 us ~28 % of chunk boundaries
PipeWire 1.16 ms 18 us 0
ALSA 0.52 ms 8 us 0

PipeWire's bias is 0.67 ms higher than ALSA's because PipeWire does not see the HDA driver's 32-frame FIFO delay. The offset setting absorbs it.

macOS (MacBook built-in mic; acoustic click loopback against an MCC DAQ; 30 min runs):

Capture Lag SD Backwards timestamp steps
QtMultimedia (before) 32.6 ms 1457 us 60690
CoreAudio 0.99 ms 13 us 0

CoreAudio's lag includes the acoustic path.

Windows (same ALC897, Microsoft HDA driver; reference: Pi GPIO + LSL markers): WASAPI 519–549 us across runs, SD 9–11 us, at the device's 44.1 kHz rate. Through Windows' 44.1 to 48 kHz resampler the offset is 0.72 ms higher; the backend now warns when it resamples.

Testing

  • watchdog_test (ctest) covers stalled backends, backends that error, and a failed start. It passes on macOS, Linux, Windows MSVC and Windows MinGW.
  • Calibration validation: --ts-offset-us shifted the timestamps by exactly the set value, on Linux (ALSA) and macOS (CoreAudio).
  • PipeWire restart: the native backend recovered in about 3 s on the same outlet. The QtMultimedia path wedged in the same test.
  • Not tested: clicking through the GUI with each native backend.

…offset calibration

- AudioCaptureCLI: headless, auto-starts streaming, stops on SIGINT/SIGTERM (replaces the RCS request, #20).
  Reads GUI .cfg files; flags override. Builds without Qt when a native backend is available.
- Capture layer (capture.h) with a runtime registry: PipeWire and ALSA (Linux), CoreAudio (macOS), QtMultimedia
  fallback. Native backends stamp each buffer with the driver's capture time instead of its arrival time.
  Measured against hardware ground truth (30 min runs): timestamp SD 953 us (Qt) -> 18 us (PipeWire), 8 us (ALSA)
  on Linux; 1457 us (Qt) -> 13 us (CoreAudio) on macOS; no backwards timestamp steps.
- CaptureSession (was reader.*): Qt-free; a watchdog restarts a failed or stalled backend on the same outlet (#12).
- --ts-offset-us / timestamp_offset_us: per-device latency correction, recorded in the stream metadata.
- scripts/measure_offset.py and scripts/pi_edge_markers.py, with a README section on measuring the offset
  against an MCC DAQ or a Raspberry Pi GPIO.
- Fix restoring the saved device and settings from the config file (#19).
Stamps each packet with IAudioCaptureClient::GetBuffer's QPC position (device time of the first frame), mapped to
steady_clock. Shared mode, event-driven, AUTOCONVERTPCM so any requested format works; warns when Windows
resamples, since its resampler adds latency the timestamps do not include. Default API on Windows ahead of
QtMultimedia; the CLI builds without Qt there. Builds with MSVC and MinGW.

Measured on a Realtek HDA line input (Pi GPIO edges + LSL markers as reference): offset 519-549 us, SD ~9-11 us at
the device rate; +0.72 ms through the 44.1 -> 48 kHz resampler.
@cboulay
cboulay merged commit fd632f1 into master Nov 26, 2026
7 checks passed
@cboulay
cboulay deleted the cli-mode branch November 26, 2026 18:39
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.

[Enhancement] RCS functionality Sample Format not parsed/recognized when loading configuration Stream hangs unexpectedly

1 participant