]> git.tue.mpg.de Git - paraslash.git/commitdiff
configurable input buffer size and prebuffering
authorAndre <maan@p133.(none)>
Fri, 24 Feb 2006 11:21:33 +0000 (12:21 +0100)
committerAndre <maan@p133.(none)>
Fri, 24 Feb 2006 11:21:33 +0000 (12:21 +0100)
play.c
play.ggo

diff --git a/play.c b/play.c
index 78eaf0c58644a2c8a5688e76e06a77722668755c..689747e900b6636672d6fc8d06679829fd272189 100644 (file)
--- a/play.c
+++ b/play.c
@@ -22,7 +22,6 @@
  * based on the vplay program by Michael Beck.
  */
 
-#define BUFFER_SIZE 1000 * 1000
 #define WAV_HEADER_LEN 44
 #include <sys/time.h> /* gettimeofday */
 #include "para.h"
@@ -109,13 +108,13 @@ static void set_alsa_params(void)
                EXIT(E_SAMPLE_FORMAT);
        if (snd_pcm_hw_params_set_channels(handle, hwparams, conf.channels_arg) < 0)
                EXIT(E_CHANNEL_COUNT);
-       if (snd_pcm_hw_params_set_rate_near(handle, hwparams, (unsigned int*) &conf.sample_rate_arg, 0) < 0)
+       if (snd_pcm_hw_params_set_rate_near(handle, hwparams,
+                       (unsigned int*) &conf.sample_rate_arg, 0) < 0)
                EXIT(E_SET_RATE);
        err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
        if (err < 0 || !buffer_time)
                EXIT(E_GET_BUFFER_TIME);
-       if (buffer_time > 500000)
-               buffer_time = 500000;
+       fprintf(stderr, "buffer time: %d\n", buffer_time);
        if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
                        &buffer_time, 0) < 0)
                EXIT(E_SET_BUFFER_TIME);
@@ -123,13 +122,14 @@ static void set_alsa_params(void)
                EXIT(E_HW_PARAMS);
        snd_pcm_hw_params_get_period_size(hwparams, &chunk_size, 0);
        snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
+       fprintf(stderr, "buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size);
        if (chunk_size == buffer_size)
                EXIT(E_BAD_PERIOD);
        snd_pcm_sw_params_current(handle, swparams);
        err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
        if (err < 0 || !xfer_align)
                EXIT(E_GET_XFER);
-       snd_pcm_sw_params_set_sleep_min(handle, swparams, 0);
+//     snd_pcm_sw_params_set_sleep_min(handle, swparams, 0);
        snd_pcm_sw_params_set_avail_min(handle, swparams, chunk_size);
        /* round to closest transfer boundary */
        start_threshold = (buffer_size / xfer_align) * xfer_align;
@@ -209,7 +209,7 @@ static int start_time_in_future(struct timeval *diff)
  */
 static void do_initial_delay(struct timeval *delay)
 {
-       fprintf(stderr, "sleeping %lums\n", tv2ms(delay));
+//     fprintf(stderr, "sleeping %lums\n", tv2ms(delay));
        do
                select(1, NULL, NULL, NULL, delay);
        while (start_time_in_future(delay));
@@ -225,41 +225,41 @@ static void do_initial_delay(struct timeval *delay)
  */
 static void play_pcm(size_t loaded)
 {
-       size_t chunk_bytes, bufsize, written = 0;
+       size_t chunk_bytes, bufsize, written = 0, prebuf_size;
        ssize_t ret;
        unsigned char *p;
-       int dont_write;
        struct timeval delay;
 
        set_alsa_params();
        chunk_bytes = chunk_size * bytes_per_frame;
-       bufsize = chunk_bytes * 1024;
+       bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
        audiobuf = realloc(audiobuf, bufsize);
        if (!audiobuf)
                EXIT(E_MEM);
+       prebuf_size = conf.prebuffer_arg * bufsize / 100;
 again:
-       dont_write = 0;
-       if (!written && start_time)
-               dont_write = start_time_in_future(&delay);
-       if (!dont_write) {
-               p = audiobuf;
-               while (loaded >= chunk_bytes) {
-                       ret = pcm_write(p, chunk_size) * bytes_per_frame;
-                       p += ret;
-                       written += ret;
-                       loaded -= ret;
-               }
-               if (loaded && p != audiobuf) {
-                       fprintf(stderr, "memcpy: %d\n", loaded);
-                       memcpy(audiobuf, p, loaded);
+       if (!written) {
+               if (loaded < prebuf_size)
+                       goto read;
+               if (start_time && start_time_in_future(&delay)) {
+                       do_initial_delay(&delay);
+                       start_time = NULL;
                }
        }
-       if (dont_write && loaded >= bufsize) {
-               do_initial_delay(&delay);
-               start_time = NULL;
-               goto again;
+       p = audiobuf;
+       while (loaded >= chunk_bytes) {
+//                     fprintf(stderr, "write (loaded = %d)\n", loaded);
+               ret = pcm_write(p, chunk_size) * bytes_per_frame;
+               p += ret;
+               written += ret;
+               loaded -= ret;
+       }
+       if (loaded && p != audiobuf) {
+//                     fprintf(stderr, "memcpy: %d@%d\n", loaded, p - audiobuf);
+               memcpy(audiobuf, p, loaded);
        }
-       ret = read(STDIN_FILENO, audiobuf, bufsize - loaded);
+read:
+       ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
        if (ret < 0)
                EXIT(E_READ);
        if (ret) {
index 6b6aa9c637fc3fb6e9798b50b3d9ab56aa020489..3ccdf592f464cc430e02500bfe3ca218d3648aa1 100644 (file)
--- a/play.ggo
+++ b/play.ggo
@@ -3,3 +3,5 @@ option "start_time" t "start playback at given time which must be in a:b format
 option "device" d "set PCM device" string typestr="device" default="plughw:0,0" no
 option "channels" c "number of channels (only neccessary for raw audio)" int typestr="num" default="2" no
 option "sample_rate" s "force given sample rate (only neccessary for raw audio)" int typestr="num" default="44100" no
+option "bufsize" b "input buffer size" int typestr="kilobytes" default="64" no
+option "prebuffer" p "delay playback until buffer is filled" int typestr="percent" default="100" no