source: roaraudio/roarclients/roarvumeter.c @ 2917:3adf5948e9cd

Last change on this file since 2917:3adf5948e9cd was 2917:3adf5948e9cd, checked in by phi, 15 years ago

a bit better algo impl.

File size: 3.8 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 (int fh, 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 (read(fh, buf, samples * 2)) {
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 (int fh, int samples, int bits, int channels, int mode) {
86 void * buf = malloc(samples*bits*2);
87
88 if ( !buf )
89  return -1;
90
91 if ( bits == 16 ) {
92  if ( channels == 2 ) {
93   vumeter16bit2ch(fh, samples, (int16_t *) buf, mode);
94   free(buf);
95   return 0;
96  } else {
97   return -1;
98  }
99 } else {
100  return -1;
101 }
102}
103
104int main (int argc, char * argv[]) {
105 int    rate     = 44100;
106 int    bits     = 16;
107 int    channels = 2;
108 int    codec    = ROAR_CODEC_DEFAULT;
109 int    samples  = 441;
110 char * server   = NULL;
111 char * k;
112 int    fh;
113 int    i;
114 int    mode = MODE_PC;
115
116 for (i = 1; i < argc; i++) {
117  k = argv[i];
118
119  if ( strcmp(k, "--server") == 0 ) {
120   server = argv[++i];
121  } else if ( strcmp(k, "--rate") == 0 ) {
122   rate = atoi(argv[++i]);
123  } else if ( strcmp(k, "--bits") == 0 ) {
124   bits = atoi(argv[++i]);
125  } else if ( strcmp(k, "--channels") == 0 ) {
126   channels = atoi(argv[++i]);
127  } else if ( strcmp(k, "--samples") == 0 ) {
128   samples = atoi(argv[++i]);
129  } else if ( strcmp(k, "--db") == 0 ) {
130   mode = MODE_DB;
131  } else if ( strcmp(k, "--help") == 0 ) {
132   usage();
133   return 0;
134  } else {
135   fprintf(stderr, "Error: unknown argument: %s\n", k);
136   usage();
137   return 1;
138  }
139 }
140
141 if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarvumeter")) == -1 ) {
142  fprintf(stderr, "Error: can not start playback\n");
143  return 1;
144 }
145
146 vumeter(fh, samples*channels, bits, channels, mode);
147
148 printf("\n"); // if the reach this then roard has quited and we should print a newline
149
150 roar_simple_close(fh);
151
152 return 0;
153}
154
155#else
156int main (void) {
157 fprintf(stderr, "Error: No Math library support compiled in.\n");
158 return 1;
159}
160#endif
161
162//ll
Note: See TracBrowser for help on using the repository browser.