4 * Author: Jaroslav Kysela <perex@suse.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
36 #include "../include/asoundlib.h"
40 char *pdevice = "hw:0,0";
41 char *cdevice = "hw:0,0";
42 snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
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 */
52 unsigned long loop_limit;
54 snd_output_t *output = NULL;
56 int setparams_stream(snd_pcm_t *handle,
57 snd_pcm_hw_params_t *params,
63 err = snd_pcm_hw_params_any(handle, params);
65 printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id);
68 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
70 printf("Access type not available for %s: %s\n", id, snd_strerror(err));
73 err = snd_pcm_hw_params_set_format(handle, params, format);
75 printf("Sample format not available for %s: %s\n", id, snd_strerror(err));
78 err = snd_pcm_hw_params_set_channels(handle, params, channels);
80 printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err));
84 err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
86 printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err));
90 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
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,
103 snd_pcm_uframes_t periodsize;
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);
109 printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err));
113 err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0);
115 printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err));
121 int setparams_set(snd_pcm_t *handle,
122 snd_pcm_hw_params_t *params,
123 snd_pcm_sw_params_t *swparams,
127 snd_pcm_uframes_t val;
128 unsigned int sleep_min = 0;
130 err = snd_pcm_hw_params(handle, params);
132 printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err));
135 err = snd_pcm_sw_params_current(handle, swparams);
137 printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err));
140 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff);
142 printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err));
148 snd_pcm_hw_params_get_period_time(params, &time, NULL);
149 snd_pcm_hw_params_get_tick_time(params, &ttime, NULL);
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);
155 sleep_min = tick_time / ttime;
158 err = snd_pcm_sw_params_set_sleep_min(handle, swparams, sleep_min);
160 printf("Unable to set minimal sleep %i for %s: %s\n", sleep_min, id, snd_strerror(err));
163 tick_time_ok = sleep_min * ttime;
169 snd_pcm_hw_params_get_period_size(params, &val, NULL);
170 if (tick_time_ok > 0)
172 err = snd_pcm_sw_params_set_avail_min(handle, swparams, val);
174 printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err));
177 val = !block ? 4 : 1;
178 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, val);
180 printf("Unable to set transfer align for %s: %s\n", id, snd_strerror(err));
183 err = snd_pcm_sw_params(handle, swparams);
185 printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err));
191 int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize)
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;
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));
210 if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
211 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
216 if (last_bufsize == *bufsize)
218 last_bufsize = *bufsize;
219 if (*bufsize > latency_max)
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));
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));
230 snd_pcm_hw_params_get_period_size(p_params, &size, NULL);
233 snd_pcm_hw_params_get_period_size(c_params, &size, NULL);
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)
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)
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)
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));
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));
259 if ((err = snd_pcm_prepare(phandle)) < 0) {
260 printf("Prepare error: %s\n", snd_strerror(err));
264 snd_pcm_dump(phandle, output);
265 snd_pcm_dump(chandle, output);
270 void showstat(snd_pcm_t *handle, size_t frames)
273 snd_pcm_status_t *status;
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));
280 printf("*** frames = %li ***\n", (long)frames);
281 snd_pcm_status_dump(status, output);
284 void showlatency(size_t latency)
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);
292 void showinmax(size_t in_max)
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);
301 void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp)
304 snd_pcm_status_t *status;
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));
311 snd_pcm_status_get_trigger_tstamp(status, timestamp);
314 void setscheduler(void)
316 struct sched_param sched_param;
318 if (sched_getparam(0, &sched_param) < 0) {
319 printf("Scheduler getparam failed...\n");
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);
328 printf("!!!Scheduler set to Round Robin with priority %i FAILED!!!\n", sched_param.sched_priority);
331 long timediff(snd_timestamp_t t1, snd_timestamp_t t2)
335 t1.tv_sec -= t2.tv_sec;
336 l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
342 return (t1.tv_sec * 1000000) + l;
345 long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max)
351 r = snd_pcm_readi(handle, buf, len);
352 } while (r == -EAGAIN);
358 // printf("read = %li\n", r);
360 int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
362 r = snd_pcm_readi(handle, buf, len);
364 buf += r * frame_bytes;
370 // printf("r = %li, len = %li\n", r, len);
371 } while (r >= 1 && len > 0);
373 // showstat(handle, 0);
377 long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames)
382 r = snd_pcm_writei(handle, buf, len);
385 // printf("write = %li\n", r);
388 // showstat(handle, 0);
396 #define FILTERSWEEP_LFO_CENTER 2000.
397 #define FILTERSWEEP_LFO_DEPTH 1800.
398 #define FILTERSWEEP_LFO_FREQ 0.2
399 #define FILTER_BANDWIDTH 50
401 /* filter the sweep variables */
402 float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3];
404 void applyeffect(char* buffer,int r)
406 short* samples = (short*) buffer;
412 fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER;
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);
423 for (chn=0;chn<channels;chn++)
425 x[chn][2] = x[chn][1];
426 x[chn][1] = x[chn][0];
428 y[chn][2] = y[chn][1];
429 y[chn][1] = y[chn][0];
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];
443 "Usage: latency [OPTION]... [FILE]...\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"
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"
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);
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"
475 int main(int argc, char *argv[])
477 struct option long_option[] =
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'},
495 snd_pcm_t *phandle, *chandle;
497 int err, latency, morehelp;
499 snd_timestamp_t p_tstamp, c_tstamp;
501 size_t frames_in, frames_out, in_max;
506 if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:s:bt:pe", long_option, NULL)) < 0)
513 pdevice = strdup(optarg);
516 cdevice = strdup(optarg);
519 err = atoi(optarg) / 2;
520 latency_min = err >= 4 ? err : 4;
521 if (latency_max < latency_min)
522 latency_max = latency_min;
525 err = atoi(optarg) / 2;
526 latency_max = latency_min > err ? latency_min : err;
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;
537 channels = err >= 1 && err < 1024 ? err : 1;
541 rate = err >= 4000 && err < 200000 ? err : 44100;
545 loop_sec = err >= 1 && err <= 100000 ? err : 30;
551 tick_time = atoi(optarg);
552 tick_time = tick_time < 0 ? 0 : tick_time;
567 err = snd_output_stdio_attach(&output, stdout, 0);
569 printf("Output failed: %s\n", snd_strerror(err));
573 loop_limit = loop_sec * rate;
574 latency = latency_min - 4;
575 buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);
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);
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));
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));
594 /* initialize the filter sweep variables */
597 BW = FILTER_BANDWIDTH;
600 dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs;
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));
611 frames_in = frames_out = 0;
612 if (setparams(phandle, chandle, &latency) < 0)
614 showlatency(latency);
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));
621 if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
622 fprintf(stderr, "silence error\n");
625 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
626 fprintf(stderr, "write error\n");
629 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
630 fprintf(stderr, "write error\n");
634 if ((err = snd_pcm_start(chandle)) < 0) {
635 printf("Go error: %s\n", snd_strerror(err));
638 gettimestamp(phandle, &p_tstamp);
639 gettimestamp(chandle, &c_tstamp);
641 printf("Playback:\n");
642 showstat(phandle, frames_out);
643 printf("Capture:\n");
644 showstat(chandle, frames_in);
649 while (ok && frames_in < loop_limit) {
651 /* use poll to wait for next event */
652 snd_pcm_wait(chandle, 1000);
654 if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0)
658 applyeffect(buffer,r);
659 if (writebuf(phandle, buffer, r, &frames_out) < 0)
667 printf("Playback:\n");
668 showstat(phandle, frames_out);
669 printf("Capture:\n");
670 showstat(chandle, frames_in);
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);
681 printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
683 (int)p_tstamp.tv_usec,
685 (int)c_tstamp.tv_usec,
686 timediff(p_tstamp, c_tstamp));
690 snd_pcm_unlink(chandle);
691 snd_pcm_hw_free(phandle);
692 snd_pcm_hw_free(chandle);
694 snd_pcm_close(phandle);
695 snd_pcm_close(chandle);