OSDN Git Service

ucm: find_exec() - fix memory leak (dir)
[android-x86/external-alsa-lib.git] / test / pcm.c
index b461b11..b8d4fe6 100644 (file)
 #include <sys/time.h>
 #include <math.h>
 
-char *device = "plughw:0,0";                   /* playback device */
-snd_pcm_format_t format = SND_PCM_FORMAT_S16;  /* sample format */
-unsigned int rate = 44100;                     /* stream rate */
-unsigned int channels = 1;                     /* count of channels */
-unsigned int buffer_time = 500000;             /* ring buffer length in us */
-unsigned int period_time = 100000;             /* period time in us */
-double freq = 440;                             /* sinusoidal wave frequency in Hz */
-int verbose = 0;                               /* verbose flag */
-int resample = 1;                              /* enable alsa-lib resampling */
-
-snd_pcm_sframes_t buffer_size;
-snd_pcm_sframes_t period_size;
-snd_output_t *output = NULL;
+static char *device = "plughw:0,0";                    /* playback device */
+static snd_pcm_format_t format = SND_PCM_FORMAT_S16;   /* sample format */
+static unsigned int rate = 44100;                      /* stream rate */
+static unsigned int channels = 1;                      /* count of channels */
+static unsigned int buffer_time = 500000;              /* ring buffer length in us */
+static unsigned int period_time = 100000;              /* period time in us */
+static double freq = 440;                              /* sinusoidal wave frequency in Hz */
+static int verbose = 0;                                        /* verbose flag */
+static int resample = 1;                               /* enable alsa-lib resampling */
+static int period_event = 0;                           /* produce poll event after each period */
+
+static snd_pcm_sframes_t buffer_size;
+static snd_pcm_sframes_t period_size;
+static snd_output_t *output = NULL;
 
 static void generate_sine(const snd_pcm_channel_area_t *areas, 
                          snd_pcm_uframes_t offset,
@@ -33,23 +34,27 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
        static double max_phase = 2. * M_PI;
        double phase = *_phase;
        double step = max_phase*freq/(double)rate;
-       double res;
-       unsigned char *samples[channels], *tmp;
+       unsigned char *samples[channels];
        int steps[channels];
-       unsigned int chn, byte;
-       int ires;
-       unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
-       int bps = snd_pcm_format_width(format) / 8;  /* bytes per sample */
-       
+       unsigned int chn;
+       int format_bits = snd_pcm_format_width(format);
+       unsigned int maxval = (1 << (format_bits - 1)) - 1;
+       int bps = format_bits / 8;  /* bytes per sample */
+       int phys_bps = snd_pcm_format_physical_width(format) / 8;
+       int big_endian = snd_pcm_format_big_endian(format) == 1;
+       int to_unsigned = snd_pcm_format_unsigned(format) == 1;
+       int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
+                       format == SND_PCM_FORMAT_FLOAT_BE);
+
        /* verify and prepare the contents of areas */
        for (chn = 0; chn < channels; chn++) {
                if ((areas[chn].first % 8) != 0) {
-                       printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
+                       printf("areas[%u].first == %u, aborting...\n", chn, areas[chn].first);
                        exit(EXIT_FAILURE);
                }
                samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
                if ((areas[chn].step % 16) != 0) {
-                       printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
+                       printf("areas[%u].step == %u, aborting...\n", chn, areas[chn].step);
                        exit(EXIT_FAILURE);
                }
                steps[chn] = areas[chn].step / 8;
@@ -57,12 +62,27 @@ static void generate_sine(const snd_pcm_channel_area_t *areas,
        }
        /* fill the channel areas */
        while (count-- > 0) {
-               res = sin(phase) * maxval;
-               ires = res;
-               tmp = (unsigned char *)(&ires);
+               union {
+                       float f;
+                       int i;
+               } fval;
+               int res, i;
+               if (is_float) {
+                       fval.f = sin(phase);
+                       res = fval.i;
+               } else
+                       res = sin(phase) * maxval;
+               if (to_unsigned)
+                       res ^= 1U << (format_bits - 1);
                for (chn = 0; chn < channels; chn++) {
-                       for (byte = 0; byte < bps; byte++)
-                               *(samples[chn] + byte) = tmp[byte];
+                       /* Generate data in native endian format */
+                       if (big_endian) {
+                               for (i = 0; i < bps; i++)
+                                       *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
+                       } else {
+                               for (i = 0; i < bps; i++)
+                                       *(samples[chn] + i) = (res >>  i * 8) & 0xff;
+                       }
                        samples[chn] += steps[chn];
                }
                phase += step;
@@ -77,6 +97,7 @@ static int set_hwparams(snd_pcm_t *handle,
                        snd_pcm_access_t access)
 {
        unsigned int rrate;
+       snd_pcm_uframes_t size;
        int err, dir;
 
        /* choose all parameters */
@@ -106,42 +127,44 @@ static int set_hwparams(snd_pcm_t *handle,
        /* set the count of channels */
        err = snd_pcm_hw_params_set_channels(handle, params, channels);
        if (err < 0) {
-               printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
+               printf("Channels count (%u) not available for playbacks: %s\n", channels, snd_strerror(err));
                return err;
        }
        /* set the stream rate */
        rrate = rate;
        err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
        if (err < 0) {
-               printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
+               printf("Rate %uHz not available for playback: %s\n", rate, snd_strerror(err));
                return err;
        }
        if (rrate != rate) {
-               printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
+               printf("Rate doesn't match (requested %uHz, get %iHz)\n", rate, err);
                return -EINVAL;
        }
        /* set the buffer time */
        err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
        if (err < 0) {
-               printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
+               printf("Unable to set buffer time %u for playback: %s\n", buffer_time, snd_strerror(err));
                return err;
        }
-       err = snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
+       err = snd_pcm_hw_params_get_buffer_size(params, &size);
        if (err < 0) {
                printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
                return err;
        }
+       buffer_size = size;
        /* set the period time */
        err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
        if (err < 0) {
-               printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
+               printf("Unable to set period time %u for playback: %s\n", period_time, snd_strerror(err));
                return err;
        }
-       err = snd_pcm_hw_params_get_period_size(params, &period_size, &dir);
+       err = snd_pcm_hw_params_get_period_size(params, &size, &dir);
        if (err < 0) {
                printf("Unable to get period size for playback: %s\n", snd_strerror(err));
                return err;
        }
+       period_size = size;
        /* write the parameters to device */
        err = snd_pcm_hw_params(handle, params);
        if (err < 0) {
@@ -169,16 +192,19 @@ static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
                return err;
        }
        /* allow the transfer when at least period_size samples can be processed */
-       err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_size);
+       /* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
+       err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
        if (err < 0) {
                printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
                return err;
        }
-       /* align all transfers to 1 sample */
-       err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
-       if (err < 0) {
-               printf("Unable to set transfer align for playback: %s\n", snd_strerror(err));
-               return err;
+       /* enable period events when requested */
+       if (period_event) {
+               err = snd_pcm_sw_params_set_period_event(handle, swparams, 1);
+               if (err < 0) {
+                       printf("Unable to set period event: %s\n", snd_strerror(err));
+                       return err;
+               }
        }
        /* write the parameters to the playback device */
        err = snd_pcm_sw_params(handle, swparams);
@@ -195,13 +221,15 @@ static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
  
 static int xrun_recovery(snd_pcm_t *handle, int err)
 {
+       if (verbose)
+               printf("stream recovery\n");
        if (err == -EPIPE) {    /* under-run */
                err = snd_pcm_prepare(handle);
                if (err < 0)
                        printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
                return 0;
        } else if (err == -ESTRPIPE) {
-               while ((err = snd_pcm_resume(handle)) == -EAGAIN || err == -EINTR)
+               while ((err = snd_pcm_resume(handle)) == -EAGAIN)
                        sleep(1);       /* wait until the suspend flag is released */
                if (err < 0) {
                        err = snd_pcm_prepare(handle);
@@ -231,7 +259,7 @@ static int write_loop(snd_pcm_t *handle,
                cptr = period_size;
                while (cptr > 0) {
                        err = snd_pcm_writei(handle, ptr, cptr);
-                       if (err == -EAGAIN || err == -EINTR)
+                       if (err == -EAGAIN)
                                continue;
                        if (err < 0) {
                                if (xrun_recovery(handle, err) < 0) {
@@ -373,11 +401,11 @@ static void async_callback(snd_async_handler_t *ahandler)
                generate_sine(areas, 0, period_size, &data->phase);
                err = snd_pcm_writei(handle, samples, period_size);
                if (err < 0) {
-                       printf("Initial write error: %s\n", snd_strerror(err));
+                       printf("Write error: %s\n", snd_strerror(err));
                        exit(EXIT_FAILURE);
                }
                if (err != period_size) {
-                       printf("Initial write error: written %i expected %li\n", err, period_size);
+                       printf("Write error: written %i expected %li\n", err, period_size);
                        exit(EXIT_FAILURE);
                }
                avail = snd_pcm_avail_update(handle);
@@ -412,10 +440,12 @@ static int async_loop(snd_pcm_t *handle,
                        exit(EXIT_FAILURE);
                }
        }
-       err = snd_pcm_start(handle);
-       if (err < 0) {
-               printf("Start error: %s\n", snd_strerror(err));
-               exit(EXIT_FAILURE);
+       if (snd_pcm_state(handle) == SND_PCM_STATE_PREPARED) {
+               err = snd_pcm_start(handle);
+               if (err < 0) {
+                       printf("Start error: %s\n", snd_strerror(err));
+                       exit(EXIT_FAILURE);
+               }
        }
 
        /* because all other work is done in the signal handler,
@@ -504,8 +534,8 @@ static void async_direct_callback(snd_async_handler_t *ahandler)
 }
 
 static int async_direct_loop(snd_pcm_t *handle,
-                            signed short *samples,
-                            snd_pcm_channel_area_t *areas)
+                            signed short *samples ATTRIBUTE_UNUSED,
+                            snd_pcm_channel_area_t *areas ATTRIBUTE_UNUSED)
 {
        struct async_private_data data;
        snd_async_handler_t *ahandler;
@@ -562,8 +592,8 @@ static int async_direct_loop(snd_pcm_t *handle,
  */
 
 static int direct_loop(snd_pcm_t *handle,
-                      signed short *samples,
-                      snd_pcm_channel_area_t *areas)
+                      signed short *samples ATTRIBUTE_UNUSED,
+                      snd_pcm_channel_area_t *areas ATTRIBUTE_UNUSED)
 {
        double phase = 0;
        const snd_pcm_channel_area_t *my_areas;
@@ -661,7 +691,7 @@ static int direct_write_loop(snd_pcm_t *handle,
                cptr = period_size;
                while (cptr > 0) {
                        err = snd_pcm_mmap_writei(handle, ptr, cptr);
-                       if (err == -EAGAIN || err == -EINTR)
+                       if (err == -EAGAIN)
                                continue;
                        if (err < 0) {
                                if (xrun_recovery(handle, err) < 0) {
@@ -714,9 +744,11 @@ static void help(void)
 "-m,--method   transfer method\n"
 "-o,--format   sample format\n"
 "-v,--verbose   show the PCM setup parameters\n"
+"-n,--noresample  do not resample\n"
+"-e,--pevent    enable poll event after each period\n"
 "\n");
         printf("Recognized sample formats are:");
-        for (k = 0; k < SND_PCM_FORMAT_LAST; ++(unsigned long) k) {
+        for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) {
                 const char *s = snd_pcm_format_name(k);
                 if (s)
                         printf(" %s", s);
@@ -743,6 +775,7 @@ int main(int argc, char *argv[])
                {"format", 1, NULL, 'o'},
                {"verbose", 1, NULL, 'v'},
                {"noresample", 1, NULL, 'n'},
+               {"pevent", 1, NULL, 'e'},
                {NULL, 0, NULL, 0},
        };
        snd_pcm_t *handle;
@@ -760,7 +793,7 @@ int main(int argc, char *argv[])
        morehelp = 0;
        while (1) {
                int c;
-               if ((c = getopt_long(argc, argv, "hD:r:c:f:b:p:m:o:vn", long_option, NULL)) < 0)
+               if ((c = getopt_long(argc, argv, "hD:r:c:f:b:p:m:o:vne", long_option, NULL)) < 0)
                        break;
                switch (c) {
                case 'h':
@@ -810,6 +843,13 @@ int main(int argc, char *argv[])
                        }
                        if (format == SND_PCM_FORMAT_LAST)
                                format = SND_PCM_FORMAT_S16;
+                       if (!snd_pcm_format_linear(format) &&
+                           !(format == SND_PCM_FORMAT_FLOAT_LE ||
+                             format == SND_PCM_FORMAT_FLOAT_BE)) {
+                               printf("Invalid (non-linear/float) format %s\n",
+                                      optarg);
+                               return 1;
+                       }
                        break;
                case 'v':
                        verbose = 1;
@@ -817,6 +857,9 @@ int main(int argc, char *argv[])
                case 'n':
                        resample = 0;
                        break;
+               case 'e':
+                       period_event = 1;
+                       break;
                }
        }
 
@@ -832,7 +875,7 @@ int main(int argc, char *argv[])
        }
 
        printf("Playback device is %s\n", device);
-       printf("Stream parameters are %iHz, %s, %i channels\n", rate, snd_pcm_format_name(format), channels);
+       printf("Stream parameters are %uHz, %s, %u channels\n", rate, snd_pcm_format_name(format), channels);
        printf("Sine wave rate is %.4fHz\n", freq);
        printf("Using transfer method: %s\n", transfer_methods[method].name);
 
@@ -853,7 +896,7 @@ int main(int argc, char *argv[])
        if (verbose > 0)
                snd_pcm_dump(handle, output);
 
-       samples = malloc((period_size * channels * snd_pcm_format_width(format)) / 8);
+       samples = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
        if (samples == NULL) {
                printf("No enough memory\n");
                exit(EXIT_FAILURE);
@@ -866,8 +909,8 @@ int main(int argc, char *argv[])
        }
        for (chn = 0; chn < channels; chn++) {
                areas[chn].addr = samples;
-               areas[chn].first = chn * snd_pcm_format_width(format);
-               areas[chn].step = channels * snd_pcm_format_width(format);
+               areas[chn].first = chn * snd_pcm_format_physical_width(format);
+               areas[chn].step = channels * snd_pcm_format_physical_width(format);
        }
 
        err = transfer_methods[method].transfer_loop(handle, samples, areas);