source: roaraudio/roarclients/roarsin.c @ 888:518573ceb8cc

Last change on this file since 888:518573ceb8cc was 888:518573ceb8cc, checked in by phi, 15 years ago

added trapece signal

File size: 2.8 KB
Line 
1//roarsin.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include <math.h>       /* sin() */
26#include <stdio.h>      /* *printf*() */
27#include <roaraudio.h>  /* libroar */
28
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
56double trap (double x) {
57 x /= 2*M_PI;
58 x -= (int)x;
59
60 if ( x < 0.125 || x > 0.875 ) {
61  return -1;
62 } else if ( x < 0.625 && x > 0.375 ) {
63  return  1;
64 } else if ( x < 0.5 ) {
65  return  8*(x-0.375) + 1;
66 } else {
67  return -8*(x-0.625) + 1;
68 }
69}
70
71int main (int argc, char * argv[]) {
72 int rate     = ROAR_RATE_DEFAULT;
73 int bits     = 16;
74 int channels = 1; /* mono */
75 int codec    = ROAR_CODEC_DEFAULT;
76 float freq   = 523.2;            /* middle C */
77 float t      = 0; /* current time */
78 float length = 5; /* 5 sec */
79 float step;       /* how much time per sample we have to encode ... */
80 int fh;
81 int i;
82 int16_t out[1024];
83 double (*func)(double x) = sin;
84
85 for (i = 1; i < argc; i++) {
86  if ( !strcmp(argv[i], "--freq") ) {
87   freq   = atof(argv[++i]);
88  } else if ( !strcmp(argv[i], "--time") ) {
89   length = atof(argv[++i]);
90  } else if ( !strcmp(argv[i], "--sin") ) {
91   func   = sin;
92  } else if ( !strcmp(argv[i], "--rect") ) {
93   func   = rect;
94  } else if ( !strcmp(argv[i], "--saw") ) {
95   func   = saw;
96  } else if ( !strcmp(argv[i], "--tri") ) {
97   func   = tri;
98  } else if ( !strcmp(argv[i], "--trap") ) {
99   func   = trap;
100  } else {
101   return 2;
102  }
103 }
104
105 step   = M_PI*2*freq/rate;
106
107 if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) {
108  fprintf(stderr, "Error: can not open playback!\n");
109  exit(1);
110 }
111
112 while (t < 2*M_PI*freq*length) {
113  for (i = 0; i < 1024; i++) {
114   out[i] = 32767*func(t);
115   t += step;
116  }
117  write(fh, out, 2048);
118 }
119
120 roar_simple_close(fh);
121
122 return 0;
123}
124
125//ll
Note: See TracBrowser for help on using the repository browser.