[2292] | 1 | //roarfctest.c: |
---|
[956] | 2 | |
---|
| 3 | /* |
---|
[3812] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[956] | 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. |
---|
[956] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[1103] | 26 | #include <roaraudio.h> /* libroar */ |
---|
| 27 | |
---|
| 28 | #ifdef ROAR_HAVE_LIBM |
---|
| 29 | |
---|
[956] | 30 | #include <math.h> /* sin() */ |
---|
| 31 | #include <stdio.h> /* *printf*() */ |
---|
| 32 | #include <libroardsp/libroardsp.h> |
---|
| 33 | |
---|
[1132] | 34 | int main (int argc, char * argv[]) { |
---|
[956] | 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 */ |
---|
[1133] | 42 | float length; |
---|
[956] | 43 | int16_t amp = 32767; |
---|
| 44 | int i; |
---|
[1132] | 45 | char * k; |
---|
[956] | 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); |
---|
[1133] | 52 | float worklen; |
---|
| 53 | float sfreq = 1, efreq = -1; |
---|
| 54 | float engage = 0; |
---|
| 55 | float tmax = 1024; |
---|
[956] | 56 | struct roardsp_filterchain fc[1]; |
---|
| 57 | struct roardsp_filter filt[3]; |
---|
| 58 | struct roar_stream stream[1]; |
---|
[1132] | 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]); |
---|
[1133] | 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]); |
---|
[1132] | 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 |
---|
[956] | 103 | |
---|
| 104 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) |
---|
| 105 | return 2; |
---|
| 106 | |
---|
[1132] | 107 | if ( roardsp_filter_init(filt, stream, ROARDSP_FILTER_DCBLOCK) == -1 ) { |
---|
[956] | 108 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 109 | return 1; |
---|
| 110 | } |
---|
| 111 | |
---|
[1132] | 112 | /* |
---|
[956] | 113 | freq = 1000; |
---|
| 114 | roardsp_filter_ctl(filt, ROARDSP_FCTL_FREQ, &freq); |
---|
[1132] | 115 | */ |
---|
[956] | 116 | |
---|
| 117 | if ( roardsp_filter_init(filt+1, stream, ROARDSP_FILTER_HIGHP) == -1 ) { |
---|
| 118 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 119 | return 1; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | freq = 1000; |
---|
| 123 | roardsp_filter_ctl(filt+1, ROARDSP_FCTL_FREQ, &freq); |
---|
| 124 | |
---|
| 125 | if ( roardsp_filter_init(filt+2, stream, ROARDSP_FILTER_AMP) == -1 ) { |
---|
| 126 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 127 | return 1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | tmp = 2; |
---|
| 131 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_DIV, &tmp); |
---|
| 132 | tmp = 3; |
---|
| 133 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_MUL, &tmp); |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | roardsp_fchain_init(fc); |
---|
| 137 | roardsp_fchain_add(fc, filt); |
---|
| 138 | // roardsp_fchain_add(fc, filt+1); |
---|
| 139 | // roardsp_fchain_add(fc, filt+2); |
---|
| 140 | |
---|
| 141 | fprintf(stderr, "Starting analysis in frequency domain...\n"); |
---|
| 142 | |
---|
[1133] | 143 | if ( efreq == -1 ) |
---|
| 144 | efreq = (float)rate/2; |
---|
| 145 | |
---|
| 146 | length = 5/sfreq; |
---|
| 147 | |
---|
| 148 | for (freq = sfreq; freq < efreq; freq *= (1+exp(1)/100), length /= (1+exp(1)/100)) { |
---|
[956] | 149 | step = M_PI*2*freq/rate; |
---|
| 150 | maxval = -amp; |
---|
| 151 | maxval_in = -amp; |
---|
| 152 | sc = 0; |
---|
| 153 | rms = 0; |
---|
[1133] | 154 | t = -engage; |
---|
| 155 | |
---|
| 156 | if ( roardsp_fchain_reset(fc, ROARDSP_RESET_STATE) == -1 ) { |
---|
| 157 | ROAR_ERR("Can not reset filterchain."); |
---|
| 158 | return 8; |
---|
| 159 | } |
---|
| 160 | |
---|
[956] | 161 | |
---|
[1133] | 162 | worklen = length; |
---|
| 163 | |
---|
| 164 | if ( worklen > tmax ) |
---|
| 165 | worklen = tmax; |
---|
| 166 | |
---|
| 167 | worklen = 2*M_PI*freq*worklen; |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | while (t < worklen) { |
---|
[956] | 171 | for (i = 0; i < 1024; i++) { |
---|
| 172 | out[i] = amp*sin(t); |
---|
| 173 | if ( out[i] > maxval_in ) |
---|
| 174 | maxval_in = out[i]; |
---|
| 175 | t += step; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | roardsp_fchain_calc(fc, out, 1024); |
---|
| 179 | |
---|
[1133] | 180 | if ( t >= 0 ) { |
---|
| 181 | for (i = 0; i < 1024; i++) { |
---|
| 182 | rms += out[i] * out[i]; |
---|
| 183 | if ( out[i] > maxval ) |
---|
| 184 | maxval = out[i]; |
---|
| 185 | } |
---|
| 186 | sc += 1024; |
---|
| 187 | } else { |
---|
| 188 | sc_tot += 1024; |
---|
[956] | 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | count++; |
---|
| 193 | sc_tot += sc; |
---|
| 194 | |
---|
| 195 | rms /= sc; |
---|
| 196 | rms = sqrt(rms); |
---|
| 197 | // rms /= amp; |
---|
| 198 | rms /= rms_in; |
---|
| 199 | |
---|
| 200 | printf("%f: %f %u %f\n", freq, (float)maxval/maxval_in, sc, rms); |
---|
[1132] | 201 | |
---|
| 202 | if ( rmsout != NULL ) |
---|
| 203 | fprintf(rmsout, "%f: %f\n", freq, rms); |
---|
[956] | 204 | } |
---|
| 205 | |
---|
[1132] | 206 | |
---|
[956] | 207 | fprintf(stderr, "Finished analysis in frequency domain: done tests on a total of %u samples on %i frequencies\n", |
---|
| 208 | sc_tot, count); |
---|
| 209 | |
---|
| 210 | roardsp_fchain_uninit(fc); |
---|
| 211 | |
---|
[1132] | 212 | if ( rmsout != NULL ) |
---|
| 213 | fclose(rmsout); |
---|
| 214 | |
---|
[956] | 215 | return 0; |
---|
| 216 | } |
---|
| 217 | |
---|
[1103] | 218 | #else |
---|
| 219 | int main (void) { |
---|
| 220 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 221 | return 1; |
---|
| 222 | } |
---|
| 223 | #endif |
---|
| 224 | |
---|
[956] | 225 | //ll |
---|