tone.c 591 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. void usage(){
  5. fprintf(stderr,"tone <frequency_Hz> [<amplitude>]\n");
  6. exit(1);
  7. }
  8. int main (int argc,char *argv[]){
  9. int i;
  10. double f;
  11. double amp=32767.;
  12. if(argc<2)usage();
  13. f=atof(argv[1]);
  14. if(argc>=3)amp=atof(argv[2])*32767.;
  15. for(i=0;i<44100*10;i++){
  16. long val=rint(amp*sin(i/44100.*f*2*M_PI));
  17. if(val>32767.)val=32767.;
  18. if(val<-32768.)val=-32768.;
  19. fprintf(stdout,"%c%c%c%c",
  20. (char)(val&0xff),
  21. (char)((val>>8)&0xff),
  22. (char)(val&0xff),
  23. (char)((val>>8)&0xff));
  24. }
  25. return(0);
  26. }