/* dodsp.c by Michael Thorpe 2008-04-04 */ #include #include #include #include #include #include #include #include #include void dousage(int exitval) { if(exitval) { fputs( "usage: dodsp [-38ehim] [-s <#>] [-d ] [ [ [..]]]\n",stderr); } else { fputs( "usage: dodsp [-38ehim] [-s <#>] [-d ] [ [ [..]]]\n" "\t-3 pass device as fd 3 instead of 0 or 1\n" "\t-8 8-bit samples (default is 16)\n" "\t-d device file to use (default is /dev/dsp)\n" "\t-e non-native-endian 16-bit samples\n" "\t-i change stdin instead of stdout\n" "\t-h show this help message\n" "\t-m mono mode (default is stereo)\n" "\t-s <#> samples per second (default is 44100)\n",stdout); } exit(exitval); } int main(int argc,char **argv) { int c,f; int format=AFMT_S16_NE,reqfd=1,stereo=1,speed=44100; char *dev="/dev/dsp"; while(EOF != (c=getopt(argc,argv,"+38ehims:d:"))) { switch(c) { case '3': reqfd=3; break; case '8': if(format != AFMT_S16_NE) dousage(1); format=AFMT_S8; break; case 'd': dev=optarg; break; case 'e': if(format != AFMT_S16_NE) dousage(1); format=AFMT_S16_LE+AFMT_S16_BE-AFMT_S16_NE; break; case 'h': dousage(0); break; case 'i': reqfd=0; break; case 's': speed=atoi(optarg); break; case 'm': stereo=0; break; case '?': dousage(1); break; } } if(optind==argc) dousage(1); close(reqfd); f=open(dev,reqfd?3==reqfd?O_RDWR:O_WRONLY:O_RDONLY); if(f==-1) { perror("open"); return(1); } if(f != reqfd) { dup2(f,reqfd); close(f); } if(ioctl(f,SNDCTL_DSP_SETFMT,&format)) { perror("ioctl"); return(-1); } c=speed; if(ioctl(f,SNDCTL_DSP_SPEED,&c)) { perror("ioctl"); return(-1); } if(c != speed) { fprintf(stderr,"Asked for speed of %d, got speed of %d\n",speed,c); return(-1); } if(ioctl(f,SNDCTL_DSP_STEREO,&stereo)) { perror("ioctl"); return(-1); } execvp(argv[optind],argv+optind); perror("exec"); return(-1); }