source: roaraudio/roarclients/roarfctest.c @ 1103:bb5ee2384821

Last change on this file since 1103:bb5ee2384821 was 1103:bb5ee2384821, checked in by phi, 15 years ago

disable options needing libm

File size: 3.7 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 <roaraudio.h>  /* libroar */
26
27#ifdef ROAR_HAVE_LIBM
28
29#include <math.h>       /* sin() */
30#include <stdio.h>      /* *printf*() */
31#include <libroardsp/libroardsp.h>
32
33int main (void) {
34 int rate     = ROAR_RATE_DEFAULT;
35 int bits     = 16;
36 int channels = 1; /* mono */
37 int codec    = ROAR_CODEC_DEFAULT;
38 float freq   = 523.2;            /* middle C */
39 float step; //   = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */
40 float t      = 0; /* current time */
41 float length = 5; /* 5 sec */
42 int16_t amp  = 32767;
43 int      i;
44 int32_t  tmp;
45 int16_t  out[1024];
46 int16_t  maxval, maxval_in;
47 uint32_t sc, sc_tot = 0;
48 int      count = 0;
49 float    rms, rms_in = amp/sqrt(2);
50 struct roardsp_filterchain fc[1];
51 struct roardsp_filter      filt[3];
52 struct roar_stream         stream[1];
53
54 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 )
55  return 2;
56
57 if ( roardsp_filter_init(filt, stream, ROARDSP_FILTER_LOWP) == -1 ) {
58  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
59  return 1;
60 }
61
62 freq = 1000;
63 roardsp_filter_ctl(filt, ROARDSP_FCTL_FREQ, &freq);
64
65 if ( roardsp_filter_init(filt+1, stream, ROARDSP_FILTER_HIGHP) == -1 ) {
66  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
67  return 1;
68 }
69
70 freq = 1000;
71 roardsp_filter_ctl(filt+1, ROARDSP_FCTL_FREQ, &freq);
72
73 if ( roardsp_filter_init(filt+2, stream, ROARDSP_FILTER_AMP) == -1 ) {
74  ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno);
75  return 1;
76 }
77
78 tmp = 2;
79 roardsp_filter_ctl(filt+2, ROARDSP_FCTL_DIV, &tmp);
80 tmp = 3;
81 roardsp_filter_ctl(filt+2, ROARDSP_FCTL_MUL, &tmp);
82
83
84 roardsp_fchain_init(fc);
85 roardsp_fchain_add(fc, filt);
86// roardsp_fchain_add(fc, filt+1);
87// roardsp_fchain_add(fc, filt+2);
88
89 fprintf(stderr, "Starting analysis in frequency domain...\n");
90
91 for (freq = 1/* 2*exp(1) */; freq < (float)rate/2; freq *= (1+exp(1)/100), length /= (1+exp(1)/100)) {
92  step      = M_PI*2*freq/rate;
93  maxval    = -amp;
94  maxval_in = -amp;
95  sc        = 0;
96  rms       = 0;
97  t         = 0;
98
99  while (t < 2*M_PI*freq*length) {
100   for (i = 0; i < 1024; i++) {
101    out[i] = amp*sin(t);
102    if ( out[i] > maxval_in )
103     maxval_in = out[i];
104    t += step;
105   }
106
107   roardsp_fchain_calc(fc, out, 1024);
108
109   for (i = 0; i < 1024; i++) {
110    sc++;
111    rms += out[i] * out[i];
112    if ( out[i] > maxval )
113     maxval = out[i];
114   }
115  }
116
117  count++;
118  sc_tot += sc;
119
120  rms /= sc;
121  rms  = sqrt(rms);
122//  rms /= amp;
123  rms /= rms_in;
124
125  printf("%f: %f %u %f\n", freq, (float)maxval/maxval_in, sc, rms);
126 }
127
128 fprintf(stderr, "Finished analysis in frequency domain: done tests on a total of %u samples on %i frequencies\n",
129           sc_tot, count);
130
131 roardsp_fchain_uninit(fc);
132
133 return 0;
134}
135
136#else
137int main (void) {
138 fprintf(stderr, "Error: No Math library support compiled in.\n");
139 return 1;
140}
141#endif
142
143//ll
Note: See TracBrowser for help on using the repository browser.