-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
208 lines (179 loc) · 6.31 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "util.h"
#include <sys/time.h>
#include <math.h>
#include <zmq.h>
#include <unistd.h>
#include <fcntl.h>
// Format seconds into a string
const char * formatSeconds(float seconds) {
if( seconds < 0.001f )
return " 0s";
static char buff[32];
if( seconds < 1.0f )
sprintf(buff, "%3dms", (int)(seconds*1000));
else
sprintf(buff, "%.1fs", seconds);
return (const char *)buff;
}
// duplicate a string, but using new instead of malloc()
char * new_strdup(const char * input) {
int len = strlen(input);
char * data = new char[len+1];
memcpy(data, input, len+1);
return data;
}
// Return time in miliseconds
const double time_ms() {
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec*1000.0 + t.tv_usec/1000.0f;
}
// Return true if the given string is only whitespace and digits
bool is_number(const char * str) {
for( int i=0; i<strlen(str); i++ ) {
if( !isdigit(str[i]) && !isspace(str[i]) )
return false;
}
return true;
}
// Print the level meter thingy that is so awesome and unnecessary
void print_level_meter( float * buffer, unsigned int num_frames, unsigned int num_channels, unsigned int bytes_per_second ) {
// AWWWWWW YEEEAAAAAHHHHH
static float * levels = NULL;
static float * peak_levels = NULL;
// Get storage area for our channels
if( levels == NULL ) {
levels = new float[num_channels];
peak_levels = new float[num_channels];
memset(peak_levels, 0, sizeof(float)*num_channels);
}
memset(levels, 0, sizeof(float)*num_channels);
// First, calculate sum(x^2) for x = each channel
for( int i=0; i<num_frames; ++i ) {
for( int k=0; k<num_channels; ++k ) {
levels[k] = fmin(1.0, fmax(levels[k], buffer[i*num_channels + k]));
}
}
for( int k=0; k<num_channels; ++k ) {
peak_levels[k] = .995*peak_levels[k];
peak_levels[k] = fmax(peak_levels[k], levels[k]);
}
// Next, output the level of each channel:
int max_space = 75;
printf(" \r");
int level_divisions = max_space/num_channels;
printf("[");
for( int k=0; k<num_channels; ++k ) {
if( k > 0 )
printf("|");
// Discretize levels[k] into level_divisions divisions
int discrete_level = (int)fmin(levels[k]*level_divisions, level_divisions);
int discrete_peak_level = (int)fmin(peak_levels[k]*level_divisions, level_divisions);
// Next, output discrete_level "=" signs radiating outward from the "|"
if( k < num_channels/2 ) {
for( int i=discrete_peak_level; i<max_space/num_channels; ++i )
printf(" ");
printf("{");
for( int i=discrete_level; i<discrete_peak_level - 1; ++i )
printf(" ");
for( int i=0; i<discrete_level - (int)(discrete_peak_level == discrete_level); i++ )
printf("=");
} else {
for( int i=0; i<discrete_level - (int)(discrete_peak_level == discrete_level); i++ )
printf("=");
for( int i=discrete_level; i<discrete_peak_level - 1; ++i )
printf(" ");
printf("}");
for( int i=discrete_peak_level; i<max_space/num_channels; ++i )
printf(" ");
}
}
printf("] %.2f kb/s\r", bytes_per_second/1024.0f);
fflush(stdout);
}
void gen_random_addr(char * addr, unsigned int max_len) {
static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
sprintf(addr, "inproc://");
for (int i = 9; i < max_len - 1; ++i)
addr[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
addr[max_len - 1] = 0;
}
bool matchBeginnings(const char * x, const char * y) {
for( int i=0; i<fmin(strlen(x), strlen(y)); ++i ) {
if( x[i] != y[i] )
return false;
}
return true;
}
int get_monitor_event (void *monitor, int *value, char **address) {
// First frame in message contains event number and value
zmq_msg_t msg;
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor, 0) == -1)
return -1; // Interruped, presumably
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
uint16_t event = *(uint16_t *) (data);
if (value)
*value = *(uint32_t *) (data + 2);
// Second frame in message contains event address
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor, 0) == -1)
return -1; // Interruped, presumably
if (address) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
size_t size = zmq_msg_size (&msg);
*address = (char *) malloc (size + 1);
memcpy (*address, data, size);
(*address)[size] = 0;
}
return event;
}
// REP socket monitor thread
void * socket_monitor_thread(void *ctx) {
void *s = zmq_socket(ctx, ZMQ_PAIR);
int rc = zmq_connect(s, "inproc://monitor");
while( true ) {
int value = 0;
char * address = NULL;
int event = get_monitor_event(s, &value, &address);
switch( event ) {
case ZMQ_EVENT_CONNECTED:
printf("EVENT: Connected! [%s]\n", address);
break;
case ZMQ_EVENT_CONNECT_DELAYED:
printf("EVENT: Connect delayed! Waiting %dms... [%s]\n", value, address);
break;
case ZMQ_EVENT_CONNECT_RETRIED:
printf("EVENT: Connect retried! [%s]\n", address);
break;
case ZMQ_EVENT_ACCEPTED:
printf("EVENT: Accepted! [%s]\n", address);
break;
case ZMQ_EVENT_CLOSED:
printf("EVENT: Closed! [%s]\n", address);
break;
case ZMQ_EVENT_DISCONNECTED:
printf("EVENT: Disconnected! [%s]\n", address);
break;
default:
printf("EVENT: [%d] [%d] [%s]\n", event, value, address);
break;
}
}
zmq_close(s);
return NULL;
}
int old_stderr = -1;
int dev_null = -1;
void squelch_stderr()
{
if( old_stderr == -1)
old_stderr = dup(STDERR_FILENO);
if( dev_null == -1 )
dev_null = open("/dev/null", O_WRONLY);
dup2(dev_null, STDERR_FILENO);
}
void restore_stderr()
{
dup2(old_stderr, STDERR_FILENO);
}