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