examples/sfexamples/oggvorbiscodec/src/libvorbis/lib/tone.c

00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include <string.h>
00005 
00006 void usage(){
00007   fprintf(stderr,"tone <frequency_Hz>,[<amplitude>] [<frequency_Hz>,[<amplitude>]...]\n");
00008   exit(1);
00009 }
00010 
00011 int main (int argc,char *argv[]){
00012   int i,j;
00013   double *f;
00014   double *amp;
00015   
00016   if(argc<2)usage();
00017 
00018   f=alloca(sizeof(*f)*(argc-1));
00019   amp=alloca(sizeof(*amp)*(argc-1));
00020 
00021   i=0;
00022   while(argv[i+1]){
00023     char *pos=strchr(argv[i+1],',');
00024     
00025     f[i]=atof(argv[i+1]);
00026     if(pos)
00027       amp[i]=atof(pos+1)*32767.f;
00028     else
00029       amp[i]=32767.f;
00030 
00031     fprintf(stderr,"%g Hz, %g amp\n",f[i],amp[i]);
00032 
00033     i++;
00034   }
00035 
00036   for(i=0;i<44100*10;i++){
00037     float val=0;
00038     int ival;
00039     for(j=0;j<argc-1;j++)
00040       val+=amp[j]*sin(i/44100.f*f[j]*2*M_PI);
00041     ival=rint(val);
00042 
00043     if(ival>32767.f)ival=32767.f;
00044     if(ival<-32768.f)ival=-32768.f;
00045 
00046     fprintf(stdout,"%c%c%c%c",
00047             (char)(ival&0xff),
00048             (char)((ival>>8)&0xff),
00049             (char)(ival&0xff),
00050             (char)((ival>>8)&0xff));
00051   }
00052   return(0);
00053 }
00054 

Generated by  doxygen 1.6.2