source: roaraudio/roarclients/roarfctest.c @ 4014:2fee6bf2dc0c

Last change on this file since 4014:2fee6bf2dc0c was 4014:2fee6bf2dc0c, checked in by phi, 14 years ago

fixed some resurce leaks

File size: 5.4 KB
Line 
1//roarfctest.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>  /* libroar */
27
28#ifdef ROAR_HAVE_LIBM
29
30#include <math.h>       /* sin() */
31#include <stdio.h>      /* *printf*() */
32#include <libroardsp/libroardsp.h>
33
34int main (int argc, char * argv[]) {
35 int rate     = ROAR_RATE_DEFAULT;
36 int bits     = 16;
37 int channels = 1; /* mono */
38 int codec    = ROAR_CODEC_DEFAULT;
39 float freq   = 523.2;            /* middle C */
40 float step; //   = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */
41 float t      = 0; /* current time */
42 float length;
43 int16_t amp  = 32767;
44 int      i;
45 char   * k;
46 int32_t  tmp;
47 int16_t  out[1024];
48 int16_t  maxval, maxval_in;
49 uint32_t sc, sc_tot = 0;
50 int      count = 0;
51 float    rms, rms_in = amp/sqrt(2);
52 float    worklen;
53 float    sfreq  = 1, efreq = -1;
54 float    engage = 0;
55 float    tmax   = 1024;
56 struct roardsp_filterchain fc[1];
57 struct roardsp_filter      filt[3];
58 struct roar_stream         stream[1];
59 FILE * rmsout = NULL;
60 int kill_var;
61#ifdef ROAR_HAVE_BIN_GNUPLOT
62 int do_gnuplot = 0;
63#endif
64
65 for (i = 1; i < argc; i++) {
66  k = argv[i];
67  kill_var = 1;
68
69  if ( !strcmp(k, "--rate") ) {
70   rate = atoi(argv[++i]);
71  } else if ( !strcmp(k, "--sfreq") ) {
72   sfreq = atof(argv[++i]);
73  } else if ( !strcmp(k, "--efreq") ) {
74   efreq = atof(argv[++i]);
75  } else if ( !strcmp(k, "--engage") ) {
76   engage = atof(argv[++i]);
77  } else if ( !strcmp(k, "--tmax") ) {
78   tmax = atof(argv[++i]);
79#ifdef ROAR_HAVE_BIN_GNUPLOT
80  } else if ( !strcmp(k, "--gnuplot") ) {
81   do_gnuplot = 1;
82#endif
83  } else if ( !strcmp(k, "--rmsout") ) {
84   rmsout = fopen(argv[++i], "w");
85  } else {
86   kill_var = 0;
87  }
88
89  if ( kill_var )
90   *k = 0;
91 }
92
93#ifdef ROAR_HAVE_BIN_GNUPLOT
94 if ( do_gnuplot && rmsout == NULL ) {
95  if ( (rmsout = popen(ROAR_HAVE_BIN_GNUPLOT, "w")) != NULL ) {
96   fprintf(rmsout, "set grid\n");
97   fprintf(rmsout, "set log x 10\n");
98   fprintf(rmsout, "set log y 10\n");
99   fprintf(rmsout, "plot \"-\" with lines title \"filter amplification\"\n");
100  }
101 }
102#endif
103
104 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) {
105  pclose(rmsout);
106  return 2;
107 }
108
109 if ( roardsp_filter_init(filt, stream, ROARDSP_FILTER_DCBLOCK) == -1 ) {
110  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
111  pclose(rmsout);
112  return 1;
113 }
114
115/*
116 freq = 1000;
117 roardsp_filter_ctl(filt, ROARDSP_FCTL_FREQ, &freq);
118*/
119
120 if ( roardsp_filter_init(filt+1, stream, ROARDSP_FILTER_HIGHP) == -1 ) {
121  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
122  pclose(rmsout);
123  return 1;
124 }
125
126 freq = 1000;
127 roardsp_filter_ctl(filt+1, ROARDSP_FCTL_FREQ, &freq);
128
129 if ( roardsp_filter_init(filt+2, stream, ROARDSP_FILTER_AMP) == -1 ) {
130  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
131  pclose(rmsout);
132  return 1;
133 }
134
135 tmp = 2;
136 roardsp_filter_ctl(filt+2, ROARDSP_FCTL_DIV, &tmp);
137 tmp = 3;
138 roardsp_filter_ctl(filt+2, ROARDSP_FCTL_MUL, &tmp);
139
140
141 roardsp_fchain_init(fc);
142 roardsp_fchain_add(fc, filt);
143// roardsp_fchain_add(fc, filt+1);
144// roardsp_fchain_add(fc, filt+2);
145
146 fprintf(stderr, "Starting analysis in frequency domain...\n");
147
148 if ( efreq == -1 )
149  efreq = (float)rate/2;
150
151 length = 5/sfreq;
152
153 for (freq = sfreq; freq < efreq; freq *= (1+exp(1)/100), length /= (1+exp(1)/100)) {
154  step      = M_PI*2*freq/rate;
155  maxval    = -amp;
156  maxval_in = -amp;
157  sc        = 0;
158  rms       = 0;
159  t         = -engage;
160
161  if ( roardsp_fchain_reset(fc, ROARDSP_RESET_STATE) == -1 ) {
162   ROAR_ERR("Can not reset filterchain.");
163   return 8;
164  }
165
166
167  worklen = length;
168
169  if ( worklen > tmax )
170   worklen = tmax;
171
172  worklen = 2*M_PI*freq*worklen;
173
174
175  while (t < worklen) {
176   for (i = 0; i < 1024; i++) {
177    out[i] = amp*sin(t);
178    if ( out[i] > maxval_in )
179     maxval_in = out[i];
180    t += step;
181   }
182
183   roardsp_fchain_calc(fc, out, 1024);
184
185   if ( t >= 0 ) {
186    for (i = 0; i < 1024; i++) {
187     rms += out[i] * out[i];
188     if ( out[i] > maxval )
189      maxval = out[i];
190    }
191    sc += 1024;
192   } else {
193    sc_tot += 1024;
194   }
195  }
196
197  count++;
198  sc_tot += sc;
199
200  rms /= sc;
201  rms  = sqrt(rms);
202//  rms /= amp;
203  rms /= rms_in;
204
205  printf("%f: %f %u %f\n", freq, (float)maxval/maxval_in, sc, rms);
206
207  if ( rmsout != NULL )
208   fprintf(rmsout, "%f: %f\n", freq, rms);
209 }
210
211
212 fprintf(stderr, "Finished analysis in frequency domain: done tests on a total of %u samples on %i frequencies\n",
213           sc_tot, count);
214
215 roardsp_fchain_uninit(fc);
216
217 if ( rmsout != NULL )
218  pclose(rmsout);
219
220 return 0;
221}
222
223#else
224int main (void) {
225 fprintf(stderr, "Error: No Math library support compiled in.\n");
226 return 1;
227}
228#endif
229
230//ll
Note: See TracBrowser for help on using the repository browser.