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.
size_t frames, bytes;
int ret;
char *data;
+ audio_buf_info abi;
ret = task_get_notification(wn->task);
if (ret < 0)
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;