Changeset 887:a5d48b62c4fc in roaraudio for roarclients/roarsin.c


Ignore:
Timestamp:
11/19/08 22:29:07 (15 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added support to change freq, time and form of signal

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roarclients/roarsin.c

    r669 r887  
    2727#include <roaraudio.h>  /* libroar */ 
    2828 
    29 int main (void) { 
     29double rect (double x) { 
     30 x /= 2*M_PI; 
     31 x -= (int)x; 
     32 
     33 if ( x < 0.5 ) 
     34  return  1; 
     35 else 
     36  return -1; 
     37} 
     38 
     39double saw (double x) { 
     40 x /= 2*M_PI; 
     41 x -= (int)x; 
     42 
     43 return 2*x - 1; 
     44} 
     45 
     46double tri (double x) { 
     47 x /= 2*M_PI; 
     48 x -= (int)x; 
     49 
     50 if ( x < 0.5 ) 
     51  return   4* x      - 1; 
     52 else 
     53  return  -4*(x-0.5) + 1; 
     54} 
     55 
     56int main (int argc, char * argv[]) { 
    3057 int rate     = ROAR_RATE_DEFAULT; 
    31  int bits     = 8; 
     58 int bits     = 16; 
    3259 int channels = 1; /* mono */ 
    3360 int codec    = ROAR_CODEC_DEFAULT; 
    3461 float freq   = 523.2;            /* middle C */ 
    35  float step   = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */ 
    3662 float t      = 0; /* current time */ 
    3763 float length = 5; /* 5 sec */ 
     64 float step;       /* how much time per sample we have to encode ... */ 
    3865 int fh; 
    3966 int i; 
    40  char out[1024]; 
     67 int16_t out[1024]; 
     68 double (*func)(double x) = sin; 
     69 
     70 for (i = 1; i < argc; i++) { 
     71  if ( !strcmp(argv[i], "--freq") ) { 
     72   freq   = atof(argv[++i]); 
     73  } else if ( !strcmp(argv[i], "--time") ) { 
     74   length = atof(argv[++i]); 
     75  } else if ( !strcmp(argv[i], "--sin") ) { 
     76   func   = sin; 
     77  } else if ( !strcmp(argv[i], "--saw") ) { 
     78   func   = saw; 
     79  } else if ( !strcmp(argv[i], "--tri") ) { 
     80   func   = tri; 
     81  } else { 
     82   return 2; 
     83  } 
     84 } 
     85 
     86 step   = M_PI*2*freq/rate; 
    4187 
    4288 if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) { 
     
    4793 while (t < 2*M_PI*freq*length) { 
    4894  for (i = 0; i < 1024; i++) { 
    49    out[i] = 127*sin(t); 
     95   out[i] = 32767*func(t); 
    5096   t += step; 
    5197  } 
    52   write(fh, out, 1024); 
     98  write(fh, out, 2048); 
    5399 } 
    54100 
Note: See TracChangeset for help on using the changeset viewer.