1 | //roarcat.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
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 | |
---|
36 | void 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 | |
---|
51 | int vumeter16bit2ch (int fh, int samples, int16_t * buf, int mode) { |
---|
52 | int i; |
---|
53 | int samples_half = samples/2; |
---|
54 | double suml, sumr; |
---|
55 | |
---|
56 | printf("\e[s"); |
---|
57 | fflush(stdout); |
---|
58 | |
---|
59 | while (read(fh, buf, samples * 2)) { |
---|
60 | suml = sumr = 0; |
---|
61 | |
---|
62 | for (i = 0; i < samples; i += 2) { |
---|
63 | suml += (double) buf[i ] * (double) buf[i ]; |
---|
64 | sumr += (double) buf[i+1] * (double) buf[i+1]; |
---|
65 | } |
---|
66 | |
---|
67 | suml = sqrt(suml/samples_half); |
---|
68 | sumr = sqrt(sumr/samples_half); |
---|
69 | |
---|
70 | switch (mode) { |
---|
71 | case MODE_PC: |
---|
72 | printf("L: %3i%% R: %3i%% \e[u", (int)(suml/327.68), (int)(sumr/327.68)); |
---|
73 | break; |
---|
74 | case MODE_DB: |
---|
75 | printf("L: %6.2fdB R: %6.2fdB \e[u", 20*log10(suml/32768), 20*log10(sumr/32768)); |
---|
76 | break; |
---|
77 | } |
---|
78 | |
---|
79 | fflush(stdout); |
---|
80 | } |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | int vumeter (int fh, int samples, int bits, int channels, int mode) { |
---|
85 | void * buf = malloc(samples*bits*2); |
---|
86 | |
---|
87 | if ( !buf ) |
---|
88 | return -1; |
---|
89 | |
---|
90 | if ( bits == 16 ) { |
---|
91 | if ( channels == 2 ) { |
---|
92 | vumeter16bit2ch(fh, samples, (int16_t *) buf, mode); |
---|
93 | free(buf); |
---|
94 | return 0; |
---|
95 | } else { |
---|
96 | return -1; |
---|
97 | } |
---|
98 | } else { |
---|
99 | return -1; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | int main (int argc, char * argv[]) { |
---|
104 | int rate = 44100; |
---|
105 | int bits = 16; |
---|
106 | int channels = 2; |
---|
107 | int codec = ROAR_CODEC_DEFAULT; |
---|
108 | int samples = 441; |
---|
109 | char * server = NULL; |
---|
110 | char * k; |
---|
111 | int fh; |
---|
112 | int i; |
---|
113 | int mode = MODE_PC; |
---|
114 | |
---|
115 | for (i = 1; i < argc; i++) { |
---|
116 | k = argv[i]; |
---|
117 | |
---|
118 | if ( strcmp(k, "--server") == 0 ) { |
---|
119 | server = argv[++i]; |
---|
120 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
121 | rate = atoi(argv[++i]); |
---|
122 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
123 | bits = atoi(argv[++i]); |
---|
124 | } else if ( strcmp(k, "--channels") == 0 ) { |
---|
125 | channels = atoi(argv[++i]); |
---|
126 | } else if ( strcmp(k, "--samples") == 0 ) { |
---|
127 | samples = atoi(argv[++i]); |
---|
128 | } else if ( strcmp(k, "--db") == 0 ) { |
---|
129 | mode = MODE_DB; |
---|
130 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
131 | usage(); |
---|
132 | return 0; |
---|
133 | } else { |
---|
134 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
135 | usage(); |
---|
136 | return 1; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarvumeter")) == -1 ) { |
---|
141 | fprintf(stderr, "Error: can not start playback\n"); |
---|
142 | return 1; |
---|
143 | } |
---|
144 | |
---|
145 | vumeter(fh, samples*channels, bits, channels, mode); |
---|
146 | |
---|
147 | printf("\n"); // if the reach this then roard has quited and we should print a newline |
---|
148 | |
---|
149 | roar_simple_close(fh); |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | #else |
---|
155 | int main (void) { |
---|
156 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
157 | return 1; |
---|
158 | } |
---|
159 | #endif |
---|
160 | |
---|
161 | //ll |
---|