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