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