-
Notifications
You must be signed in to change notification settings - Fork 0
/
xwax.c
674 lines (504 loc) · 16.7 KB
/
xwax.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
* Copyright (C) 2021 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#include <assert.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h> /* mlockall() */
#include <SDL.h> /* may override main() */
#include "alsa.h"
#include "controller.h"
#include "device.h"
#include "dicer.h"
#include "dummy.h"
#include "interface.h"
#include "jack.h"
#include "library.h"
#include "oss.h"
#include "realtime.h"
#include "thread.h"
#include "rig.h"
#include "timecoder.h"
#include "track.h"
#include "xwax.h"
#define DEFAULT_OSS_BUFFERS 8
#define DEFAULT_OSS_FRAGMENT 7
#define DEFAULT_ALSA_BUFFER 240 /* samples */
#define DEFAULT_PRIORITY 80
#define DEFAULT_IMPORTER EXECDIR "/xwax-import"
#define DEFAULT_SCANNER EXECDIR "/xwax-scan"
#define DEFAULT_TIMECODE "serato_2a"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
char *banner = "xwax " VERSION \
" (C) Copyright 2021 Mark Hills <mark@xwax.org>";
size_t ndeck;
struct deck deck[3];
static size_t nctl;
static struct controller ctl[2];
static struct rt rt;
static double speed;
static bool protect, phono;
static const char *importer;
static struct timecode_def *timecode;
static void usage(FILE *fd)
{
fprintf(fd, "Usage: xwax [<options>]\n\n");
fprintf(fd, "Program-wide options:\n"
" -k Lock real-time memory into RAM\n"
" -q <n> Real-time priority (0 for no priority, default %d)\n"
" -g <s> Set display geometry (see man page)\n"
" --no-decor Request a window with no decorations\n"
" -h Display this message to stdout and exit\n\n",
DEFAULT_PRIORITY);
fprintf(fd, "Music library options:\n"
" -l <path> Location to scan for audio tracks\n"
" -s <program> Library scanner (default '%s')\n\n",
DEFAULT_SCANNER);
fprintf(fd, "Deck options:\n"
" -t <name> Timecode name\n"
" -33 Use timecode at 33.3RPM (default)\n"
" -45 Use timecode at 45RPM\n"
" -c Protect against certain operations while playing\n"
" -u Allow all operations when playing\n"
" --line Line level signal (default)\n"
" --phono Tolerate cartridge level signal ('software pre-amp')\n"
" -i <program> Importer (default '%s')\n"
" --dummy Build a dummy deck with no audio device\n\n",
DEFAULT_IMPORTER);
#ifdef WITH_OSS
fprintf(fd, "OSS device options:\n"
" -d <device> Build a deck connected to OSS audio device\n"
" --rate <hz> Sample rate (default 48000Hz)\n"
" -b <n> Number of buffers (default %d)\n"
" -f <n> Buffer size to request (2^n bytes, default %d)\n\n",
DEFAULT_OSS_BUFFERS, DEFAULT_OSS_FRAGMENT);
#endif
#ifdef WITH_ALSA
fprintf(fd, "ALSA device options:\n"
" -a <device> Build a deck connected to ALSA audio device\n"
" --rate <hz> Sample rate (default is automatic)\n"
" --buffer <n> Buffer size (default %d samples)\n\n",
DEFAULT_ALSA_BUFFER);
#endif
#ifdef WITH_JACK
fprintf(fd, "JACK device options:\n"
" -j <name> Create a JACK deck with the given name\n\n");
#endif
#ifdef WITH_ALSA
fprintf(fd, "MIDI control:\n"
" --dicer <dev> Novation Dicer\n\n");
#endif
fprintf(fd,
"The ordering of options is important. Options apply to subsequent\n"
"music libraries or decks, which can be given multiple times. See the\n"
"manual for details.\n\n"
"Available timecodes (for use with -t):\n"
" serato_2a (default), serato_2b, serato_cd,\n"
" pioneer_a, pioneer_b,\n"
" traktor_a, traktor_b,\n"
" mixvibes_v2, mixvibes_7inch\n\n"
"See the xwax(1) man page for full information and examples.\n");
}
static struct device* start_deck(const char *desc)
{
fprintf(stderr, "Initialising deck %zd (%s)...\n", ndeck, desc);
if (ndeck == ARRAY_SIZE(deck)) {
fprintf(stderr, "Too many decks.\n");
return NULL;
}
return &deck[ndeck].device;
}
static int commit_deck(void)
{
int r;
struct deck *d;
size_t n;
/* Fallback to a default timecode. Don't initialise this at the
* front of the program to avoid buildling unnecessary LUTs */
if (timecode == NULL) {
timecode = timecoder_find_definition(DEFAULT_TIMECODE);
assert(timecode != NULL);
}
d = &deck[ndeck];
r = deck_init(d, &rt, timecode, importer, speed, phono, protect);
if (r == -1)
return -1;
/* Connect this deck to available controllers */
for (n = 0; n < nctl; n++)
controller_add_deck(&ctl[n], d);
ndeck++;
return 0;
}
int main(int argc, char *argv[])
{
int rc = -1, n, priority;
const char *scanner, *geo;
char *endptr;
bool use_mlock, decor;
struct library library;
#if defined WITH_OSS || WITH_ALSA
unsigned int rate; /* or 0 for 'automatic' */
#endif
#ifdef WITH_OSS
int oss_buffers, oss_fragment;
#endif
#ifdef WITH_ALSA
unsigned int alsa_buffer;
#endif
fprintf(stderr, "%s\n\n" NOTICE "\n\n", banner);
if (setlocale(LC_ALL, "") == NULL) {
fprintf(stderr, "Could not honour the local encoding\n");
return -1;
}
/* Explicit formatting for numbers; parsing and printing. Match
* the user's expectations, and the documentation */
if (setlocale(LC_NUMERIC, "POSIX") == NULL) {
fprintf(stderr, "Could not set numeric encoding\n");
return -1;
}
if (thread_global_init() == -1)
return -1;
if (library_global_init() == -1)
return -1;
if (rig_init() == -1)
return -1;
rt_init(&rt);
library_init(&library);
ndeck = 0;
geo = "";
decor = true;
nctl = 0;
priority = DEFAULT_PRIORITY;
importer = DEFAULT_IMPORTER;
scanner = DEFAULT_SCANNER;
timecode = NULL;
speed = 1.0;
protect = false;
phono = false;
use_mlock = false;
#if defined WITH_OSS || WITH_ALSA
rate = 0; /* automatic */
#endif
#ifdef WITH_ALSA
alsa_buffer = DEFAULT_ALSA_BUFFER;
#endif
#ifdef WITH_OSS
oss_fragment = DEFAULT_OSS_FRAGMENT;
oss_buffers = DEFAULT_OSS_BUFFERS;
#endif
/* Skip over command name */
argv++;
argc--;
while (argc > 0) {
if (!strcmp(argv[0], "-h")) {
usage(stdout);
return 0;
#ifdef WITH_OSS
} else if (!strcmp(argv[0], "-f")) {
/* Set fragment size for subsequent devices */
if (argc < 2) {
fprintf(stderr, "-f requires an integer argument.\n");
return -1;
}
oss_fragment = strtol(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "-f requires an integer argument.\n");
return -1;
}
/* Fragment sizes greater than the default aren't useful
* as they are dependent on DEVICE_FRAME */
if (oss_fragment < DEFAULT_OSS_FRAGMENT) {
fprintf(stderr, "Fragment size must be %d or more; aborting.\n",
DEFAULT_OSS_FRAGMENT);
return -1;
}
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-b")) {
/* Set number of buffers for subsequent devices */
if (argc < 2) {
fprintf(stderr, "-b requires an integer argument.\n");
return -1;
}
oss_buffers = strtol(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "-b requires an integer argument.\n");
return -1;
}
argv += 2;
argc -= 2;
#endif
#if defined WITH_OSS || WITH_ALSA
} else if (!strcmp(argv[0], "--rate") || !strcmp(argv[0], "-r")) {
if (!strcmp(argv[0], "-r"))
fprintf(stderr, "-r will be removed in future, use --rate instead\n");
/* Set sample rate for subsequence devices */
if (argc < 2) {
fprintf(stderr, "--rate requires an integer argument.\n");
return -1;
}
rate = strtoul(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "--rate requires an integer argument.\n");
return -1;
}
if (rate < 8000) {
fprintf(stderr, "--rate must be a positive integer, in Hz.\n");
return -1;
}
argv += 2;
argc -= 2;
#endif
#ifdef WITH_ALSA
} else if (!strcmp(argv[0], "-m")) {
fprintf(stderr, "-m is no longer available, check the man page for --buffer in samples\n");
return -1;
} else if (!strcmp(argv[0], "--buffer")) {
/* Set size of ALSA buffer for subsequence devices */
if (argc < 2) {
fprintf(stderr, "--buffer requires an integer argument.\n");
return -1;
}
alsa_buffer = strtoul(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "--buffer requires an integer argument.\n");
return -1;
}
argv += 2;
argc -= 2;
#endif
} else if (!strcmp(argv[0], "-d") || !strcmp(argv[0], "-a") ||
!strcmp(argv[0], "-j"))
{
int r;
struct device *device;
/* Create a deck */
if (argc < 2) {
fprintf(stderr, "-%c requires a device name as an argument.\n",
argv[0][1]);
return -1;
}
device = start_deck(argv[1]);
if (device == NULL)
return -1;
/* Work out which device type we are using, and initialise
* an appropriate device. */
switch(argv[0][1]) {
#ifdef WITH_OSS
case 'd':
r = oss_init(device, argv[1], rate ? rate : 48000,
oss_buffers, oss_fragment);
break;
#endif
#ifdef WITH_ALSA
case 'a':
r = alsa_init(device, argv[1], rate, alsa_buffer);
break;
#endif
#ifdef WITH_JACK
case 'j':
r = jack_init(device, argv[1]);
break;
#endif
default:
fprintf(stderr, "Device type is not supported by this "
"distribution of xwax.\n");
return -1;
}
if (r == -1)
return -1;
commit_deck();
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "--dummy")) {
struct device *v;
v = start_deck("dummy");
if (v == NULL)
return -1;
dummy_init(v);
commit_deck();
argv++;
argc--;
} else if (!strcmp(argv[0], "-t")) {
/* Set the timecode definition to use */
if (argc < 2) {
fprintf(stderr, "-t requires a name as an argument.\n");
return -1;
}
timecode = timecoder_find_definition(argv[1]);
if (timecode == NULL) {
fprintf(stderr, "Timecode '%s' is not known.\n", argv[1]);
return -1;
}
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-33")) {
speed = 1.0;
argv++;
argc--;
} else if (!strcmp(argv[0], "-45")) {
speed = 1.35;
argv++;
argc--;
} else if (!strcmp(argv[0], "-c")) {
protect = true;
argv++;
argc--;
} else if (!strcmp(argv[0], "-u")) {
protect = false;
argv++;
argc--;
} else if (!strcmp(argv[0], "--line")) {
phono = false;
argv++;
argc--;
} else if (!strcmp(argv[0], "--phono")) {
phono = true;
argv++;
argc--;
} else if (!strcmp(argv[0], "-k")) {
use_mlock = true;
track_use_mlock();
argv++;
argc--;
} else if (!strcmp(argv[0], "-q")) {
if (argc < 2) {
fprintf(stderr, "-q requires an integer argument.\n");
return -1;
}
priority = strtol(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "-q requires an integer argument.\n");
return -1;
}
if (priority < 0) {
fprintf(stderr, "Priority (%d) must be zero or positive.\n",
priority);
return -1;
}
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-g")) {
if (argc < 2) {
fprintf(stderr, "-g requires an argument.\n");
return -1;
}
geo = argv[1];
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "--no-decor")) {
decor = false;
argv++;
argc--;
} else if (!strcmp(argv[0], "-i")) {
/* Importer script for subsequent decks */
if (argc < 2) {
fprintf(stderr, "-i requires an executable path "
"as an argument.\n");
return -1;
}
importer = argv[1];
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-s")) {
/* Scan script for subsequent libraries */
if (argc < 2) {
fprintf(stderr, "-s requires an executable path "
"as an argument.\n");
return -1;
}
scanner = argv[1];
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-l")) {
/* Load in a music library */
if (argc < 2) {
fprintf(stderr, "-l requires a pathname as an argument.\n");
return -1;
}
if (library_import(&library, scanner, argv[1]) == -1)
return -1;
argv += 2;
argc -= 2;
#ifdef WITH_ALSA
} else if (!strcmp(argv[0], "--dicer")) {
struct controller *c;
if (nctl == sizeof ctl) {
fprintf(stderr, "Too many controllers; aborting.\n");
return -1;
}
c = &ctl[nctl];
if (argc < 2) {
fprintf(stderr, "Dicer requires an ALSA device name.\n");
return -1;
}
if (dicer_init(c, &rt, argv[1]) == -1)
return -1;
nctl++;
argv += 2;
argc -= 2;
#endif
} else {
fprintf(stderr, "'%s' argument is unknown; try -h.\n", argv[0]);
return -1;
}
}
#ifdef WITH_ALSA
alsa_clear_config_cache();
#endif
if (ndeck == 0) {
fprintf(stderr, "You need to give at least one audio device to use "
"as a deck; try -h.\n");
return -1;
}
rc = EXIT_FAILURE; /* until clean exit */
/* Order is important: launch realtime thread first, then mlock.
* Don't mlock the interface, use sparingly for audio threads */
if (rt_start(&rt, priority) == -1)
return -1;
if (use_mlock && mlockall(MCL_CURRENT) == -1) {
perror("mlockall");
goto out_rt;
}
if (interface_start(&library, geo, decor) == -1)
goto out_rt;
if (rig_main() == -1)
goto out_interface;
rc = EXIT_SUCCESS;
fprintf(stderr, "Exiting cleanly...\n");
out_interface:
interface_stop();
out_rt:
rt_stop(&rt);
for (n = 0; n < ndeck; n++)
deck_clear(&deck[n]);
for (n = 0; n < nctl; n++)
controller_clear(&ctl[n]);
timecoder_free_lookup();
library_clear(&library);
rt_clear(&rt);
rig_clear();
library_global_clear();
thread_global_clear();
if (rc == EXIT_SUCCESS)
fprintf(stderr, "Done.\n");
return rc;
}