[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> |
---|
[1103] | 26 | |
---|
| 27 | #ifdef ROAR_HAVE_LIBM |
---|
| 28 | |
---|
[224] | 29 | #include <math.h> |
---|
| 30 | |
---|
| 31 | #define BUFSIZE 1024 |
---|
| 32 | |
---|
[1633] | 33 | #define MODE_PC 1 |
---|
| 34 | #define MODE_DB 2 |
---|
| 35 | |
---|
[224] | 36 | void usage (void) { |
---|
| 37 | printf("roarvumeter [OPTIONS]...\n"); |
---|
| 38 | |
---|
| 39 | printf("\nOptions:\n\n"); |
---|
| 40 | |
---|
| 41 | printf(" --server SERVER - Set server hostname\n" |
---|
| 42 | " --rate RATE - Set sample rate\n" |
---|
| 43 | " --bits BITS - Set bits per sample\n" |
---|
| 44 | " --chans CHANNELS - Set number of channels\n" |
---|
| 45 | " --samples SAMPLES - Set number of samples\n" |
---|
| 46 | " --help - Show this help\n" |
---|
| 47 | ); |
---|
| 48 | |
---|
| 49 | } |
---|
| 50 | |
---|
[2918] | 51 | int vumeter16bit2ch (struct roar_vio_calls * vio, int samples, int16_t * buf, int mode) { |
---|
[224] | 52 | int i; |
---|
| 53 | int samples_half = samples/2; |
---|
[2917] | 54 | int64_t suml, sumr; |
---|
| 55 | double rmsl, rmsr; |
---|
[224] | 56 | |
---|
| 57 | printf("\e[s"); |
---|
| 58 | fflush(stdout); |
---|
| 59 | |
---|
[2918] | 60 | while (roar_vio_read(vio, buf, samples * 2) > 0) { |
---|
[224] | 61 | suml = sumr = 0; |
---|
| 62 | |
---|
| 63 | for (i = 0; i < samples; i += 2) { |
---|
[2917] | 64 | suml += (int64_t) buf[i ] * (int64_t) buf[i ]; |
---|
| 65 | sumr += (int64_t) buf[i+1] * (int64_t) buf[i+1]; |
---|
[224] | 66 | } |
---|
| 67 | |
---|
[2917] | 68 | rmsl = sqrt((double)suml/(double)samples_half); |
---|
| 69 | rmsr = sqrt((double)sumr/(double)samples_half); |
---|
[224] | 70 | |
---|
[1633] | 71 | switch (mode) { |
---|
| 72 | case MODE_PC: |
---|
[2917] | 73 | printf("L: %3i%% R: %3i%% \e[u", (int)(rmsl/327.68), (int)(rmsr/327.68)); |
---|
[1633] | 74 | break; |
---|
| 75 | case MODE_DB: |
---|
[2917] | 76 | printf("L: %6.2fdB R: %6.2fdB \e[u", 20*log10(rmsl/32768.), 20*log10(rmsr/32768.)); |
---|
[1633] | 77 | break; |
---|
| 78 | } |
---|
| 79 | |
---|
[224] | 80 | fflush(stdout); |
---|
| 81 | } |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
[2918] | 85 | int vumeter (struct roar_vio_calls * vio, int samples, int bits, int channels, int mode) { |
---|
[2919] | 86 | void * buf = malloc((samples*bits*channels)/8); |
---|
[224] | 87 | |
---|
| 88 | if ( !buf ) |
---|
| 89 | return -1; |
---|
| 90 | |
---|
| 91 | if ( bits == 16 ) { |
---|
| 92 | if ( channels == 2 ) { |
---|
[2918] | 93 | vumeter16bit2ch(vio, samples, (int16_t *) buf, mode); |
---|
[224] | 94 | free(buf); |
---|
| 95 | return 0; |
---|
| 96 | } else { |
---|
[2919] | 97 | free(buf); |
---|
[224] | 98 | return -1; |
---|
| 99 | } |
---|
| 100 | } else { |
---|
[2919] | 101 | free(buf); |
---|
[224] | 102 | return -1; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int main (int argc, char * argv[]) { |
---|
| 107 | int rate = 44100; |
---|
| 108 | int bits = 16; |
---|
| 109 | int channels = 2; |
---|
| 110 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 111 | int samples = 441; |
---|
| 112 | char * server = NULL; |
---|
| 113 | char * k; |
---|
[2918] | 114 | struct roar_vio_calls stream, re; |
---|
[224] | 115 | int i; |
---|
[1633] | 116 | int mode = MODE_PC; |
---|
[224] | 117 | |
---|
| 118 | for (i = 1; i < argc; i++) { |
---|
| 119 | k = argv[i]; |
---|
| 120 | |
---|
| 121 | if ( strcmp(k, "--server") == 0 ) { |
---|
| 122 | server = argv[++i]; |
---|
| 123 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
| 124 | rate = atoi(argv[++i]); |
---|
| 125 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
| 126 | bits = atoi(argv[++i]); |
---|
| 127 | } else if ( strcmp(k, "--channels") == 0 ) { |
---|
| 128 | channels = atoi(argv[++i]); |
---|
| 129 | } else if ( strcmp(k, "--samples") == 0 ) { |
---|
| 130 | samples = atoi(argv[++i]); |
---|
[1633] | 131 | } else if ( strcmp(k, "--db") == 0 ) { |
---|
| 132 | mode = MODE_DB; |
---|
[224] | 133 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 134 | usage(); |
---|
| 135 | return 0; |
---|
| 136 | } else { |
---|
| 137 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 138 | usage(); |
---|
| 139 | return 1; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[2918] | 143 | if ( roar_vio_simple_stream(&stream, rate, channels, bits, codec, server, ROAR_DIR_MONITOR, "roarvumeter") == -1) { |
---|
| 144 | fprintf(stderr, "Error: can not start monetoring\n"); |
---|
[224] | 145 | return 1; |
---|
| 146 | } |
---|
| 147 | |
---|
[2918] | 148 | if ( roar_vio_open_re(&re, &stream) == -1 ) { |
---|
| 149 | roar_vio_close(&stream); |
---|
| 150 | fprintf(stderr, "Error: can not open RE VIO layer\n"); |
---|
| 151 | return 1; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | vumeter(&re, samples*channels, bits, channels, mode); |
---|
[224] | 155 | |
---|
[225] | 156 | printf("\n"); // if the reach this then roard has quited and we should print a newline |
---|
| 157 | |
---|
[2918] | 158 | roar_vio_close(&re); |
---|
[224] | 159 | |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
[1103] | 163 | #else |
---|
| 164 | int main (void) { |
---|
| 165 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 166 | return 1; |
---|
| 167 | } |
---|
| 168 | #endif |
---|
| 169 | |
---|
[224] | 170 | //ll |
---|