From 2ba45411ace62b419ce03a085491077deea38878 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 8 Sep 2014 22:38:35 +0200 Subject: [PATCH] oss: Avoid sound artefacts on some setups. Large buffers which result in short writes cause sound artefacts on certain setups. For example, 22KHz audio on Linux/ALSA in OSS mode is affected. This patch caps the number of bytes to write to the maximum possible value. This value is obtained through the SNDCTL_DSP_GETOSPACE ioctl. --- oss_write.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/oss_write.c b/oss_write.c index 5b910742..35f7b628 100644 --- a/oss_write.c +++ b/oss_write.c @@ -189,6 +189,7 @@ static int oss_post_select(__a_unused struct sched *s, void *context) size_t frames, bytes; int ret; char *data; + audio_buf_info abi; ret = task_get_notification(wn->task); if (ret < 0) @@ -220,6 +221,15 @@ static int oss_post_select(__a_unused struct sched *s, void *context) ret = 0; if (!FD_ISSET(powd->fd, &s->wfds)) goto out; + /* get maximal number of bytes that can be written */ + ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi); + if (ret >= 0) { + size_t max_frames = abi.bytes / powd->bytes_per_frame; + if (max_frames == 0) + goto out; + /* cap number of frames to avoid sound artefacts */ + frames = PARA_MIN(frames, max_frames); + } ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame); if (ret < 0) goto out; -- 2.39.2