From e77b82b9495abdc23ca4f983c284a494fb454719 Mon Sep 17 00:00:00 2001 From: borine <32966433+borine@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:31:34 +0000 Subject: [PATCH] aplay: Account for delay due to FIFO and buffering --- utils/aplay/aplay.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/utils/aplay/aplay.c b/utils/aplay/aplay.c index 69a281987..cdaf348d8 100644 --- a/utils/aplay/aplay.c +++ b/utils/aplay/aplay.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -820,11 +821,19 @@ static void *io_worker_routine(struct io_worker *w) { ffb_shift(&buffer, frames * w->ba_pcm.channels); int ret; - if ((ret = snd_pcm_delay(w->snd_pcm, - &pcm_delay_frames[pcm_delay_frames_i++ % ARRAYSIZE(pcm_delay_frames)])) != 0) + snd_pcm_sframes_t delay_frames = 0; + if ((ret = snd_pcm_delay(w->snd_pcm, &delay_frames)) != 0) warn("Couldn't get PCM delay: %s", snd_strerror(ret)); else { + unsigned int buffered = 0; + ioctl(w->ba_pcm_fd, FIONREAD, &buffered); + buffered += ffb_blen_out(&buffer); + delay_frames += buffered / (w->ba_pcm.channels * pcm_format_size); + + pcm_delay_frames[pcm_delay_frames_i % ARRAYSIZE(pcm_delay_frames)] = delay_frames; + pcm_delay_frames_i++; + struct timespec ts_now; /* Rate limit delay updates to 1 update per second. */ struct timespec ts_delay = { .tv_sec = 1 };