/test/latency.c

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

Generated on Fri Jan 23 21:14:29 2009 for ALSA project - the C library reference by  doxygen 1.4.7