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