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 | #include <math.h> |
---|
27 | |
---|
28 | #define BUFSIZE 1024 |
---|
29 | |
---|
30 | void usage (void) { |
---|
31 | printf("roarvumeter [OPTIONS]...\n"); |
---|
32 | |
---|
33 | printf("\nOptions:\n\n"); |
---|
34 | |
---|
35 | printf(" --server SERVER - Set server hostname\n" |
---|
36 | " --rate RATE - Set sample rate\n" |
---|
37 | " --bits BITS - Set bits per sample\n" |
---|
38 | " --chans CHANNELS - Set number of channels\n" |
---|
39 | " --samples SAMPLES - Set number of samples\n" |
---|
40 | " --help - Show this help\n" |
---|
41 | ); |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | int vumeter16bit2ch (int fh, int samples, int16_t * buf) { |
---|
46 | int i; |
---|
47 | int samples_half = samples/2; |
---|
48 | double suml, sumr; |
---|
49 | |
---|
50 | printf("\e[s"); |
---|
51 | fflush(stdout); |
---|
52 | |
---|
53 | while (read(fh, buf, samples * 2)) { |
---|
54 | suml = sumr = 0; |
---|
55 | |
---|
56 | for (i = 0; i < samples; i += 2) { |
---|
57 | suml += (double) buf[i ] * (double) buf[i ]; |
---|
58 | sumr += (double) buf[i+1] * (double) buf[i+1]; |
---|
59 | } |
---|
60 | |
---|
61 | suml = sqrt(suml/samples_half)/327.68; |
---|
62 | sumr = sqrt(sumr/samples_half)/327.68; |
---|
63 | |
---|
64 | printf("L: %3i%% R: %3i%% \e[u", (int)suml, (int)sumr); |
---|
65 | fflush(stdout); |
---|
66 | } |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | int vumeter (int fh, int samples, int bits, int channels) { |
---|
71 | void * buf = malloc(samples*bits*2); |
---|
72 | |
---|
73 | if ( !buf ) |
---|
74 | return -1; |
---|
75 | |
---|
76 | if ( bits == 16 ) { |
---|
77 | if ( channels == 2 ) { |
---|
78 | vumeter16bit2ch(fh, samples, (int16_t *) buf); |
---|
79 | free(buf); |
---|
80 | return 0; |
---|
81 | } else { |
---|
82 | return -1; |
---|
83 | } |
---|
84 | } else { |
---|
85 | return -1; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | int main (int argc, char * argv[]) { |
---|
90 | int rate = 44100; |
---|
91 | int bits = 16; |
---|
92 | int channels = 2; |
---|
93 | int codec = ROAR_CODEC_DEFAULT; |
---|
94 | int samples = 441; |
---|
95 | char * server = NULL; |
---|
96 | char * k; |
---|
97 | int fh; |
---|
98 | int i; |
---|
99 | |
---|
100 | for (i = 1; i < argc; i++) { |
---|
101 | k = argv[i]; |
---|
102 | |
---|
103 | if ( strcmp(k, "--server") == 0 ) { |
---|
104 | server = argv[++i]; |
---|
105 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
106 | rate = atoi(argv[++i]); |
---|
107 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
108 | bits = atoi(argv[++i]); |
---|
109 | } else if ( strcmp(k, "--channels") == 0 ) { |
---|
110 | channels = atoi(argv[++i]); |
---|
111 | } else if ( strcmp(k, "--samples") == 0 ) { |
---|
112 | samples = atoi(argv[++i]); |
---|
113 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
114 | usage(); |
---|
115 | return 0; |
---|
116 | } else { |
---|
117 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
118 | usage(); |
---|
119 | return 1; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | if ( (fh = roar_simple_monitor(rate, channels, bits, codec, server, "roarvumeter")) == -1 ) { |
---|
124 | fprintf(stderr, "Error: can not start playback\n"); |
---|
125 | return 1; |
---|
126 | } |
---|
127 | |
---|
128 | vumeter(fh, samples*channels, bits, channels); |
---|
129 | |
---|
130 | printf("\n"); // if the reach this then roard has quited and we should print a newline |
---|
131 | |
---|
132 | roar_simple_close(fh); |
---|
133 | |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|
137 | //ll |
---|