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