oss: Avoid sound artefacts on some setups.
authorAndre Noll <maan@tuebingen.mpg.de>
Mon, 8 Sep 2014 20:38:35 +0000 (22:38 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 21 Sep 2014 11:32:01 +0000 (13:32 +0200)
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

index 5b910742e3ada1ac084bac89183d3b8e75d03852..35f7b6280115ec53bf88d5f2e303e34f8cc45d98 100644 (file)
@@ -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;