OSDN Git Service

Switch from old to new PCM API retaining binary compatibility
[android-x86/external-alsa-lib.git] / test / latency.c
1 /*
2  *  Latency test program
3  *
4  *     Author: Jaroslav Kysela <perex@suse.cz>
5  *
6  *     Author of bandpass filter sweep effect:
7  *             Maarten de Boer <mdeboer@iua.upf.es>
8  *
9  *  This small demo program can be used for measuring latency between
10  *  capture and playback. This latency is measured from driver (diff when
11  *  playback and capture was started). Scheduler is set to SCHED_RR.
12  *
13  *
14  *   This program is free software; you can redistribute it and/or modify
15  *   it under the terms of the GNU General Public License as published by
16  *   the Free Software Foundation; either version 2 of the License, or
17  *   (at your option) any later version.
18  *
19  *   This program is distributed in the hope that it will be useful,
20  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   GNU General Public License for more details.
23  *
24  *   You should have received a copy of the GNU General Public License
25  *   along with this program; if not, write to the Free Software
26  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
27  *
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sched.h>
34 #include <errno.h>
35 #include <getopt.h>
36 #include "../include/asoundlib.h"
37 #include <sys/time.h>
38 #include <math.h>
39
40 char *pdevice = "hw:0,0";
41 char *cdevice = "hw:0,0";
42 snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
43 int rate = 22050;
44 int channels = 2;
45 int latency_min = 32;           /* in frames / 2 */
46 int latency_max = 2048;         /* in frames / 2 */
47 int loop_sec = 30;              /* seconds */
48 int block = 0;                  /* block mode */
49 int tick_time = 0;              /* disabled, otherwise in us */
50 int tick_time_ok = 0;
51 int use_poll = 0;
52 unsigned long loop_limit;
53
54 snd_output_t *output = NULL;
55
56 int setparams_stream(snd_pcm_t *handle,
57                      snd_pcm_hw_params_t *params,
58                      const char *id)
59 {
60         int err;
61         unsigned int rrate;
62
63         err = snd_pcm_hw_params_any(handle, params);
64         if (err < 0) {
65                 printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id);
66                 return err;
67         }
68         err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
69         if (err < 0) {
70                 printf("Access type not available for %s: %s\n", id, snd_strerror(err));
71                 return err;
72         }
73         err = snd_pcm_hw_params_set_format(handle, params, format);
74         if (err < 0) {
75                 printf("Sample format not available for %s: %s\n", id, snd_strerror(err));
76                 return err;
77         }
78         err = snd_pcm_hw_params_set_channels(handle, params, channels);
79         if (err < 0) {
80                 printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err));
81                 return err;
82         }
83         rrate = rate;
84         err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
85         if (err < 0) {
86                 printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err));
87                 return err;
88         }
89         if (rrate != rate) {
90                 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
91                 return -EINVAL;
92         }
93         return 0;
94 }
95
96 int setparams_bufsize(snd_pcm_t *handle,
97                       snd_pcm_hw_params_t *params,
98                       snd_pcm_hw_params_t *tparams,
99                       snd_pcm_uframes_t bufsize,
100                       const char *id)
101 {
102         int err;
103         snd_pcm_uframes_t periodsize;
104
105         snd_pcm_hw_params_copy(params, tparams);
106         periodsize = bufsize * 2;
107         err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &periodsize);
108         if (err < 0) {
109                 printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err));
110                 return err;
111         }
112         periodsize /= 2;
113         err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0);
114         if (err < 0) {
115                 printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err));
116                 return err;
117         }
118         return 0;
119 }
120
121 int setparams_set(snd_pcm_t *handle,
122                   snd_pcm_hw_params_t *params,
123                   snd_pcm_sw_params_t *swparams,
124                   const char *id)
125 {
126         int err;
127         snd_pcm_uframes_t val;
128         unsigned int sleep_min = 0;
129
130         err = snd_pcm_hw_params(handle, params);
131         if (err < 0) {
132                 printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err));
133                 return err;
134         }
135         err = snd_pcm_sw_params_current(handle, swparams);
136         if (err < 0) {
137                 printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err));
138                 return err;
139         }
140         err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff);
141         if (err < 0) {
142                 printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err));
143                 return err;
144         }
145         tick_time_ok = 0;
146         if (tick_time > 0) {
147                 int time, ttime;
148                 snd_pcm_hw_params_get_period_time(params, &time, NULL);
149                  snd_pcm_hw_params_get_tick_time(params, &ttime, NULL);
150                 if (time < ttime) {
151                         printf("Skipping to set minimal sleep: period time < tick time\n");
152                 } else if (ttime <= 0) {
153                         printf("Skipping to set minimal sleep: tick time <= 0 (%i)\n", ttime);
154                 } else {
155                         sleep_min = tick_time / ttime;
156                         if (sleep_min <= 0)
157                                 sleep_min = 1;
158                         err = snd_pcm_sw_params_set_sleep_min(handle, swparams, sleep_min);
159                         if (err < 0) {
160                                 printf("Unable to set minimal sleep %i for %s: %s\n", sleep_min, id, snd_strerror(err));
161                                 return err;
162                         }
163                         tick_time_ok = sleep_min * ttime;
164                 }
165         }
166         if (!block)
167                 val = 4;
168         else
169                 snd_pcm_hw_params_get_period_size(params, &val, NULL);
170         if (tick_time_ok > 0)
171                 val = 16;
172         err = snd_pcm_sw_params_set_avail_min(handle, swparams, val);
173         if (err < 0) {
174                 printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err));
175                 return err;
176         }
177         val = !block ? 4 : 1;
178         err = snd_pcm_sw_params_set_xfer_align(handle, swparams, val);
179         if (err < 0) {
180                 printf("Unable to set transfer align for %s: %s\n", id, snd_strerror(err));
181                 return err;
182         }
183         err = snd_pcm_sw_params(handle, swparams);
184         if (err < 0) {
185                 printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err));
186                 return err;
187         }
188         return 0;
189 }
190
191 int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize)
192 {
193         int err, last_bufsize = *bufsize;
194         snd_pcm_hw_params_t *pt_params, *ct_params;     /* templates with rate, format and channels */
195         snd_pcm_hw_params_t *p_params, *c_params;
196         snd_pcm_sw_params_t *p_swparams, *c_swparams;
197         snd_pcm_uframes_t size, p_size, c_size, p_psize, c_psize;
198         unsigned int p_time, c_time;
199
200         snd_pcm_hw_params_alloca(&p_params);
201         snd_pcm_hw_params_alloca(&c_params);
202         snd_pcm_hw_params_alloca(&pt_params);
203         snd_pcm_hw_params_alloca(&ct_params);
204         snd_pcm_sw_params_alloca(&p_swparams);
205         snd_pcm_sw_params_alloca(&c_swparams);
206         if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) {
207                 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
208                 exit(0);
209         }
210         if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
211                 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
212                 exit(0);
213         }
214
215       __again:
216         if (last_bufsize == *bufsize)
217                 *bufsize += 4;
218         last_bufsize = *bufsize;
219         if (*bufsize > latency_max)
220                 return -1;
221         if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) {
222                 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
223                 exit(0);
224         }
225         if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) {
226                 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
227                 exit(0);
228         }
229
230         snd_pcm_hw_params_get_period_size(p_params, &size, NULL);
231         if (size > *bufsize)
232                 *bufsize = size;
233         snd_pcm_hw_params_get_period_size(c_params, &size, NULL);
234         if (size > *bufsize)
235                 *bufsize = size;
236         snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL);
237         snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL);
238         if (p_time != c_time)
239                 goto __again;
240
241         snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
242         snd_pcm_hw_params_get_buffer_size(p_params, &p_size);
243         if (p_psize * 2 < p_size)
244                 goto __again;
245         snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
246         snd_pcm_hw_params_get_buffer_size(c_params, &c_size);
247         if (c_psize * 2 < c_size)
248                 goto __again;
249
250         if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) {
251                 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
252                 exit(0);
253         }
254         if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) {
255                 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
256                 exit(0);
257         }
258
259         if ((err = snd_pcm_prepare(phandle)) < 0) {
260                 printf("Prepare error: %s\n", snd_strerror(err));
261                 exit(0);
262         }
263
264         snd_pcm_dump(phandle, output);
265         snd_pcm_dump(chandle, output);
266         fflush(stdout);
267         return 0;
268 }
269
270 void showstat(snd_pcm_t *handle, size_t frames)
271 {
272         int err;
273         snd_pcm_status_t *status;
274
275         snd_pcm_status_alloca(&status);
276         if ((err = snd_pcm_status(handle, status)) < 0) {
277                 printf("Stream status error: %s\n", snd_strerror(err));
278                 exit(0);
279         }
280         printf("*** frames = %li ***\n", (long)frames);
281         snd_pcm_status_dump(status, output);
282 }
283
284 void showlatency(size_t latency)
285 {
286         double d;
287         latency *= 2;
288         d = (double)latency / (double)rate;
289         printf("Trying latency %li frames, %.3fus, %.6fms (%.4fHz)\n", (long)latency, d * 1000000, d * 1000, (double)1 / d);
290 }
291
292 void showinmax(size_t in_max)
293 {
294         double d;
295
296         printf("Maximum read: %li frames\n", (long)in_max);
297         d = (double)in_max / (double)rate;
298         printf("Maximum read latency: %.3fus, %.6fms (%.4fHz)\n", d * 1000000, d * 1000, (double)1 / d);
299 }
300
301 void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp)
302 {
303         int err;
304         snd_pcm_status_t *status;
305
306         snd_pcm_status_alloca(&status);
307         if ((err = snd_pcm_status(handle, status)) < 0) {
308                 printf("Stream status error: %s\n", snd_strerror(err));
309                 exit(0);
310         }
311         snd_pcm_status_get_trigger_tstamp(status, timestamp);
312 }
313
314 void setscheduler(void)
315 {
316         struct sched_param sched_param;
317
318         if (sched_getparam(0, &sched_param) < 0) {
319                 printf("Scheduler getparam failed...\n");
320                 return;
321         }
322         sched_param.sched_priority = sched_get_priority_max(SCHED_RR);
323         if (!sched_setscheduler(0, SCHED_RR, &sched_param)) {
324                 printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority);
325                 fflush(stdout);
326                 return;
327         }
328         printf("!!!Scheduler set to Round Robin with priority %i FAILED!!!\n", sched_param.sched_priority);
329 }
330
331 long timediff(snd_timestamp_t t1, snd_timestamp_t t2)
332 {
333         signed long l;
334
335         t1.tv_sec -= t2.tv_sec;
336         l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
337         if (l < 0) {
338                 t1.tv_sec--;
339                 l = -l;
340                 l %= 1000000;
341         }
342         return (t1.tv_sec * 1000000) + l;
343 }
344
345 long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max)
346 {
347         long r;
348
349         if (!block) {
350                 do {
351                         r = snd_pcm_readi(handle, buf, len);
352                 } while (r == -EAGAIN);
353                 if (r > 0) {
354                         *frames += r;
355                         if (*max < r)
356                                 *max = r;
357                 }
358                 // printf("read = %li\n", r);
359         } else {
360                 int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
361                 do {
362                         r = snd_pcm_readi(handle, buf, len);
363                         if (r > 0) {
364                                 buf += r * frame_bytes;
365                                 len -= r;
366                                 *frames += r;
367                                 if (*max < r)
368                                         *max = r;
369                         }
370                         // printf("r = %li, len = %li\n", r, len);
371                 } while (r >= 1 && len > 0);
372         }
373         // showstat(handle, 0);
374         return r;
375 }
376
377 long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames)
378 {
379         long r;
380
381         while (len > 0) {
382                 r = snd_pcm_writei(handle, buf, len);
383                 if (r == -EAGAIN)
384                         continue;
385                 // printf("write = %li\n", r);
386                 if (r < 0)
387                         return r;
388                 // showstat(handle, 0);
389                 buf += r * 4;
390                 len -= r;
391                 *frames += r;
392         }
393         return 0;
394 }
395                         
396 #define FILTERSWEEP_LFO_CENTER 2000.
397 #define FILTERSWEEP_LFO_DEPTH 1800.
398 #define FILTERSWEEP_LFO_FREQ 0.2
399 #define FILTER_BANDWIDTH 50
400
401 /* filter the sweep variables */
402 float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3];
403
404 void applyeffect(char* buffer,int r)
405 {
406         short* samples = (short*) buffer;
407         int i;
408         for (i=0;i<r;i++)
409         {
410                 int chn;
411
412                 fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER;
413                 lfo += dlfo;
414                 if (lfo>2.*M_PI) lfo -= 2.*M_PI;
415                 C = 1./tan(M_PI*BW/fs);
416                 D = 2.*cos(2*M_PI*fc/fs);
417                 a0 = 1./(1.+C);
418                 a1 = 0;
419                 a2 = -a0;
420                 b1 = -C*D*a0;
421                 b2 = (C-1)*a0;
422
423                 for (chn=0;chn<channels;chn++)
424                 {
425                         x[chn][2] = x[chn][1];
426                         x[chn][1] = x[chn][0];
427
428                         y[chn][2] = y[chn][1];
429                         y[chn][1] = y[chn][0];
430
431                         x[chn][0] = samples[i*channels+chn];
432                         y[chn][0] = a0*x[chn][0] + a1*x[chn][1] + a2*x[chn][2] 
433                                 - b1*y[chn][1] - b2*y[chn][2];
434                         samples[i*channels+chn] = y[chn][0];
435                 }
436         }
437 }
438
439 void help(void)
440 {
441         int k;
442         printf(
443 "Usage: latency [OPTION]... [FILE]...\n"
444 "-h,--help      help\n"
445 "-P,--pdevice   playback device\n"
446 "-C,--cdevice   capture device\n"
447 "-m,--min       minimum latency in frames\n"
448 "-M,--max       maximum latency in frames\n"
449 "-F,--frames    frames to transfer\n"
450 "-f,--format    sample format\n"
451 "-c,--channels  channels\n"
452 "-r,--rate      rate\n"
453 "-s,--seconds   duration of test in seconds\n"
454 "-b,--block     block mode\n"
455 "-t,--time      maximal tick time in us\n"
456 "-p,--poll      use poll (wait for event - reduces CPU usage)\n"
457 "-e,--effect    apply an effect (bandpass filter sweep)\n"
458 );
459         printf("Recognized sample formats are:");
460         for (k = 0; k < SND_PCM_FORMAT_LAST; ++(unsigned long) k) {
461                 const char *s = snd_pcm_format_name(k);
462                 if (s)
463                         printf(" %s", s);
464         }
465         printf("\n\n");
466         printf(
467 "Tip #1 (usable latency with large periods, non-blocking mode, good CPU usage,\n"
468 "        superb xrun prevention):\n"
469 "  latency -m 8192 -M 8192 -t 1 -p\n"
470 "Tip #2 (superb latency, non-blocking mode, but heavy CPU usage):\n"
471 "  latency -m 128 -M 128\n"
472 );
473 }
474
475 int main(int argc, char *argv[])
476 {
477         struct option long_option[] =
478         {
479                 {"help", 0, NULL, 'h'},
480                 {"pdevice", 1, NULL, 'P'},
481                 {"cdevice", 1, NULL, 'C'},
482                 {"min", 1, NULL, 'm'},
483                 {"max", 1, NULL, 'M'},
484                 {"frames", 1, NULL, 'F'},
485                 {"format", 1, NULL, 'f'},
486                 {"channels", 1, NULL, 'c'},
487                 {"rate", 1, NULL, 'r'},
488                 {"seconds", 1, NULL, 's'},
489                 {"block", 0, NULL, 'b'},
490                 {"time", 1, NULL, 't'},
491                 {"poll", 0, NULL, 'p'},
492                 {"effect", 0, NULL, 'e'},
493                 {NULL, 0, NULL, 0},
494         };
495         snd_pcm_t *phandle, *chandle;
496         char *buffer;
497         int err, latency, morehelp;
498         int ok;
499         snd_timestamp_t p_tstamp, c_tstamp;
500         ssize_t r;
501         size_t frames_in, frames_out, in_max;
502         int effect = 0;
503         morehelp = 0;
504         while (1) {
505                 int c;
506                 if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:s:bt:pe", long_option, NULL)) < 0)
507                         break;
508                 switch (c) {
509                 case 'h':
510                         morehelp++;
511                         break;
512                 case 'P':
513                         pdevice = strdup(optarg);
514                         break;
515                 case 'C':
516                         cdevice = strdup(optarg);
517                         break;
518                 case 'm':
519                         err = atoi(optarg) / 2;
520                         latency_min = err >= 4 ? err : 4;
521                         if (latency_max < latency_min)
522                                 latency_max = latency_min;
523                         break;
524                 case 'M':
525                         err = atoi(optarg) / 2;
526                         latency_max = latency_min > err ? latency_min : err;
527                         break;
528                 case 'f':
529                         format = snd_pcm_format_value(optarg);
530                         if (format == SND_PCM_FORMAT_UNKNOWN) {
531                                 printf("Unknown format, setting to default S16_LE\n");
532                                 format = SND_PCM_FORMAT_S16_LE;
533                         }
534                         break;
535                 case 'c':
536                         err = atoi(optarg);
537                         channels = err >= 1 && err < 1024 ? err : 1;
538                         break;
539                 case 'r':
540                         err = atoi(optarg);
541                         rate = err >= 4000 && err < 200000 ? err : 44100;
542                         break;
543                 case 's':
544                         err = atoi(optarg);
545                         loop_sec = err >= 1 && err <= 100000 ? err : 30;
546                         break;
547                 case 'b':
548                         block = 1;
549                         break;
550                 case 't':
551                         tick_time = atoi(optarg);
552                         tick_time = tick_time < 0 ? 0 : tick_time;
553                         break;
554                 case 'p':
555                         use_poll = 1;
556                         break;
557                 case 'e':
558                         effect = 1;
559                         break;
560                 }
561         }
562
563         if (morehelp) {
564                 help();
565                 return 0;
566         }
567         err = snd_output_stdio_attach(&output, stdout, 0);
568         if (err < 0) {
569                 printf("Output failed: %s\n", snd_strerror(err));
570                 return 0;
571         }
572
573         loop_limit = loop_sec * rate;
574         latency = latency_min - 4;
575         buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);
576
577         setscheduler();
578
579         printf("Playback device is %s\n", pdevice);
580         printf("Capture device is %s\n", cdevice);
581         printf("Parameters are %iHz, %s, %i channels, %s mode\n", rate, snd_pcm_format_name(format), channels, block ? "blocking" : "non-blocking");
582         printf("Wanted tick time: %ius, poll mode: %s\n", tick_time, use_poll ? "yes" : "no");
583         printf("Loop limit is %li frames, minimum latency = %i, maximum latency = %i\n", loop_limit, latency_min * 2, latency_max * 2);
584
585         if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
586                 printf("Playback open error: %s\n", snd_strerror(err));
587                 return 0;
588         }
589         if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
590                 printf("Record open error: %s\n", snd_strerror(err));
591                 return 0;
592         }
593
594         /* initialize the filter sweep variables */
595         if (effect) {
596                 fs = (float) rate;
597                 BW = FILTER_BANDWIDTH;
598
599                 lfo = 0;
600                 dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs;
601
602                 x[0] = (float*) malloc(channels*sizeof(float));         
603                 x[1] = (float*) malloc(channels*sizeof(float));         
604                 x[2] = (float*) malloc(channels*sizeof(float));         
605                 y[0] = (float*) malloc(channels*sizeof(float));         
606                 y[1] = (float*) malloc(channels*sizeof(float));         
607                 y[2] = (float*) malloc(channels*sizeof(float));         
608         }
609                           
610         while (1) {
611                 frames_in = frames_out = 0;
612                 if (setparams(phandle, chandle, &latency) < 0)
613                         break;
614                 showlatency(latency);
615                 if (tick_time_ok)
616                         printf("Using tick time %ius\n", tick_time_ok);
617                 if ((err = snd_pcm_link(chandle, phandle)) < 0) {
618                         printf("Streams link error: %s\n", snd_strerror(err));
619                         exit(0);
620                 }
621                 if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
622                         fprintf(stderr, "silence error\n");
623                         break;
624                 }
625                 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
626                         fprintf(stderr, "write error\n");
627                         break;
628                 }
629                 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
630                         fprintf(stderr, "write error\n");
631                         break;
632                 }
633
634                 if ((err = snd_pcm_start(chandle)) < 0) {
635                         printf("Go error: %s\n", snd_strerror(err));
636                         exit(0);
637                 }
638                 gettimestamp(phandle, &p_tstamp);
639                 gettimestamp(chandle, &c_tstamp);
640 #if 0
641                 printf("Playback:\n");
642                 showstat(phandle, frames_out);
643                 printf("Capture:\n");
644                 showstat(chandle, frames_in);
645 #endif
646
647                 ok = 1;
648                 in_max = 0;
649                 while (ok && frames_in < loop_limit) {
650                         if (use_poll) {
651                                 /* use poll to wait for next event */
652                                 snd_pcm_wait(chandle, 1000);
653                         }
654                         if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0)
655                                 ok = 0;
656                         else {
657                                 if (effect)
658                                         applyeffect(buffer,r);
659                                 if (writebuf(phandle, buffer, r, &frames_out) < 0)
660                                         ok = 0;
661                         }
662                 }
663                 if (ok)
664                         printf("Success\n");
665                 else
666                         printf("Failure\n");
667                 printf("Playback:\n");
668                 showstat(phandle, frames_out);
669                 printf("Capture:\n");
670                 showstat(chandle, frames_in);
671                 showinmax(in_max);
672                 if (p_tstamp.tv_sec == p_tstamp.tv_sec &&
673                     p_tstamp.tv_usec == c_tstamp.tv_usec)
674                         printf("Hardware sync\n");
675                 snd_pcm_drop(chandle);
676                 snd_pcm_nonblock(phandle, 0);
677                 snd_pcm_drain(phandle);
678                 snd_pcm_nonblock(phandle, !block ? 1 : 0);
679                 if (ok) {
680 #if 1
681                         printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
682                                p_tstamp.tv_sec,
683                                (int)p_tstamp.tv_usec,
684                                c_tstamp.tv_sec,
685                                (int)c_tstamp.tv_usec,
686                                timediff(p_tstamp, c_tstamp));
687 #endif
688                         break;
689                 }
690                 snd_pcm_unlink(chandle);
691                 snd_pcm_hw_free(phandle);
692                 snd_pcm_hw_free(chandle);
693         }
694         snd_pcm_close(phandle);
695         snd_pcm_close(chandle);
696         return 0;
697 }