[client] audio: redesign playback synchronization

Model playback latency from the device period, arrival jitter, source
packet phase, and resampler delay. Treat audio:latencyOffset as an
explicit addition to this minimum and align the first device pull to
the next packet deadline. This starts playback near its steady-state
target without unnecessary prefill or startup underruns. Use a
512-frame default period as a practical low-latency baseline.

Replace the startup clock hold with a one-sided proportional
acquisition controller, then hand off to source/device rate
feed-forward and a slow phase loop. Calibrate the logical device
timeline at handoff, discard correction that opposes the current
error, and slew-limit rate changes. This prevents startup drain,
integral wind-up, overshoot, and long convergence while preserving
clock-drift compensation.

Allow audio backends to expose a real-time resampler and use
PipeWire's adaptive resampler when version 1.4 or newer supports it.
Retain libsamplerate as the fallback and add audio:resampler to select
the implementation. Wait for PipeWire stream setup to complete and
propagate rate-control failures cleanly.

Track PipeWire input-consumption and output-equivalent clocks
separately. The input clock measures ring latency while the output
clock drives feed-forward using the ratio that governed each request.
This removes delayed self-feedback that made adaptive resampling
oscillate between the correction limits.

Reduce audio:debug output to useful latency, clock, jitter, and xrun
values, and scale the playback graph from the startup estimate. Update
the option names and documentation for the new latency model.
This commit is contained in:
Geoffrey McRae
2026-07-29 04:55:16 +10:00
parent 814f1797fe
commit 737614bc69
7 changed files with 642 additions and 286 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -50,6 +50,10 @@ static bool optRotateValidate (struct Option * opt, const char ** error
static bool optMicDefaultParse (struct Option * opt, const char * str);
static StringList optMicDefaultValues (struct Option * opt);
static char * optMicDefaultToString(struct Option * opt);
static bool optAudioResamplerParse (struct Option * opt,
const char * str);
static StringList optAudioResamplerValues (struct Option * opt);
static char * optAudioResamplerToString(struct Option * opt);
static void doLicense(void);
@@ -542,14 +546,23 @@ static struct Option options[] =
.name = "periodSize",
.description = "Requested audio device period size in samples (0 = 10 ms)",
.type = OPTION_TYPE_INT,
.value.x_int = 0
.value.x_int = 512
},
{
.module = "audio",
.name = "bufferLatency",
.description = "Additional buffer latency in milliseconds",
.name = "latencyOffset",
.description = "Latency offset added to the calculated minimum in milliseconds",
.type = OPTION_TYPE_INT,
.value.x_int = 4
.value.x_int = 6
},
{
.module = "audio",
.name = "resampler",
.description = "Audio resampler to use (auto, libsamplerate, backend)",
.type = OPTION_TYPE_CUSTOM,
.parser = optAudioResamplerParse,
.getValues = optAudioResamplerValues,
.toString = optAudioResamplerToString
},
{
.module = "audio",
@@ -772,7 +785,7 @@ bool config_load(int argc, char * argv[])
g_params.audioDebug = option_get_bool("audio", "debug");
g_params.audioPeriodSize = option_get_int("audio", "periodSize");
g_params.audioBufferLatency = option_get_int("audio", "bufferLatency");
g_params.audioLatencyOffset = option_get_int("audio", "latencyOffset");
g_params.micShowIndicator = option_get_bool("audio", "micShowIndicator");
g_params.audioSyncVolume = option_get_bool("audio", "syncVolume");
@@ -1055,3 +1068,48 @@ static char * optMicDefaultToString(struct Option * opt)
return NULL;
}
static bool optAudioResamplerParse(
struct Option * opt, const char * str)
{
if (!str)
return false;
if (strcasecmp(str, "auto") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_AUTO;
else if (strcasecmp(str, "libsamplerate") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_LIBSAMPLERATE;
else if (strcasecmp(str, "backend") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_BACKEND;
else
return false;
return true;
}
static StringList optAudioResamplerValues(struct Option * opt)
{
StringList sl = stringlist_new(false);
if (!sl)
return NULL;
stringlist_push(sl, (char *)"auto");
stringlist_push(sl, (char *)"libsamplerate");
stringlist_push(sl, (char *)"backend");
return sl;
}
static char * optAudioResamplerToString(struct Option * opt)
{
switch (g_params.audioResampler)
{
case AUDIO_RESAMPLER_AUTO:
return strdup("auto");
case AUDIO_RESAMPLER_LIBSAMPLERATE:
return strdup("libsamplerate");
case AUDIO_RESAMPLER_BACKEND:
return strdup("backend");
}
return NULL;
}

View File

@@ -53,6 +53,12 @@ enum MicDefaultState {
};
#define MIC_DEFAULT_MAX (MIC_DEFAULT_DENY + 1)
enum AudioResampler {
AUDIO_RESAMPLER_AUTO,
AUDIO_RESAMPLER_LIBSAMPLERATE,
AUDIO_RESAMPLER_BACKEND
};
struct AppState
{
_Atomic(enum RunState) state;
@@ -239,7 +245,8 @@ struct AppParams
bool audioDebug;
int audioPeriodSize;
int audioBufferLatency;
int audioLatencyOffset;
enum AudioResampler audioResampler;
bool micShowIndicator;
enum MicDefaultState micDefaultState;
bool audioSyncVolume;