/* * set the sample rate of the audio device */ #include #include #include #include #include #define TRUE 1 #define FALSE 0 extern int errno; int verbose = FALSE; char *progname; /* Program name */ void usage() { fprintf(stderr, "Usage: %s -d audio_device_name -r rate\n", progname); exit(0); } int main(int argc, char *argv[]) { extern char *optarg; /* for the getopt() system call */ extern int optind, opterr, optopt; int errflg=FALSE; /* set if there is a command line error */ int c; int dspFD; int ioctlParam; int rate=44100; /* default is 44100 */ char *dspname; char default_dspname[]="/dev/sound/dsp"; dspname=default_dspname; progname = argv[0]; /* * Decode argument list */ while ((c = getopt(argc, argv, "d:hr:v")) != EOF) switch (c) { case 'v': verbose = TRUE; break; case 'h': case '?': usage(); break; case 'd': if (verbose) fprintf(stdout, "selected audio device is %s\n",optarg); dspname=optarg; break; case ':': /* -d or -r without arguments */ fprintf(stderr, "Option -%c requires an argument\n", optopt); errflg++; break; case 'r': sscanf(optarg, "%d", &rate); if (verbose) fprintf(stdout, "selected audio rate is %d\n", rate); break; } if (errflg) usage(); if ((dspFD = open(dspname, O_RDWR)) < 0) { perror(dspname); exit(1); } ioctlParam = rate; if( ioctl( dspFD, SNDCTL_DSP_SPEED, &ioctlParam ) == -1 ) { perror("SNDCTL_DSP_SPEED"); exit(-1); } if (verbose && (ioctlParam != rate) ) { fprintf(stdout, "Failed to set sample rate to %d\n", rate); } if (verbose) fprintf(stderr, "actual speed is %d\n", ioctlParam); close(dspFD); }