[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 */ |
---|
| 41 | float length = 5; /* 5 sec */ |
---|
| 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); |
---|
| 51 | struct roardsp_filterchain fc[1]; |
---|
| 52 | struct roardsp_filter filt[3]; |
---|
| 53 | struct roar_stream stream[1]; |
---|
[1132] | 54 | FILE * rmsout = NULL; |
---|
| 55 | int kill_var; |
---|
| 56 | #ifdef ROAR_HAVE_BIN_GNUPLOT |
---|
| 57 | int do_gnuplot = 0; |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | for (i = 1; i < argc; i++) { |
---|
| 61 | k = argv[i]; |
---|
| 62 | kill_var = 1; |
---|
| 63 | |
---|
| 64 | if ( !strcmp(k, "--rate") ) { |
---|
| 65 | rate = atoi(argv[++i]); |
---|
| 66 | #ifdef ROAR_HAVE_BIN_GNUPLOT |
---|
| 67 | } else if ( !strcmp(k, "--gnuplot") ) { |
---|
| 68 | do_gnuplot = 1; |
---|
| 69 | #endif |
---|
| 70 | } else if ( !strcmp(k, "--rmsout") ) { |
---|
| 71 | rmsout = fopen(argv[++i], "w"); |
---|
| 72 | } else { |
---|
| 73 | kill_var = 0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if ( kill_var ) |
---|
| 77 | *k = 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | #ifdef ROAR_HAVE_BIN_GNUPLOT |
---|
| 81 | if ( do_gnuplot && rmsout == NULL ) { |
---|
| 82 | if ( (rmsout = popen(ROAR_HAVE_BIN_GNUPLOT, "w")) != NULL ) { |
---|
| 83 | fprintf(rmsout, "set grid\n"); |
---|
| 84 | fprintf(rmsout, "set log x 10\n"); |
---|
| 85 | fprintf(rmsout, "set log y 10\n"); |
---|
| 86 | fprintf(rmsout, "plot \"-\" with lines title \"filter amplification\"\n"); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | #endif |
---|
[956] | 90 | |
---|
| 91 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) |
---|
| 92 | return 2; |
---|
| 93 | |
---|
[1132] | 94 | if ( roardsp_filter_init(filt, stream, ROARDSP_FILTER_DCBLOCK) == -1 ) { |
---|
[956] | 95 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
[1132] | 99 | /* |
---|
[956] | 100 | freq = 1000; |
---|
| 101 | roardsp_filter_ctl(filt, ROARDSP_FCTL_FREQ, &freq); |
---|
[1132] | 102 | */ |
---|
[956] | 103 | |
---|
| 104 | if ( roardsp_filter_init(filt+1, stream, ROARDSP_FILTER_HIGHP) == -1 ) { |
---|
| 105 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 106 | return 1; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | freq = 1000; |
---|
| 110 | roardsp_filter_ctl(filt+1, ROARDSP_FCTL_FREQ, &freq); |
---|
| 111 | |
---|
| 112 | if ( roardsp_filter_init(filt+2, stream, ROARDSP_FILTER_AMP) == -1 ) { |
---|
| 113 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
| 114 | return 1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | tmp = 2; |
---|
| 118 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_DIV, &tmp); |
---|
| 119 | tmp = 3; |
---|
| 120 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_MUL, &tmp); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | roardsp_fchain_init(fc); |
---|
| 124 | roardsp_fchain_add(fc, filt); |
---|
| 125 | // roardsp_fchain_add(fc, filt+1); |
---|
| 126 | // roardsp_fchain_add(fc, filt+2); |
---|
| 127 | |
---|
| 128 | fprintf(stderr, "Starting analysis in frequency domain...\n"); |
---|
| 129 | |
---|
[1132] | 130 | for (freq = 1/* 2*exp(1) */; freq < /* (float)rate/2 */ 100; freq *= (1+exp(1)/100), length /= (1+exp(1)/100)) { |
---|
[956] | 131 | step = M_PI*2*freq/rate; |
---|
| 132 | maxval = -amp; |
---|
| 133 | maxval_in = -amp; |
---|
| 134 | sc = 0; |
---|
| 135 | rms = 0; |
---|
| 136 | t = 0; |
---|
| 137 | |
---|
| 138 | while (t < 2*M_PI*freq*length) { |
---|
| 139 | for (i = 0; i < 1024; i++) { |
---|
| 140 | out[i] = amp*sin(t); |
---|
| 141 | if ( out[i] > maxval_in ) |
---|
| 142 | maxval_in = out[i]; |
---|
| 143 | t += step; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | roardsp_fchain_calc(fc, out, 1024); |
---|
| 147 | |
---|
| 148 | for (i = 0; i < 1024; i++) { |
---|
| 149 | sc++; |
---|
| 150 | rms += out[i] * out[i]; |
---|
| 151 | if ( out[i] > maxval ) |
---|
| 152 | maxval = out[i]; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | count++; |
---|
| 157 | sc_tot += sc; |
---|
| 158 | |
---|
| 159 | rms /= sc; |
---|
| 160 | rms = sqrt(rms); |
---|
| 161 | // rms /= amp; |
---|
| 162 | rms /= rms_in; |
---|
| 163 | |
---|
| 164 | printf("%f: %f %u %f\n", freq, (float)maxval/maxval_in, sc, rms); |
---|
[1132] | 165 | |
---|
| 166 | if ( rmsout != NULL ) |
---|
| 167 | fprintf(rmsout, "%f: %f\n", freq, rms); |
---|
[956] | 168 | } |
---|
| 169 | |
---|
[1132] | 170 | |
---|
[956] | 171 | fprintf(stderr, "Finished analysis in frequency domain: done tests on a total of %u samples on %i frequencies\n", |
---|
| 172 | sc_tot, count); |
---|
| 173 | |
---|
| 174 | roardsp_fchain_uninit(fc); |
---|
| 175 | |
---|
[1132] | 176 | if ( rmsout != NULL ) |
---|
| 177 | fclose(rmsout); |
---|
| 178 | |
---|
[956] | 179 | return 0; |
---|
| 180 | } |
---|
| 181 | |
---|
[1103] | 182 | #else |
---|
| 183 | int main (void) { |
---|
| 184 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 185 | return 1; |
---|
| 186 | } |
---|
| 187 | #endif |
---|
| 188 | |
---|
[956] | 189 | //ll |
---|