source: roaraudio/roarclients/roarsin.c @ 5533:42f48072307c

Last change on this file since 5533:42f48072307c was 5533:42f48072307c, checked in by phi, 12 years ago

Fixed usage of -R/-B/-C/-E as well as --aiprofile in roarclients (Closes: #176)

File size: 4.5 KB
RevLine 
[154]1//roarsin.c:
2
[669]3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[669]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[669]23 *
24 */
25
[1103]26#include <roaraudio.h>  /* libroar */
27
28#ifdef ROAR_HAVE_LIBM
29
[154]30#include <math.h>       /* sin() */
31#include <stdio.h>      /* *printf*() */
32
[887]33double rect (double x) {
34 x /= 2*M_PI;
35 x -= (int)x;
36
37 if ( x < 0.5 )
38  return  1;
39 else
40  return -1;
41}
42
43double saw (double x) {
44 x /= 2*M_PI;
45 x -= (int)x;
46
47 return 2*x - 1;
48}
49
50double tri (double x) {
51 x /= 2*M_PI;
52 x -= (int)x;
53
54 if ( x < 0.5 )
55  return   4* x      - 1;
56 else
57  return  -4*(x-0.5) + 1;
58}
59
[888]60double trap (double x) {
61 x /= 2*M_PI;
62 x -= (int)x;
63
64 if ( x < 0.125 || x > 0.875 ) {
65  return -1;
66 } else if ( x < 0.625 && x > 0.375 ) {
67  return  1;
68 } else if ( x < 0.5 ) {
69  return  8*(x-0.375) + 1;
70 } else {
71  return -8*(x-0.625) + 1;
72 }
73}
74
[5533]75static void usage(const char * progname) {
76 fprintf(stderr, "Usage: %s [OPTIONS] [FUNCTION]\n", progname);
77
78 fprintf(stderr, "\nOptions:\n\n");
79 fprintf(stderr,
80         "  --help                  - Show this help\n"
81         "  --server SERVER         - Set server address\n"
82         "  --rate   -R  RATE       - Set sample rate to use\n"
83         "  --freq FREQ             - Set frequency (in Hz)\n"
84         "  --time TIME             - Set time (in sec)\n"
85        );
86
87 fprintf(stderr, "\nFunctions:\n\n");
88 fprintf(stderr,
89         "  --sin                   - Use Sinus\n"
90         "  --rect                  - Use Rectangle\n"
91         "  --saw                   - Use Saw\n"
92         "  --tri                   - Use Triangle\n"
93         "  --trap                  - Use Trap\n"
94        );
95}
96
[887]97int main (int argc, char * argv[]) {
[5533]98 const char * server = NULL;
[154]99 int rate     = ROAR_RATE_DEFAULT;
[887]100 int bits     = 16;
[154]101 int channels = 1; /* mono */
102 int codec    = ROAR_CODEC_DEFAULT;
103 float freq   = 523.2;            /* middle C */
104 float t      = 0; /* current time */
[5186]105 float tcalc  = 0; /* current time for calculation */
[154]106 float length = 5; /* 5 sec */
[887]107 float step;       /* how much time per sample we have to encode ... */
[4885]108 roar_vs_t * vss;
109 int err;
[154]110 int i;
[887]111 int16_t out[1024];
112 double (*func)(double x) = sin;
113
114 for (i = 1; i < argc; i++) {
115  if ( !strcmp(argv[i], "--freq") ) {
116   freq   = atof(argv[++i]);
117  } else if ( !strcmp(argv[i], "--time") ) {
118   length = atof(argv[++i]);
[5533]119  } else if ( !strcmp(argv[i], "--server") ) {
120   server = argv[++i];
121  } else if ( !strcmp(argv[i], "--rate") || !strcmp(argv[i], "-R") ) {
122   rate = roar_str2rate(argv[++i]);
[887]123  } else if ( !strcmp(argv[i], "--sin") ) {
124   func   = sin;
[888]125  } else if ( !strcmp(argv[i], "--rect") ) {
126   func   = rect;
[887]127  } else if ( !strcmp(argv[i], "--saw") ) {
128   func   = saw;
129  } else if ( !strcmp(argv[i], "--tri") ) {
130   func   = tri;
[888]131  } else if ( !strcmp(argv[i], "--trap") ) {
132   func   = trap;
[5533]133  } else if ( !strcmp(argv[i], "--help") ) {
134   usage(argv[0]);
135   return 0;
[887]136  } else {
[5533]137   usage(argv[0]);
[887]138   return 2;
139  }
140 }
141
142 step   = M_PI*2*freq/rate;
[154]143
[5533]144 if ( (vss = roar_vs_new_playback(server, "sine gen", rate, channels, codec, bits, &err)) == NULL ) {
[4885]145  fprintf(stderr, "Error: can not open playback: %s\n", roar_vs_strerr(err));
[154]146  exit(1);
147 }
148
149 while (t < 2*M_PI*freq*length) {
150  for (i = 0; i < 1024; i++) {
[5186]151   out[i] = 32767*func(tcalc);
152   t     += step;
153   tcalc += step;
[154]154  }
[5249]155  if ( roar_vs_write(vss, out, 2048, &err) != (ssize_t)2048 ) {
156   fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err));
157   break;
158  }
[5186]159
160  // this code enables us to generate the same signal for a long periode of time
161  // without loosing accuracy of the float type.
162  while (tcalc > 2*M_PI)
163   tcalc -= 2*M_PI;
[154]164 }
165
[4885]166 roar_vs_close(vss, ROAR_VS_FALSE, NULL);
[154]167
168 return 0;
169}
170
[1103]171#else
172int main (void) {
173 fprintf(stderr, "Error: No Math library support compiled in.\n");
174 return 1;
175}
176#endif
177
[154]178//ll
Note: See TracBrowser for help on using the repository browser.