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

Generated on Wed Jan 25 10:30:30 2012 for ALSA project - the C library reference by  doxygen 1.5.0