• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/iserver/alsa-lib-1.0.26/test/

Lines Matching defs:*

2  *  Latency test program
4 * Author: Jaroslav Kysela <perex@perex.cz>
6 * Author of bandpass filter sweep effect:
7 * Maarten de Boer <mdeboer@iua.upf.es>
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.
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.
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.
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
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>
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;
55 snd_output_t *output = NULL;
57 int setparams_stream(snd_pcm_t *handle,
58 snd_pcm_hw_params_t *params,
59 const char *id)
61 int err;
62 unsigned int rrate;
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;
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;
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;
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;
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;
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;
95 if ((int)rrate != rate) {
96 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
97 return -EINVAL;
99 return 0;
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)
108 int err;
109 snd_pcm_uframes_t periodsize;
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;
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;
127 return 0;
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)
135 int err;
136 snd_pcm_uframes_t val;
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;
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;
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;
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;
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;
167 return 0;
170 int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize)
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 unsigned int val;
180 snd_pcm_hw_params_alloca(&p_params);
181 snd_pcm_hw_params_alloca(&c_params);
182 snd_pcm_hw_params_alloca(&pt_params);
183 snd_pcm_hw_params_alloca(&ct_params);
184 snd_pcm_sw_params_alloca(&p_swparams);
185 snd_pcm_sw_params_alloca(&c_swparams);
186 if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) {
187 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
188 exit(0);
190 if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
191 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
192 exit(0);
195 if (buffer_size > 0) {
196 *bufsize = buffer_size;
197 goto __set_it;
200 __again:
201 if (buffer_size > 0)
202 return -1;
203 if (last_bufsize == *bufsize)
204 *bufsize += 4;
205 last_bufsize = *bufsize;
206 if (*bufsize > latency_max)
207 return -1;
208 __set_it:
209 if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) {
210 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
211 exit(0);
213 if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) {
214 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
215 exit(0);
218 snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
219 if (p_psize > (unsigned int)*bufsize)
220 *bufsize = p_psize;
221 snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
222 if (c_psize > (unsigned int)*bufsize)
223 *bufsize = c_psize;
224 snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL);
225 snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL);
226 if (p_time != c_time)
227 goto __again;
229 snd_pcm_hw_params_get_buffer_size(p_params, &p_size);
230 if (p_psize * 2 < p_size) {
231 snd_pcm_hw_params_get_periods_min(p_params, &val, NULL);
232 if (val > 2) {
233 printf("playback device does not support 2 periods per buffer\n");
234 exit(0);
236 goto __again;
238 snd_pcm_hw_params_get_buffer_size(c_params, &c_size);
239 if (c_psize * 2 < c_size) {
240 snd_pcm_hw_params_get_periods_min(c_params, &val, NULL);
241 if (val > 2 ) {
242 printf("capture device does not support 2 periods per buffer\n");
243 exit(0);
245 goto __again;
247 if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) {
248 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
249 exit(0);
251 if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) {
252 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
253 exit(0);
256 if ((err = snd_pcm_prepare(phandle)) < 0) {
257 printf("Prepare error: %s\n", snd_strerror(err));
258 exit(0);
261 snd_pcm_dump(phandle, output);
262 snd_pcm_dump(chandle, output);
263 fflush(stdout);
264 return 0;
267 void showstat(snd_pcm_t *handle, size_t frames)
269 int err;
270 snd_pcm_status_t *status;
272 snd_pcm_status_alloca(&status);
273 if ((err = snd_pcm_status(handle, status)) < 0) {
274 printf("Stream status error: %s\n", snd_strerror(err));
275 exit(0);
277 printf("*** frames = %li ***\n", (long)frames);
278 snd_pcm_status_dump(status, output);
281 void showlatency(size_t latency)
283 double d;
284 latency *= 2;
285 d = (double)latency / (double)rate;
286 printf("Trying latency %li frames, %.3fus, %.6fms (%.4fHz)\n", (long)latency, d * 1000000, d * 1000, (double)1 / d);
289 void showinmax(size_t in_max)
291 double d;
293 printf("Maximum read: %li frames\n", (long)in_max);
294 d = (double)in_max / (double)rate;
295 printf("Maximum read latency: %.3fus, %.6fms (%.4fHz)\n", d * 1000000, d * 1000, (double)1 / d);
298 void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp)
300 int err;
301 snd_pcm_status_t *status;
303 snd_pcm_status_alloca(&status);
304 if ((err = snd_pcm_status(handle, status)) < 0) {
305 printf("Stream status error: %s\n", snd_strerror(err));
306 exit(0);
308 snd_pcm_status_get_trigger_tstamp(status, timestamp);
311 void setscheduler(void)
313 struct sched_param sched_param;
315 if (sched_getparam(0, &sched_param) < 0) {
316 printf("Scheduler getparam failed...\n");
317 return;
319 sched_param.sched_priority = sched_get_priority_max(SCHED_RR);
320 if (!sched_setscheduler(0, SCHED_RR, &sched_param)) {
321 printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority);
322 fflush(stdout);
323 return;
325 printf("!!!Scheduler set to Round Robin with priority %i FAILED!!!\n", sched_param.sched_priority);
328 long timediff(snd_timestamp_t t1, snd_timestamp_t t2)
330 signed long l;
332 t1.tv_sec -= t2.tv_sec;
333 l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
334 if (l < 0) {
335 t1.tv_sec--;
336 l = 1000000 + l;
337 l %= 1000000;
339 return (t1.tv_sec * 1000000) + l;
342 long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max)
344 long r;
346 if (!block) {
347 do {
348 r = snd_pcm_readi(handle, buf, len);
349 } while (r == -EAGAIN);
350 if (r > 0) {
351 *frames += r;
352 if ((long)*max < r)
353 *max = r;
355 // printf("read = %li\n", r);
356 } else {
357 int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
358 do {
359 r = snd_pcm_readi(handle, buf, len);
360 if (r > 0) {
361 buf += r * frame_bytes;
362 len -= r;
363 *frames += r;
364 if ((long)*max < r)
365 *max = r;
367 // printf("r = %li, len = %li\n", r, len);
368 } while (r >= 1 && len > 0);
370 // showstat(handle, 0);
371 return r;
374 long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames)
376 long r;
378 while (len > 0) {
379 r = snd_pcm_writei(handle, buf, len);
380 if (r == -EAGAIN)
381 continue;
382 // printf("write = %li\n", r);
383 if (r < 0)
384 return r;
385 // showstat(handle, 0);
386 buf += r * 4;
387 len -= r;
388 *frames += r;
390 return 0;
393 #define FILTERSWEEP_LFO_CENTER 2000.
394 #define FILTERSWEEP_LFO_DEPTH 1800.
395 #define FILTERSWEEP_LFO_FREQ 0.2
396 #define FILTER_BANDWIDTH 50
398 /* filter the sweep variables */
399 float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3];
401 void applyeffect(char* buffer,int r)
403 short* samples = (short*) buffer;
404 int i;
405 for (i=0;i<r;i++)
407 int chn;
409 fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER;
410 lfo += dlfo;
411 if (lfo>2.*M_PI) lfo -= 2.*M_PI;
412 C = 1./tan(M_PI*BW/fs);
413 D = 2.*cos(2*M_PI*fc/fs);
414 a0 = 1./(1.+C);
415 a1 = 0;
416 a2 = -a0;
417 b1 = -C*D*a0;
418 b2 = (C-1)*a0;
420 for (chn=0;chn<channels;chn++)
422 x[chn][2] = x[chn][1];
423 x[chn][1] = x[chn][0];
425 y[chn][2] = y[chn][1];
426 y[chn][1] = y[chn][0];
428 x[chn][0] = samples[i*channels+chn];
429 y[chn][0] = a0*x[chn][0] + a1*x[chn][1] + a2*x[chn][2]
430 - b1*y[chn][1] - b2*y[chn][2];
431 samples[i*channels+chn] = y[chn][0];
436 void help(void)
438 int k;
439 printf(
440 "Usage: latency [OPTION]... [FILE]...\n"
441 "-h,--help help\n"
442 "-P,--pdevice playback device\n"
443 "-C,--cdevice capture device\n"
444 "-m,--min minimum latency in frames\n"
445 "-M,--max maximum latency in frames\n"
446 "-F,--frames frames to transfer\n"
447 "-f,--format sample format\n"
448 "-c,--channels channels\n"
449 "-r,--rate rate\n"
450 "-B,--buffer buffer size in frames\n"
451 "-E,--period period size in frames\n"
452 "-s,--seconds duration of test in seconds\n"
453 "-b,--block block mode\n"
454 "-p,--poll use poll (wait for event - reduces CPU usage)\n"
455 "-e,--effect apply an effect (bandpass filter sweep)\n"
457 printf("Recognized sample formats are:");
458 for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) {
459 const char *s = snd_pcm_format_name(k);
460 if (s)
461 printf(" %s", s);
463 printf("\n\n");
464 printf(
465 "Tip #1 (usable latency with large periods, non-blocking mode, good CPU usage,\n"
466 " superb xrun prevention):\n"
467 " latency -m 8192 -M 8192 -t 1 -p\n"
468 "Tip #2 (superb latency, non-blocking mode, but heavy CPU usage):\n"
469 " latency -m 128 -M 128\n"
473 int main(int argc, char *argv[])
475 struct option long_option[] =
477 {"help", 0, NULL, 'h'},
478 {"pdevice", 1, NULL, 'P'},
479 {"cdevice", 1, NULL, 'C'},
480 {"min", 1, NULL, 'm'},
481 {"max", 1, NULL, 'M'},
482 {"frames", 1, NULL, 'F'},
483 {"format", 1, NULL, 'f'},
484 {"channels", 1, NULL, 'c'},
485 {"rate", 1, NULL, 'r'},
486 {"buffer", 1, NULL, 'B'},
487 {"period", 1, NULL, 'E'},
488 {"seconds", 1, NULL, 's'},
489 {"block", 0, NULL, 'b'},
490 {"poll", 0, NULL, 'p'},
491 {"effect", 0, NULL, 'e'},
492 {NULL, 0, NULL, 0},
494 snd_pcm_t *phandle, *chandle;
495 char *buffer;
496 int err, latency, morehelp;
497 int ok;
498 snd_timestamp_t p_tstamp, c_tstamp;
499 ssize_t r;
500 size_t frames_in, frames_out, in_max;
501 int effect = 0;
502 morehelp = 0;
503 while (1) {
504 int c;
505 if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:B:E:s:bpen", long_option, NULL)) < 0)
506 break;
507 switch (c) {
508 case 'h':
509 morehelp++;
510 break;
511 case 'P':
512 pdevice = strdup(optarg);
513 break;
514 case 'C':
515 cdevice = strdup(optarg);
516 break;
517 case 'm':
518 err = atoi(optarg) / 2;
519 latency_min = err >= 4 ? err : 4;
520 if (latency_max < latency_min)
521 latency_max = latency_min;
522 break;
523 case 'M':
524 err = atoi(optarg) / 2;
525 latency_max = latency_min > err ? latency_min : err;
526 break;
527 case 'f':
528 format = snd_pcm_format_value(optarg);
529 if (format == SND_PCM_FORMAT_UNKNOWN) {
530 printf("Unknown format, setting to default S16_LE\n");
531 format = SND_PCM_FORMAT_S16_LE;
533 break;
534 case 'c':
535 err = atoi(optarg);
536 channels = err >= 1 && err < 1024 ? err : 1;
537 break;
538 case 'r':
539 err = atoi(optarg);
540 rate = err >= 4000 && err < 200000 ? err : 44100;
541 break;
542 case 'B':
543 err = atoi(optarg);
544 buffer_size = err >= 32 && err < 200000 ? err : 0;
545 break;
546 case 'E':
547 err = atoi(optarg);
548 period_size = err >= 32 && err < 200000 ? err : 0;
549 break;
550 case 's':
551 err = atoi(optarg);
552 loop_sec = err >= 1 && err <= 100000 ? err : 30;
553 break;
554 case 'b':
555 block = 1;
556 break;
557 case 'p':
558 use_poll = 1;
559 break;
560 case 'e':
561 effect = 1;
562 break;
563 case 'n':
564 resample = 0;
565 break;
569 if (morehelp) {
570 help();
571 return 0;
573 err = snd_output_stdio_attach(&output, stdout, 0);
574 if (err < 0) {
575 printf("Output failed: %s\n", snd_strerror(err));
576 return 0;
579 loop_limit = loop_sec * rate;
580 latency = latency_min - 4;
581 buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);
583 setscheduler();
585 printf("Playback device is %s\n", pdevice);
586 printf("Capture device is %s\n", cdevice);
587 printf("Parameters are %iHz, %s, %i channels, %s mode\n", rate, snd_pcm_format_name(format), channels, block ? "blocking" : "non-blocking");
588 printf("Poll mode: %s\n", use_poll ? "yes" : "no");
589 printf("Loop limit is %li frames, minimum latency = %i, maximum latency = %i\n", loop_limit, latency_min * 2, latency_max * 2);
591 if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
592 printf("Playback open error: %s\n", snd_strerror(err));
593 return 0;
595 if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) {
596 printf("Record open error: %s\n", snd_strerror(err));
597 return 0;
600 /* initialize the filter sweep variables */
601 if (effect) {
602 fs = (float) rate;
603 BW = FILTER_BANDWIDTH;
605 lfo = 0;
606 dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs;
608 x[0] = (float*) malloc(channels*sizeof(float));
609 x[1] = (float*) malloc(channels*sizeof(float));
610 x[2] = (float*) malloc(channels*sizeof(float));
611 y[0] = (float*) malloc(channels*sizeof(float));
612 y[1] = (float*) malloc(channels*sizeof(float));
613 y[2] = (float*) malloc(channels*sizeof(float));
616 while (1) {
617 frames_in = frames_out = 0;
618 if (setparams(phandle, chandle, &latency) < 0)
619 break;
620 showlatency(latency);
621 if ((err = snd_pcm_link(chandle, phandle)) < 0) {
622 printf("Streams link error: %s\n", snd_strerror(err));
623 exit(0);
625 if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
626 fprintf(stderr, "silence error\n");
627 break;
629 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
630 fprintf(stderr, "write error\n");
631 break;
633 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
634 fprintf(stderr, "write error\n");
635 break;
638 if ((err = snd_pcm_start(chandle)) < 0) {
639 printf("Go error: %s\n", snd_strerror(err));
640 exit(0);
642 gettimestamp(phandle, &p_tstamp);
643 gettimestamp(chandle, &c_tstamp);
644 #if 0
645 printf("Playback:\n");
646 showstat(phandle, frames_out);
647 printf("Capture:\n");
648 showstat(chandle, frames_in);
649 #endif
651 ok = 1;
652 in_max = 0;
653 while (ok && frames_in < loop_limit) {
654 if (use_poll) {
655 /* use poll to wait for next event */
656 snd_pcm_wait(chandle, 1000);
658 if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0)
659 ok = 0;
660 else {
661 if (effect)
662 applyeffect(buffer,r);
663 if (writebuf(phandle, buffer, r, &frames_out) < 0)
664 ok = 0;
667 if (ok)
668 printf("Success\n");
669 else
670 printf("Failure\n");
671 printf("Playback:\n");
672 showstat(phandle, frames_out);
673 printf("Capture:\n");
674 showstat(chandle, frames_in);
675 showinmax(in_max);
676 if (p_tstamp.tv_sec == p_tstamp.tv_sec &&
677 p_tstamp.tv_usec == c_tstamp.tv_usec)
678 printf("Hardware sync\n");
679 snd_pcm_drop(chandle);
680 snd_pcm_nonblock(phandle, 0);
681 snd_pcm_drain(phandle);
682 snd_pcm_nonblock(phandle, !block ? 1 : 0);
683 if (ok) {
684 #if 1
685 printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
686 p_tstamp.tv_sec,
687 (int)p_tstamp.tv_usec,
688 c_tstamp.tv_sec,
689 (int)c_tstamp.tv_usec,
690 timediff(p_tstamp, c_tstamp));
691 #endif
692 break;
694 snd_pcm_unlink(chandle);
695 snd_pcm_hw_free(phandle);
696 snd_pcm_hw_free(chandle);
698 snd_pcm_close(phandle);
699 snd_pcm_close(chandle);
700 return 0;