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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 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 */
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,
64 err = snd_pcm_hw_params_any(handle, params);
66 printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id);
69 err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
71 printf("Resample setup failed for %s (val %i): %s\n", id, resample, snd_strerror(err));
74 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
76 printf("Access type not available for %s: %s\n", id, snd_strerror(err));
79 err = snd_pcm_hw_params_set_format(handle, params, format);
81 printf("Sample format not available for %s: %s\n", id, snd_strerror(err));
84 err = snd_pcm_hw_params_set_channels(handle, params, channels);
86 printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err));
90 err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
92 printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err));
95 if ((int)rrate != rate) {
96 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
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,
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);
115 printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err));
119 periodsize = period_size;
122 err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0);
124 printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err));
130 int setparams_set(snd_pcm_t *handle,
131 snd_pcm_hw_params_t *params,
132 snd_pcm_sw_params_t *swparams,
136 snd_pcm_uframes_t val;
138 err = snd_pcm_hw_params(handle, params);
140 printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err));
143 err = snd_pcm_sw_params_current(handle, swparams);
145 printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err));
148 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff);
150 printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err));
156 snd_pcm_hw_params_get_period_size(params, &val, NULL);
157 err = snd_pcm_sw_params_set_avail_min(handle, swparams, val);
159 printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err));
162 err = snd_pcm_sw_params(handle, swparams);
164 printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err));
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;
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));
190 if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
191 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
195 if (buffer_size > 0) {
196 *bufsize = buffer_size;
203 if (last_bufsize == *bufsize)
205 last_bufsize = *bufsize;
206 if (*bufsize > latency_max)
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));
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));
218 snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
219 if (p_psize > (unsigned int)*bufsize)
221 snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
222 if (c_psize > (unsigned int)*bufsize)
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)
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);
233 printf("playback device does not support 2 periods per buffer\n");
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);
242 printf("capture device does not support 2 periods per buffer\n");
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));
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));
256 if ((err = snd_pcm_prepare(phandle)) < 0) {
257 printf("Prepare error: %s\n", snd_strerror(err));
261 snd_pcm_dump(phandle, output);
262 snd_pcm_dump(chandle, output);
267 void showstat(snd_pcm_t *handle, size_t frames)
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));
277 printf("*** frames = %li ***\n", (long)frames);
278 snd_pcm_status_dump(status, output);
281 void showlatency(size_t latency)
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)
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)
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));
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");
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);
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)
332 t1.tv_sec -= t2.tv_sec;
333 l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
339 return (t1.tv_sec * 1000000) + l;
342 long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max)
348 r = snd_pcm_readi(handle, buf, len);
349 } while (r == -EAGAIN);
355 // printf("read = %li\n", r);
357 int frame_bytes = (snd_pcm_format_width(format) / 8) * channels;
359 r = snd_pcm_readi(handle, buf, len);
361 buf += r * frame_bytes;
367 // printf("r = %li, len = %li\n", r, len);
368 } while (r >= 1 && len > 0);
370 // showstat(handle, 0);
374 long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames)
379 r = snd_pcm_writei(handle, buf, len);
382 // printf("write = %li\n", r);
385 // showstat(handle, 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;
409 fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER;
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);
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];
440 "Usage: latency [OPTION]... [FILE]...\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"
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);
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'},
494 snd_pcm_t *phandle, *chandle;
496 int err, latency, morehelp;
498 snd_timestamp_t p_tstamp, c_tstamp;
500 size_t frames_in, frames_out, in_max;
505 if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:B:E:s:bpen", long_option, NULL)) < 0)
512 pdevice = strdup(optarg);
515 cdevice = strdup(optarg);
518 err = atoi(optarg) / 2;
519 latency_min = err >= 4 ? err : 4;
520 if (latency_max < latency_min)
521 latency_max = latency_min;
524 err = atoi(optarg) / 2;
525 latency_max = latency_min > err ? latency_min : err;
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;
536 channels = err >= 1 && err < 1024 ? err : 1;
540 rate = err >= 4000 && err < 200000 ? err : 44100;
544 buffer_size = err >= 32 && err < 200000 ? err : 0;
548 period_size = err >= 32 && err < 200000 ? err : 0;
552 loop_sec = err >= 1 && err <= 100000 ? err : 30;
573 err = snd_output_stdio_attach(&output, stdout, 0);
575 printf("Output failed: %s\n", snd_strerror(err));
579 loop_limit = loop_sec * rate;
580 latency = latency_min - 4;
581 buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2);
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));
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));
600 /* initialize the filter sweep variables */
603 BW = FILTER_BANDWIDTH;
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));
617 frames_in = frames_out = 0;
618 if (setparams(phandle, chandle, &latency) < 0)
620 showlatency(latency);
621 if ((err = snd_pcm_link(chandle, phandle)) < 0) {
622 printf("Streams link error: %s\n", snd_strerror(err));
625 if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) {
626 fprintf(stderr, "silence error\n");
629 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
630 fprintf(stderr, "write error\n");
633 if (writebuf(phandle, buffer, latency, &frames_out) < 0) {
634 fprintf(stderr, "write error\n");
638 if ((err = snd_pcm_start(chandle)) < 0) {
639 printf("Go error: %s\n", snd_strerror(err));
642 gettimestamp(phandle, &p_tstamp);
643 gettimestamp(chandle, &c_tstamp);
645 printf("Playback:\n");
646 showstat(phandle, frames_out);
647 printf("Capture:\n");
648 showstat(chandle, frames_in);
653 while (ok && frames_in < loop_limit) {
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)
662 applyeffect(buffer,r);
663 if (writebuf(phandle, buffer, r, &frames_out) < 0)
671 printf("Playback:\n");
672 showstat(phandle, frames_out);
673 printf("Capture:\n");
674 showstat(chandle, frames_in);
676 if (p_tstamp.tv_sec == c_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);
685 printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
687 (int)p_tstamp.tv_usec,
689 (int)c_tstamp.tv_usec,
690 timediff(p_tstamp, c_tstamp));
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);