OSDN Git Service

added support for more sample widths
[android-x86/external-alsa-lib.git] / test / pcm.c
1 /*
2  *  This small demo sends a simple sinusoidal wave to your speakers.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sched.h>
9 #include <errno.h>
10 #include <getopt.h>
11 #include "../include/asoundlib.h"
12 #include <sys/time.h>
13 #include <math.h>
14
15 char *device = "plughw:0,0";                    /* playback device */
16 snd_pcm_format_t format = SND_PCM_FORMAT_S16;   /* sample format */
17 unsigned int rate = 44100;                      /* stream rate */
18 unsigned int channels = 1;                      /* count of channels */
19 unsigned int buffer_time = 500000;              /* ring buffer length in us */
20 unsigned int period_time = 100000;              /* period time in us */
21 double freq = 440;                              /* sinusoidal wave frequency in Hz */
22 int verbose = 0;                                /* verbose flag */
23 int resample = 1;                               /* enable alsa-lib resampling */
24
25 snd_pcm_sframes_t buffer_size;
26 snd_pcm_sframes_t period_size;
27 snd_output_t *output = NULL;
28
29 static void generate_sine(const snd_pcm_channel_area_t *areas, 
30                           snd_pcm_uframes_t offset,
31                           int count, double *_phase)
32 {
33         static double max_phase = 2. * M_PI;
34         double phase = *_phase;
35         double step = max_phase*freq/(double)rate;
36         double res;
37         unsigned char *samples[channels], *tmp;
38         int steps[channels];
39         unsigned int chn, byte;
40         int ires;
41         unsigned int maxval = (1 << (snd_pcm_format_width(format) - 1)) - 1;
42         int bps = snd_pcm_format_width(format) / 8;  /* bytes per sample */
43         
44         /* verify and prepare the contents of areas */
45         for (chn = 0; chn < channels; chn++) {
46                 if ((areas[chn].first % 8) != 0) {
47                         printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
48                         exit(EXIT_FAILURE);
49                 }
50                 samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
51                 if ((areas[chn].step % 16) != 0) {
52                         printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
53                         exit(EXIT_FAILURE);
54                 }
55                 steps[chn] = areas[chn].step / 8;
56                 samples[chn] += offset * steps[chn];
57         }
58         /* fill the channel areas */
59         while (count-- > 0) {
60                 res = sin(phase) * maxval;
61                 ires = res;
62                 tmp = (unsigned char *)(&ires);
63                 for (chn = 0; chn < channels; chn++) {
64                         for (byte = 0; byte < bps; byte++)
65                                 *(samples[chn] + byte) = tmp[byte];
66                         samples[chn] += steps[chn];
67                 }
68                 phase += step;
69                 if (phase >= max_phase)
70                         phase -= max_phase;
71         }
72         *_phase = phase;
73 }
74
75 static int set_hwparams(snd_pcm_t *handle,
76                         snd_pcm_hw_params_t *params,
77                         snd_pcm_access_t access)
78 {
79         unsigned int rrate;
80         int err, dir;
81
82         /* choose all parameters */
83         err = snd_pcm_hw_params_any(handle, params);
84         if (err < 0) {
85                 printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
86                 return err;
87         }
88         /* set hardware resampling */
89         err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
90         if (err < 0) {
91                 printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
92                 return err;
93         }
94         /* set the interleaved read/write format */
95         err = snd_pcm_hw_params_set_access(handle, params, access);
96         if (err < 0) {
97                 printf("Access type not available for playback: %s\n", snd_strerror(err));
98                 return err;
99         }
100         /* set the sample format */
101         err = snd_pcm_hw_params_set_format(handle, params, format);
102         if (err < 0) {
103                 printf("Sample format not available for playback: %s\n", snd_strerror(err));
104                 return err;
105         }
106         /* set the count of channels */
107         err = snd_pcm_hw_params_set_channels(handle, params, channels);
108         if (err < 0) {
109                 printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
110                 return err;
111         }
112         /* set the stream rate */
113         rrate = rate;
114         err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
115         if (err < 0) {
116                 printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
117                 return err;
118         }
119         if (rrate != rate) {
120                 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
121                 return -EINVAL;
122         }
123         /* set the buffer time */
124         err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
125         if (err < 0) {
126                 printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
127                 return err;
128         }
129         err = snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
130         if (err < 0) {
131                 printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
132                 return err;
133         }
134         /* set the period time */
135         err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
136         if (err < 0) {
137                 printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
138                 return err;
139         }
140         err = snd_pcm_hw_params_get_period_size(params, &period_size, &dir);
141         if (err < 0) {
142                 printf("Unable to get period size for playback: %s\n", snd_strerror(err));
143                 return err;
144         }
145         /* write the parameters to device */
146         err = snd_pcm_hw_params(handle, params);
147         if (err < 0) {
148                 printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
149                 return err;
150         }
151         return 0;
152 }
153
154 static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
155 {
156         int err;
157
158         /* get the current swparams */
159         err = snd_pcm_sw_params_current(handle, swparams);
160         if (err < 0) {
161                 printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
162                 return err;
163         }
164         /* start the transfer when the buffer is almost full: */
165         /* (buffer_size / avail_min) * avail_min */
166         err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
167         if (err < 0) {
168                 printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
169                 return err;
170         }
171         /* allow the transfer when at least period_size samples can be processed */
172         err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_size);
173         if (err < 0) {
174                 printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
175                 return err;
176         }
177         /* align all transfers to 1 sample */
178         err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
179         if (err < 0) {
180                 printf("Unable to set transfer align for playback: %s\n", snd_strerror(err));
181                 return err;
182         }
183         /* write the parameters to the playback device */
184         err = snd_pcm_sw_params(handle, swparams);
185         if (err < 0) {
186                 printf("Unable to set sw params for playback: %s\n", snd_strerror(err));
187                 return err;
188         }
189         return 0;
190 }
191
192 /*
193  *   Underrun and suspend recovery
194  */
195  
196 static int xrun_recovery(snd_pcm_t *handle, int err)
197 {
198         if (err == -EPIPE) {    /* under-run */
199                 err = snd_pcm_prepare(handle);
200                 if (err < 0)
201                         printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
202                 return 0;
203         } else if (err == -ESTRPIPE) {
204                 while ((err = snd_pcm_resume(handle)) == -EAGAIN)
205                         sleep(1);       /* wait until the suspend flag is released */
206                 if (err < 0) {
207                         err = snd_pcm_prepare(handle);
208                         if (err < 0)
209                                 printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
210                 }
211                 return 0;
212         }
213         return err;
214 }
215
216 /*
217  *   Transfer method - write only
218  */
219
220 static int write_loop(snd_pcm_t *handle,
221                       signed short *samples,
222                       snd_pcm_channel_area_t *areas)
223 {
224         double phase = 0;
225         signed short *ptr;
226         int err, cptr;
227
228         while (1) {
229                 generate_sine(areas, 0, period_size, &phase);
230                 ptr = samples;
231                 cptr = period_size;
232                 while (cptr > 0) {
233                         err = snd_pcm_writei(handle, ptr, cptr);
234                         if (err == -EAGAIN)
235                                 continue;
236                         if (err < 0) {
237                                 if (xrun_recovery(handle, err) < 0) {
238                                         printf("Write error: %s\n", snd_strerror(err));
239                                         exit(EXIT_FAILURE);
240                                 }
241                                 break;  /* skip one period */
242                         }
243                         ptr += err * channels;
244                         cptr -= err;
245                 }
246         }
247 }
248  
249 /*
250  *   Transfer method - write and wait for room in buffer using poll
251  */
252
253 static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
254 {
255         unsigned short revents;
256
257         while (1) {
258                 poll(ufds, count, -1);
259                 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
260                 if (revents & POLLERR)
261                         return -EIO;
262                 if (revents & POLLOUT)
263                         return 0;
264         }
265 }
266
267 static int write_and_poll_loop(snd_pcm_t *handle,
268                                signed short *samples,
269                                snd_pcm_channel_area_t *areas)
270 {
271         struct pollfd *ufds;
272         double phase = 0;
273         signed short *ptr;
274         int err, count, cptr, init;
275
276         count = snd_pcm_poll_descriptors_count (handle);
277         if (count <= 0) {
278                 printf("Invalid poll descriptors count\n");
279                 return count;
280         }
281
282         ufds = malloc(sizeof(struct pollfd) * count);
283         if (ufds == NULL) {
284                 printf("No enough memory\n");
285                 return -ENOMEM;
286         }
287         if ((err = snd_pcm_poll_descriptors(handle, ufds, count)) < 0) {
288                 printf("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
289                 return err;
290         }
291
292         init = 1;
293         while (1) {
294                 if (!init) {
295                         err = wait_for_poll(handle, ufds, count);
296                         if (err < 0) {
297                                 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
298                                     snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
299                                         err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
300                                         if (xrun_recovery(handle, err) < 0) {
301                                                 printf("Write error: %s\n", snd_strerror(err));
302                                                 exit(EXIT_FAILURE);
303                                         }
304                                         init = 1;
305                                 } else {
306                                         printf("Wait for poll failed\n");
307                                         return err;
308                                 }
309                         }
310                 }
311
312                 generate_sine(areas, 0, period_size, &phase);
313                 ptr = samples;
314                 cptr = period_size;
315                 while (cptr > 0) {
316                         err = snd_pcm_writei(handle, ptr, cptr);
317                         if (err < 0) {
318                                 if (xrun_recovery(handle, err) < 0) {
319                                         printf("Write error: %s\n", snd_strerror(err));
320                                         exit(EXIT_FAILURE);
321                                 }
322                                 init = 1;
323                                 break;  /* skip one period */
324                         }
325                         if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING)
326                                 init = 0;
327                         ptr += err * channels;
328                         cptr -= err;
329                         if (cptr == 0)
330                                 break;
331                         /* it is possible, that the initial buffer cannot store */
332                         /* all data from the last period, so wait awhile */
333                         err = wait_for_poll(handle, ufds, count);
334                         if (err < 0) {
335                                 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
336                                     snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
337                                         err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
338                                         if (xrun_recovery(handle, err) < 0) {
339                                                 printf("Write error: %s\n", snd_strerror(err));
340                                                 exit(EXIT_FAILURE);
341                                         }
342                                         init = 1;
343                                 } else {
344                                         printf("Wait for poll failed\n");
345                                         return err;
346                                 }
347                         }
348                 }
349         }
350 }
351
352 /*
353  *   Transfer method - asynchronous notification
354  */
355
356 struct async_private_data {
357         signed short *samples;
358         snd_pcm_channel_area_t *areas;
359         double phase;
360 };
361
362 static void async_callback(snd_async_handler_t *ahandler)
363 {
364         snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
365         struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
366         signed short *samples = data->samples;
367         snd_pcm_channel_area_t *areas = data->areas;
368         snd_pcm_sframes_t avail;
369         int err;
370         
371         avail = snd_pcm_avail_update(handle);
372         while (avail >= period_size) {
373                 generate_sine(areas, 0, period_size, &data->phase);
374                 err = snd_pcm_writei(handle, samples, period_size);
375                 if (err < 0) {
376                         printf("Initial write error: %s\n", snd_strerror(err));
377                         exit(EXIT_FAILURE);
378                 }
379                 if (err != period_size) {
380                         printf("Initial write error: written %i expected %li\n", err, period_size);
381                         exit(EXIT_FAILURE);
382                 }
383                 avail = snd_pcm_avail_update(handle);
384         }
385 }
386
387 static int async_loop(snd_pcm_t *handle,
388                       signed short *samples,
389                       snd_pcm_channel_area_t *areas)
390 {
391         struct async_private_data data;
392         snd_async_handler_t *ahandler;
393         int err, count;
394
395         data.samples = samples;
396         data.areas = areas;
397         data.phase = 0;
398         err = snd_async_add_pcm_handler(&ahandler, handle, async_callback, &data);
399         if (err < 0) {
400                 printf("Unable to register async handler\n");
401                 exit(EXIT_FAILURE);
402         }
403         for (count = 0; count < 2; count++) {
404                 generate_sine(areas, 0, period_size, &data.phase);
405                 err = snd_pcm_writei(handle, samples, period_size);
406                 if (err < 0) {
407                         printf("Initial write error: %s\n", snd_strerror(err));
408                         exit(EXIT_FAILURE);
409                 }
410                 if (err != period_size) {
411                         printf("Initial write error: written %i expected %li\n", err, period_size);
412                         exit(EXIT_FAILURE);
413                 }
414         }
415         err = snd_pcm_start(handle);
416         if (err < 0) {
417                 printf("Start error: %s\n", snd_strerror(err));
418                 exit(EXIT_FAILURE);
419         }
420
421         /* because all other work is done in the signal handler,
422            suspend the process */
423         while (1) {
424                 sleep(1);
425         }
426 }
427
428 /*
429  *   Transfer method - asynchronous notification + direct write
430  */
431
432 static void async_direct_callback(snd_async_handler_t *ahandler)
433 {
434         snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
435         struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
436         const snd_pcm_channel_area_t *my_areas;
437         snd_pcm_uframes_t offset, frames, size;
438         snd_pcm_sframes_t avail, commitres;
439         snd_pcm_state_t state;
440         int first = 0, err;
441         
442         while (1) {
443                 state = snd_pcm_state(handle);
444                 if (state == SND_PCM_STATE_XRUN) {
445                         err = xrun_recovery(handle, -EPIPE);
446                         if (err < 0) {
447                                 printf("XRUN recovery failed: %s\n", snd_strerror(err));
448                                 exit(EXIT_FAILURE);
449                         }
450                         first = 1;
451                 } else if (state == SND_PCM_STATE_SUSPENDED) {
452                         err = xrun_recovery(handle, -ESTRPIPE);
453                         if (err < 0) {
454                                 printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
455                                 exit(EXIT_FAILURE);
456                         }
457                 }
458                 avail = snd_pcm_avail_update(handle);
459                 if (avail < 0) {
460                         err = xrun_recovery(handle, avail);
461                         if (err < 0) {
462                                 printf("avail update failed: %s\n", snd_strerror(err));
463                                 exit(EXIT_FAILURE);
464                         }
465                         first = 1;
466                         continue;
467                 }
468                 if (avail < period_size) {
469                         if (first) {
470                                 first = 0;
471                                 err = snd_pcm_start(handle);
472                                 if (err < 0) {
473                                         printf("Start error: %s\n", snd_strerror(err));
474                                         exit(EXIT_FAILURE);
475                                 }
476                         } else {
477                                 break;
478                         }
479                         continue;
480                 }
481                 size = period_size;
482                 while (size > 0) {
483                         frames = size;
484                         err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
485                         if (err < 0) {
486                                 if ((err = xrun_recovery(handle, err)) < 0) {
487                                         printf("MMAP begin avail error: %s\n", snd_strerror(err));
488                                         exit(EXIT_FAILURE);
489                                 }
490                                 first = 1;
491                         }
492                         generate_sine(my_areas, offset, frames, &data->phase);
493                         commitres = snd_pcm_mmap_commit(handle, offset, frames);
494                         if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
495                                 if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
496                                         printf("MMAP commit error: %s\n", snd_strerror(err));
497                                         exit(EXIT_FAILURE);
498                                 }
499                                 first = 1;
500                         }
501                         size -= frames;
502                 }
503         }
504 }
505
506 static int async_direct_loop(snd_pcm_t *handle,
507                              signed short *samples,
508                              snd_pcm_channel_area_t *areas)
509 {
510         struct async_private_data data;
511         snd_async_handler_t *ahandler;
512         const snd_pcm_channel_area_t *my_areas;
513         snd_pcm_uframes_t offset, frames, size;
514         snd_pcm_sframes_t commitres;
515         int err, count;
516
517         data.samples = NULL;    /* we do not require the global sample area for direct write */
518         data.areas = NULL;      /* we do not require the global areas for direct write */
519         data.phase = 0;
520         err = snd_async_add_pcm_handler(&ahandler, handle, async_direct_callback, &data);
521         if (err < 0) {
522                 printf("Unable to register async handler\n");
523                 exit(EXIT_FAILURE);
524         }
525         for (count = 0; count < 2; count++) {
526                 size = period_size;
527                 while (size > 0) {
528                         frames = size;
529                         err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
530                         if (err < 0) {
531                                 if ((err = xrun_recovery(handle, err)) < 0) {
532                                         printf("MMAP begin avail error: %s\n", snd_strerror(err));
533                                         exit(EXIT_FAILURE);
534                                 }
535                         }
536                         generate_sine(my_areas, offset, frames, &data.phase);
537                         commitres = snd_pcm_mmap_commit(handle, offset, frames);
538                         if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
539                                 if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
540                                         printf("MMAP commit error: %s\n", snd_strerror(err));
541                                         exit(EXIT_FAILURE);
542                                 }
543                         }
544                         size -= frames;
545                 }
546         }
547         err = snd_pcm_start(handle);
548         if (err < 0) {
549                 printf("Start error: %s\n", snd_strerror(err));
550                 exit(EXIT_FAILURE);
551         }
552
553         /* because all other work is done in the signal handler,
554            suspend the process */
555         while (1) {
556                 sleep(1);
557         }
558 }
559
560 /*
561  *   Transfer method - direct write only
562  */
563
564 static int direct_loop(snd_pcm_t *handle,
565                        signed short *samples,
566                        snd_pcm_channel_area_t *areas)
567 {
568         double phase = 0;
569         const snd_pcm_channel_area_t *my_areas;
570         snd_pcm_uframes_t offset, frames, size;
571         snd_pcm_sframes_t avail, commitres;
572         snd_pcm_state_t state;
573         int err, first = 1;
574
575         while (1) {
576                 state = snd_pcm_state(handle);
577                 if (state == SND_PCM_STATE_XRUN) {
578                         err = xrun_recovery(handle, -EPIPE);
579                         if (err < 0) {
580                                 printf("XRUN recovery failed: %s\n", snd_strerror(err));
581                                 return err;
582                         }
583                         first = 1;
584                 } else if (state == SND_PCM_STATE_SUSPENDED) {
585                         err = xrun_recovery(handle, -ESTRPIPE);
586                         if (err < 0) {
587                                 printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
588                                 return err;
589                         }
590                 }
591                 avail = snd_pcm_avail_update(handle);
592                 if (avail < 0) {
593                         err = xrun_recovery(handle, avail);
594                         if (err < 0) {
595                                 printf("avail update failed: %s\n", snd_strerror(err));
596                                 return err;
597                         }
598                         first = 1;
599                         continue;
600                 }
601                 if (avail < period_size) {
602                         if (first) {
603                                 first = 0;
604                                 err = snd_pcm_start(handle);
605                                 if (err < 0) {
606                                         printf("Start error: %s\n", snd_strerror(err));
607                                         exit(EXIT_FAILURE);
608                                 }
609                         } else {
610                                 err = snd_pcm_wait(handle, -1);
611                                 if (err < 0) {
612                                         if ((err = xrun_recovery(handle, err)) < 0) {
613                                                 printf("snd_pcm_wait error: %s\n", snd_strerror(err));
614                                                 exit(EXIT_FAILURE);
615                                         }
616                                         first = 1;
617                                 }
618                         }
619                         continue;
620                 }
621                 size = period_size;
622                 while (size > 0) {
623                         frames = size;
624                         err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
625                         if (err < 0) {
626                                 if ((err = xrun_recovery(handle, err)) < 0) {
627                                         printf("MMAP begin avail error: %s\n", snd_strerror(err));
628                                         exit(EXIT_FAILURE);
629                                 }
630                                 first = 1;
631                         }
632                         generate_sine(my_areas, offset, frames, &phase);
633                         commitres = snd_pcm_mmap_commit(handle, offset, frames);
634                         if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
635                                 if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
636                                         printf("MMAP commit error: %s\n", snd_strerror(err));
637                                         exit(EXIT_FAILURE);
638                                 }
639                                 first = 1;
640                         }
641                         size -= frames;
642                 }
643         }
644 }
645  
646 /*
647  *   Transfer method - direct write only using mmap_write functions
648  */
649
650 static int direct_write_loop(snd_pcm_t *handle,
651                              signed short *samples,
652                              snd_pcm_channel_area_t *areas)
653 {
654         double phase = 0;
655         signed short *ptr;
656         int err, cptr;
657
658         while (1) {
659                 generate_sine(areas, 0, period_size, &phase);
660                 ptr = samples;
661                 cptr = period_size;
662                 while (cptr > 0) {
663                         err = snd_pcm_mmap_writei(handle, ptr, cptr);
664                         if (err == -EAGAIN)
665                                 continue;
666                         if (err < 0) {
667                                 if (xrun_recovery(handle, err) < 0) {
668                                         printf("Write error: %s\n", snd_strerror(err));
669                                         exit(EXIT_FAILURE);
670                                 }
671                                 break;  /* skip one period */
672                         }
673                         ptr += err * channels;
674                         cptr -= err;
675                 }
676         }
677 }
678  
679 /*
680  *
681  */
682
683 struct transfer_method {
684         const char *name;
685         snd_pcm_access_t access;
686         int (*transfer_loop)(snd_pcm_t *handle,
687                              signed short *samples,
688                              snd_pcm_channel_area_t *areas);
689 };
690
691 static struct transfer_method transfer_methods[] = {
692         { "write", SND_PCM_ACCESS_RW_INTERLEAVED, write_loop },
693         { "write_and_poll", SND_PCM_ACCESS_RW_INTERLEAVED, write_and_poll_loop },
694         { "async", SND_PCM_ACCESS_RW_INTERLEAVED, async_loop },
695         { "async_direct", SND_PCM_ACCESS_MMAP_INTERLEAVED, async_direct_loop },
696         { "direct_interleaved", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_loop },
697         { "direct_noninterleaved", SND_PCM_ACCESS_MMAP_NONINTERLEAVED, direct_loop },
698         { "direct_write", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_write_loop },
699         { NULL, SND_PCM_ACCESS_RW_INTERLEAVED, NULL }
700 };
701
702 static void help(void)
703 {
704         int k;
705         printf(
706 "Usage: pcm [OPTION]... [FILE]...\n"
707 "-h,--help      help\n"
708 "-D,--device    playback device\n"
709 "-r,--rate      stream rate in Hz\n"
710 "-c,--channels  count of channels in stream\n"
711 "-f,--frequency sine wave frequency in Hz\n"
712 "-b,--buffer    ring buffer size in us\n"
713 "-p,--period    period size in us\n"
714 "-m,--method    transfer method\n"
715 "-o,--format    sample format\n"
716 "-v,--verbose   show the PCM setup parameters\n"
717 "\n");
718         printf("Recognized sample formats are:");
719         for (k = 0; k < SND_PCM_FORMAT_LAST; ++(unsigned long) k) {
720                 const char *s = snd_pcm_format_name(k);
721                 if (s)
722                         printf(" %s", s);
723         }
724         printf("\n");
725         printf("Recognized transfer methods are:");
726         for (k = 0; transfer_methods[k].name; k++)
727                 printf(" %s", transfer_methods[k].name);
728         printf("\n");
729 }
730
731 int main(int argc, char *argv[])
732 {
733         struct option long_option[] =
734         {
735                 {"help", 0, NULL, 'h'},
736                 {"device", 1, NULL, 'D'},
737                 {"rate", 1, NULL, 'r'},
738                 {"channels", 1, NULL, 'c'},
739                 {"frequency", 1, NULL, 'f'},
740                 {"buffer", 1, NULL, 'b'},
741                 {"period", 1, NULL, 'p'},
742                 {"method", 1, NULL, 'm'},
743                 {"format", 1, NULL, 'o'},
744                 {"verbose", 1, NULL, 'v'},
745                 {"noresample", 1, NULL, 'n'},
746                 {NULL, 0, NULL, 0},
747         };
748         snd_pcm_t *handle;
749         int err, morehelp;
750         snd_pcm_hw_params_t *hwparams;
751         snd_pcm_sw_params_t *swparams;
752         int method = 0;
753         signed short *samples;
754         unsigned int chn;
755         snd_pcm_channel_area_t *areas;
756
757         snd_pcm_hw_params_alloca(&hwparams);
758         snd_pcm_sw_params_alloca(&swparams);
759
760         morehelp = 0;
761         while (1) {
762                 int c;
763                 if ((c = getopt_long(argc, argv, "hD:r:c:f:b:p:m:o:vn", long_option, NULL)) < 0)
764                         break;
765                 switch (c) {
766                 case 'h':
767                         morehelp++;
768                         break;
769                 case 'D':
770                         device = strdup(optarg);
771                         break;
772                 case 'r':
773                         rate = atoi(optarg);
774                         rate = rate < 4000 ? 4000 : rate;
775                         rate = rate > 196000 ? 196000 : rate;
776                         break;
777                 case 'c':
778                         channels = atoi(optarg);
779                         channels = channels < 1 ? 1 : channels;
780                         channels = channels > 1024 ? 1024 : channels;
781                         break;
782                 case 'f':
783                         freq = atoi(optarg);
784                         freq = freq < 50 ? 50 : freq;
785                         freq = freq > 5000 ? 5000 : freq;
786                         break;
787                 case 'b':
788                         buffer_time = atoi(optarg);
789                         buffer_time = buffer_time < 1000 ? 1000 : buffer_time;
790                         buffer_time = buffer_time > 1000000 ? 1000000 : buffer_time;
791                         break;
792                 case 'p':
793                         period_time = atoi(optarg);
794                         period_time = period_time < 1000 ? 1000 : period_time;
795                         period_time = period_time > 1000000 ? 1000000 : period_time;
796                         break;
797                 case 'm':
798                         for (method = 0; transfer_methods[method].name; method++)
799                                         if (!strcasecmp(transfer_methods[method].name, optarg))
800                                         break;
801                         if (transfer_methods[method].name == NULL)
802                                 method = 0;
803                         break;
804                 case 'o':
805                         for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {
806                                 const char *format_name = snd_pcm_format_name(format);
807                                 if (format_name)
808                                         if (!strcasecmp(format_name, optarg))
809                                         break;
810                         }
811                         if (format == SND_PCM_FORMAT_LAST)
812                                 format = SND_PCM_FORMAT_S16;
813                         break;
814                 case 'v':
815                         verbose = 1;
816                         break;
817                 case 'n':
818                         resample = 0;
819                         break;
820                 }
821         }
822
823         if (morehelp) {
824                 help();
825                 return 0;
826         }
827
828         err = snd_output_stdio_attach(&output, stdout, 0);
829         if (err < 0) {
830                 printf("Output failed: %s\n", snd_strerror(err));
831                 return 0;
832         }
833
834         printf("Playback device is %s\n", device);
835         printf("Stream parameters are %iHz, %s, %i channels\n", rate, snd_pcm_format_name(format), channels);
836         printf("Sine wave rate is %.4fHz\n", freq);
837         printf("Using transfer method: %s\n", transfer_methods[method].name);
838
839         if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
840                 printf("Playback open error: %s\n", snd_strerror(err));
841                 return 0;
842         }
843         
844         if ((err = set_hwparams(handle, hwparams, transfer_methods[method].access)) < 0) {
845                 printf("Setting of hwparams failed: %s\n", snd_strerror(err));
846                 exit(EXIT_FAILURE);
847         }
848         if ((err = set_swparams(handle, swparams)) < 0) {
849                 printf("Setting of swparams failed: %s\n", snd_strerror(err));
850                 exit(EXIT_FAILURE);
851         }
852
853         if (verbose > 0)
854                 snd_pcm_dump(handle, output);
855
856         samples = malloc((period_size * channels * snd_pcm_format_width(format)) / 8);
857         if (samples == NULL) {
858                 printf("No enough memory\n");
859                 exit(EXIT_FAILURE);
860         }
861         
862         areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
863         if (areas == NULL) {
864                 printf("No enough memory\n");
865                 exit(EXIT_FAILURE);
866         }
867         for (chn = 0; chn < channels; chn++) {
868                 areas[chn].addr = samples;
869                 areas[chn].first = chn * snd_pcm_format_width(format);
870                 areas[chn].step = channels * snd_pcm_format_width(format);
871         }
872
873         err = transfer_methods[method].transfer_loop(handle, samples, areas);
874         if (err < 0)
875                 printf("Transfer failed: %s\n", snd_strerror(err));
876
877         free(areas);
878         free(samples);
879         snd_pcm_close(handle);
880         return 0;
881 }
882