source: roaraudio/roarclients/roarvumeter.c @ 3019:ec0dd85f5e06

Last change on this file since 3019:ec0dd85f5e06 was 3019:ec0dd85f5e06, checked in by phi, 15 years ago

use default sample rate, not fixed 44100 as default, use 10Hz refresh rate, not 100Hz

File size: 4.2 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
27#ifdef ROAR_HAVE_LIBM
28
29#include <math.h>
30
31#define BUFSIZE 1024
32
33#define MODE_PC    1
34#define MODE_DB    2
35
36void 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
51int vumeter16bit2ch (struct roar_vio_calls * vio, int samples, int16_t * buf, int mode) {
52 int i;
53 int samples_half = samples/2;
54 int64_t suml, sumr;
55 double  rmsl, rmsr;
56
57 printf("\e[s");
58 fflush(stdout);
59
60 while (roar_vio_read(vio, buf, samples * 2) > 0) {
61  suml = sumr = 0;
62
63  for (i = 0; i < samples; i += 2) {
64   suml += (int64_t) buf[i  ] * (int64_t) buf[i  ];
65   sumr += (int64_t) buf[i+1] * (int64_t) buf[i+1];
66  }
67
68  rmsl = sqrt((double)suml/(double)samples_half);
69  rmsr = sqrt((double)sumr/(double)samples_half);
70
71  switch (mode) {
72   case MODE_PC:
73     printf("L: %3i%% R: %3i%%          \e[u", (int)(rmsl/327.68), (int)(rmsr/327.68));
74    break;
75   case MODE_DB:
76     printf("L: %6.2fdB R: %6.2fdB          \e[u", 20*log10(rmsl/32768.), 20*log10(rmsr/32768.));
77    break;
78  }
79
80  fflush(stdout);
81 }
82 return 0;
83}
84
85int vumeter (struct roar_vio_calls * vio, int samples, int bits, int channels, int mode) {
86 void * buf = malloc((samples*bits*channels)/8);
87
88 if ( !buf )
89  return -1;
90
91 if ( bits == 16 ) {
92  if ( channels == 2 ) {
93   vumeter16bit2ch(vio, samples, (int16_t *) buf, mode);
94   free(buf);
95   return 0;
96  } else {
97   free(buf);
98   return -1;
99  }
100 } else {
101  free(buf);
102  return -1;
103 }
104}
105
106int main (int argc, char * argv[]) {
107 int    rate     = ROAR_RATE_DEFAULT;
108 int    bits     = 16;
109 int    channels = 2;
110 int    codec    = ROAR_CODEC_DEFAULT;
111 int    samples  = -1;
112 char * server   = NULL;
113 char * k;
114 struct roar_vio_calls stream, re;
115 int    i;
116 int    mode = MODE_PC;
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]);
131  } else if ( strcmp(k, "--db") == 0 ) {
132   mode = MODE_DB;
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
143 if ( samples == -1 )
144  samples = rate/10;
145
146 if ( roar_vio_simple_stream(&stream, rate, channels, bits, codec, server, ROAR_DIR_MONITOR, "roarvumeter") == -1) {
147  fprintf(stderr, "Error: can not start monetoring\n");
148  return 1;
149 }
150
151 if ( roar_vio_open_re(&re, &stream) == -1 ) {
152  roar_vio_close(&stream);
153  fprintf(stderr, "Error: can not open RE VIO layer\n");
154  return 1;
155 }
156
157 vumeter(&re, samples*channels, bits, channels, mode);
158
159 printf("\n"); // if the reach this then roard has quited and we should print a newline
160
161 roar_vio_close(&re);
162
163 return 0;
164}
165
166#else
167int main (void) {
168 fprintf(stderr, "Error: No Math library support compiled in.\n");
169 return 1;
170}
171#endif
172
173//ll
Note: See TracBrowser for help on using the repository browser.