From 36bea732fb411c1097d47b756eb9a323ce1e3a56 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 6 Aug 2023 16:18:10 +0200 Subject: [PATCH] ao_audiotrack: align buffer size to sample size This looks like a pretty bad bug but only became a problem with the last commit that allows rates like 22.5kHz to pass through directly instead of being resampled. --- audio/out/ao_audiotrack.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/audio/out/ao_audiotrack.c b/audio/out/ao_audiotrack.c index dd4678011a..acd39fb869 100644 --- a/audio/out/ao_audiotrack.c +++ b/audio/out/ao_audiotrack.c @@ -714,11 +714,17 @@ static int init(struct ao *ao) return -1; } - int min = 0.075 * p->samplerate * af_fmt_to_bytes(ao->format) * ao->channels.num; + // Choose double of the minimum buffer size suggested by the driver, but not + // less than 75ms or more than 150ms. + const int bps = af_fmt_to_bytes(ao->format); + int min = 0.075 * p->samplerate * bps * ao->channels.num; int max = min * 2; + min = MP_ALIGN_UP(min, bps); + max = MP_ALIGN_UP(max, bps); p->size = MPCLAMP(buffer_size * 2, min, max); MP_VERBOSE(ao, "Setting bufferSize = %d (driver=%d, min=%d, max=%d)\n", p->size, buffer_size, min, max); - ao->device_buffer = p->size / af_fmt_to_bytes(ao->format); + assert(p->size % bps == 0); + ao->device_buffer = p->size / bps; p->chunksize = p->size; p->chunk = talloc_size(ao, p->size);