00001 /* 00002 * Latency test program 00003 * 00004 * Author: Jaroslav Kysela <perex@suse.cz> 00005 * 00006 * Author of bandpass filter sweep effect: 00007 * Maarten de Boer <mdeboer@iua.upf.es> 00008 * 00009 * This small demo program can be used for measuring latency between 00010 * capture and playback. This latency is measured from driver (diff when 00011 * playback and capture was started). Scheduler is set to SCHED_RR. 00012 * 00013 * 00014 * This program is free software; you can redistribute it and/or modify 00015 * it under the terms of the GNU General Public License as published by 00016 * the Free Software Foundation; either version 2 of the License, or 00017 * (at your option) any later version. 00018 * 00019 * This program is distributed in the hope that it will be useful, 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 * GNU General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU General Public License 00025 * along with this program; if not, write to the Free Software 00026 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00027 * 00028 */ 00029 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 #include <string.h> 00033 #include <sched.h> 00034 #include <errno.h> 00035 #include <getopt.h> 00036 #include "../include/asoundlib.h" 00037 #include <sys/time.h> 00038 #include <math.h> 00039 00040 char *pdevice = "hw:0,0"; 00041 char *cdevice = "hw:0,0"; 00042 snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; 00043 int rate = 22050; 00044 int channels = 2; 00045 int buffer_size = 0; /* auto */ 00046 int period_size = 0; /* auto */ 00047 int latency_min = 32; /* in frames / 2 */ 00048 int latency_max = 2048; /* in frames / 2 */ 00049 int loop_sec = 30; /* seconds */ 00050 int block = 0; /* block mode */ 00051 int tick_time = 0; /* disabled, otherwise in us */ 00052 int tick_time_ok = 0; 00053 int use_poll = 0; 00054 int resample = 1; 00055 unsigned long loop_limit; 00056 00057 snd_output_t *output = NULL; 00058 00059 int setparams_stream(snd_pcm_t *handle, 00060 snd_pcm_hw_params_t *params, 00061 const char *id) 00062 { 00063 int err; 00064 unsigned int rrate; 00065 00066 err = snd_pcm_hw_params_any(handle, params); 00067 if (err < 0) { 00068 printf("Broken configuration for %s PCM: no configurations available: %s\n", snd_strerror(err), id); 00069 return err; 00070 } 00071 err = snd_pcm_hw_params_set_rate_resample(handle, params, resample); 00072 if (err < 0) { 00073 printf("Resample setup failed for %s (val %i): %s\n", id, resample, snd_strerror(err)); 00074 return err; 00075 } 00076 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); 00077 if (err < 0) { 00078 printf("Access type not available for %s: %s\n", id, snd_strerror(err)); 00079 return err; 00080 } 00081 err = snd_pcm_hw_params_set_format(handle, params, format); 00082 if (err < 0) { 00083 printf("Sample format not available for %s: %s\n", id, snd_strerror(err)); 00084 return err; 00085 } 00086 err = snd_pcm_hw_params_set_channels(handle, params, channels); 00087 if (err < 0) { 00088 printf("Channels count (%i) not available for %s: %s\n", channels, id, snd_strerror(err)); 00089 return err; 00090 } 00091 rrate = rate; 00092 err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0); 00093 if (err < 0) { 00094 printf("Rate %iHz not available for %s: %s\n", rate, id, snd_strerror(err)); 00095 return err; 00096 } 00097 if ((int)rrate != rate) { 00098 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); 00099 return -EINVAL; 00100 } 00101 return 0; 00102 } 00103 00104 int setparams_bufsize(snd_pcm_t *handle, 00105 snd_pcm_hw_params_t *params, 00106 snd_pcm_hw_params_t *tparams, 00107 snd_pcm_uframes_t bufsize, 00108 const char *id) 00109 { 00110 int err; 00111 snd_pcm_uframes_t periodsize; 00112 00113 snd_pcm_hw_params_copy(params, tparams); 00114 periodsize = bufsize * 2; 00115 err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &periodsize); 00116 if (err < 0) { 00117 printf("Unable to set buffer size %li for %s: %s\n", bufsize * 2, id, snd_strerror(err)); 00118 return err; 00119 } 00120 if (period_size > 0) 00121 periodsize = period_size; 00122 else 00123 periodsize /= 2; 00124 err = snd_pcm_hw_params_set_period_size_near(handle, params, &periodsize, 0); 00125 if (err < 0) { 00126 printf("Unable to set period size %li for %s: %s\n", periodsize, id, snd_strerror(err)); 00127 return err; 00128 } 00129 return 0; 00130 } 00131 00132 int setparams_set(snd_pcm_t *handle, 00133 snd_pcm_hw_params_t *params, 00134 snd_pcm_sw_params_t *swparams, 00135 const char *id) 00136 { 00137 int err; 00138 snd_pcm_uframes_t val; 00139 unsigned int sleep_min = 0; 00140 00141 err = snd_pcm_hw_params(handle, params); 00142 if (err < 0) { 00143 printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err)); 00144 return err; 00145 } 00146 err = snd_pcm_sw_params_current(handle, swparams); 00147 if (err < 0) { 00148 printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err)); 00149 return err; 00150 } 00151 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff); 00152 if (err < 0) { 00153 printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err)); 00154 return err; 00155 } 00156 tick_time_ok = 0; 00157 if (tick_time > 0) { 00158 unsigned int time, ttime; 00159 snd_pcm_hw_params_get_period_time(params, &time, NULL); 00160 snd_pcm_hw_params_get_tick_time(params, &ttime, NULL); 00161 if (time < ttime) { 00162 printf("Skipping to set minimal sleep: period time < tick time\n"); 00163 } else if (ttime <= 0) { 00164 printf("Skipping to set minimal sleep: tick time <= 0 (%i)\n", ttime); 00165 } else { 00166 sleep_min = tick_time / ttime; 00167 if (sleep_min <= 0) 00168 sleep_min = 1; 00169 err = snd_pcm_sw_params_set_sleep_min(handle, swparams, sleep_min); 00170 if (err < 0) { 00171 printf("Unable to set minimal sleep %i for %s: %s\n", sleep_min, id, snd_strerror(err)); 00172 return err; 00173 } 00174 tick_time_ok = sleep_min * ttime; 00175 } 00176 } 00177 if (!block) 00178 val = 4; 00179 else 00180 snd_pcm_hw_params_get_period_size(params, &val, NULL); 00181 if (tick_time_ok > 0) 00182 val = 16; 00183 err = snd_pcm_sw_params_set_avail_min(handle, swparams, val); 00184 if (err < 0) { 00185 printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err)); 00186 return err; 00187 } 00188 val = !block ? 4 : 1; 00189 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, val); 00190 if (err < 0) { 00191 printf("Unable to set transfer align for %s: %s\n", id, snd_strerror(err)); 00192 return err; 00193 } 00194 err = snd_pcm_sw_params(handle, swparams); 00195 if (err < 0) { 00196 printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err)); 00197 return err; 00198 } 00199 return 0; 00200 } 00201 00202 int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize) 00203 { 00204 int err, last_bufsize = *bufsize; 00205 snd_pcm_hw_params_t *pt_params, *ct_params; /* templates with rate, format and channels */ 00206 snd_pcm_hw_params_t *p_params, *c_params; 00207 snd_pcm_sw_params_t *p_swparams, *c_swparams; 00208 snd_pcm_uframes_t p_size, c_size, p_psize, c_psize; 00209 unsigned int p_time, c_time; 00210 00211 snd_pcm_hw_params_alloca(&p_params); 00212 snd_pcm_hw_params_alloca(&c_params); 00213 snd_pcm_hw_params_alloca(&pt_params); 00214 snd_pcm_hw_params_alloca(&ct_params); 00215 snd_pcm_sw_params_alloca(&p_swparams); 00216 snd_pcm_sw_params_alloca(&c_swparams); 00217 if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) { 00218 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err)); 00219 exit(0); 00220 } 00221 if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) { 00222 printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err)); 00223 exit(0); 00224 } 00225 00226 if (buffer_size > 0) { 00227 *bufsize = buffer_size; 00228 goto __set_it; 00229 } 00230 00231 __again: 00232 if (buffer_size > 0) 00233 return -1; 00234 if (last_bufsize == *bufsize) 00235 *bufsize += 4; 00236 last_bufsize = *bufsize; 00237 if (*bufsize > latency_max) 00238 return -1; 00239 __set_it: 00240 if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) { 00241 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); 00242 exit(0); 00243 } 00244 if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) { 00245 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); 00246 exit(0); 00247 } 00248 00249 snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL); 00250 if (p_psize > (unsigned int)*bufsize) 00251 *bufsize = p_psize; 00252 snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL); 00253 if (c_psize > (unsigned int)*bufsize) 00254 *bufsize = c_psize; 00255 snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL); 00256 snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL); 00257 if (p_time != c_time) 00258 goto __again; 00259 00260 snd_pcm_hw_params_get_buffer_size(p_params, &p_size); 00261 if (p_psize * 2 < p_size) 00262 goto __again; 00263 snd_pcm_hw_params_get_buffer_size(c_params, &c_size); 00264 if (c_psize * 2 < c_size) 00265 goto __again; 00266 00267 if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) { 00268 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); 00269 exit(0); 00270 } 00271 if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) { 00272 printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err)); 00273 exit(0); 00274 } 00275 00276 if ((err = snd_pcm_prepare(phandle)) < 0) { 00277 printf("Prepare error: %s\n", snd_strerror(err)); 00278 exit(0); 00279 } 00280 00281 snd_pcm_dump(phandle, output); 00282 snd_pcm_dump(chandle, output); 00283 fflush(stdout); 00284 return 0; 00285 } 00286 00287 void showstat(snd_pcm_t *handle, size_t frames) 00288 { 00289 int err; 00290 snd_pcm_status_t *status; 00291 00292 snd_pcm_status_alloca(&status); 00293 if ((err = snd_pcm_status(handle, status)) < 0) { 00294 printf("Stream status error: %s\n", snd_strerror(err)); 00295 exit(0); 00296 } 00297 printf("*** frames = %li ***\n", (long)frames); 00298 snd_pcm_status_dump(status, output); 00299 } 00300 00301 void showlatency(size_t latency) 00302 { 00303 double d; 00304 latency *= 2; 00305 d = (double)latency / (double)rate; 00306 printf("Trying latency %li frames, %.3fus, %.6fms (%.4fHz)\n", (long)latency, d * 1000000, d * 1000, (double)1 / d); 00307 } 00308 00309 void showinmax(size_t in_max) 00310 { 00311 double d; 00312 00313 printf("Maximum read: %li frames\n", (long)in_max); 00314 d = (double)in_max / (double)rate; 00315 printf("Maximum read latency: %.3fus, %.6fms (%.4fHz)\n", d * 1000000, d * 1000, (double)1 / d); 00316 } 00317 00318 void gettimestamp(snd_pcm_t *handle, snd_timestamp_t *timestamp) 00319 { 00320 int err; 00321 snd_pcm_status_t *status; 00322 00323 snd_pcm_status_alloca(&status); 00324 if ((err = snd_pcm_status(handle, status)) < 0) { 00325 printf("Stream status error: %s\n", snd_strerror(err)); 00326 exit(0); 00327 } 00328 snd_pcm_status_get_trigger_tstamp(status, timestamp); 00329 } 00330 00331 void setscheduler(void) 00332 { 00333 struct sched_param sched_param; 00334 00335 if (sched_getparam(0, &sched_param) < 0) { 00336 printf("Scheduler getparam failed...\n"); 00337 return; 00338 } 00339 sched_param.sched_priority = sched_get_priority_max(SCHED_RR); 00340 if (!sched_setscheduler(0, SCHED_RR, &sched_param)) { 00341 printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority); 00342 fflush(stdout); 00343 return; 00344 } 00345 printf("!!!Scheduler set to Round Robin with priority %i FAILED!!!\n", sched_param.sched_priority); 00346 } 00347 00348 long timediff(snd_timestamp_t t1, snd_timestamp_t t2) 00349 { 00350 signed long l; 00351 00352 t1.tv_sec -= t2.tv_sec; 00353 l = (signed long) t1.tv_usec - (signed long) t2.tv_usec; 00354 if (l < 0) { 00355 t1.tv_sec--; 00356 l = -l; 00357 l %= 1000000; 00358 } 00359 return (t1.tv_sec * 1000000) + l; 00360 } 00361 00362 long readbuf(snd_pcm_t *handle, char *buf, long len, size_t *frames, size_t *max) 00363 { 00364 long r; 00365 00366 if (!block) { 00367 do { 00368 r = snd_pcm_readi(handle, buf, len); 00369 } while (r == -EAGAIN); 00370 if (r > 0) { 00371 *frames += r; 00372 if ((long)*max < r) 00373 *max = r; 00374 } 00375 // printf("read = %li\n", r); 00376 } else { 00377 int frame_bytes = (snd_pcm_format_width(format) / 8) * channels; 00378 do { 00379 r = snd_pcm_readi(handle, buf, len); 00380 if (r > 0) { 00381 buf += r * frame_bytes; 00382 len -= r; 00383 *frames += r; 00384 if ((long)*max < r) 00385 *max = r; 00386 } 00387 // printf("r = %li, len = %li\n", r, len); 00388 } while (r >= 1 && len > 0); 00389 } 00390 // showstat(handle, 0); 00391 return r; 00392 } 00393 00394 long writebuf(snd_pcm_t *handle, char *buf, long len, size_t *frames) 00395 { 00396 long r; 00397 00398 while (len > 0) { 00399 r = snd_pcm_writei(handle, buf, len); 00400 if (r == -EAGAIN) 00401 continue; 00402 // printf("write = %li\n", r); 00403 if (r < 0) 00404 return r; 00405 // showstat(handle, 0); 00406 buf += r * 4; 00407 len -= r; 00408 *frames += r; 00409 } 00410 return 0; 00411 } 00412 00413 #define FILTERSWEEP_LFO_CENTER 2000. 00414 #define FILTERSWEEP_LFO_DEPTH 1800. 00415 #define FILTERSWEEP_LFO_FREQ 0.2 00416 #define FILTER_BANDWIDTH 50 00417 00418 /* filter the sweep variables */ 00419 float lfo,dlfo,fs,fc,BW,C,D,a0,a1,a2,b1,b2,*x[3],*y[3]; 00420 00421 void applyeffect(char* buffer,int r) 00422 { 00423 short* samples = (short*) buffer; 00424 int i; 00425 for (i=0;i<r;i++) 00426 { 00427 int chn; 00428 00429 fc = sin(lfo)*FILTERSWEEP_LFO_DEPTH+FILTERSWEEP_LFO_CENTER; 00430 lfo += dlfo; 00431 if (lfo>2.*M_PI) lfo -= 2.*M_PI; 00432 C = 1./tan(M_PI*BW/fs); 00433 D = 2.*cos(2*M_PI*fc/fs); 00434 a0 = 1./(1.+C); 00435 a1 = 0; 00436 a2 = -a0; 00437 b1 = -C*D*a0; 00438 b2 = (C-1)*a0; 00439 00440 for (chn=0;chn<channels;chn++) 00441 { 00442 x[chn][2] = x[chn][1]; 00443 x[chn][1] = x[chn][0]; 00444 00445 y[chn][2] = y[chn][1]; 00446 y[chn][1] = y[chn][0]; 00447 00448 x[chn][0] = samples[i*channels+chn]; 00449 y[chn][0] = a0*x[chn][0] + a1*x[chn][1] + a2*x[chn][2] 00450 - b1*y[chn][1] - b2*y[chn][2]; 00451 samples[i*channels+chn] = y[chn][0]; 00452 } 00453 } 00454 } 00455 00456 void help(void) 00457 { 00458 int k; 00459 printf( 00460 "Usage: latency [OPTION]... [FILE]...\n" 00461 "-h,--help help\n" 00462 "-P,--pdevice playback device\n" 00463 "-C,--cdevice capture device\n" 00464 "-m,--min minimum latency in frames\n" 00465 "-M,--max maximum latency in frames\n" 00466 "-F,--frames frames to transfer\n" 00467 "-f,--format sample format\n" 00468 "-c,--channels channels\n" 00469 "-r,--rate rate\n" 00470 "-B,--buffer buffer size in frames\n" 00471 "-E,--period period size in frames\n" 00472 "-s,--seconds duration of test in seconds\n" 00473 "-b,--block block mode\n" 00474 "-t,--time maximal tick time in us\n" 00475 "-p,--poll use poll (wait for event - reduces CPU usage)\n" 00476 "-e,--effect apply an effect (bandpass filter sweep)\n" 00477 ); 00478 printf("Recognized sample formats are:"); 00479 for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) { 00480 const char *s = snd_pcm_format_name(k); 00481 if (s) 00482 printf(" %s", s); 00483 } 00484 printf("\n\n"); 00485 printf( 00486 "Tip #1 (usable latency with large periods, non-blocking mode, good CPU usage,\n" 00487 " superb xrun prevention):\n" 00488 " latency -m 8192 -M 8192 -t 1 -p\n" 00489 "Tip #2 (superb latency, non-blocking mode, but heavy CPU usage):\n" 00490 " latency -m 128 -M 128\n" 00491 ); 00492 } 00493 00494 int main(int argc, char *argv[]) 00495 { 00496 struct option long_option[] = 00497 { 00498 {"help", 0, NULL, 'h'}, 00499 {"pdevice", 1, NULL, 'P'}, 00500 {"cdevice", 1, NULL, 'C'}, 00501 {"min", 1, NULL, 'm'}, 00502 {"max", 1, NULL, 'M'}, 00503 {"frames", 1, NULL, 'F'}, 00504 {"format", 1, NULL, 'f'}, 00505 {"channels", 1, NULL, 'c'}, 00506 {"rate", 1, NULL, 'r'}, 00507 {"buffer", 1, NULL, 'B'}, 00508 {"period", 1, NULL, 'E'}, 00509 {"seconds", 1, NULL, 's'}, 00510 {"block", 0, NULL, 'b'}, 00511 {"time", 1, NULL, 't'}, 00512 {"poll", 0, NULL, 'p'}, 00513 {"effect", 0, NULL, 'e'}, 00514 {NULL, 0, NULL, 0}, 00515 }; 00516 snd_pcm_t *phandle, *chandle; 00517 char *buffer; 00518 int err, latency, morehelp; 00519 int ok; 00520 snd_timestamp_t p_tstamp, c_tstamp; 00521 ssize_t r; 00522 size_t frames_in, frames_out, in_max; 00523 int effect = 0; 00524 morehelp = 0; 00525 while (1) { 00526 int c; 00527 if ((c = getopt_long(argc, argv, "hP:C:m:M:F:f:c:r:s:bt:pen", long_option, NULL)) < 0) 00528 break; 00529 switch (c) { 00530 case 'h': 00531 morehelp++; 00532 break; 00533 case 'P': 00534 pdevice = strdup(optarg); 00535 break; 00536 case 'C': 00537 cdevice = strdup(optarg); 00538 break; 00539 case 'm': 00540 err = atoi(optarg) / 2; 00541 latency_min = err >= 4 ? err : 4; 00542 if (latency_max < latency_min) 00543 latency_max = latency_min; 00544 break; 00545 case 'M': 00546 err = atoi(optarg) / 2; 00547 latency_max = latency_min > err ? latency_min : err; 00548 break; 00549 case 'f': 00550 format = snd_pcm_format_value(optarg); 00551 if (format == SND_PCM_FORMAT_UNKNOWN) { 00552 printf("Unknown format, setting to default S16_LE\n"); 00553 format = SND_PCM_FORMAT_S16_LE; 00554 } 00555 break; 00556 case 'c': 00557 err = atoi(optarg); 00558 channels = err >= 1 && err < 1024 ? err : 1; 00559 break; 00560 case 'r': 00561 err = atoi(optarg); 00562 rate = err >= 4000 && err < 200000 ? err : 44100; 00563 break; 00564 case 'B': 00565 err = atoi(optarg); 00566 buffer_size = err >= 32 && err < 200000 ? err : 0; 00567 break; 00568 case 'E': 00569 err = atoi(optarg); 00570 period_size = err >= 32 && err < 200000 ? err : 0; 00571 break; 00572 case 's': 00573 err = atoi(optarg); 00574 loop_sec = err >= 1 && err <= 100000 ? err : 30; 00575 break; 00576 case 'b': 00577 block = 1; 00578 break; 00579 case 't': 00580 tick_time = atoi(optarg); 00581 tick_time = tick_time < 0 ? 0 : tick_time; 00582 break; 00583 case 'p': 00584 use_poll = 1; 00585 break; 00586 case 'e': 00587 effect = 1; 00588 break; 00589 case 'n': 00590 resample = 0; 00591 break; 00592 } 00593 } 00594 00595 if (morehelp) { 00596 help(); 00597 return 0; 00598 } 00599 err = snd_output_stdio_attach(&output, stdout, 0); 00600 if (err < 0) { 00601 printf("Output failed: %s\n", snd_strerror(err)); 00602 return 0; 00603 } 00604 00605 loop_limit = loop_sec * rate; 00606 latency = latency_min - 4; 00607 buffer = malloc((latency_max * snd_pcm_format_width(format) / 8) * 2); 00608 00609 setscheduler(); 00610 00611 printf("Playback device is %s\n", pdevice); 00612 printf("Capture device is %s\n", cdevice); 00613 printf("Parameters are %iHz, %s, %i channels, %s mode\n", rate, snd_pcm_format_name(format), channels, block ? "blocking" : "non-blocking"); 00614 printf("Wanted tick time: %ius, poll mode: %s\n", tick_time, use_poll ? "yes" : "no"); 00615 printf("Loop limit is %li frames, minimum latency = %i, maximum latency = %i\n", loop_limit, latency_min * 2, latency_max * 2); 00616 00617 if ((err = snd_pcm_open(&phandle, pdevice, SND_PCM_STREAM_PLAYBACK, block ? 0 : SND_PCM_NONBLOCK)) < 0) { 00618 printf("Playback open error: %s\n", snd_strerror(err)); 00619 return 0; 00620 } 00621 if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK)) < 0) { 00622 printf("Record open error: %s\n", snd_strerror(err)); 00623 return 0; 00624 } 00625 00626 /* initialize the filter sweep variables */ 00627 if (effect) { 00628 fs = (float) rate; 00629 BW = FILTER_BANDWIDTH; 00630 00631 lfo = 0; 00632 dlfo = 2.*M_PI*FILTERSWEEP_LFO_FREQ/fs; 00633 00634 x[0] = (float*) malloc(channels*sizeof(float)); 00635 x[1] = (float*) malloc(channels*sizeof(float)); 00636 x[2] = (float*) malloc(channels*sizeof(float)); 00637 y[0] = (float*) malloc(channels*sizeof(float)); 00638 y[1] = (float*) malloc(channels*sizeof(float)); 00639 y[2] = (float*) malloc(channels*sizeof(float)); 00640 } 00641 00642 while (1) { 00643 frames_in = frames_out = 0; 00644 if (setparams(phandle, chandle, &latency) < 0) 00645 break; 00646 showlatency(latency); 00647 if (tick_time_ok) 00648 printf("Using tick time %ius\n", tick_time_ok); 00649 if ((err = snd_pcm_link(chandle, phandle)) < 0) { 00650 printf("Streams link error: %s\n", snd_strerror(err)); 00651 exit(0); 00652 } 00653 if (snd_pcm_format_set_silence(format, buffer, latency*channels) < 0) { 00654 fprintf(stderr, "silence error\n"); 00655 break; 00656 } 00657 if (writebuf(phandle, buffer, latency, &frames_out) < 0) { 00658 fprintf(stderr, "write error\n"); 00659 break; 00660 } 00661 if (writebuf(phandle, buffer, latency, &frames_out) < 0) { 00662 fprintf(stderr, "write error\n"); 00663 break; 00664 } 00665 00666 if ((err = snd_pcm_start(chandle)) < 0) { 00667 printf("Go error: %s\n", snd_strerror(err)); 00668 exit(0); 00669 } 00670 gettimestamp(phandle, &p_tstamp); 00671 gettimestamp(chandle, &c_tstamp); 00672 #if 0 00673 printf("Playback:\n"); 00674 showstat(phandle, frames_out); 00675 printf("Capture:\n"); 00676 showstat(chandle, frames_in); 00677 #endif 00678 00679 ok = 1; 00680 in_max = 0; 00681 while (ok && frames_in < loop_limit) { 00682 if (use_poll) { 00683 /* use poll to wait for next event */ 00684 snd_pcm_wait(chandle, 1000); 00685 } 00686 if ((r = readbuf(chandle, buffer, latency, &frames_in, &in_max)) < 0) 00687 ok = 0; 00688 else { 00689 if (effect) 00690 applyeffect(buffer,r); 00691 if (writebuf(phandle, buffer, r, &frames_out) < 0) 00692 ok = 0; 00693 } 00694 } 00695 if (ok) 00696 printf("Success\n"); 00697 else 00698 printf("Failure\n"); 00699 printf("Playback:\n"); 00700 showstat(phandle, frames_out); 00701 printf("Capture:\n"); 00702 showstat(chandle, frames_in); 00703 showinmax(in_max); 00704 if (p_tstamp.tv_sec == p_tstamp.tv_sec && 00705 p_tstamp.tv_usec == c_tstamp.tv_usec) 00706 printf("Hardware sync\n"); 00707 snd_pcm_drop(chandle); 00708 snd_pcm_nonblock(phandle, 0); 00709 snd_pcm_drain(phandle); 00710 snd_pcm_nonblock(phandle, !block ? 1 : 0); 00711 if (ok) { 00712 #if 1 00713 printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n", 00714 p_tstamp.tv_sec, 00715 (int)p_tstamp.tv_usec, 00716 c_tstamp.tv_sec, 00717 (int)c_tstamp.tv_usec, 00718 timediff(p_tstamp, c_tstamp)); 00719 #endif 00720 break; 00721 } 00722 snd_pcm_unlink(chandle); 00723 snd_pcm_hw_free(phandle); 00724 snd_pcm_hw_free(chandle); 00725 } 00726 snd_pcm_close(phandle); 00727 snd_pcm_close(chandle); 00728 return 0; 00729 }