1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 6 void usage(){ 7 fprintf(stderr,"tone <frequency_Hz>,[<amplitude>] [<frequency_Hz>,[<amplitude>]...]\n"); 8 exit(1); 9 } 10 11 int main (int argc,char *argv[]){ 12 int i,j; 13 double *f; 14 double *amp; 15 16 if(argc<2)usage(); 17 18 f=alloca(sizeof(*f)*(argc-1)); 19 amp=alloca(sizeof(*amp)*(argc-1)); 20 21 i=0; 22 while(argv[i+1]){ 23 char *pos=strchr(argv[i+1],','); 24 25 f[i]=atof(argv[i+1]); 26 if(pos) 27 amp[i]=atof(pos+1)*32767.f; 28 else 29 amp[i]=32767.f; 30 31 fprintf(stderr,"%g Hz, %g amp\n",f[i],amp[i]); 32 33 i++; 34 } 35 36 for(i=0;i<44100*10;i++){ 37 float val=0; 38 int ival; 39 for(j=0;j<argc-1;j++) 40 val+=amp[j]*sin(i/44100.f*f[j]*2*M_PI); 41 ival=rint(val); 42 43 if(ival>32767.f)ival=32767.f; 44 if(ival<-32768.f)ival=-32768.f; 45 46 fprintf(stdout,"%c%c%c%c", 47 (char)(ival&0xff), 48 (char)((ival>>8)&0xff), 49 (char)(ival&0xff), 50 (char)((ival>>8)&0xff)); 51 } 52 return(0); 53 } 54 55