[126] | 1 | //roarfilt.c: |
---|
| 2 | |
---|
[669] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[669] | 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. |
---|
[669] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[126] | 26 | #include <roaraudio.h> |
---|
[682] | 27 | #include <libroardsp/libroardsp.h> |
---|
[1103] | 28 | |
---|
| 29 | #ifdef ROAR_HAVE_LIBM |
---|
[130] | 30 | #include <math.h> |
---|
[1103] | 31 | #endif |
---|
[126] | 32 | |
---|
| 33 | #define BUFSIZE 1024 |
---|
[1103] | 34 | #ifdef ROAR_HAVE_LIBM |
---|
[646] | 35 | struct { |
---|
[648] | 36 | uint16_t a, b; |
---|
| 37 | int16_t old[ROAR_MAX_CHANNELS]; |
---|
[646] | 38 | } g_lowpass; |
---|
[1103] | 39 | #endif |
---|
[126] | 40 | |
---|
| 41 | void usage (void) { |
---|
[233] | 42 | printf("roarfilt [OPTIONS]...\n"); |
---|
[126] | 43 | |
---|
| 44 | printf("\nOptions:\n\n"); |
---|
| 45 | |
---|
| 46 | printf(" --server SERVER - Set server hostname\n" |
---|
| 47 | " --rate RATE - Set sample rate\n" |
---|
| 48 | " --bits BITS - Set bits per sample\n" |
---|
| 49 | " --chans CHANNELS - Set number of channels\n" |
---|
| 50 | " --help - Show this help\n" |
---|
[128] | 51 | "\n" |
---|
| 52 | " --half - half the volume\n" |
---|
| 53 | " --double - double the volume\n" |
---|
[130] | 54 | " --amp VAL - Set amplification\n" |
---|
| 55 | " --mul VAL - Set mul\n" |
---|
| 56 | " --div VAL - Set div\n" |
---|
[1103] | 57 | #ifdef ROAR_HAVE_LIBM |
---|
[646] | 58 | " --lowpass freq - lowpass filter\n" |
---|
[1103] | 59 | #endif |
---|
[682] | 60 | " --filter name - add filter name\n" |
---|
| 61 | " --ffreq freq - set filter freq\n" |
---|
[884] | 62 | " --fmul mult - set filter multiplier\n" |
---|
| 63 | " --fdiv div - set filter divider\n" |
---|
[985] | 64 | " --fn N - set filter N parameter\n" |
---|
| 65 | " --flimit limit - set filter limit parameter\n" |
---|
[1005] | 66 | " --fmode mode - set filter mode parameter\n" |
---|
| 67 | " --fq Q - set filter quality\n" |
---|
[126] | 68 | ); |
---|
| 69 | |
---|
| 70 | } |
---|
| 71 | |
---|
[128] | 72 | void vol2 (void * data, int mul, int div, int len) { |
---|
| 73 | int16_t * samples = (int16_t *) data; |
---|
| 74 | int i; |
---|
| 75 | |
---|
| 76 | len /= 2; |
---|
| 77 | |
---|
| 78 | for (i = 0; i < len; i++) |
---|
| 79 | samples[i] = ((int) samples[i] * mul) / div; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void vol1 (void * data, int mul, int div, int len) { |
---|
| 83 | int8_t * samples = (int8_t *) data; |
---|
| 84 | int i; |
---|
| 85 | |
---|
[130] | 86 | for (i = 0; i < len; i++) |
---|
| 87 | samples[i] = ((int) samples[i] * mul) / div; |
---|
| 88 | } |
---|
| 89 | |
---|
[1103] | 90 | #ifdef ROAR_HAVE_LIBM |
---|
[140] | 91 | void logs2 (void * data, float scale, int len) { |
---|
[130] | 92 | int16_t * samples = (int16_t *) data; |
---|
| 93 | int i; |
---|
| 94 | float div = logf(scale); |
---|
[140] | 95 | float scalemul = scale - 1; |
---|
[130] | 96 | int neg; |
---|
| 97 | |
---|
[128] | 98 | len /= 2; |
---|
| 99 | |
---|
[140] | 100 | //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div); |
---|
[130] | 101 | |
---|
| 102 | for (i = 0; i < len; i++) { |
---|
| 103 | if ( (neg = (samples[i] < 0)) ) |
---|
| 104 | samples[i] = abs(samples[i]); |
---|
| 105 | |
---|
| 106 | |
---|
[140] | 107 | samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div; |
---|
[130] | 108 | |
---|
| 109 | if ( neg ) |
---|
| 110 | samples[i] *= -1; |
---|
| 111 | } |
---|
[128] | 112 | } |
---|
| 113 | |
---|
[646] | 114 | void lowpass2 (void * data, int len, int channels) { |
---|
| 115 | int16_t * samples = (int16_t *) data; |
---|
| 116 | register int32_t s; |
---|
| 117 | int i, c; |
---|
| 118 | |
---|
| 119 | if ( channels > ROAR_MAX_CHANNELS ) |
---|
| 120 | return; |
---|
| 121 | |
---|
| 122 | len /= 2 * channels; |
---|
| 123 | |
---|
| 124 | // * output[N] = input[N] * A + output[N-1] * B |
---|
| 125 | |
---|
| 126 | for (i = 0; i < len; i++) { |
---|
| 127 | for (c = 0; c < channels; c++) { |
---|
| 128 | s = samples[i*channels + c] * g_lowpass.a + g_lowpass.old[c] * g_lowpass.b; |
---|
| 129 | |
---|
| 130 | s /= 65536; |
---|
| 131 | |
---|
| 132 | samples[i*channels + c] = s; |
---|
| 133 | g_lowpass.old[ c] = s; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
[1103] | 137 | #endif |
---|
[646] | 138 | |
---|
[126] | 139 | int main (int argc, char * argv[]) { |
---|
| 140 | int rate = 44100; |
---|
| 141 | int bits = 16; |
---|
| 142 | int channels = 2; |
---|
| 143 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 144 | char * server = NULL; |
---|
| 145 | char * k; |
---|
| 146 | int i; |
---|
[128] | 147 | int mul = 1, div = 1; |
---|
[5172] | 148 | int filter_id; |
---|
[884] | 149 | int32_t tmp; |
---|
[140] | 150 | float logscale = 0; |
---|
[646] | 151 | float lp = 0; |
---|
[126] | 152 | char buf[BUFSIZE]; |
---|
[682] | 153 | struct roardsp_filterchain fc; |
---|
[884] | 154 | struct roardsp_filter filter_real[8]; |
---|
| 155 | struct roardsp_filter * filter = filter_real - 1; |
---|
[682] | 156 | struct roar_stream stream; |
---|
[2846] | 157 | struct roar_vio_calls svio; |
---|
[126] | 158 | |
---|
[1103] | 159 | #ifdef ROAR_HAVE_LIBM |
---|
[646] | 160 | memset(&g_lowpass, 0, sizeof(g_lowpass)); |
---|
[1103] | 161 | #endif |
---|
[646] | 162 | |
---|
[682] | 163 | roardsp_fchain_init(&fc); |
---|
| 164 | |
---|
[126] | 165 | for (i = 1; i < argc; i++) { |
---|
| 166 | k = argv[i]; |
---|
| 167 | |
---|
[1635] | 168 | if ( strcmp(k, "--server") == 0 || strcmp(k, "-s") == 0 ) { |
---|
[126] | 169 | server = argv[++i]; |
---|
[1507] | 170 | } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 || strcmp(k, "-r") == 0 ) { |
---|
[126] | 171 | rate = atoi(argv[++i]); |
---|
[1507] | 172 | } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) { |
---|
[126] | 173 | bits = atoi(argv[++i]); |
---|
[1507] | 174 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) { |
---|
[126] | 175 | channels = atoi(argv[++i]); |
---|
[1507] | 176 | } else if ( strcmp(k, "-b") == 0 ) { |
---|
| 177 | bits = 8; |
---|
| 178 | } else if ( strcmp(k, "-m") == 0 ) { |
---|
| 179 | channels = 1; |
---|
| 180 | } else if ( strcmp(k, "--half") == 0 || strcmp(k, "-half") == 0 ) { |
---|
[128] | 181 | div *= 2; |
---|
[1507] | 182 | } else if ( strcmp(k, "--double") == 0 || strcmp(k, "-double") == 0 ) { |
---|
[128] | 183 | mul *= 2; |
---|
[130] | 184 | } else if ( strcmp(k, "--amp") == 0 ) { |
---|
| 185 | mul *= atoi(argv[++i]); |
---|
| 186 | } else if ( strcmp(k, "--mul") == 0 ) { |
---|
| 187 | mul = atoi(argv[++i]); |
---|
| 188 | } else if ( strcmp(k, "--div") == 0 ) { |
---|
| 189 | div = atoi(argv[++i]); |
---|
| 190 | } else if ( strcmp(k, "--log") == 0 ) { |
---|
[140] | 191 | logscale = atof(argv[++i]); |
---|
[1103] | 192 | #ifdef ROAR_HAVE_LIBM |
---|
[646] | 193 | } else if ( strcmp(k, "--lowpass") == 0 ) { |
---|
| 194 | lp = exp(-2 * M_PI * atof(argv[++i]) / rate) * 65536; |
---|
| 195 | g_lowpass.b = lp; |
---|
| 196 | g_lowpass.a = 65536 - lp; |
---|
[1103] | 197 | #endif |
---|
[648] | 198 | // printf("lowpass: A=%i, B=%i\n", g_lowpass.a, g_lowpass.b); |
---|
[682] | 199 | } else if ( strcmp(k, "--filter") == 0 ) { |
---|
| 200 | stream.info.channels = channels; |
---|
| 201 | stream.info.bits = bits; |
---|
| 202 | stream.info.rate = rate; |
---|
[5172] | 203 | filter_id = roardsp_filter_str2id(argv[++i]); |
---|
| 204 | if ( filter_id == -1 ) { |
---|
| 205 | ROAR_WARN("Can not add filter as filter ID is unknown: %s: %s", argv[i], roar_error2str(roar_error)); |
---|
| 206 | } else { |
---|
| 207 | filter++; |
---|
| 208 | if ( roardsp_filter_init(filter, &stream, filter_id) == -1 ) { |
---|
| 209 | ROAR_WARN("Can not add filter: %s: %s", argv[i], roar_error2str(roar_error)); |
---|
| 210 | filter--; |
---|
| 211 | } else { |
---|
| 212 | roardsp_fchain_add(&fc, filter); |
---|
| 213 | } |
---|
| 214 | } |
---|
[682] | 215 | } else if ( strcmp(k, "--ffreq") == 0 ) { |
---|
| 216 | lp = atof(argv[++i]); |
---|
[884] | 217 | roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &lp); |
---|
| 218 | } else if ( strcmp(k, "--fmul") == 0 ) { |
---|
| 219 | tmp = atoi(argv[++i]); |
---|
| 220 | roardsp_filter_ctl(filter, ROARDSP_FCTL_MUL, &tmp); |
---|
| 221 | } else if ( strcmp(k, "--fdiv") == 0 ) { |
---|
| 222 | tmp = atoi(argv[++i]); |
---|
| 223 | roardsp_filter_ctl(filter, ROARDSP_FCTL_DIV, &tmp); |
---|
[985] | 224 | } else if ( strcmp(k, "--fn") == 0 ) { |
---|
| 225 | tmp = atoi(argv[++i]); |
---|
| 226 | roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp); |
---|
[1005] | 227 | } else if ( strcmp(k, "--fq") == 0 ) { |
---|
| 228 | tmp = atoi(argv[++i]); |
---|
| 229 | roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp); |
---|
[985] | 230 | } else if ( strcmp(k, "--flimit") == 0 ) { |
---|
| 231 | tmp = atoi(argv[++i]); |
---|
| 232 | roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp); |
---|
[1005] | 233 | } else if ( strcmp(k, "--fmode") == 0 ) { |
---|
| 234 | tmp = atoi(argv[++i]); |
---|
| 235 | roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp); |
---|
[1635] | 236 | } else if ( strcmp(k, "--help") == 0 || strcmp(k, "-h") == 0 ) { |
---|
[126] | 237 | usage(); |
---|
| 238 | return 0; |
---|
| 239 | } else { |
---|
| 240 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 241 | usage(); |
---|
| 242 | return 1; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
[2846] | 246 | if ( roar_vio_simple_stream(&svio, rate, channels, bits, codec, server, ROAR_DIR_FILTER, "roarfilt") == -1 ) { |
---|
[126] | 247 | fprintf(stderr, "Error: can not start playback\n"); |
---|
| 248 | return 1; |
---|
| 249 | } |
---|
| 250 | |
---|
[1103] | 251 | if ( mul == div && |
---|
| 252 | #ifdef ROAR_HAVE_LIBM |
---|
| 253 | logscale == 0 && g_lowpass.a == 0 && |
---|
| 254 | #endif |
---|
| 255 | roardsp_fchain_num(&fc) == 0 ) { |
---|
[128] | 256 | fprintf(stderr, "Error: filter is useless!\n"); |
---|
| 257 | return 0; |
---|
| 258 | } |
---|
| 259 | |
---|
[2847] | 260 | switch (bits) { |
---|
[5176] | 261 | case 8: |
---|
| 262 | while((i = roar_vio_read(&svio, buf, BUFSIZE))) { |
---|
| 263 | vol1((void*)buf, mul, div, i); |
---|
| 264 | roardsp_fchain_calc(&fc, (void*)buf, (8*i)/bits); |
---|
| 265 | if (roar_vio_write(&svio, buf, i) != i) |
---|
| 266 | break; |
---|
| 267 | } |
---|
| 268 | break; |
---|
[2847] | 269 | case 16: |
---|
| 270 | while((i = roar_vio_read(&svio, buf, BUFSIZE))) { |
---|
| 271 | if ( mul != div ) |
---|
| 272 | vol2((void*)buf, mul, div, i); |
---|
[1103] | 273 | #ifdef ROAR_HAVE_LIBM |
---|
[2847] | 274 | if ( logscale ) |
---|
| 275 | logs2((void*)buf, logscale, i); |
---|
| 276 | if ( g_lowpass.a ) |
---|
| 277 | lowpass2((void*)buf, i, channels); |
---|
[1103] | 278 | #endif |
---|
[2847] | 279 | roardsp_fchain_calc(&fc, (void*)buf, (8*i)/bits); |
---|
| 280 | if (roar_vio_write(&svio, buf, i) != i) |
---|
| 281 | break; |
---|
| 282 | } |
---|
| 283 | break; |
---|
[5176] | 284 | case 32: |
---|
[2847] | 285 | while((i = roar_vio_read(&svio, buf, BUFSIZE))) { |
---|
[5176] | 286 | roardsp_fchain_calc(&fc, (void*)buf, (8*i)/bits); |
---|
[2847] | 287 | if (roar_vio_write(&svio, buf, i) != i) |
---|
| 288 | break; |
---|
| 289 | } |
---|
| 290 | break; |
---|
| 291 | default: |
---|
| 292 | fprintf(stderr, "Error: %i bits per sample is not supported!\n", bits); |
---|
| 293 | return 1; |
---|
[128] | 294 | } |
---|
[126] | 295 | |
---|
[2846] | 296 | roar_vio_close(&svio); |
---|
[126] | 297 | |
---|
[682] | 298 | roardsp_fchain_uninit(&fc); |
---|
| 299 | |
---|
[126] | 300 | return 0; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | //ll |
---|