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