Debian Codegraph @

src/out123.c

raw
1 /*
2 out123: stream data from libmpg123 or libsyn123 to an audio output device
3
4 copyright 1995-2025 by the mpg123 project,
5 free software under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7
8 initially written by Thomas Orgis (extracted from mpg123.c)
9
10 This is a stripped down mpg123 that only uses libout123 to write standard
11 input to an audio device. Of course, it got some enhancements with the
12 advent of libsyn123.
13
14 Please bear in mind that the code started out as a nasty hack on a very
15 old piece made out of nasty hacks and plain ugly code. Some nastiness
16 (like lax parameter checking) even serves a purpose: Test the robustness
17 of our libraries in catching bad caller behaviour.
18
19 TODO: Add basic parsing of WAV headers to be able to pipe in WAV files, especially
20 from something like mpg123 -w -.
21
22 TODO: Add option for phase shift between channels (delaying the second one).
23 This might be useful with generated signals, to locate left/right speakers
24 or just generally enhance the experience ... compensating for speaker locations.
25 This also means the option of mixing, channel attentuation. This is not
26 too hard to implement and might be useful for debugging outputs.
27 */
28
29 #define ME "out123"
30 #include "mpg123config.h"
31 #include "version.h"
32 #include "compat/compat.h"
33 #include <ctype.h>
34 #ifdef _WIN32
35 #include "win32_support.h"
36 #endif
37 #include "out123.h"
38
39 #include "local.h"
40
41 #ifdef HAVE_SYS_WAIT_H
42 #include <sys/wait.h>
43 #endif
44 #ifdef HAVE_SYS_RESOURCE_H
45 #include <sys/resource.h>
46 #endif
47
48 #include <errno.h>
49 #include <string.h>
50 #include <time.h>
51
52 #ifdef HAVE_SCHED_H
53 #include <sched.h>
54 #endif
55
56 #include "sysutil.h"
57 #include "getlopt.h"
58
59 #include "syn123.h"
60 #include "filters.h"
61
62 #include "common/debug.h"
63
64 /* be paranoid about setpriority support */
65 #ifndef PRIO_PROCESS
66 #undef HAVE_SETPRIORITY
67 #endif
68
69 static int intflag = FALSE;
70
71 static void usage(int err);
72 static void want_usage(char* arg, topt *opts);
73 static void long_usage(int err);
74 static void want_long_usage(char* arg, topt *opts);
75 static void print_title(FILE* o);
76 static void give_version(char* arg, topt *opts);
77 static void give_libversion(char* arg, topt *);
78
79 static int verbose = 0;
80 static int quiet = FALSE;
81 enum runmodes
82 {
83 RUN_MAIN = 0
84 , LIST_MODULES
85 , LIST_DEVICES
86 };
87
88 static int runmode = RUN_MAIN;
89
90 static FILE* input = NULL;
91 static char *encoding_name = NULL;
92 static int encoding = MPG123_ENC_SIGNED_16;
93 static char *inputenc_name = NULL;
94 static int inputenc = 0;
95 static int mixenc = -1;
96 static int channels = 2;
97 static int inputch = 0;
98 static long rate = 44100;
99 static long inputrate = 0;
100 static double speed = 1.;
101 static long outputrate = 0;
102 static char *driver = NULL;
103 static char *device = NULL;
104 int also_stdout = FALSE;
105 size_t buffer_kb = 0;
106 static int realtime = FALSE;
107 #ifdef HAVE_WINDOWS_H
108 static int w32_priority = 0;
109 #endif
110 #ifdef HAVE_SETPRIORITY
111 static int aggressive = FALSE;
112 #endif
113 static double preload = 0.2;
114 static long outflags = 0;
115 double preamp = 0.;
116 double preamp_factor = 1.;
117 double preamp_offset = 0.;
118 static const char *name = NULL; /* Let the out123 library choose "out123". */
119 static double device_buffer; /* output device buffer */
120 long timelimit_samples = -1;
121 double timelimit_seconds = -1.;
122 off_t offset = 0;
123 off_t timelimit = -1;
124 const char *clip_mode = "implicit";
125 int soft_clip = FALSE;
126 int do_clip = FALSE;
127 int do_filter = FALSE;
128 int do_preamp = FALSE;
129 int do_resample = FALSE;
130 int dither = 0;
131 double clip_limit = 1.;
132 double clip_width = 0.0234;
133
134 char *wave_patterns = NULL;
135 char *wave_freqs = NULL;
136 char *wave_phases = NULL;
137 char *wave_direction = NULL;
138 double sweep_freq = 0.;
139 double sweep_time = 0.;
140 int sweep_hard = 0;
141 long sweep_count = -1;
142 const char *sweep_type = "quad";
143 const char *signal_source = "file";
144 /* Default to around 2 MiB memory for the table. */
145 long wave_limit = 300000;
146 int pink_rows = 0;
147 double geiger_activity = 17;
148 char *resampler = "fine";
149
150 char *filter_spec = NULL;
151
152 size_t pcmblock = 1152; /* samples (pcm frames) we treat en bloc */
153 size_t resample_block = 0; /* resample that many samples in one go */
154 /* To be set after settling format. */
155 size_t pcmframe = 0; // Bytes for one PCM frame of output format.
156 size_t pcminframe = 0; // ... of input format
157 size_t mixframe = 0; // ... of mixing format
158 unsigned char *audio = NULL; // in/output buffer
159 unsigned char *inaudio = NULL; // separate input buffer
160 unsigned char *mixaudio = NULL; // separate mixing buffer
161 float *resaudio = NULL; // separate resampling buffer, always 32 bit float
162 char *mixmat_string = NULL;
163 double *mixmat = NULL;
164
165 // Option to play some oscillatory test signals.
166 // Also used for conversions.
167 syn123_handle *waver = NULL;
168 int generate = FALSE; // Wheter to use the syn123 generator.
169 // Input and output byte swappery.
170 long byte_in_flags = 0;
171 long byte_out_flags = 0;
172
173 out123_handle *ao = NULL;
174 char *cmd_name = NULL;
175 /* ThOr: pointers are not TRUE or FALSE */
176 char *equalfile = NULL;
177 int fresh = TRUE;
178
179 char *fullprogname = NULL; /* Copy of argv[0]. */
180 char *binpath; /* Path to myself. */
181
182 /* File-global storage of command line arguments.
183 They may be needed for cleanup after charset conversion. */
184 static char **argv = NULL;
185 static int argc = 0;
186
187 /* Drain output device/buffer, but still give the option to interrupt things. */
188 static void controlled_drain(void)
189 {
190 int framesize;
191 long rate;
192 size_t drain_block;
193
194 if(intflag || !out123_buffered(ao))
195 return;
196 if(out123_getformat(ao, &rate, NULL, NULL, &framesize))
197 return;
198 drain_block = 1024*framesize;
199 if(!quiet)
200 fprintf( stderr
201 , "\n"ME": draining buffer of %.1f s (you may interrupt)\n"
202 , (double)out123_buffered(ao)/framesize/rate );
203 do {
204 out123_ndrain(ao, drain_block);
205 } while(!intflag && out123_buffered(ao));
206 }
207
208 static void safe_exit(int code)
209 {
210 const char *dummy, *dammy;
211
212 if(input && input != stdin)
213 INT123_compat_fclose(input);
214
215 if(!code)
216 controlled_drain();
217 if(intflag || code)
218 out123_drop(ao);
219 out123_del(ao);
220 #ifdef WANT_WIN32_UNICODE
221 win32_cmdline_free(argc, argv); /* This handles the premature argv == NULL, too. */
222 #endif
223 /* It's ugly... but let's just fix this still-reachable memory chunk of static char*. */
224 split_dir_file("", &dummy, &dammy);
225 if(fullprogname) free(fullprogname);
226 if(mixmat) free(mixmat);
227 if(inaudio && inaudio != audio) free(inaudio);
228 if(audio) free(audio);
229 if(mixaudio) free(mixaudio);
230 if(resaudio) free(resaudio);
231 if(waver) syn123_del(waver);
232 exit(code);
233 }
234
235 static void check_fatal_output(int code)
236 {
237 if(code)
238 {
239 if(!quiet)
240 error2( "out123 error %i: %s"
241 , out123_errcode(ao), out123_strerror(ao) );
242 safe_exit(133);
243 }
244 }
245
246 static void check_fatal_syn(const char *step, int code)
247 {
248 if(code)
249 {
250 if(!quiet)
251 merror("%s: syn123 error %i: %s", step, code, syn123_strerror(code));
252 safe_exit(132);
253 }
254 }
255
256 static void set_output_module(char *arg, topt *opts)
257 {
258 unsigned int i;
259
260 /* Search for a colon and set the device if found */
261 for(i=0; i< strlen( arg ); i++) {
262 if (arg[i] == ':') {
263 arg[i] = 0;
264 getlopt_set_char(opts, "audiodevice", &arg[i+1]);
265 debug1("Setting output device: %s", device);
266 break;
267 }
268 }
269 /* Set the output module */
270 driver = arg;
271 debug1("Setting output module: %s", driver );
272 }
273
274 static void set_output_flag(int flag)
275 {
276 if(outflags <= 0) outflags = flag;
277 else outflags |= flag;
278 }
279
280 static void set_output_h(char *a, topt *opts)
281 {
282 set_output_flag(OUT123_HEADPHONES);
283 }
284
285 static void set_output_s(char *a, topt *opts)
286 {
287 set_output_flag(OUT123_INTERNAL_SPEAKER);
288 }
289
290 static void set_output_l(char *a, topt *opts)
291 {
292 set_output_flag(OUT123_LINE_OUT);
293 }
294
295 static void set_output(char *arg, topt *opts)
296 {
297 /* If single letter, it's the legacy output switch for AIX/HP/Sun.
298 If longer, it's module[:device] . If zero length, it's rubbish. */
299 if(strlen(arg) <= 1) switch(arg[0])
300 {
301 case 'h': set_output_h(arg, opts); break;
302 case 's': set_output_s(arg, opts); break;
303 case 'l': set_output_l(arg, opts); break;
304 default:
305 error1("\"%s\" is no valid output", arg);
306 safe_exit(1);
307 }
308 else set_output_module(arg, opts);
309 }
310
311 enum byteflags
312 {
313 byte_nothing = 0
314 , byte_native = 1
315 , byte_swap = 2
316 , byte_little = 4
317 , byte_big = 8
318 };
319
320 // Input has same endianess as output unless something was specified.
321 static void finish_endian(void)
322 {
323 long outbyte = byte_out_flags & (byte_big|byte_little|byte_native);
324 if(outbyte && !byte_in_flags)
325 byte_in_flags = outbyte;
326 }
327
328 static void set_endian(char *arg, long *flags)
329 {
330 *flags &= ~(byte_big|byte_little|byte_native);
331 if(!strcmp(arg, "big"))
332 *flags |= byte_big;
333 else if(!strcmp(arg, "little"))
334 *flags |= byte_little;
335 else if(!strcmp(arg, "native"))
336 *flags |= byte_native;
337 else
338 merror("bad endianess choice: %s", arg);
339 }
340
341 static void set_in_endian(char *arg, topt *opts)
342 {
343 set_endian(arg, &byte_in_flags);
344 }
345
346 static void set_out_endian(char *arg, topt *opts)
347 {
348 set_endian(arg, &byte_out_flags);
349 }
350
351 static void set_byteswap(char *arg, topt *opts)
352 {
353 byte_out_flags |= byte_swap;
354 }
355
356 static void set_verbose (char *arg, topt *opts)
357 {
358 verbose++;
359 }
360
361 static void set_quiet (char *arg, topt *opts)
362 {
363 verbose=0;
364 quiet=TRUE;
365 }
366
367 static void set_out_wav(char *arg, topt *opts)
368 {
369 driver = "wav";
370 getlopt_set_char(opts, "audiodevice", arg);
371 }
372
373 void set_out_cdr(char *arg, topt *opts)
374 {
375 driver = "cdr";
376 getlopt_set_char(opts, "audiodevice", arg);
377 }
378
379 void set_out_au(char *arg, topt *opts)
380 {
381 driver = "au";
382 getlopt_set_char(opts, "audiodevice", arg);
383 }
384
385 void set_out_test(char *arg, topt *opts)
386 {
387 driver = "test";
388 getlopt_set_char(opts, "audiodevice", NULL);
389 }
390
391 static void set_out_file(char *arg, topt *opts)
392 {
393 driver = "raw";
394 getlopt_set_char(opts, "audiodevice", arg);
395 }
396
397 static void set_out_stdout(char *arg, topt *opts)
398 {
399 driver = "raw";
400 getlopt_set_char(opts, "audiodevice", NULL);
401 }
402
403 static void set_out_stdout1(char *arg, topt *opts)
404 {
405 also_stdout = TRUE;
406 }
407
408 #if !defined (HAVE_SCHED_SETSCHEDULER) && !defined (HAVE_WINDOWS_H)
409 static void realtime_not_compiled(char *arg, topt *opts)
410 {
411 fprintf(stderr, ME": Option '-T / --realtime' not compiled into this binary.\n");
412 }
413 #endif
414
415 static void list_output_modules(void)
416 {
417 char **names = NULL;
418 char **descr = NULL;
419 int count = -1;
420 out123_handle *lao;
421
422 if((lao=out123_new()))
423 {
424 out123_param_string(lao, OUT123_BINDIR, binpath);
425 out123_param_int(lao, OUT123_VERBOSE, verbose);
426 if(quiet)
427 out123_param_int(lao, OUT123_FLAGS, OUT123_QUIET);
428 if((count=out123_drivers(lao, &names, &descr)) >= 0)
429 {
430 int i;
431 for(i=0; i<count; ++i)
432 {
433 printf( "%-15s\t%s\n", names[i], descr[i] );
434 free(names[i]);
435 free(descr[i]);
436 }
437 free(names);
438 free(descr);
439 }
440 out123_del(lao);
441 }
442 else if(!quiet)
443 error("Failed to create an out123 handle.");
444 exit(count >= 0 ? 0 : 1);
445 }
446
447 static void list_output_devices(void)
448 {
449 int count = 0;
450 char **names = NULL;
451 char **descr = NULL;
452 out123_handle *ao = NULL;
453 ao = out123_new();
454 char *real_driver = NULL;
455 count = out123_devices(ao, driver, &names, &descr, &real_driver);
456 if(count >= 0)
457 {
458 printf("Devices for output module %s:\n", real_driver);
459 for(int i=0; i<count; ++i)
460 {
461 // Always print like it's a terminal to avoid confusion with extra line breaks.
462 print_outstr(stdout, names[i], 1, 1);
463 printf("\t");
464 print_outstr(stdout, descr[i], 1, 1);
465 printf("\n");
466 }
467 out123_stringlists_free(names, descr, count);
468 free(real_driver);
469 } else
470 {
471 merror("Failed to enumerate output devices: %s", out123_strerror(ao));
472 }
473 out123_del(ao);
474 exit(count < 0 ? 1 : 0);
475 }
476
477 static void list_encodings(char *arg, topt *opts)
478 {
479 int i;
480 int enc_count = 0;
481 int *enc_codes = NULL;
482
483 enc_count = out123_enc_list(&enc_codes);
484 /* All of the returned encodings have to have proper names!
485 It is a libout123 bug if not, and it should be quickly caught. */
486 for(i=0;i<enc_count;++i)
487 printf( "%s:\t%s\n"
488 , out123_enc_name(enc_codes[i]), out123_enc_longname(enc_codes[i]) );
489 free(enc_codes);
490 exit(0);
491 }
492
493 static int getencs(void)
494 {
495 int encs = 0;
496 out123_handle *lao;
497 if(verbose)
498 fprintf( stderr
499 , ME": getting supported encodings for %li Hz, %i channels\n"
500 , rate, channels );
501 if((lao=out123_new()))
502 {
503 out123_param_int(lao, OUT123_VERBOSE, verbose);
504 if(quiet)
505 out123_param_int(lao, OUT123_FLAGS, OUT123_QUIET);
506 if(!out123_open(lao, driver, device))
507 encs = out123_encodings(lao, rate, channels);
508 else if(!quiet)
509 error1("cannot open driver: %s", out123_strerror(lao));
510 out123_del(lao);
511 }
512 else if(!quiet)
513 error("Failed to create an out123 handle.");
514 return encs;
515 }
516
517 static void test_format(char *arg, topt *opts)
518 {
519 int encs;
520 encs = getencs();
521 exit((encs & encoding) ? 0 : -1);
522 }
523
524 static void test_encodings(char *arg, topt *opts)
525 {
526 int encs, enc_count, *enc_codes, i;
527
528 encs = getencs();
529 enc_count = out123_enc_list(&enc_codes);
530 for(i=0;i<enc_count;++i)
531 {
532 if((encs & enc_codes[i]) == enc_codes[i])
533 printf("%s\n", out123_enc_name(enc_codes[i]));
534 }
535 free(enc_codes);
536 exit(!encs);
537 }
538
539 static void query_format(char *arg, topt *opts)
540 {
541 out123_handle *lao;
542
543 if(verbose)
544 fprintf(stderr, ME": querying default format\n");
545 if((lao=out123_new()))
546 {
547 out123_param_int(lao, OUT123_VERBOSE, verbose);
548 if(quiet)
549 out123_param_int(lao, OUT123_FLAGS, OUT123_QUIET);
550 if(!out123_open(lao, driver, device))
551 {
552 struct mpg123_fmt *fmts = NULL;
553 int count;
554 count = out123_formats(lao, NULL, 0, 0, 0, &fmts);
555 if(count > 0 && fmts[0].encoding > 0)
556 {
557 const char *encname = out123_enc_name(fmts[0].encoding);
558 printf( "--rate %li --channels %i --encoding %s\n"
559 , fmts[0].rate, fmts[0].channels
560 , encname ? encname : "???" );
561 }
562 else
563 {
564 if(verbose)
565 fprintf(stderr, ME": no default format found\n");
566 }
567 free(fmts);
568 }
569 else if(!quiet)
570 error1("cannot open driver: %s", out123_strerror(lao));
571 out123_del(lao);
572 }
573 else if(!quiet)
574 error("Failed to create an out123 handle.");
575 exit(0);
576 }
577
578 void set_wave_freqs(char *arg, topt *opts)
579 {
580 getlopt_set_char(opts, "source", "wave");
581 wave_freqs = arg;
582 }
583
584 void set_pink_rows(char *arg, topt *opts)
585 {
586 getlopt_set_char(opts, "source", "pink");
587 pink_rows = atoi(arg);
588 }
589
590 void set_geiger_act(char *arg, topt *opts)
591 {
592 getlopt_set_char(opts, "source", "geiger");
593 geiger_activity = atof(arg);
594 }
595
596 void set_sweep_freq(char *arg, topt *opts)
597 {
598 getlopt_set_char(opts, "source", "sweep");
599 sweep_freq = atof(arg);
600 }
601
602 /* Please note: GLO_NUM expects point to LONG! */
603 /* ThOr:
604 * Yeah, and despite that numerous addresses to int variables were
605 passed.
606 * That's not good on my Alpha machine with int=32bit and long=64bit!
607 * Introduced GLO_INT and GLO_LONG as different bits to make that clear.
608 * GLO_NUM no longer exists.
609 */
610 topt opts[] = {
611 {'t', "test", GLO_INT, set_out_test, NULL, 0},
612 {'s', "stdout", GLO_INT, set_out_stdout, NULL, 0},
613 {'S', "STDOUT", GLO_INT, set_out_stdout1, NULL, 0},
614 {'O', "outfile", GLO_ARG | GLO_CHAR, set_out_file, NULL, 0},
615 {'v', "verbose", 0, set_verbose, 0, 0},
616 {'q', "quiet", 0, set_quiet, 0, 0},
617 {'m', "mono", GLO_INT, 0, &channels, 1},
618 {0, "stereo", GLO_INT, 0, &channels, 2},
619 {'c', "channels", GLO_ARG | GLO_INT, 0, &channels, 0},
620 {'C', "inputch", GLO_ARG | GLO_INT, 0, &inputch, 0},
621 {'M', "mix", GLO_ARG | GLO_CHAR, 0, &mixmat_string, 0},
622 {0, "filter", GLO_ARG | GLO_CHAR, 0, &filter_spec, 0},
623 {'P', "preamp", GLO_ARG | GLO_DOUBLE, 0, &preamp, 0},
624 {0, "offset", GLO_ARG | GLO_DOUBLE, 0, &preamp_offset, 0},
625 {'r', "rate", GLO_ARG | GLO_LONG, 0, &rate, 0},
626 {'R', "inputrate", GLO_ARG | GLO_LONG, 0, &inputrate, 0},
627 {0, "speed", GLO_ARG | GLO_DOUBLE, 0, &speed, 0},
628 {0, "clip", GLO_ARG | GLO_CHAR, 0, &clip_mode, 0},
629 {0, "dither", GLO_INT, 0, &dither, 1},
630 {0, "headphones", 0, set_output_h, 0,0},
631 {0, "speaker", 0, set_output_s, 0,0},
632 {0, "lineout", 0, set_output_l, 0,0},
633 {'o', "output", GLO_ARG | GLO_CHAR, set_output, 0, 0},
634 {0, "list-modules",GLO_INT, 0, &runmode, LIST_MODULES},
635 {0, "list-devices",GLO_INT, 0, &runmode, LIST_DEVICES},
636 {'a', "audiodevice", GLO_ARG | GLO_CHAR, 0, &device, 0},
637 #ifndef NOXFERMEM
638 {'b', "buffer", GLO_ARG | GLO_LONG, 0, &buffer_kb, 0},
639 {0, "preload", GLO_ARG|GLO_DOUBLE, 0, &preload, 0},
640 #endif
641 #ifdef HAVE_SETPRIORITY
642 {0, "aggressive", GLO_INT, 0, &aggressive, 2},
643 #endif
644 #if defined (HAVE_SCHED_SETSCHEDULER) || defined (HAVE_WINDOWS_H)
645 /* check why this should be a long variable instead of int! */
646 {'T', "realtime", GLO_INT, 0, &realtime, TRUE },
647 #else
648 {'T', "realtime", 0, realtime_not_compiled, 0, 0 },
649 #endif
650 #ifdef HAVE_WINDOWS_H
651 {0, "priority", GLO_ARG | GLO_INT, 0, &w32_priority, 0},
652 #endif
653 {'w', "wav", GLO_ARG | GLO_CHAR, set_out_wav, 0, 0 },
654 {0, "cdr", GLO_ARG | GLO_CHAR, set_out_cdr, 0, 0 },
655 {0, "au", GLO_ARG | GLO_CHAR, set_out_au, 0, 0 },
656 {'?', "help", 0, want_usage, 0, 0 },
657 {0 , "longhelp" , 0, want_long_usage, 0, 0 },
658 {0 , "version" , 0, give_version, 0, 0 },
659 {0 , "libversion" , 0, give_libversion, 0, 0 },
660 {'e', "encoding", GLO_ARG|GLO_CHAR, 0, &encoding_name, 0},
661 {0, "endian", GLO_ARG|GLO_CHAR, set_out_endian, 0, 0},
662 {'E', "inputenc", GLO_ARG|GLO_CHAR, 0, &inputenc_name, 0},
663 {0, "inputend", GLO_ARG|GLO_CHAR, set_in_endian, 0, 0},
664 {0, "byteswap", 0, set_byteswap, 0, 0},
665 {0, "list-encodings", 0, list_encodings, 0, 0 },
666 {0, "test-format", 0, test_format, 0, 0 },
667 {0, "test-encodings", 0, test_encodings, 0, 0},
668 {0, "query-format", 0, query_format, 0, 0},
669 {0, "name", GLO_ARG|GLO_CHAR, 0, &name, 0},
670 {0, "devbuffer", GLO_ARG|GLO_DOUBLE, 0, &device_buffer, 0},
671 {0, "timelimit", GLO_ARG|GLO_LONG, 0, &timelimit_samples, 0},
672 {0, "seconds", GLO_ARG|GLO_DOUBLE, 0, &timelimit_seconds, 0},
673 {0, "source", GLO_ARG|GLO_CHAR, 0, &signal_source, 0},
674 {0, "wave-pat", GLO_ARG|GLO_CHAR, 0, &wave_patterns, 0},
675 {0, "wave-freq", GLO_ARG|GLO_CHAR, set_wave_freqs, 0, 0},
676 {0, "wave-phase", GLO_ARG|GLO_CHAR, 0, &wave_phases, 0},
677 {0, "wave-direction", GLO_ARG|GLO_CHAR, 0, &wave_direction, 0},
678 {0, "wave-limit", GLO_ARG|GLO_LONG, 0, &wave_limit, 0},
679 {0, "genbuffer", GLO_ARG|GLO_LONG, 0, &wave_limit, 0},
680 {0, "pink-rows", GLO_ARG|GLO_INT, set_pink_rows, 0, 0},
681 {0, "geiger-activity", GLO_ARG|GLO_DOUBLE, set_geiger_act, 0, 0},
682 {0, "wave-sweep", GLO_ARG|GLO_DOUBLE, set_sweep_freq, 0, 0},
683 {0, "sweep-type", GLO_ARG|GLO_CHAR, 0, &sweep_type, 0 },
684 {0, "sweep-time", GLO_ARG|GLO_DOUBLE, 0, &sweep_time, 0},
685 {0, "sweep-hard", GLO_INT, 0, &sweep_hard, TRUE},
686 {0, "sweep-count", GLO_ARG|GLO_LONG, 0, &sweep_count, 0},
687 {0, "resample", GLO_ARG|GLO_CHAR, 0, &resampler, 0},
688 {0, 0, 0, 0, 0, 0}
689 };
690
691 /* An strtok() that also returns empty tokens on multiple separators. */
692
693 static size_t mytok_count(const char *choppy)
694 {
695 size_t count = 0;
696 if(choppy)
697 {
698 count = 1;
699 do {
700 if(*choppy == ',')
701 ++count;
702 } while(*(++choppy));
703 }
704 return count;
705 }
706
707 static char *mytok(char **choppy)
708 {
709 char *tok;
710 if(!*choppy)
711 return NULL;
712 tok = *choppy;
713 while(**choppy && **choppy != ',')
714 ++(*choppy);
715 /* Another token follows if we found a separator. */
716 if(**choppy == ',')
717 {
718 *(*choppy)++ = 0;
719 while(isspace(**choppy))
720 *(*choppy)++ = 0;
721 }
722 else
723 *choppy = NULL; /* Nothing left. */
724 return tok;
725 }
726
727 static void setup_wavegen(void)
728 {
729 size_t count = 0;
730 size_t i;
731 double *freq = NULL;
732 double *freq_real = NULL;
733 double *phase = NULL;
734 int *backwards = NULL;
735 int *id = NULL;
736 int synerr = 0;
737 size_t common = 0;
738
739 if(!generate)
740 wave_limit = 0;
741 waver = syn123_new(inputrate, inputch, inputenc, wave_limit, &synerr);
742 check_fatal_syn("waver init", synerr);
743 check_fatal_syn("dither", syn123_dither(waver, dither, NULL));
744 if(!waver)
745 safe_exit(132);
746 if(do_resample)
747 {
748 int dirty = -1;
749 if(!strcasecmp(resampler, "fine"))
750 dirty = 0;
751 else if(!strcasecmp(resampler, "dirty"))
752 dirty = 1;
753 else
754 {
755 if(!quiet)
756 error1("Bad value for resampler type given: %s\n", resampler);
757 safe_exit(132);
758 }
759 check_fatal_syn( "resampling setup", syn123_setup_resample( waver
760 , inputrate, outputrate, channels, dirty, 0 ) );
761 }
762 if(do_filter)
763 {
764 size_t err_count = 0;
765 struct filterlist *fl = parse_filterspec(filter_spec);
766 if(!fl)
767 {
768 error1("Got no valid filter specification out of: %s", filter_spec);
769 safe_exit(135);
770 }
771 for(size_t fi=0; fi<fl->count; ++fi)
772 {
773 int err = syn123_setup_filter( waver, 1
774 , fl->f[fi].order, fl->f[fi].b, fl->f[fi].a
775 , mixenc, channels, 1 );
776 if(err)
777 {
778 merror("Cannot init filter %zu: %s", fi, syn123_strerror(err));
779 ++err_count;
780 }
781 }
782 free_filterlist(fl);
783 if(err_count)
784 {
785 merror("%zu errors during filter setup", err_count);
786 safe_exit(135);
787 }
788 }
789 // At least have waver handy for conversions.
790 if(!generate)
791 return;
792
793 if(!strcmp(signal_source, "pink"))
794 {
795 synerr = syn123_setup_pink(waver, pink_rows, 123456, &common);
796 if(synerr)
797 {
798 if(!quiet)
799 merror("setting up pink noise generator: %s\n", syn123_strerror(synerr));
800 safe_exit(132);
801 }
802 if(verbose)
803 {
804 fprintf( stderr
805 , ME ": pink noise with %i generator rows (0=internal default)\n"
806 , pink_rows );
807 }
808 goto setup_waver_end;
809 }
810 if(!strcmp(signal_source, "white"))
811 {
812 synerr = syn123_setup_white(waver, 123456, &common);
813 if(synerr)
814 {
815 if(!quiet)
816 merror("setting up white noise generator: %s\n", syn123_strerror(synerr));
817 safe_exit(132);
818 }
819 if(verbose)
820 {
821 fprintf( stderr, ME ": white noise\n" );
822 }
823 goto setup_waver_end;
824 }
825 else if(!strcmp(signal_source, "geiger"))
826 {
827 synerr = syn123_setup_geiger(waver, geiger_activity, 123456, &common);
828 if(synerr)
829 {
830 if(!quiet)
831 merror("setting up geiger generator: %s\n", syn123_strerror(synerr));
832 safe_exit(132);
833 }
834 if(verbose)
835 {
836 fprintf(stderr, ME ": geiger with actvity %g\n", geiger_activity);
837 }
838 goto setup_waver_end;
839 }
840 else if(!strcmp(signal_source, "sweep"))
841 {
842 double f1 = 0.;
843 double f2 = sweep_freq;
844 int wid = SYN123_WAVE_SINE;
845 int backwards = FALSE;
846 int sid = SYN123_SWEEP_QUAD;
847 // Yes, could overflow. You get a short time, then.
848 size_t duration = timelimit > -1 ? (size_t)timelimit : inputrate;
849 size_t period = 0;
850 double sweep_phase = 0.;
851 double endphase = -1;
852 if(sweep_type)
853 {
854 if(!strncmp("lin", sweep_type, 3))
855 sid = SYN123_SWEEP_LIN;
856 else if(!strncmp("qua", sweep_type, 3))
857 sid = SYN123_SWEEP_QUAD;
858 else if(!strncmp("exp", sweep_type, 3))
859 sid = SYN123_SWEEP_EXP;
860 else
861 {
862 if(!quiet)
863 merror("bad sweep choice: %s", sweep_type);
864 safe_exit(132);
865 }
866 }
867 if(wave_freqs)
868 {
869 char *next = wave_freqs;
870 f1 = atof(mytok(&next));
871 }
872 if(wave_patterns)
873 {
874 char *next = wave_patterns;
875 wid = syn123_wave_id(mytok(&next));
876 if(wid < 0 && !quiet)
877 fprintf(stderr, ME ": Warning: bad wave pattern: %s\n", wave_patterns);
878 }
879 if(wave_phases)
880 {
881 char *next = wave_phases;
882 sweep_phase = atof(mytok(&next));
883 if(sweep_phase < 0)
884 {
885 backwards = TRUE;
886 sweep_phase = -sweep_phase;
887 }
888 }
889 if(wave_direction)
890 {
891 char *next = wave_phases;
892 backwards = atof(mytok(&next)) < 0 ? TRUE : FALSE;
893 }
894 if(sweep_time > 0)
895 duration = (size_t)(sweep_time*inputrate);
896 synerr = syn123_setup_sweep( waver, wid, sweep_phase, backwards
897 , sid, &f1, &f2, !sweep_hard, duration, &endphase, &period, &common);
898 if(synerr)
899 {
900 if(!quiet)
901 merror("setting up sweep generator: %s\n", syn123_strerror(synerr));
902 safe_exit(132);
903 }
904 if(sweep_count > -1)
905 timelimit = sweep_count * period;
906 if(verbose)
907 {
908 fprintf( stderr, ME ": %s sweep of %zu samples (%s)\n"
909 , sweep_type
910 , (timelimit > -1 && timelimit < period) ? (size_t)timelimit : (size_t)period
911 , (timelimit > -1 && timelimit < period)
912 ? ((timelimit == duration) ? "exactly" : "cut off" )
913 : (sweep_hard ? "periodic with phase jumps" : "smoothly periodic") );
914 fprintf( stderr, ME ": from: %s @ %g Hz p %g\n"
915 , syn123_wave_name(wid), f1, sweep_phase );
916 fprintf( stderr, ME ": to: %s @ %g Hz p %g\n"
917 , syn123_wave_name(wid), f2
918 , (timelimit < 0 || timelimit >= period) ? sweep_phase : endphase );
919 }
920 goto setup_waver_end;
921 }
922 else if(strcmp(signal_source, "wave"))
923 {
924 if(!quiet)
925 merror("unknown signal source: %s", signal_source);
926 safe_exit(132);
927 }
928
929 // The big default code block is for wave setup.
930 // Exceptions jump over it.
931 if(wave_freqs)
932 {
933 char *tok;
934 char *next;
935 count = mytok_count(wave_freqs);
936 freq = malloc(sizeof(double)*count);
937 freq_real = malloc(sizeof(double)*count);
938 if(!freq || !freq_real){ error("OOM!"); safe_exit(1); }
939 next = wave_freqs;
940 for(i=0; i<count; ++i)
941 {
942 tok = mytok(&next);
943 if(tok && *tok)
944 freq[i] = atof(tok);
945 else if(i)
946 freq[i] = freq[i-1];
947 else
948 freq[i] = 0;
949 }
950 memcpy(freq_real, freq, sizeof(double)*count);
951 }
952 else if(wave_patterns || wave_phases || wave_direction)
953 {
954 if(!quiet)
955 fprintf( stderr
956 , ME ": Warning: wave setup ignored without specified frequency\n" );
957 }
958
959
960 if(count && wave_patterns)
961 {
962 char *tok;
963 char *next = wave_patterns;
964 id = malloc(sizeof(int)*count);
965 if(!id){ error("OOM!"); safe_exit(1); }
966 for(i=0; i<count; ++i)
967 {
968 tok = mytok(&next);
969 if((tok && *tok) || i==0)
970 {
971 id[i] = syn123_wave_id(tok);
972 if(id[i] < 0 && !quiet)
973 fprintf(stderr, ME ": Warning: bad wave pattern: %s\n", tok);
974 }
975 else
976 id[i] = id[i-1];
977 }
978 }
979
980 if(count && wave_phases)
981 {
982 char *tok;
983 char *next = wave_phases;
984 phase = malloc(sizeof(double)*count);
985 backwards = malloc(sizeof(int)*count);
986 if(!phase || !backwards){ error("OOM!"); safe_exit(1); }
987 for(i=0; i<count; ++i)
988 {
989 tok = mytok(&next);
990 if(tok && *tok)
991 phase[i] = atof(tok);
992 else if(i)
993 phase[i] = phase[i-1];
994 else
995 phase[i] = 0;
996 if(phase[i] < 0)
997 {
998 phase[i] *= -1;
999 backwards[i] = TRUE;
1000 }
1001 else
1002 backwards[i] = FALSE;
1003 }
1004 }
1005
1006 if(count && wave_direction)
1007 {
1008 char *tok;
1009 char *next = wave_direction;
1010 if(!backwards)
1011 backwards = malloc(sizeof(int)*count);
1012 if(!backwards){ error("OOM!"); safe_exit(1); }
1013 for(i=0; i<count; ++i)
1014 {
1015 tok = mytok(&next);
1016 if(tok && *tok)
1017 backwards[i] = atof(tok) < 0 ? TRUE : FALSE;
1018 else if(i)
1019 backwards[i] = backwards[i-1];
1020 else
1021 backwards[i] = FALSE;
1022 }
1023 }
1024
1025 synerr = syn123_setup_waves( waver, count
1026 , id, freq_real, phase, backwards, &common );
1027 if(synerr)
1028 {
1029 if(!quiet)
1030 merror("setting up wave generator: %s\n", syn123_strerror(synerr));
1031 safe_exit(132);
1032 }
1033 if(verbose)
1034 {
1035 if(count) for(i=0; i<count; ++i)
1036 fprintf( stderr, ME ": wave %zu: %s @ %g Hz (%g Hz) p %g\n"
1037 , i
1038 , syn123_wave_name(id ? id[i] : SYN123_WAVE_SINE)
1039 , freq[i], freq_real[i]
1040 , phase ? phase[i] : 0 );
1041 else
1042 fprintf(stderr, ME ": default sine wave\n");
1043 }
1044
1045 setup_waver_end:
1046 if(verbose)
1047 {
1048 if(common)
1049 fprintf(stderr, ME ": periodic signal table of %zu samples\n", common);
1050 else
1051 fprintf(stderr, ME ": live signal generation\n");
1052 }
1053 if(phase)
1054 free(phase);
1055 if(id)
1056 free(id);
1057 if(freq)
1058 free(freq);
1059 if(freq_real)
1060 free(freq_real);
1061 }
1062
1063
1064 void push_output(unsigned char *buf, size_t samples)
1065 {
1066 errno = 0;
1067 size_t bytes = samples*pcmframe;
1068
1069 if(byte_out_flags & byte_big)
1070 syn123_host2be( buf, pcmframe/channels, samples*channels);
1071 if(byte_out_flags & byte_little)
1072 syn123_host2le( buf, pcmframe/channels, samples*channels);
1073 if(byte_out_flags & byte_swap)
1074 syn123_swap_bytes(buf, pcmframe/channels, samples*channels);
1075
1076 mdebug("playing %zu bytes", bytes);
1077 check_fatal_output(out123_play(ao, buf, bytes) < (int)bytes);
1078 if(also_stdout && INT123_unintr_fwrite(buf, pcmframe, samples, stdout) < samples)
1079 {
1080 if(!quiet)
1081 error1( "failed to copy stream to stdout: %s", INT123_strerror(errno));
1082 safe_exit(133);
1083 }
1084 }
1085
1086 // Clipping helper, called with mixbuf, resampler buffer, playback buffer ...
1087 // This implies the output channel count.
1088 void clip(void *buf, int enc, size_t samples)
1089 {
1090 size_t clipped = soft_clip
1091 ? syn123_soft_clip( buf, enc, samples*channels
1092 , clip_limit, clip_width, waver )
1093 : syn123_clip(buf, enc, samples*channels);
1094 if(verbose > 1 && clipped)
1095 fprintf(stderr, ME ": explicitly clipped %zu samples\n", clipped);
1096 }
1097
1098 static int had_something = 0;
1099 static int just_stdin = 0;
1100 FILE* open_next_file(int argc, char** argv, int firstrun)
1101 {
1102 FILE *in = NULL;
1103 if(firstrun && loptind >= argc)
1104 {
1105 just_stdin = 1;
1106 had_something = 1;
1107 in = stdin;
1108 } else
1109 while(!in && loptind < argc)
1110 {
1111 char *filename = argv[loptind++];
1112 errno = 0;
1113 in = strcmp(filename, "-") ? INT123_compat_fopen(filename, "rb") : stdin;
1114 if(!in)
1115 merror( "Failed to open input file '%s' (%s), ignoring."
1116 , filename, INT123_strerror(errno) );
1117 else
1118 had_something = 1;
1119 }
1120 return in;
1121 }
1122
1123 /* return 1 on success, 0 on failure */
1124 int play_frame(void)
1125 {
1126 size_t got_samples = 0;
1127 size_t get_samples = pcmblock;
1128 debug("play_frame");
1129 if(timelimit >= 0)
1130 {
1131 if(offset >= timelimit)
1132 return 0;
1133 else if(timelimit < offset+get_samples)
1134 get_samples = (off_t)timelimit-offset;
1135 }
1136 if(generate)
1137 got_samples = syn123_read(waver, inaudio, get_samples*pcminframe)
1138 / pcminframe;
1139 else
1140 got_samples = input ? fread(inaudio, pcminframe, get_samples, input) : 0;
1141 /* Play what is there to play (starting with second decode_frame call!) */
1142 if(!got_samples)
1143 return 0;
1144
1145 if(byte_in_flags & byte_big)
1146 syn123_be2host(inaudio, pcminframe/channels, got_samples*inputch);
1147 if(byte_in_flags & byte_little)
1148 syn123_le2host(inaudio, pcminframe/channels, got_samples*inputch);
1149
1150 if(mixaudio)
1151 {
1152 // All complicated cases trigger mixing/conversion into intermediate
1153 // buffer.
1154 // First either mix or convert into the mixing buffer.
1155 if(mixmat)
1156 check_fatal_syn("buffer mix", syn123_mix( mixaudio, mixenc, channels
1157 , inaudio, inputenc, inputch, mixmat, got_samples, TRUE, NULL, waver ));
1158 else
1159 check_fatal_syn("buffer conv", syn123_conv( mixaudio, mixenc, got_samples*mixframe
1160 , inaudio, inputenc, got_samples*pcminframe, NULL, NULL, waver ));
1161 // Apply filters.
1162 if(do_filter)
1163 check_fatal_syn("buffer filter", syn123_filter( waver, mixaudio, mixenc, got_samples ));
1164 // Do pre-amplification in-place.
1165 if(do_preamp)
1166 check_fatal_syn("buffer amp", syn123_amp( mixaudio, mixenc, got_samples*channels
1167 , preamp_factor, preamp_offset, NULL, NULL ));
1168 // Resampling needs another buffer.
1169 if(do_resample)
1170 {
1171 // Resampling, clipping, and conversion to end buffer in a loop
1172 // of small blocks since resampling can mean a lot of output from tiny
1173 // input.
1174 // Mixaudio has to carry 32 bit float!
1175 float * fmixaudio = (float*)mixaudio;
1176 size_t insamples = got_samples;
1177 size_t inoff = 0;
1178 while(insamples)
1179 {
1180 size_t inblock = insamples < resample_block
1181 ? insamples
1182 : resample_block;
1183 debug("resample");
1184 size_t outsamples = syn123_resample( waver
1185 , resaudio, fmixaudio+inoff*channels, inblock );
1186 debug("clip?");
1187 if(do_clip)
1188 clip(resaudio, MPG123_ENC_FLOAT_32, outsamples);
1189 // Damn, another loop inside the loop for a smaller output buffer?!
1190 // No. I will ensure pcmblock being as large as the largest to be
1191 // expected resampling block output!
1192 size_t clipped = 0;
1193 debug("conv");
1194 check_fatal_syn("buffer resample conv", syn123_conv( audio, encoding, outsamples*pcmframe
1195 , resaudio, MPG123_ENC_FLOAT_32, outsamples*sizeof(float)*channels
1196 , NULL, &clipped, waver ));
1197 if(verbose > 1 && clipped)
1198 fprintf(stderr, ME ": clipped %zu samples\n", clipped);
1199 // Finally, some output!
1200 push_output(audio, outsamples);
1201 // Advance.
1202 insamples -= inblock;
1203 inoff += inblock;
1204 if(intflag)
1205 return 1; // 1 or 0? Does it matter?
1206 }
1207 } else
1208 {
1209 // Just handle explicit clipping and do final conversion.
1210 if(do_clip)
1211 clip(mixaudio, mixenc, got_samples);
1212 // Finally, convert to output.
1213 size_t clipped = 0;
1214 check_fatal_syn("buffer conv", syn123_conv( audio, encoding, got_samples*pcmframe
1215 , mixaudio, mixenc, got_samples*mixframe, NULL, &clipped, waver ));
1216 if(verbose > 1 && clipped)
1217 fprintf(stderr, ME ": clipped %zu samples\n", clipped);
1218 }
1219 } else
1220 {
1221 size_t clipped = 0;
1222 // No separate mixing buffer, but possibly some mixing or conversion
1223 // directly into the output buffer. This path should only be taken if
1224 // there is either only one operation happening, or if the output
1225 // is in floating point encoding which makes intermediate conversions
1226 // unnecessary. The path should still be correct for other cases, just
1227 // slow and with suboptimal quality.
1228 if(inaudio != audio)
1229 {
1230 if(mixmat)
1231 {
1232 check_fatal_syn("direct mix", syn123_mix( audio, encoding, channels
1233 , inaudio, inputenc, inputch, mixmat, got_samples, TRUE, &clipped
1234 , waver ));
1235 } else
1236 {
1237 check_fatal_syn("direct conv", syn123_conv( audio, encoding, got_samples*pcmframe
1238 , inaudio, inputenc, got_samples*pcminframe, NULL, &clipped
1239 , waver ));
1240 }
1241 }
1242 if(do_preamp)
1243 {
1244 check_fatal_syn("preamp", syn123_amp (audio, encoding, got_samples*channels
1245 , preamp_factor, preamp_offset, &clipped, waver ));
1246 if(verbose > 1 && clipped)
1247 fprintf(stderr, ME ": clipped %zu samples\n", clipped);
1248 }
1249 if(do_clip)
1250 clip(audio, encoding, got_samples);
1251 }
1252
1253 // To recap: Without any conversion, the simplest code path has
1254 // optional preamp and possibly clipping, but only one of those.
1255 // The next stage is a combination, mixing or conversion before
1256 // those. If resampling is desired, the complex
1257 // code path with mixing buffer and resampling buffer is chosen, with
1258 // all the other bits. Soft clipping is probably a default with resampling,
1259 // for integer output. Also, if soft clip and preamp is desired for
1260 // integer output, this also triggers the complex code path. I rather
1261 // create separate copies than to convert from/to integers multiple
1262 // times.
1263
1264 // The resampling loop triggers its own output. All other
1265 // paths want output of the prepared data here.
1266 if(!do_resample)
1267 push_output(audio, got_samples);
1268 offset += got_samples;
1269 return 1;
1270 }
1271
1272 #if !defined(WIN32) && !defined(GENERIC)
1273 static void catch_interrupt(int sig)
1274 {
1275 intflag = TRUE;
1276 }
1277 #endif
1278
1279 static void *fatal_malloc(size_t bytes)
1280 {
1281 void *buf;
1282 if(!(buf = malloc(bytes)))
1283 {
1284 if(!quiet)
1285 error("OOM");
1286 safe_exit(1);
1287 }
1288 return buf;
1289 }
1290
1291 // Prepare all the buffers and some of the flags for the processing chain.
1292 // The goal is to avoid unnecessary copies and format conversions.
1293 // If we do multiple operations on the intermediate data, it gets a separate
1294 // buffer in floating point encoding. If there is not much to do, this
1295 // intermediate copy is skipped.
1296 static void setup_processing(void)
1297 {
1298 pcminframe = out123_encsize(inputenc)*inputch;
1299 pcmframe = out123_encsize(encoding)*channels;
1300 size_t resample_out = 0;
1301
1302 unsigned int op_count = 0;
1303
1304 // Full mixing is initiated if channel counts differ or a non-empty
1305 // mixing matrix has been specified.
1306 if(inputch != channels || (mixmat_string && mixmat_string[0]))
1307 {
1308 mixmat = fatal_malloc(sizeof(double)*inputch*channels);
1309 size_t mmcount = (mixmat_string && mixmat_string[0])
1310 ? mytok_count(mixmat_string)
1311 : 0;
1312 // Special cases of trivial down/upmixing need no user input.
1313 if(mmcount == 0 && inputch == 1)
1314 {
1315 for(int oc=0; oc<channels; ++oc)
1316 mixmat[oc] = 1.;
1317 }
1318 else if(mmcount == 0 && channels == 1)
1319 {
1320 for(int ic=0; ic<inputch; ++ic)
1321 mixmat[ic] = 1./inputch;
1322 }
1323 else if(mmcount != inputch*channels)
1324 {
1325 merror( "Need %i mixing matrix entries, got %zu."
1326 , inputch*channels, mmcount );
1327 safe_exit(1);
1328 } else
1329 {
1330 char *next = mixmat_string;
1331 for(int i=0; i<inputch*channels; ++i)
1332 {
1333 char *tok = mytok(&next);
1334 mixmat[i] = tok ? atof(tok) : 0.;
1335 }
1336 }
1337 }
1338
1339 if(inputrate != outputrate)
1340 {
1341 do_resample = TRUE;
1342 ++op_count;
1343 // Buffer computations only make sense if resampling possibly can
1344 // be set up at all.
1345 if( inputrate > syn123_resample_maxrate()
1346 || outputrate > syn123_resample_maxrate() )
1347 {
1348 error("Sampling rates out of range for the resampler.");
1349 safe_exit(134);
1350 }
1351 // Settle resampling block size, which determines the resampling
1352 // buffer size.
1353 // First attempt: Try if pcmblock input yields a reasonable
1354 // number of samples/frames for the resampling output.
1355 // if it is too large, reduce until we are below a threshold.
1356 // How big should the threshold be? When I say 10 times pcmblock ...
1357 // I still to the channel count as variable ... but let's say that.
1358 // Then the baddest resampling ratio that could barely work is about
1359 // 1:10000. That's quite generous. Even fixing things at pcmblock
1360 // max in the output would still be ok for factors close to 1000.
1361 // Well, let's try this to stay reasonable: limit to 10*pcmblock
1362 // at first. If that is not enough, reduce resample_block down to
1363 // 100 samples or so, to avoid too degenerate situations. If
1364 // that lower limit of resample_block is reached and still produces
1365 // more than 10*pcmblock output, it is attempted to just allocate
1366 // that bigger buffer. If you have the memory, you can choose the
1367 // rates. Reasonable resampling tasks should be well within the
1368 // factor 10 allowed for by default.
1369 // Another question: Should I try to increase pcmblock for the case of
1370 // extreme downsampling? This is just a matter of efficiency, having
1371 // lots of code only producing a single sample now and then, the
1372 // block treatment in libsyn123 not getting active.
1373 resample_block = pcmblock;
1374 while( (resample_out = syn123_resample_count( inputrate, outputrate
1375 , resample_block )) > 10*pcmblock)
1376 resample_block /= 2;
1377 if(resample_block < 128)
1378 {
1379 resample_block = 128;
1380 resample_out = syn123_resample_count(inputrate, outputrate, resample_block);
1381 }
1382 if(verbose)
1383 fprintf( stderr, ME": resampling %zu samples @ %ld Hz"
1384 " to up to %zu samples @ %ld Hz\n", resample_block, inputrate
1385 , resample_out, outputrate );
1386 if(!resample_out)
1387 {
1388 error("Cannot compute resampler output count.");
1389 safe_exit(134);
1390 }
1391 // Now we got some number of resampler output samples.
1392 // That buffer will always be big enough when handing in resample_block.
1393 resaudio = fatal_malloc(resample_out*sizeof(float)*channels);
1394 }
1395
1396 audio = fatal_malloc((resample_out>pcmblock ? resample_out : pcmblock)*pcmframe);
1397
1398 // If converting or mixing, use separate input buffer.
1399 if(inputenc != encoding || mixmat)
1400 {
1401 inaudio = fatal_malloc(pcmblock*pcminframe);
1402 ++op_count; // conversion or mixing
1403 }
1404 else
1405 inaudio = audio;
1406
1407 if(preamp != 0. || preamp_offset != 0.)
1408 {
1409 preamp_factor = syn123_db2lin(preamp);
1410 // Store limited value for proper reporting.
1411 preamp = syn123_lin2db(preamp_factor);
1412 if(preamp_offset == 0. && mixmat)
1413 {
1414 // If we are mixing already, just include preamp in this.
1415 for(int i=0; i<inputch*channels; ++i)
1416 mixmat[i] *= preamp_factor;
1417 preamp_factor = 1.;
1418 }
1419 do_preamp = TRUE;
1420 ++op_count;
1421 }
1422
1423 do_clip = FALSE;
1424 if(!strcasecmp(clip_mode, "soft"))
1425 {
1426 do_clip = TRUE;
1427 soft_clip = TRUE;
1428 }
1429 else if(!strcasecmp(clip_mode, "hard"))
1430 {
1431 if(encoding & MPG123_ENC_FLOAT)
1432 do_clip = TRUE;
1433 soft_clip = FALSE;
1434 }
1435 else if(strcasecmp(clip_mode, "implicit"))
1436 {
1437 if(!quiet)
1438 error1("Bad value for clipping mode given: %s\n", clip_mode);
1439 safe_exit(135);
1440 }
1441 if(do_clip)
1442 ++op_count;
1443
1444 if(filter_spec && *filter_spec)
1445 {
1446 do_filter = TRUE;
1447 ++op_count;
1448 }
1449
1450 if(do_filter || do_resample || op_count > 1)
1451 {
1452 // Create a separate mixing buffer for the complicated cases.
1453 // Resampling limits quality to 32 bits float anyway, so avoid
1454 // trying to mix in double.
1455 debug("employing separate mixing buffer");
1456 mixenc = do_resample
1457 ? MPG123_ENC_FLOAT_32
1458 : syn123_mixenc(inputenc, encoding);
1459 mixframe = out123_encsize(mixenc)*channels;
1460 mixaudio = fatal_malloc(mixframe*pcmblock);
1461 }
1462 }
1463
1464
1465 int main(int sys_argc, char ** sys_argv)
1466 {
1467 int result;
1468 INT123_compat_binmode(STDIN_FILENO, TRUE);
1469 check_locale();
1470
1471 #if defined (WANT_WIN32_UNICODE)
1472 if(win32_cmdline_utf8(&argc, &argv) != 0)
1473 {
1474 error("Cannot convert command line to UTF8!");
1475 safe_exit(76);
1476 }
1477 #else
1478 argv = sys_argv;
1479 argc = sys_argc;
1480 #endif
1481
1482 if(!(fullprogname = INT123_compat_strdup(argv[0])))
1483 {
1484 error("OOM"); /* Out Of Memory. Don't waste bytes on that error. */
1485 safe_exit(1);
1486 }
1487 /* Extract binary and path, take stuff before/after last / or \ . */
1488 if( (cmd_name = strrchr(fullprogname, '/'))
1489 || (cmd_name = strrchr(fullprogname, '\\')))
1490 {
1491 /* There is some explicit path. */
1492 cmd_name[0] = 0; /* End byte for path. */
1493 cmd_name++;
1494 binpath = fullprogname;
1495 }
1496 else
1497 {
1498 cmd_name = fullprogname; /* No path separators there. */
1499 binpath = NULL; /* No path at all. */
1500 }
1501
1502 /* Get default flags. */
1503 {
1504 out123_handle *paro = out123_new();
1505 out123_getparam_int(paro, OUT123_FLAGS, &outflags);
1506 out123_del(paro);
1507 }
1508
1509 #ifdef __OS2__
1510 _wildcard(&argc,&argv);
1511 #endif
1512
1513 while ((result = getlopt(argc, argv, opts)))
1514 switch (result) {
1515 case GLO_UNKNOWN:
1516 fprintf (stderr, ME": invalid argument: %s\n", loptarg);
1517 usage(1);
1518 case GLO_NOARG:
1519 fprintf (stderr, ME": missing argument for parameter: %s\n", loptarg);
1520 usage(1);
1521 case GLO_BADARG:
1522 fprintf(stderr, ME": bad option argument: %s\n", loptarg);
1523 usage(1);
1524 }
1525 finish_endian();
1526 mdebug("input byte flags: %ld, output byte flags: %ld", byte_in_flags, byte_out_flags);
1527
1528 if(quiet)
1529 verbose = 0;
1530
1531 switch(runmode)
1532 {
1533 case RUN_MAIN:
1534 break;
1535 case LIST_MODULES:
1536 list_output_modules();
1537 case LIST_DEVICES:
1538 list_output_devices();
1539 default:
1540 merror("Invalid run mode %d", runmode);
1541 safe_exit(79);
1542 }
1543
1544 if(inputrate < 1)
1545 inputrate = rate;
1546 outputrate = -1;
1547 if(speed > 0)
1548 { // Compute rate from speed and safely convert to long.
1549 double outputrater = (1./speed) * rate;
1550 double limit = (double)syn123_resample_maxrate();
1551 double diff = 1;
1552 // LONG_MAX will likely be rounded in the conversion to float.
1553 // Ensure that the resulting limit is _smaller_ by subtracting
1554 // until we see a rounding step down.
1555 while((limit-diff) == limit)
1556 diff *= 2;
1557 limit -= diff;
1558 if(outputrater+0.5 > limit)
1559 outputrate = syn123_resample_maxrate();
1560 else if(outputrater > 0)
1561 outputrate = (long)(outputrater+0.5);
1562 }
1563 if(outputrate < 1)
1564 {
1565 fprintf(stderr, ME": no valid output rate with given speed and input rate\n");
1566 exit(1);
1567 }
1568 /* Ensure cleanup before we cause too much mess. */
1569 #if !defined(WIN32) && !defined(GENERIC)
1570 INT123_catchsignal(SIGINT, catch_interrupt);
1571 INT123_catchsignal(SIGTERM, catch_interrupt);
1572 #endif
1573 ao = out123_new();
1574 if(!ao){ error("Failed to allocate output."); exit(1); }
1575
1576 if
1577 ( 0
1578 || out123_param_int(ao, OUT123_FLAGS, outflags)
1579 || out123_param_float(ao, OUT123_PRELOAD, preload)
1580 || out123_param_int(ao, OUT123_VERBOSE, verbose)
1581 || out123_param_string(ao, OUT123_NAME, name)
1582 || out123_param_string(ao, OUT123_BINDIR, binpath)
1583 || out123_param_float(ao, OUT123_DEVICEBUFFER, device_buffer)
1584 )
1585 {
1586 error("Error setting output parameters. Do you need a usage reminder?");
1587 usage(1);
1588 }
1589
1590 #ifdef HAVE_SETPRIORITY
1591 if(aggressive) { /* tst */
1592 int mypid = getpid();
1593 if(!quiet) fprintf(stderr, ME": Aggressively trying to increase priority.\n");
1594 if(setpriority(PRIO_PROCESS,mypid,-20))
1595 error("Failed to aggressively increase priority.\n");
1596 }
1597 #endif
1598
1599 #if defined (HAVE_SCHED_SETSCHEDULER) && !defined (__CYGWIN__) && !defined (HAVE_WINDOWS_H)
1600 /* Cygwin --realtime seems to fail when accessing network, using win32 set priority instead */
1601 /* MinGW may have pthread installed, we prefer win32API */
1602 if(realtime)
1603 { /* Get real-time priority */
1604 struct sched_param sp;
1605 if(!quiet) fprintf(stderr, ME": Getting real-time priority\n");
1606 memset(&sp, 0, sizeof(struct sched_param));
1607 sp.sched_priority = sched_get_priority_min(SCHED_FIFO);
1608 if (sched_setscheduler(0, SCHED_RR, &sp) == -1)
1609 error("Can't get realtime priority\n");
1610 }
1611 #endif
1612
1613 /* make sure not Cygwin, it doesn't need it */
1614 #if defined(WIN32) && defined(HAVE_WINDOWS_H)
1615 /* argument "3" is equivalent to realtime priority class */
1616 win32_set_priority(realtime ? 3 : w32_priority);
1617 #endif
1618
1619 if(encoding_name)
1620 {
1621 encoding = out123_enc_byname(encoding_name);
1622 if(encoding < 0)
1623 {
1624 error1("Unknown encoding '%s' given!\n", encoding_name);
1625 safe_exit(1);
1626 }
1627 }
1628
1629 if(strcmp(signal_source, "file"))
1630 {
1631 had_something = 1;
1632 generate = TRUE;
1633 }
1634 else
1635 input = open_next_file(argc, argv, 1);
1636
1637 // Genererally generate signal in floating point for later conversion
1638 // after possible additional processing.
1639 inputenc = generate && encoding != MPG123_ENC_FLOAT_64
1640 ? MPG123_ENC_FLOAT_32
1641 : encoding;
1642 // User can override.
1643 if(inputenc_name)
1644 {
1645 inputenc = out123_enc_byname(inputenc_name);
1646 if(inputenc < 0)
1647 {
1648 error1("Unknown input encoding '%s' given!\n", inputenc_name);
1649 safe_exit(1);
1650 }
1651 }
1652 if(!inputch)
1653 inputch = channels;
1654
1655 // Settle buffers and the mixing/conversion code path.
1656 setup_processing();
1657 check_fatal_output(out123_set_buffer(ao, buffer_kb*1024));
1658 check_fatal_output(out123_open(ao, driver, device));
1659
1660 if(timelimit_seconds >= 0.)
1661 timelimit = timelimit_seconds*inputrate;
1662 if(timelimit_samples >= 0)
1663 timelimit = timelimit_samples;
1664
1665 if(verbose)
1666 {
1667 long props = 0;
1668 const char *encname;
1669 char *realname = NULL;
1670 if(inaudio != audio)
1671 {
1672 encname = out123_enc_name(inputenc);
1673 fprintf( stderr, ME": input format: %li Hz, %i channels, %s\n"
1674 , inputrate, inputch, encname ? encname : "???" );
1675 encname = out123_enc_name(syn123_mixenc(inputenc, encoding));
1676 if(mixmat)
1677 {
1678 fprintf( stderr, ME": mixing in %s\n", encname ? encname : "???" );
1679 for(int oc=0; oc<channels; ++oc)
1680 {
1681 fprintf(stderr, ME": out ch %i mix:", oc);
1682 for(int ic=0; ic<inputch; ++ic)
1683 fprintf(stderr, " %6.2f", mixmat[SYN123_IOFF(oc,ic,inputch)]);
1684 fprintf(stderr, "\n");
1685 }
1686 }
1687 else
1688 fprintf( stderr, ME": converting via %s\n", encname ? encname : "???" );
1689 }
1690 if(outputrate != rate)
1691 fprintf( stderr, ME": virtual output rate %li for actual speed %g\n"
1692 , outputrate, (double)rate/outputrate );
1693 encname = out123_enc_name(encoding);
1694 fprintf(stderr, ME": format: %li Hz, %i channels, %s\n"
1695 , rate, channels, encname ? encname : "???" );
1696 if(preamp != 0.)
1697 fprintf( stderr, ME": preamp: %.1f dB%s\n", preamp
1698 , preamp_factor != 1. ? "" : " (during mixing)" );
1699 if(preamp_offset != 0.)
1700 fprintf(stderr, ME": applying scaled offset: %f\n", preamp_offset);
1701 out123_getparam_string(ao, OUT123_NAME, &realname);
1702 if(realname)
1703 fprintf(stderr, ME": output real name: %s\n", realname);
1704 out123_getparam_int(ao, OUT123_PROPFLAGS, &props);
1705 if(props & OUT123_PROP_LIVE)
1706 fprintf(stderr, ME": This is a live sink.\n");
1707 }
1708 check_fatal_output(out123_start(ao, rate, channels, encoding));
1709
1710 setup_wavegen(); // Used also for conversion/mixing.
1711
1712 do
1713 {
1714 while(play_frame() && !intflag)
1715 {
1716 /* be happy */
1717 }
1718 if(!intflag && !generate && !just_stdin)
1719 {
1720 if(input && input != stdin)
1721 INT123_compat_fclose(input);
1722 input = open_next_file(argc, argv, 0);
1723 }
1724 } while(!intflag && !generate && input && !just_stdin);
1725 if(intflag) /* Make it quick! */
1726 {
1727 if(!quiet)
1728 fprintf(stderr, ME": Interrupted. Dropping the ball.\n");
1729 out123_drop(ao);
1730 }
1731
1732 safe_exit(had_something ? 0 : 2); /* That closes output and restores terminal, too. */
1733 return 0;
1734 }
1735
1736 static char* output_enclist(void)
1737 {
1738 int i;
1739 char *list = NULL;
1740 char *pos;
1741 size_t len = 0;
1742 int enc_count = 0;
1743 int *enc_codes = NULL;
1744
1745 enc_count = out123_enc_list(&enc_codes);
1746 if(enc_count < 0 || !enc_codes)
1747 return NULL;
1748 for(i=0;i<enc_count;++i)
1749 len += strlen(out123_enc_name(enc_codes[i]));
1750 len += enc_count;
1751
1752 if((pos = list = malloc(len))) for(i=0;i<enc_count;++i)
1753 {
1754 const char *name = out123_enc_name(enc_codes[i]);
1755 if(i>0)
1756 *(pos++) = ' ';
1757 strcpy(pos, name);
1758 pos+=strlen(name);
1759 }
1760 free(enc_codes);
1761 return list;
1762 }
1763
1764 static void print_title(FILE *o)
1765 {
1766 fprintf(o, "Simple audio output with raw PCM input\n");
1767 fprintf(o, "\tversion %s; derived from mpg123 by Michael Hipp and others\n", MPG123_VERSION);
1768 fprintf(o, "\tfree software (LGPL) without any warranty but with best wishes\n");
1769 }
1770
1771 static void usage(int err) /* print syntax & exit */
1772 {
1773 FILE* o = stdout;
1774 if(err)
1775 {
1776 o = stderr;
1777 fprintf(o, ME": You made some mistake in program usage... let me briefly remind you:\n\n");
1778 }
1779 print_title(o);
1780 fprintf(o,"\nusage: %s [option(s)] [file(s) | URL(s) | -]\n", cmd_name);
1781 fprintf(o,"supported options [defaults in brackets]:\n");
1782 fprintf(o," -v increase verbosity level -q quiet (only print errors)\n");
1783 fprintf(o," -t testmode (no output) -s write to stdout\n");
1784 fprintf(o," -w f write output as WAV file\n");
1785 fprintf(o," -b n output buffer: n Kbytes [0] \n");
1786 fprintf(o," -r n set samplerate [44100]\n");
1787 fprintf(o," -o m select output module -a d set audio device\n");
1788 fprintf(o," -m single-channel (mono) instead of stereo\n");
1789 #ifdef HAVE_SCHED_SETSCHEDULER
1790 fprintf(o," -T get realtime priority\n");
1791 #endif
1792 fprintf(o," -? this help --version print name + version\n");
1793 fprintf(o,"See the manpage out123(1) or call %s with --longhelp for more parameters and information.\n", cmd_name);
1794 safe_exit(err);
1795 }
1796
1797 static void want_usage(char* arg, topt *opts)
1798 {
1799 usage(0);
1800 }
1801
1802 static void long_usage(int err)
1803 {
1804 char *enclist;
1805 FILE* o = stdout;
1806 if(err)
1807 {
1808 o = stderr;
1809 fprintf(o, "You made some mistake in program usage... let me remind you:\n\n");
1810 }
1811 enclist = output_enclist();
1812 print_title(o);
1813 fprintf(o,"\nusage: %s [option(s)] [file(s) | -]\n", cmd_name);
1814
1815 fprintf(o," --name <n> set instance name (p.ex. JACK client)\n");
1816 fprintf(o," -o <o> --output <o> select audio output module\n");
1817 fprintf(o," --list-modules list the available modules\n");
1818 fprintf(o," --list-devices list the available output devices for given output module\n");
1819 fprintf(o," -a <d> --audiodevice <d> select audio device (for files, empty or - is stdout)\n");
1820 fprintf(o," -s --stdout write raw audio to stdout (-o raw -a -)\n");
1821 fprintf(o," -S --STDOUT play AND output stream to stdout\n");
1822 fprintf(o," -O <f> --outfile <f> raw output to given file (-o raw -a <f>)\n");
1823 fprintf(o," -w <f> --wav <f> write samples as WAV file in <f> (-o wav -a <f>)\n");
1824 fprintf(o," --au <f> write samples as Sun AU file in <f> (-o au -a <f>)\n");
1825 fprintf(o," --cdr <f> write samples as raw CD audio file in <f> (-o cdr -a <f>)\n");
1826 fprintf(o," -r <r> --rate <r> set the audio output rate in Hz (default 44100)\n");
1827 fprintf(o," -R <r> --inputrate <r> set intput rate in Hz for conversion (if > 0)\n"
1828 " (always last operation before output)\n");
1829 fprintf(o," --speed <f> play at given speed factor by resampling\n");
1830 fprintf(o," --resample <s> set resampler method (fine (default) or dirty)\n");
1831 fprintf(o," -c <n> --channels <n> set channel count to <n>\n");
1832 fprintf(o," -m --mono set output channel count to 1\n");
1833 fprintf(o," --stereo set output channel count to 2 (default)\n");
1834 fprintf(o," -C <n> --inputch <n> set input channel count for conversion\n");
1835 fprintf(o," -e <c> --encoding <c> set output encoding (%s)\n"
1836 , enclist != NULL ? enclist : "OOM!");
1837 fprintf(o," --endian <s> set output endianess: big, little, native (default)\n");
1838 fprintf(o," -E <c> --inputenc <c> set input encoding for conversion\n");
1839 fprintf(o," --inputend <s> set input endianess: big, little, or native\n");
1840 fprintf(o," (default being same as output endianess)\n");
1841 fprintf(o," --byteswap swap byte order at end (possibly again)\n");
1842 fprintf(o," --list-encodings list of encoding short and long names\n");
1843 fprintf(o," --mix <m> mixing matrix <m> between input and output channels\n");
1844 fprintf(o," as linear factors, comma separated list for output\n");
1845 fprintf(o," channel 1, then 2, ... default unity if channel counts\n");
1846 fprintf(o," match, 0.5,0.5 for stereo to mono, 1,1 for the other way\n");
1847 fprintf(o," --filter <coeff> apply filter(s) before preamp stage, with coeff as\n");
1848 fprintf(o," b_0,...,b_N,a_0,...,a_N (a_0=1 is mandatory) and\n");
1849 fprintf(o," multiple filters separated by ':'.\n");
1850 fprintf(o," -P <p> --preamp <p> amplify signal with <p> dB before output\n");
1851 fprintf(o," --offset <o> apply PCM offset (floating point scaled in [-1:1]");
1852 fprintf(o," --clip <s> select clipping mode: soft or hard for forced\n"
1853 " clipping also for floating point output, implicit\n"
1854 " (default) for implied clipping during conversion\n" );
1855 fprintf(o," --dither enable dithering for conversions to integer\n");
1856 fprintf(o," --test-format return 0 if audio format set by preceeding options is supported\n");
1857 fprintf(o," --test-encodings print out possible encodings with given channels/rate\n");
1858 fprintf(o," --query-format print out default format for given device, if any\n");
1859 fprintf(o," -o h --headphones (aix/hp/sun) output on headphones\n");
1860 fprintf(o," -o s --speaker (aix/hp/sun) output on speaker\n");
1861 fprintf(o," -o l --lineout (aix/hp/sun) output to lineout\n");
1862 #ifndef NOXFERMEM
1863 fprintf(o," -b <n> --buffer <n> set play buffer (\"output cache\")\n");
1864 fprintf(o," --preload <value> fraction of buffer to fill before playback\n");
1865 #endif
1866 fprintf(o," --devbuffer <s> set device buffer in seconds; <= 0 means default\n");
1867 fprintf(o," --timelimit <s> set time limit in PCM samples if >= 0\n");
1868 fprintf(o," --seconds <s> set time limit in seconds if >= 0\n");
1869 fprintf(o," --source <s> choose signal source: file (default),\n");
1870 fprintf(o," wave, sweep, pink, geiger (implied by\n");
1871 fprintf(o," --wave-freq, --wave-sweep,\n");
1872 fprintf(o," --pink-rows, --geiger-activity), or white for\n");
1873 fprintf(o," white noise\n");
1874 fprintf(o," --wave-freq <f> set wave generator frequency or list of those\n");
1875 fprintf(o," with comma separation for enabling a generated\n");
1876 fprintf(o," test signal instead of standard input,\n");
1877 fprintf(o," empty value repeating the previous\n");
1878 fprintf(o," --wave-pat <p> set wave pattern(s) (out of those:\n");
1879 {
1880 int i=0;
1881 const char* wn;
1882 while((wn=syn123_wave_name(i++)) && wn[0] != '?')
1883 fprintf(o, " %s\n", wn);
1884 }
1885 fprintf(o," ),\n");
1886 fprintf(o," empty value repeating the previous\n");
1887 fprintf(o," --wave-phase <p> set wave phase shift(s), negative values\n");
1888 fprintf(o," inverting the pattern in time and\n");
1889 fprintf(o," empty value repeating the previous,\n");
1890 fprintf(o," --wave-direction overriding the negative bit\n");
1891 fprintf(o," --wave-direction <d> set direction explicitly (the sign counts)\n");
1892 fprintf(o," --wave-sweep <f> sweep a generated wave to frequency f, from\n");
1893 fprintf(o," first one specified for --wave-freq, using the\n");
1894 fprintf(o," first wave pattern and direction, too\n");
1895 fprintf(o," --sweep-time <s> set frequency sweep duration to s seconds if > 0\n");
1896 fprintf(o," (defaulting to timelimit if set, otherwise one second)\n");
1897 fprintf(o," --sweep-count <c> set timelimit to exactly produce that many\n");
1898 fprintf(o," (smooth) sweeps\n");
1899 fprintf(o," --sweep-type <t> set sweep type: lin(ear), qua(d) (default),\n");
1900 fprintf(o," exp(onential)\n");
1901 fprintf(o," --sweep-hard disable post-sweep smoothing for periodicity\n");
1902 fprintf(o," --genbuffer <b> buffer size (limit) for signal generators,\n");
1903 fprintf(o," if > 0 (default), this enforces a periodic\n");
1904 fprintf(o," buffer also for non-periodic signals, benefit:\n");
1905 fprintf(o," less runtime CPU overhead\n");
1906 fprintf(o," --wave-limit <l> alias for --genbuffer\n");
1907 fprintf(o," --pink-rows <r> activate pink noise source and choose rows for\n");
1908 fprintf(o," ` the algorithm (<1 chooses default)\n");
1909 fprintf(o," --geiger-activity <a> a Geiger-Mueller counter as source, with\n");
1910 fprintf(o," <a> average events per second\n");
1911 fprintf(o," -t --test no output, just read and discard data (-o test)\n");
1912 fprintf(o," -v[*] --verbose increase verboselevel\n");
1913 fprintf(o," -q --quiet quiet mode\n");
1914 #ifdef HAVE_SETPRIORITY
1915 fprintf(o," --aggressive tries to get higher priority (nice)\n");
1916 #endif
1917 #if defined (HAVE_SCHED_SETSCHEDULER) || defined (HAVE_WINDOWS_H)
1918 fprintf(o," -T --realtime tries to get realtime priority\n");
1919 #endif
1920 #ifdef HAVE_WINDOWS_H
1921 fprintf(o," --priority <n> use specified process priority\n");
1922 fprintf(o," accepts -2 to 3 as integer arguments\n");
1923 fprintf(o," -2 as idle, 0 as normal and 3 as realtime.\n");
1924 #endif
1925 fprintf(o," -? --help give compact help\n");
1926 fprintf(o," --longhelp give this long help listing\n");
1927 fprintf(o," --version give name / version string\n");
1928
1929 fprintf(o,"\nSee the manpage out123(1) for more information. Also, note that\n");
1930 fprintf(o,"any numeric arguments are parsed in C locale (pi is 3.14, not 3,14).\n");
1931 free(enclist);
1932 safe_exit(err);
1933 }
1934
1935 static void want_long_usage(char* arg, topt *opts)
1936 {
1937 long_usage(0);
1938 }
1939
1940 static void give_version(char* arg, topt *opts)
1941 {
1942 fprintf(stdout, "out123 "MPG123_VERSION"\n");
1943 safe_exit(0);
1944 }
1945
1946 static void give_libversion(char* arg, topt *opts)
1947 {
1948 unsigned int pl = 0;
1949 unsigned int al = out123_libversion(&pl);
1950 printf("libout123 from mpg123 %s, API version %u, patchlevel %u\n", out123_distversion(NULL, NULL, NULL), al, pl);
1951 al = syn123_libversion(&pl);
1952 printf("libsyn123 from mpg123 %s, API version %u, patchlevel %u\n", syn123_distversion(NULL, NULL, NULL), al, pl);
1953 safe_exit(0);
1954 }
1955