NEWS,md: Add introductory text for v0.5.7.
[paraslash.git] / oss_write.c
index 5b910742e3ada1ac084bac89183d3b8e75d03852..20186667e2f383880538275d61d1607203ed2b8a 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 2009-2014 Andre Noll <maan@tuebingen.mpg.de>
+ * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -8,7 +8,6 @@
 
 #include <regex.h>
 #include <sys/ioctl.h>
 
 #include <regex.h>
 #include <sys/ioctl.h>
-#include <fcntl.h>
 #include <sys/soundcard.h>
 
 #include "para.h"
 #include <sys/soundcard.h>
 
 #include "para.h"
@@ -147,7 +146,7 @@ static int oss_init(struct writer_node *wn, unsigned sample_rate,
         * Set sampling rate
         *
         * If we request a higher sampling rate than is supported by the
         * Set sampling rate
         *
         * If we request a higher sampling rate than is supported by the
-        * device, the the highest possible speed is automatically used. The
+        * device, the highest possible speed is automatically used. The
         * value actually used is returned as the new value of the argument.
         */
        rate = sample_rate;
         * value actually used is returned as the new value of the argument.
         */
        rate = sample_rate;
@@ -166,7 +165,7 @@ static int oss_init(struct writer_node *wn, unsigned sample_rate,
                ret = -E_BAD_SAMPLERATE;
                if (100 * max > 110 * min) /* more than 10% deviation */
                        goto err;
                ret = -E_BAD_SAMPLERATE;
                if (100 * max > 110 * min) /* more than 10% deviation */
                        goto err;
-               PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
+               PARA_NOTICE_LOG("using %uHz rather than %uHz\n", rate,
                        sample_rate);
        }
        wn->min_iqs = powd->bytes_per_frame;
                        sample_rate);
        }
        wn->min_iqs = powd->bytes_per_frame;
@@ -189,6 +188,7 @@ static int oss_post_select(__a_unused struct sched *s, void *context)
        size_t frames, bytes;
        int ret;
        char *data;
        size_t frames, bytes;
        int ret;
        char *data;
+       audio_buf_info abi;
 
        ret = task_get_notification(wn->task);
        if (ret < 0)
 
        ret = task_get_notification(wn->task);
        if (ret < 0)
@@ -220,6 +220,15 @@ static int oss_post_select(__a_unused struct sched *s, void *context)
        ret = 0;
        if (!FD_ISSET(powd->fd, &s->wfds))
                goto out;
        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;
        ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
        if (ret < 0)
                goto out;