-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c++
319 lines (310 loc) · 8.3 KB
/
misc.c++
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <poll.h> // struct pollfd
#include <unistd.h>
#include "vssSrv.h"
using std::cerr;
void Msgsend(struct sockaddr_in* addr /*always vcl_addr*/, const char* msg) {
if (!addr)
return;
#if defined VSS_LINUX || defined VSS_CYGWIN32_NT40 || defined VSS_MAC
#define SPOOGE (const struct sockaddr*)
#else
#define SPOOGE
#endif
if (!sendto(vpfd.fd, msg, strlen(msg)+1, 0, SPOOGE addr, sizeof(*addr))) {
printf("send failed\n");
return;
}
#undef SPOOGE
// printf("sendto %s:%d '%s'\n", inet_ntoa(addr->sin_addr), addr->sin_port, msg);
}
#ifdef VSS_WINDOWS
extern int vfMMIO;
#endif
extern int vfSoftClip;
extern int vfLimitClip;
extern void SetSoundIn(int fSoundIn);
// For parsing numbers from argv[] in ParseArgs(),
// more robustly than atoi() and atof().
// On error, simply exit(1). But if fOptional, return -1.
int parseInt(const char* s, bool fOptional=false)
{
const auto saved = errno;
errno = 0;
char* tmp;
const auto val = strtol(s, &tmp, 0);
const auto failed = tmp == s || *tmp != '\0' || errno == ERANGE || errno == EINVAL;
if (failed && !fOptional) {
std::cerr << "Failed to parse int from '" << s << "'.\n";
exit(1);
}
if (errno == 0)
errno = saved;
return failed && fOptional ? -1 : int(val);
}
double parseFloat(const char* s)
{
const auto saved = errno;
errno = 0;
char* tmp;
const auto val = strtod(s, &tmp);
if (tmp == s || *tmp != '\0' || errno == ERANGE || errno == EINVAL) {
std::cerr << "Failed to parse float from '" << s << "'.\n";
exit(1);
}
if (errno == 0)
errno = saved;
return val;
}
void ParseArgs(int argc,char *argv[],int * /*udp_port*/, int *liveaudio,
float *sample_rate, int *nchansVSS, int *nchansIn, int *nchansOut, int *hog, int * /*lwm*/, int * /*hwm*/, char* ofile)
{
#ifdef VSS_WINDOWS
int vfNeededMMIO = 0;
#endif
*nchansIn = 0;
int nargs = 0;
argc--; argv++;++nargs;
while(argc>0)
{
if(strcmp(*argv, "-srate")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -srate with missing value.\n";
else {
*sample_rate = parseFloat(*argv);
if (*sample_rate < 8000.)
{
cerr << "vss: boosting sampling rate to 8000 from " << *sample_rate << "\n";
*sample_rate = 8000.;
}
if (*sample_rate > 48000.)
{
cerr << "vss: limiting sampling rate to 48000 from " << *sample_rate << "\n";
*sample_rate = 48000.;
}
argc--; argv++;++nargs;
}
}
else if(strcmp(*argv, "-hog")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -hog with missing value.\n";
else {
*hog = parseInt(*argv);
argc--; argv++;++nargs;
}
}
else if(strcmp(*argv, "-limit")==0)
{
argc--; argv++;++nargs;
vfLimitClip = true;
}
else if(strcmp(*argv,"-nopanel")==0)
{
argc--; argv++;++nargs;
// Silently ignore, for backwards compatibility.
}
else if(strcmp(*argv, "-silent")==0)
{
argc--; argv++;++nargs;
*liveaudio = false;
}
#ifdef VSS_WINDOWS
else if(strcmp(*argv, "-mmio")==0)
{
argc--; argv++;++nargs;
vfMMIO = true;
if (vfNeededMMIO)
cerr << "vss remark: try using -mmio before other flags.\n";
}
#endif
else if(strcmp(*argv, "-port")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -port with missing value.\n";
else {
globs.udp_port = parseInt(*argv);
if (globs.udp_port < 1000 || globs.udp_port > 65535)
{
cerr << "vss: port number " << globs.udp_port << " out of range [1000, 65535]. Using default 7999.\n";
globs.udp_port = 7999;
}
argc--; argv++;++nargs;
}
}
else if(strcmp(*argv, "-soft")==0)
{
argc--; argv++;++nargs;
vfSoftClip = true;
}
else if(strcmp(*argv, "-lowlatency")==0)
{
argc--; argv++;++nargs;
globs.lwm = 128;
globs.hwm = 384;
}
else if(strcmp(*argv, "-latency")==0)
{
argc--; argv++;++nargs;
if(argc>1)
{
const auto lwm = parseInt(*argv);
argc--; argv++;++nargs;
const auto hwm = parseInt(*argv);
argc--; argv++;++nargs;
if (0 < lwm && lwm < hwm)
{
globs.lwm = lwm;
globs.hwm = hwm;
}
else
cerr << "vss: ignoring -latency with invalid args " << argv[-2] << " and " << argv[-1] << ".\n";
}
else
{
cerr << "vss: ignoring -latency with missing values.\n";
if (argc>0)
{
argc--; argv++;++nargs;
}
}
}
else if(strcmp(*argv, "-antidropout")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -antidropout with missing value.\n";
else {
const auto msec = parseFloat(*argv);
if (msec > 0.)
globs.msecAntidropout = msec;
else
cerr << "vss: ignoring -antidropout with nonpositive duration.\n";
argc--; argv++;++nargs;
}
}
else if(strcmp(*argv, "-input")==0)
{
argc--; argv++;++nargs;
SetSoundIn(true);
if (argc <= 0) {
*nchansIn = 1; // Default # of input channels. No trailing args at all.
} else {
const auto w = parseInt(*argv, true);
if (w > 0) {
// it really was "-input 4", not "-input -graphoutput" (for example).
*nchansIn = w;
argc--; argv++;++nargs;
} // Otherwise, *argv is another arg after -input_lacking_a_following_int.
}
}
else if(strcmp(*argv, "-ofile")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -ofile with missing value.\n";
else {
strcpy(ofile, *argv);
argc--; argv++;++nargs;
}
}
else if(strcmp(*argv, "-chans")==0)
{
argc--; argv++;++nargs;
if (argc <= 0)
cerr << "vss: ignoring -chans with missing value.\n";
else {
if (strstr(*argv, "into"))
{
// Fancy "-chans 8into4:0,1,2,3" parsing.
int cch = 0;
int i;
char* pch = *argv;
if (2 != sscanf(pch, "%dinto%d:%n", nchansVSS, nchansOut, &cch))
goto LChansError;
pch += cch;
for (i=0; i<*nchansOut; i++)
{
int w;
if (1 != sscanf(pch, "%d%n", &w, &cch))
{
LChansError:
cerr << "vss: unexpected \"-chans " << argv[0] << "\": playing 1 channel.\n";
*nchansVSS = *nchansOut = 1;
goto LDoneChans;
}
if (w<0 || w>= *nchansVSS)
{
cerr << "vss: channel " << w << "out of range [0, " << *nchansVSS - 1 << "]. Using 0.\n";
w = 0;
}
globs.rgwRemappedOutput[i] = w;
pch += cch+1; // just skip the comma, don't parse it.
}
if (pch[-1] == ',')
cerr << "vss: ignoring extra channel numbers \"" << pch-1 << "\"\n";
globs.fRemappedOutput = 1;
printf("vss: outputing channels ( ");
for (i=0; i<*nchansOut; i++)
printf("%d ", globs.rgwRemappedOutput[i]);
printf(") of %d into %d channels.\n", *nchansVSS, *nchansOut);
LDoneChans:;
}
else
{
// Simple case "-chans x".
*nchansVSS = *nchansOut = parseInt(*argv);
globs.fRemappedOutput = 0;
}
argc--; argv++;++nargs;
}
// globs.fdOfile via AIFF supports 1 to 8 channels, even non-power-of-two.
if (*nchansVSS < 1)
{
cerr << "vss: outputing to 1 channel instead of " << *nchansVSS << "\n";
*nchansVSS = *nchansOut = 1;
globs.fRemappedOutput = 0;
}
#ifdef VSS_WINDOWS
else if (*nchansVSS > 2 && !vfMMIO)
{
vfNeededMMIO = 1;
cerr << "vss warning: outputing to 2 channels (DirectSound's limit) instead of " << *nchansVSS << ".\n";
*nchansVSS = *nchansOut = 2;
globs.fRemappedOutput = 0;
}
#endif
if (*nchansVSS > MaxNumChannels)
{
cerr << "vss: playing only " << MaxNumChannels << " channels instead of " << *nchansVSS << ".\n";
*nchansVSS = *nchansOut = MaxNumChannels;
globs.fRemappedOutput = 0;
// Otherwise Synth() causes a buffer overflow.
}
}
else
{
if (*argv)
cerr << "vss: ignoring unexpected flag \"" << *argv << "\"\n";
cerr <<"Usage: vss \
\n\t-chans n \n\t-srate n \n\t-silent \n\t-ofile filename\n\t-input \n\t-port n \
\n\n\t-hog [012] \n\t-lowlatency \n\t-latency lwm hwm \n\t-antidropout msec \
\n\n\t-limit \n\t-soft\n";
#ifdef VSS_WINDOWS
cerr <<"\n\n\t-mmio";
#endif
exit(-1);
}
}
if (VssInputBuffer() && *liveaudio == 0)
{
cerr << "vss: ignoring audio input because -silent.\n";
SetSoundIn(0);
}
}