source: roaraudio/roarclients/roarvumeter.c @ 3028:86eeb8b7e737

Last change on this file since 3028:86eeb8b7e737 was 3028:86eeb8b7e737, checked in by phi, 14 years ago

smaller trashold

File size: 6.7 KB
Line 
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_NONE  0x00
35#define MODE_PC    0x01
36#define MODE_DB    0x02
37#define MODE_BEAT  0x04
38
39void 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
54int vumeter16bit2ch (struct roar_vio_calls * vio, int samples, int16_t * buf, int mode, struct roardsp_filterchain * fc) {
55 struct roar_stream    beat_stream[1];
56 struct roardsp_filter beat_lp[1];
57 float                 beat_lpfreq = 1;
58 int i;
59 int samples_half = samples/2;
60 int64_t suml, sumr;
61 double  rmsl, rmsr;
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];
66 int have_beat;
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 }
76
77 printf("\e[s");
78 fflush(stdout);
79
80 while (roar_vio_read(vio, buf, samples * 2) > 0) {
81  suml = sumr = 0;
82
83  if ( run_filters ) {
84   roardsp_fchain_calc(fc, buf, samples * 2);
85  }
86
87  for (i = 0; i < samples; i += 2) {
88   suml += (int64_t) buf[i  ] * (int64_t) buf[i  ];
89   sumr += (int64_t) buf[i+1] * (int64_t) buf[i+1];
90  }
91
92  rmsl = sqrt((double)suml/(double)samples_half);
93  rmsr = sqrt((double)sumr/(double)samples_half);
94
95  if ( beat_detection ) {
96   beat_old = beat_val = (rmsl + rmsr) / 2;
97   roardsp_filter_calc(beat_lp, &beat_val, 2);
98   if ( (float)beat_old > (float)beat_val*1.1f ) {
99    dbeat = beat[1];
100    have_beat = 1;
101   } else {
102    dbeat = beat[0];
103    have_beat = 0;
104   }
105  }
106
107  switch (mode) {
108   case MODE_NONE: // beat only
109     if ( have_beat )
110      printf("%s\n", dbeat);
111    break;
112   case MODE_PC:
113     printf("L: %3i%% R: %3i%% %s          \e[u", (int)(rmsl/327.68), (int)(rmsr/327.68), dbeat);
114    break;
115   case MODE_DB:
116     printf("L: %6.2fdB R: %6.2fdB %s          \e[u", 20*log10(rmsl/32768.), 20*log10(rmsr/32768.), dbeat);
117    break;
118  }
119
120  fflush(stdout);
121 }
122
123 roardsp_filter_uninit(beat_lp);
124
125 return 0;
126}
127
128int vumeter (struct roar_vio_calls * vio, int samples, int bits, int channels, int mode, struct roardsp_filterchain * fc) {
129 void * buf = malloc((samples*bits*channels)/8);
130
131 if ( !buf )
132  return -1;
133
134 if ( bits == 16 ) {
135  if ( channels == 2 ) {
136   vumeter16bit2ch(vio, samples, (int16_t *) buf, mode, fc);
137   free(buf);
138   return 0;
139  } else {
140   free(buf);
141   return -1;
142  }
143 } else {
144  free(buf);
145  return -1;
146 }
147}
148
149int main (int argc, char * argv[]) {
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;
155 int    rate     = ROAR_RATE_DEFAULT;
156 int    bits     = 16;
157 int    channels = 2;
158 int    codec    = ROAR_CODEC_DEFAULT;
159 int    samples  = -1;
160 char * server   = NULL;
161 char * k;
162 struct roar_vio_calls stream, re;
163 int    i;
164 int    mode = 0;
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]);
179  } else if ( strcmp(k, "--db") == 0 ) {
180   mode |= MODE_DB;
181  } else if ( strcmp(k, "--beat") == 0 ) {
182   mode |= MODE_BEAT;
183  } else if ( strcmp(k, "--lowpass") == 0 ) {
184   lowpass_freq = atof(argv[++i]);
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
195 if ( !mode )
196  mode = MODE_PC;
197
198 if ( samples == -1 )
199  samples = rate/10;
200
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) {
207  fprintf(stderr, "Error: can not start monetoring\n");
208  return 1;
209 }
210
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
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 ) {
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   }
231
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   }
238
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   }
245  }
246 }
247
248 vumeter(&re, samples*channels, bits, channels, mode, &fchain);
249
250 printf("\n"); // if the reach this then roard has quited and we should print a newline
251
252 roar_vio_close(&re);
253
254 roar_disconnect(&con);
255
256 roardsp_fchain_uninit(&fchain);
257
258 return 0;
259}
260
261#else
262int main (void) {
263 fprintf(stderr, "Error: No Math library support compiled in.\n");
264 return 1;
265}
266#endif
267
268//ll
Note: See TracBrowser for help on using the repository browser.