introduce para_select()
[paraslash.git] / play.c
diff --git a/play.c b/play.c
index 689747e..fd8c9b2 100644 (file)
--- a/play.c
+++ b/play.c
@@ -25,6 +25,7 @@
 #define WAV_HEADER_LEN 44
 #include <sys/time.h> /* gettimeofday */
 #include "para.h"
+#include "fd.h"
 #include "play.cmdline.h"
 #include <alsa/asoundlib.h>
 
@@ -66,6 +67,15 @@ static size_t bytes_per_frame;
 static struct timeval *start_time;
 static struct gengetopt_args_info conf;
 
+void para_log(__unused int ll, const char* fmt,...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       vfprintf(stderr, fmt, argp);
+       va_end(argp);
+}
+
 /*
  * read_wav_header - read WAV_HEADER_LEN bytes from stdin to audio buffer
  *
@@ -114,7 +124,7 @@ static void set_alsa_params(void)
        err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
        if (err < 0 || !buffer_time)
                EXIT(E_GET_BUFFER_TIME);
-       fprintf(stderr, "buffer time: %d\n", buffer_time);
+       PARA_DEBUG_LOG("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);
@@ -122,7 +132,7 @@ 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);
+       PARA_DEBUG_LOG("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);
@@ -163,7 +173,7 @@ static snd_pcm_sframes_t pcm_write(u_char *data, size_t count)
                /* write interleaved frames */
                r = snd_pcm_writei(handle, data, count);
                if (r < 0)
-                       fprintf(stderr, "write error: %s\n", snd_strerror(r));
+                       PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
                if (r == -EAGAIN || (r >= 0 && r < count))
                        snd_pcm_wait(handle, 1);
                else if (r == -EPIPE)
@@ -209,9 +219,8 @@ static int start_time_in_future(struct timeval *diff)
  */
 static void do_initial_delay(struct timeval *delay)
 {
-//     fprintf(stderr, "sleeping %lums\n", tv2ms(delay));
        do
-               select(1, NULL, NULL, NULL, delay);
+               para_select(1, NULL, NULL, delay);
        while (start_time_in_future(delay));
 }
 
@@ -248,16 +257,13 @@ 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);
+       if (loaded && p != audiobuf)
                memcpy(audiobuf, p, loaded);
-       }
 read:
        ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
        if (ret < 0)
@@ -282,8 +288,6 @@ static size_t check_wave(void)
                return WAV_HEADER_LEN;
        conf.channels_arg = (unsigned) a[22];
        conf.sample_rate_arg = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
-//     fprintf(stderr, "channels: %d, rate: %d\n", conf.channels_arg,
-//             conf.sample_rate_arg);
        return 0;
 }
 
@@ -301,7 +305,6 @@ int main(int argc, char *argv[])
                        EXIT(E_SYNTAX);
                start_time = &tv;
        }
-//     fprintf(stderr, "argc=%d, argv[1]=%s\n",argc,  argv[1]);
        snd_pcm_info_alloca(&info);
        if (snd_output_stdio_attach(&log, stderr, 0) < 0)
                EXIT(E_LOG);