[2292] | 1 | //roarvumeter.c: |
---|
[224] | 2 | |
---|
[669] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[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 | |
---|
[224] | 26 | #include <roaraudio.h> |
---|
[3024] | 27 | #include <libroardsp/libroardsp.h> |
---|
[1103] | 28 | |
---|
| 29 | #ifdef ROAR_HAVE_LIBM |
---|
| 30 | |
---|
[224] | 31 | #include <math.h> |
---|
| 32 | |
---|
| 33 | #define BUFSIZE 1024 |
---|
| 34 | |
---|
[3091] | 35 | #define LOWPASS_ORDER 6 |
---|
| 36 | |
---|
[3027] | 37 | #define MODE_NONE 0x00 |
---|
[3025] | 38 | #define MODE_PC 0x01 |
---|
| 39 | #define MODE_DB 0x02 |
---|
| 40 | #define MODE_BEAT 0x04 |
---|
[1633] | 41 | |
---|
[224] | 42 | void usage (void) { |
---|
| 43 | printf("roarvumeter [OPTIONS]...\n"); |
---|
| 44 | |
---|
| 45 | printf("\nOptions:\n\n"); |
---|
| 46 | |
---|
| 47 | printf(" --server SERVER - Set server hostname\n" |
---|
| 48 | " --rate RATE - Set sample rate\n" |
---|
| 49 | " --bits BITS - Set bits per sample\n" |
---|
| 50 | " --chans CHANNELS - Set number of channels\n" |
---|
[3091] | 51 | " --samples SAMPLES - Set number of input samples per block\n" |
---|
| 52 | " --pc - Use percent scale\n" |
---|
| 53 | " --db - Use dB scale\n" |
---|
| 54 | " --beat - Enable beat detection\n" |
---|
| 55 | " --lowpass FREQ - Use lowpass to filter input (-%idB/dec)\n" |
---|
| 56 | " --help - Show this help\n", |
---|
| 57 | LOWPASS_ORDER * 20 |
---|
[224] | 58 | ); |
---|
| 59 | |
---|
| 60 | } |
---|
| 61 | |
---|
[3024] | 62 | int vumeter16bit2ch (struct roar_vio_calls * vio, int samples, int16_t * buf, int mode, struct roardsp_filterchain * fc) { |
---|
[3025] | 63 | struct roar_stream beat_stream[1]; |
---|
| 64 | struct roardsp_filter beat_lp[1]; |
---|
| 65 | float beat_lpfreq = 1; |
---|
[224] | 66 | int i; |
---|
| 67 | int samples_half = samples/2; |
---|
[2917] | 68 | int64_t suml, sumr; |
---|
| 69 | double rmsl, rmsr; |
---|
[3025] | 70 | int run_filters = roardsp_fchain_num(fc); |
---|
| 71 | int beat_detection = mode & MODE_BEAT; |
---|
| 72 | char * beat[2] = {" ", "Beat!"}; |
---|
| 73 | char * dbeat = beat[0]; |
---|
[3641] | 74 | int have_beat = 0; |
---|
[3025] | 75 | int16_t beat_val, beat_old; |
---|
| 76 | |
---|
| 77 | if ( beat_detection ) { |
---|
| 78 | mode -= MODE_BEAT; |
---|
| 79 | |
---|
| 80 | roar_stream_new(beat_stream, 10, 1, 16, ROAR_CODEC_PCM); |
---|
| 81 | roardsp_filter_init(beat_lp, beat_stream, ROARDSP_FILTER_LOWP); |
---|
| 82 | roardsp_filter_ctl(beat_lp, ROARDSP_FCTL_FREQ, &beat_lpfreq); |
---|
| 83 | } |
---|
[224] | 84 | |
---|
| 85 | printf("\e[s"); |
---|
| 86 | fflush(stdout); |
---|
| 87 | |
---|
[2918] | 88 | while (roar_vio_read(vio, buf, samples * 2) > 0) { |
---|
[224] | 89 | suml = sumr = 0; |
---|
| 90 | |
---|
[3024] | 91 | if ( run_filters ) { |
---|
| 92 | roardsp_fchain_calc(fc, buf, samples * 2); |
---|
| 93 | } |
---|
| 94 | |
---|
[224] | 95 | for (i = 0; i < samples; i += 2) { |
---|
[2917] | 96 | suml += (int64_t) buf[i ] * (int64_t) buf[i ]; |
---|
| 97 | sumr += (int64_t) buf[i+1] * (int64_t) buf[i+1]; |
---|
[224] | 98 | } |
---|
| 99 | |
---|
[2917] | 100 | rmsl = sqrt((double)suml/(double)samples_half); |
---|
| 101 | rmsr = sqrt((double)sumr/(double)samples_half); |
---|
[224] | 102 | |
---|
[3025] | 103 | if ( beat_detection ) { |
---|
| 104 | beat_old = beat_val = (rmsl + rmsr) / 2; |
---|
| 105 | roardsp_filter_calc(beat_lp, &beat_val, 2); |
---|
[3028] | 106 | if ( (float)beat_old > (float)beat_val*1.1f ) { |
---|
[3025] | 107 | dbeat = beat[1]; |
---|
[3027] | 108 | have_beat = 1; |
---|
[3025] | 109 | } else { |
---|
| 110 | dbeat = beat[0]; |
---|
[3027] | 111 | have_beat = 0; |
---|
[3025] | 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
[1633] | 115 | switch (mode) { |
---|
[3027] | 116 | case MODE_NONE: // beat only |
---|
| 117 | if ( have_beat ) |
---|
| 118 | printf("%s\n", dbeat); |
---|
| 119 | break; |
---|
[1633] | 120 | case MODE_PC: |
---|
[3025] | 121 | printf("L: %3i%% R: %3i%% %s \e[u", (int)(rmsl/327.68), (int)(rmsr/327.68), dbeat); |
---|
[1633] | 122 | break; |
---|
| 123 | case MODE_DB: |
---|
[3025] | 124 | printf("L: %6.2fdB R: %6.2fdB %s \e[u", 20*log10(rmsl/32768.), 20*log10(rmsr/32768.), dbeat); |
---|
[1633] | 125 | break; |
---|
| 126 | } |
---|
| 127 | |
---|
[224] | 128 | fflush(stdout); |
---|
| 129 | } |
---|
[3025] | 130 | |
---|
[3746] | 131 | if ( beat_detection ) { |
---|
| 132 | roardsp_filter_uninit(beat_lp); |
---|
| 133 | } |
---|
[3025] | 134 | |
---|
[224] | 135 | return 0; |
---|
| 136 | } |
---|
| 137 | |
---|
[3024] | 138 | int vumeter (struct roar_vio_calls * vio, int samples, int bits, int channels, int mode, struct roardsp_filterchain * fc) { |
---|
[5179] | 139 | void * buf = roar_mm_malloc((samples*bits*channels)/8); |
---|
[224] | 140 | |
---|
[5179] | 141 | if ( buf == NULL ) |
---|
[224] | 142 | return -1; |
---|
| 143 | |
---|
| 144 | if ( bits == 16 ) { |
---|
| 145 | if ( channels == 2 ) { |
---|
[3024] | 146 | vumeter16bit2ch(vio, samples, (int16_t *) buf, mode, fc); |
---|
[5179] | 147 | roar_mm_free(buf); |
---|
[224] | 148 | return 0; |
---|
| 149 | } else { |
---|
[5179] | 150 | roar_mm_free(buf); |
---|
[224] | 151 | return -1; |
---|
| 152 | } |
---|
| 153 | } else { |
---|
[5179] | 154 | roar_mm_free(buf); |
---|
[224] | 155 | return -1; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | int main (int argc, char * argv[]) { |
---|
[3024] | 160 | struct roar_connection con; |
---|
| 161 | struct roar_stream s; |
---|
| 162 | struct roardsp_filterchain fchain; |
---|
| 163 | struct roardsp_filter * filter; |
---|
| 164 | float lowpass_freq = 0; |
---|
[3019] | 165 | int rate = ROAR_RATE_DEFAULT; |
---|
[224] | 166 | int bits = 16; |
---|
| 167 | int channels = 2; |
---|
| 168 | int codec = ROAR_CODEC_DEFAULT; |
---|
[3019] | 169 | int samples = -1; |
---|
[224] | 170 | char * server = NULL; |
---|
| 171 | char * k; |
---|
[2918] | 172 | struct roar_vio_calls stream, re; |
---|
[224] | 173 | int i; |
---|
[3025] | 174 | int mode = 0; |
---|
[224] | 175 | |
---|
| 176 | for (i = 1; i < argc; i++) { |
---|
| 177 | k = argv[i]; |
---|
| 178 | |
---|
| 179 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 180 | server = argv[++i]; |
---|
| 181 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
[5179] | 182 | rate = roar_str2rate(argv[++i]); |
---|
[224] | 183 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
[5179] | 184 | bits = roar_str2bits(argv[++i]); |
---|
[3091] | 185 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0) { |
---|
[5179] | 186 | channels = roar_str2channels(argv[++i]); |
---|
[224] | 187 | } else if ( strcmp(k, "--samples") == 0 ) { |
---|
| 188 | samples = atoi(argv[++i]); |
---|
[1633] | 189 | } else if ( strcmp(k, "--db") == 0 ) { |
---|
[3091] | 190 | mode |= MODE_PC; |
---|
| 191 | mode -= MODE_PC; |
---|
[3025] | 192 | mode |= MODE_DB; |
---|
[3091] | 193 | } else if ( strcmp(k, "--pc") == 0 ) { |
---|
| 194 | mode |= MODE_DB; |
---|
| 195 | mode -= MODE_DB; |
---|
| 196 | mode |= MODE_PC; |
---|
[3025] | 197 | } else if ( strcmp(k, "--beat") == 0 ) { |
---|
| 198 | mode |= MODE_BEAT; |
---|
[3024] | 199 | } else if ( strcmp(k, "--lowpass") == 0 ) { |
---|
| 200 | lowpass_freq = atof(argv[++i]); |
---|
[224] | 201 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 202 | usage(); |
---|
| 203 | return 0; |
---|
| 204 | } else { |
---|
| 205 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 206 | usage(); |
---|
| 207 | return 1; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
[3025] | 211 | if ( !mode ) |
---|
| 212 | mode = MODE_PC; |
---|
| 213 | |
---|
[3019] | 214 | if ( samples == -1 ) |
---|
| 215 | samples = rate/10; |
---|
| 216 | |
---|
[3024] | 217 | if ( roar_simple_connect(&con, server, "roarvumeter") == -1 ) { |
---|
| 218 | fprintf(stderr, "Error: can not connect to server!\n"); |
---|
| 219 | return 1; |
---|
| 220 | } |
---|
| 221 | |
---|
[5289] | 222 | if ( roar_vio_simple_new_stream_obj(&stream, &con, &s, rate, channels, bits, codec, ROAR_DIR_MONITOR, -1) == -1) { |
---|
[2918] | 223 | fprintf(stderr, "Error: can not start monetoring\n"); |
---|
[224] | 224 | return 1; |
---|
| 225 | } |
---|
| 226 | |
---|
[2918] | 227 | if ( roar_vio_open_re(&re, &stream) == -1 ) { |
---|
| 228 | roar_vio_close(&stream); |
---|
| 229 | fprintf(stderr, "Error: can not open RE VIO layer\n"); |
---|
| 230 | return 1; |
---|
| 231 | } |
---|
| 232 | |
---|
[3024] | 233 | if ( roardsp_fchain_init(&fchain) == -1 ) { |
---|
| 234 | roar_vio_close(&re); |
---|
| 235 | fprintf(stderr, "Error: can not init filterchain\n"); |
---|
| 236 | return 1; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | if ( lowpass_freq > 1 ) { |
---|
[3091] | 240 | for (i = 0; i < LOWPASS_ORDER; i++) { |
---|
[3026] | 241 | if ( roardsp_filter_new(&filter, &s, ROARDSP_FILTER_LOWP) == -1 ) { |
---|
| 242 | fprintf(stderr, "Error: can not open lowpass\n"); |
---|
| 243 | roar_vio_close(&re); |
---|
| 244 | roardsp_fchain_uninit(&fchain); |
---|
| 245 | return 1; |
---|
| 246 | } |
---|
[3024] | 247 | |
---|
[3026] | 248 | if ( roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &lowpass_freq) == -1 ) { |
---|
| 249 | fprintf(stderr, "Error: can not set filter frequency\n"); |
---|
| 250 | roar_vio_close(&re); |
---|
| 251 | roardsp_fchain_uninit(&fchain); |
---|
| 252 | return 1; |
---|
| 253 | } |
---|
[3024] | 254 | |
---|
[3026] | 255 | if ( roardsp_fchain_add(&fchain, filter) == -1 ) { |
---|
| 256 | fprintf(stderr, "Error: can not set filter frequency\n"); |
---|
| 257 | roar_vio_close(&re); |
---|
| 258 | roardsp_fchain_uninit(&fchain); |
---|
| 259 | return 1; |
---|
| 260 | } |
---|
[3024] | 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | vumeter(&re, samples*channels, bits, channels, mode, &fchain); |
---|
[224] | 265 | |
---|
[225] | 266 | printf("\n"); // if the reach this then roard has quited and we should print a newline |
---|
| 267 | |
---|
[2918] | 268 | roar_vio_close(&re); |
---|
[224] | 269 | |
---|
[3024] | 270 | roar_disconnect(&con); |
---|
| 271 | |
---|
| 272 | roardsp_fchain_uninit(&fchain); |
---|
| 273 | |
---|
[224] | 274 | return 0; |
---|
| 275 | } |
---|
| 276 | |
---|
[1103] | 277 | #else |
---|
| 278 | int main (void) { |
---|
| 279 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 280 | return 1; |
---|
| 281 | } |
---|
| 282 | #endif |
---|
| 283 | |
---|
[224] | 284 | //ll |
---|