play.c: Let alsa_init() return the number of bytes per chunk
authorAndre <maan@p133.(none)>
Mon, 17 Apr 2006 18:59:12 +0000 (20:59 +0200)
committerAndre <maan@p133.(none)>
Mon, 17 Apr 2006 18:59:12 +0000 (20:59 +0200)
play.c

diff --git a/play.c b/play.c
index 1a30285..0a53331 100644 (file)
--- a/play.c
+++ b/play.c
@@ -98,7 +98,7 @@ static void read_wav_header(void)
  *
  * Install PCM software and hardware configuration. Exit on errors.
  */
  *
  * Install PCM software and hardware configuration. Exit on errors.
  */
-static void alsa_init(void)
+static int alsa_init(void)
 {
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
 {
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
@@ -169,6 +169,7 @@ static void alsa_init(void)
        if (snd_pcm_sw_params(handle, swparams) < 0)
                EXIT(E_SW_PARAMS);
        bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8;
        if (snd_pcm_sw_params(handle, swparams) < 0)
                EXIT(E_SW_PARAMS);
        bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8;
+       return chunk_size * bytes_per_frame;
 }
 
 /**
 }
 
 /**
@@ -180,14 +181,14 @@ static void alsa_init(void)
  */
 int alsa_write(u_char *data, size_t nbytes)
 {
  */
 int alsa_write(u_char *data, size_t nbytes)
 {
-       size_t count = nbytes / bytes_per_frame;
+       size_t frames = nbytes / bytes_per_frame;
        snd_pcm_sframes_t r, result = 0;
        snd_pcm_sframes_t r, result = 0;
-       while (count > 0) {
+       while (frames > 0) {
                /* write interleaved frames */
                /* write interleaved frames */
-               r = snd_pcm_writei(handle, data, count);
+               r = snd_pcm_writei(handle, data, frames);
                if (r < 0)
                        PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
                if (r < 0)
                        PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
-               if (r == -EAGAIN || (r >= 0 && r < count))
+               if (r == -EAGAIN || (r >= 0 && r < frames))
                        snd_pcm_wait(handle, 1);
                else if (r == -EPIPE)
                        snd_pcm_prepare(handle);
                        snd_pcm_wait(handle, 1);
                else if (r == -EPIPE)
                        snd_pcm_prepare(handle);
@@ -195,7 +196,7 @@ int alsa_write(u_char *data, size_t nbytes)
                        EXIT(E_WRITE);
                if (r > 0) {
                        result += r;
                        EXIT(E_WRITE);
                if (r > 0) {
                        result += r;
-                       count -= r;
+                       frames -= r;
                        data += r * bytes_per_frame;
                }
        }
                        data += r * bytes_per_frame;
                }
        }
@@ -254,13 +255,12 @@ static void do_initial_delay(struct timeval *delay)
  */
 static void play_pcm(size_t loaded)
 {
  */
 static void play_pcm(size_t loaded)
 {
-       size_t chunk_bytes, bufsize, written = 0, prebuf_size;
+       size_t bufsize, written = 0, prebuf_size;
        ssize_t ret;
        unsigned char *p;
        struct timeval delay;
        ssize_t ret;
        unsigned char *p;
        struct timeval delay;
+       int chunk_bytes = alsa_init();
 
 
-       alsa_init();
-       chunk_bytes = chunk_size * bytes_per_frame;
        bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
        audiobuf = realloc(audiobuf, bufsize);
        if (!audiobuf)
        bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
        audiobuf = realloc(audiobuf, bufsize);
        if (!audiobuf)