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