-
Notifications
You must be signed in to change notification settings - Fork 2
/
performSox.c
3111 lines (2761 loc) · 102 KB
/
performSox.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "emscripten.h"
#include <sox.h>
#include "soxconfig.h"
#include "util.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#if defined(HAVE_WIN32_GLOB_H)
#include "win32-glob.h"
#define HAVE_GLOB_H 1
#elif defined(HAVE_GLOB_H)
#include <glob.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_SUN_AUDIOIO_H
#include <sun/audioio.h>
#define HAVE_AUDIOIO_H 1
#else
#ifdef HAVE_SYS_AUDIOIO_H
#include <sys/audioio.h>
#define HAVE_AUDIOIO_H 1
#endif
#endif
#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#define HAVE_SOUNDCARD_H 1
#else
#ifdef HAVE_MACHINE_SOUNDCARD_H
#include <machine/soundcard.h>
#define HAVE_SOUNDCARD_H 1
#endif
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETTIMEOFDAY
#define TIME_FRAC 1e6
#else
#define timeval timeb
#define gettimeofday(a,b) ftime(a)
#define tv_sec time
#define tv_usec millitm
#define TIME_FRAC 1e3
#endif
#if !defined(HAVE_CONIO_H) && !defined(HAVE_TERMIOS_H) && (defined(_MSC_VER) || defined(__MINGW32__))
#define HAVE_CONIO_H 1
#endif
#ifdef HAVE_CONIO_H
/* _kbhit and _getch */
#include <conio.h>
#undef HAVE_TERMIOS_H
#endif
/*#define MORE_INTERACTIVE 1*/
#define SOX_OPTS "SOX_OPTS"
static lsx_getopt_t optstate;
/* argv[0] options */
static char const * myname = NULL;
static enum {sox_sox, sox_play, sox_rec, sox_soxi} sox_mode;
/* gopts */
static enum {
sox_sequence, sox_concatenate, sox_mix, sox_mix_power,
sox_merge, sox_multiply, sox_default
} combine_method = sox_default;
static enum { sox_single, sox_multiple } output_method = sox_single;
#define is_serial(m) ((m) <= sox_concatenate)
#define is_parallel(m) (!is_serial(m))
static sox_bool no_clobber = sox_false, interactive = sox_false;
static sox_bool uservolume = sox_false;
typedef enum {RG_off, RG_track, RG_album, RG_default} rg_mode;
static lsx_enum_item const rg_modes[] = {
LSX_ENUM_ITEM(RG_,off)
LSX_ENUM_ITEM(RG_,track)
LSX_ENUM_ITEM(RG_,album)
{0, 0}};
static rg_mode replay_gain_mode = RG_default;
static sox_option_t show_progress = sox_option_default;
/* Input & output files */
typedef struct {
char * filename;
/* fopts */
char const * filetype;
sox_signalinfo_t signal;
sox_encodinginfo_t encoding;
double volume;
double replay_gain;
sox_oob_t oob;
sox_bool no_glob;
sox_format_t * ft; /* libSoX file descriptor */
uint64_t volume_clips;
rg_mode replay_gain_mode;
} file_t;
static file_t * * files = NULL; /* Array tracking input and output files */
#define ofile files[file_count - 1]
static size_t file_count = 0;
static size_t input_count = 0;
static size_t output_count = 0;
/* Effects */
/* We parse effects into a temporary effects table and then place into
* the real effects chain. This allows scanning all effects to give
* hints to what input effect options should be as well as determining
* when mixer or resample effects need to be auto-inserted as well.
*/
static sox_effect_t **user_efftab = NULL;
static size_t user_efftab_size = 0;
static sox_effects_chain_t *effects_chain = NULL;
static sox_effect_t *save_output_eff = NULL;
static struct { char *name; int argc; char **argv; size_t argv_size; } **user_effargs = NULL;
static size_t *user_effargs_size = NULL; /* array: size of user_effargs for each chain */
/* Size of memory structures related to effects arguments (user_effargs[i],
* user_effargs[i][j].argv) to be extended in steps of EFFARGS_STEP */
#define EFFARGS_STEP 8
static unsigned *nuser_effects = NULL; /* array: number of effects in each chain */
static int current_eff_chain = 0;
static int eff_chain_count = 0;
static sox_bool very_first_effchain = sox_true;
/* Indicates that not only the first effects chain is in effect (hrm), but
also that it has never been restarted. Only then we may use the
optimize_trim() hack. */
static char *effects_filename = NULL;
static char * play_rate_arg = NULL;
static char *norm_level = NULL;
/* Flowing */
static sox_signalinfo_t combiner_signal, ofile_signal_options;
static sox_encodinginfo_t combiner_encoding, ofile_encoding_options;
static uint64_t mixing_clips = 0;
static size_t current_input = 0;
static uint64_t input_wide_samples = 0;
static uint64_t read_wide_samples = 0;
static uint64_t output_samples = 0;
static sox_bool input_eof = sox_false;
static sox_bool output_eof = sox_false;
static sox_bool user_abort = sox_false;
static sox_bool user_skip = sox_false;
static sox_bool user_restart_eff = sox_false;
static int success = 0;
static int cleanup_called = 0;
static sox_sample_t omax[2], omin[2];
#ifdef HAVE_TERMIOS_H
#include <termios.h>
static struct termios original_termios;
static sox_bool original_termios_saved = sox_false;
#endif
static sox_bool stdin_is_a_tty, is_player, is_guarded, do_guarded_norm, no_dither, reported_sox_opts;
static void cleanup(void)
{
size_t i;
if (!success && !reported_sox_opts) {
char const * env_opts = getenv(SOX_OPTS);
if (env_opts && *env_opts)
lsx_report("used "SOX_OPTS"=%s", env_opts);
}
/* Close the input and output files before exiting. */
for (i = 0; i < input_count; i++) {
if (files[i]->ft) {
sox_close(files[i]->ft);
}
free(files[i]->filename);
free(files[i]);
}
if (file_count) {
if (ofile->ft) {
if (!success && ofile->ft->fp) { /* If we failed part way through */
struct stat st; /* writing a normal file, remove it. */
if (!stat(ofile->ft->filename, &st) &&
(st.st_mode & S_IFMT) == S_IFREG)
unlink(ofile->ft->filename);
}
sox_close(ofile->ft); /* Assume we can unlink a file before closing it. */
}
free(ofile->filename);
free(ofile);
}
free(files);
#ifdef HAVE_TERMIOS_H
if (original_termios_saved)
tcsetattr(fileno(stdin), TCSANOW, &original_termios);
#endif
free(user_efftab);
free(sox_globals.tmp_path);
sox_globals.tmp_path = NULL;
free(play_rate_arg);
free(effects_filename);
free(norm_level);
sox_quit();
cleanup_called = 1;
}
/* Cleanup atexit() function, hence always called. */
static void atexit_cleanup(void)
{
/* Do not call cleanup using atexit() if possible. pthread's can
* act unpredictable if called outside of main().
*/
if (!cleanup_called)
cleanup();
}
static char const * str_time(double seconds)
{
static char string[16][50];
static int i;
int hours, mins = seconds / 60;
seconds -= mins * 60;
hours = mins / 60;
mins -= hours * 60;
i = (i+1) & 15;
sprintf(string[i], "%02i:%02i:%05.2f", hours, mins, seconds);
return string[i];
}
static char const * size_and_bitrate(sox_format_t * ft, char const * * text)
{
struct stat st; /* ft->fp may validly be NULL, so stat not fstat */
if (stat(ft->filename, &st) || (st.st_mode & S_IFMT) != S_IFREG)
return NULL;
if (ft->signal.length && ft->signal.channels && ft->signal.rate && text) {
double secs = ft->signal.length / ft->signal.channels / ft->signal.rate;
*text = lsx_sigfigs3(8. * st.st_size / secs);
}
return lsx_sigfigs3((double)st.st_size);
}
static void play_file_info(sox_format_t * ft, file_t * f, sox_bool full)
{
FILE * const output = sox_mode == sox_soxi? stdout : stderr;
char const * text, * text2 = NULL;
char buffer[30];
uint64_t ws = ft->signal.length / ft->signal.channels;
(void)full;
fprintf(output, "\n");
if (ft->filename[0]) {
fprintf(output, "%s:", ft->filename);
if (strcmp(ft->filename, "-") == 0 || (ft->handler.flags & SOX_FILE_DEVICE))
fprintf(output, " (%s)", ft->handler.names[0]);
fprintf(output, "\n\n");
}
if ((text = size_and_bitrate(ft, &text2))) {
fprintf(output, " File Size: %-10s", text);
if (text2)
fprintf(output, "Bit Rate: %s", text2);
fprintf(output, "\n");
}
fprintf(output, " Encoding: %-14s", sox_encodings_info[ft->encoding.encoding].name);
text = sox_find_comment(f->ft->oob.comments, "Comment");
if (!text)
text = sox_find_comment(f->ft->oob.comments, "Description");
if (!text)
text = sox_find_comment(f->ft->oob.comments, "Year");
if (text)
fprintf(output, "Info: %s", text);
fprintf(output, "\n");
sprintf(buffer, " Channels: %u @ %u-bit", ft->signal.channels, ft->signal.precision);
fprintf(output, "%-25s", buffer);
text = sox_find_comment(f->ft->oob.comments, "Tracknumber");
if (text) {
fprintf(output, "Track: %s", text);
text = sox_find_comment(f->ft->oob.comments, "Tracktotal");
if (text)
fprintf(output, " of %s", text);
}
fprintf(output, "\n");
sprintf(buffer, "Samplerate: %gHz", ft->signal.rate);
fprintf(output, "%-25s", buffer);
text = sox_find_comment(f->ft->oob.comments, "Album");
if (text)
fprintf(output, "Album: %s", text);
fprintf(output, "\n");
if (f && f->replay_gain != HUGE_VAL){
sprintf(buffer, "%s gain: %+.1fdB", lsx_find_enum_value(f->replay_gain_mode, rg_modes)->text, f->replay_gain);
buffer[0] += 'A' - 'a';
fprintf(output, "%-24s", buffer);
} else
fprintf(output, "%-24s", "Replaygain: off");
text = sox_find_comment(f->ft->oob.comments, "Artist");
if (text)
fprintf(output, "Artist: %s", text);
fprintf(output, "\n");
fprintf(output, " Duration: %-13s", ft->signal.length? str_time((double)ws / ft->signal.rate) : "unknown");
text = sox_find_comment(f->ft->oob.comments, "Title");
if (text)
fprintf(output, "Title: %s", text);
fprintf(output, "\n\n");
}
static void display_file_info(sox_format_t * ft, file_t * f, sox_bool full)
{
static char const * const no_yes[] = {"no", "yes"};
FILE * const output = sox_mode == sox_soxi? stdout : stderr;
char const * filetype = lsx_find_file_extension(ft->filename);
sox_bool show_type = sox_true;
size_t i;
if (is_player && sox_globals.verbosity < 3) {
play_file_info(ft, f, full);
return;
}
fprintf(output, "\n%s: '%s'",
ft->mode == 'r'? "Input File " : "Output File ", ft->filename);
if (filetype) for (i = 0; ft->handler.names[i] && show_type; ++i)
if (!strcasecmp(filetype, ft->handler.names[i]))
show_type = sox_false;
if (show_type)
fprintf(output, " (%s)", ft->handler.names[0]);
fprintf(output, "\n");
fprintf(output,
"Channels : %u\n"
"Sample Rate : %g\n"
"Precision : %u-bit\n",
ft->signal.channels,
ft->signal.rate,
ft->signal.precision);
if (ft->signal.length && ft->signal.channels && ft->signal.rate) {
uint64_t ws = ft->signal.length / ft->signal.channels;
char const * text, * text2 = NULL;
fprintf(output,
"Duration : %s = %" PRIu64 " samples %c %g CDDA sectors\n",
str_time((double)ws / ft->signal.rate),
ws, "~="[ft->signal.rate == 44100],
(double)ws / ft->signal.rate * 44100 / 588);
if (ft->mode == 'r' && (text = size_and_bitrate(ft, &text2))) {
fprintf(output, "File Size : %s\n", text);
if (text2)
fprintf(output, "Bit Rate : %s\n", text2);
}
}
if (ft->encoding.encoding) {
char buffer[20] = {'\0'};
if (ft->encoding.bits_per_sample)
sprintf(buffer, "%u-bit ", ft->encoding.bits_per_sample);
fprintf(output, "Sample Encoding: %s%s\n", buffer,
sox_encodings_info[ft->encoding.encoding].desc);
}
if (full) {
if (ft->encoding.bits_per_sample > 8 || (ft->handler.flags & SOX_FILE_ENDIAN))
fprintf(output, "Endian Type : %s\n",
ft->encoding.reverse_bytes != MACHINE_IS_BIGENDIAN ? "big" : "little");
if (ft->encoding.bits_per_sample)
fprintf(output,
"Reverse Nibbles: %s\n"
"Reverse Bits : %s\n",
no_yes[ft->encoding.reverse_nibbles],
no_yes[ft->encoding.reverse_bits]);
}
if (f && f->replay_gain != HUGE_VAL)
fprintf(output, "Replay gain : %+g dB (%s)\n" , f->replay_gain,
lsx_find_enum_value(f->replay_gain_mode, rg_modes)->text);
if (f && f->volume != HUGE_VAL)
fprintf(output, "Level adjust : %g (linear gain)\n" , f->volume);
if (!(ft->handler.flags & SOX_FILE_DEVICE) && ft->oob.comments) {
if (sox_num_comments(ft->oob.comments) > 1) {
sox_comments_t p = ft->oob.comments;
fprintf(output, "Comments : \n");
do fprintf(output, "%s\n", *p);
while (*++p);
}
else fprintf(output, "Comment : '%s'\n", ft->oob.comments[0]);
}
fprintf(output, "\n");
}
static void report_file_info(file_t * f)
{
if (sox_globals.verbosity > 2)
display_file_info(f->ft, f, sox_true);
}
static void progress_to_next_input_file(file_t * f, sox_effect_t * effp)
{
if (user_skip) {
user_skip = sox_false;
fprintf(stderr, "\nSkipped (Ctrl-C twice to quit).\n");
}
read_wide_samples = 0;
input_wide_samples = f->ft->signal.length / f->ft->signal.channels;
if (show_progress && (sox_globals.verbosity < 3 ||
(is_serial(combine_method) && input_count > 1)))
display_file_info(f->ft, f, sox_false);
if (f->volume == HUGE_VAL)
f->volume = 1;
if (f->replay_gain != HUGE_VAL)
f->volume *= pow(10.0, f->replay_gain / 20);
if (effp && f->volume != floor(f->volume))
effp->out_signal.precision = SOX_SAMPLE_PRECISION;
f->ft->sox_errno = errno = 0;
}
/* Read up to max `wide' samples. A wide sample contains one sample per channel
* from the input audio. */
static size_t sox_read_wide(sox_format_t * ft, sox_sample_t * buf, size_t max)
{
size_t len = max / combiner_signal.channels;
len = sox_read(ft, buf, len * ft->signal.channels) / ft->signal.channels;
if (!len && ft->sox_errno)
lsx_fail("`%s' %s: %s",
ft->filename, ft->sox_errstr, sox_strerror(ft->sox_errno));
return len;
}
static void balance_input(sox_sample_t * buf, size_t ws, file_t * f)
{
size_t s = ws * f->ft->signal.channels;
if (f->volume != 1) while (s--) {
double d = f->volume * *buf;
*buf++ = SOX_ROUND_CLIP_COUNT(d, f->volume_clips);
}
}
/* The input combiner: contains one sample buffer per input file, but only
* needed if is_parallel(combine_method) */
typedef struct {
sox_sample_t * * ibuf;
size_t * ilen;
} input_combiner_t;
static int combiner_start(sox_effect_t *effp)
{
input_combiner_t * z = (input_combiner_t *) effp->priv;
uint64_t ws;
size_t i;
if (is_serial(combine_method))
progress_to_next_input_file(files[current_input], effp);
else {
ws = 0;
z->ibuf = lsx_malloc(input_count * sizeof(*z->ibuf));
for (i = 0; i < input_count; i++) {
z->ibuf[i] = lsx_malloc(sox_globals.bufsiz * sizeof(sox_sample_t));
progress_to_next_input_file(files[i], effp);
ws = max(ws, input_wide_samples);
}
input_wide_samples = ws; /* Output length is that of longest input file. */
}
z->ilen = lsx_malloc(input_count * sizeof(*z->ilen));
return SOX_SUCCESS;
}
static sox_bool can_segue(size_t i)
{
return
files[i]->ft->signal.channels == files[i - 1]->ft->signal.channels &&
files[i]->ft->signal.rate == files[i - 1]->ft->signal.rate;
}
static int combiner_drain(sox_effect_t *effp, sox_sample_t * obuf, size_t * osamp)
{
input_combiner_t * z = (input_combiner_t *) effp->priv;
size_t ws, s, i;
size_t olen = 0;
if (is_serial(combine_method)) {
while (sox_true) {
if (!user_skip)
olen = sox_read_wide(files[current_input]->ft, obuf, *osamp);
if (olen == 0) { /* If EOF, go to the next input file. */
if (++current_input < input_count) {
if (combine_method == sox_sequence && !can_segue(current_input))
break;
progress_to_next_input_file(files[current_input], NULL);
continue;
}
}
balance_input(obuf, olen, files[current_input]);
break;
} /* while */
} /* is_serial */ else { /* else is_parallel() */
sox_sample_t * p = obuf;
for (i = 0; i < input_count; ++i) {
z->ilen[i] = sox_read_wide(files[i]->ft, z->ibuf[i], *osamp);
balance_input(z->ibuf[i], z->ilen[i], files[i]);
olen = max(olen, z->ilen[i]);
}
for (ws = 0; ws < olen; ++ws) { /* wide samples */
if (combine_method == sox_mix || combine_method == sox_mix_power) {
for (s = 0; s < effp->in_signal.channels; ++s, ++p) { /* sum samples */
*p = 0;
for (i = 0; i < input_count; ++i)
if (ws < z->ilen[i] && s < files[i]->ft->signal.channels) {
/* Cast to double prevents integer overflow */
double sample = *p + (double)z->ibuf[i][ws * files[i]->ft->signal.channels + s];
*p = SOX_ROUND_CLIP_COUNT(sample, mixing_clips);
}
}
} /* sox_mix */ else if (combine_method == sox_multiply) {
for (s = 0; s < effp->in_signal.channels; ++s, ++p) { /* multiply samples */
i = 0;
*p = ws < z->ilen[i] && s < files[i]->ft->signal.channels?
z->ibuf[i][ws * files[i]->ft->signal.channels + s] : 0;
for (++i; i < input_count; ++i) {
double sample = *p * (-1. / SOX_SAMPLE_MIN) * (ws < z->ilen[i] && s < files[i]->ft->signal.channels? z->ibuf[i][ws * files[i]->ft->signal.channels + s] : 0);
*p = SOX_ROUND_CLIP_COUNT(sample, mixing_clips);
}
}
} /* sox_multiply */ else { /* sox_merge: like a multi-track recorder */
for (i = 0; i < input_count; ++i)
for (s = 0; s < files[i]->ft->signal.channels; ++s)
*p++ = (ws < z->ilen[i]) * z->ibuf[i][ws * files[i]->ft->signal.channels + s];
} /* sox_merge */
} /* wide samples */
} /* is_parallel */
read_wide_samples += olen;
olen *= effp->in_signal.channels;
*osamp = olen;
input_eof = olen ? sox_false : sox_true;
if (input_eof && is_parallel(combine_method))
current_input += input_count;
return olen? SOX_SUCCESS : SOX_EOF;
}
static int combiner_stop(sox_effect_t *effp)
{
input_combiner_t * z = (input_combiner_t *) effp->priv;
size_t i;
if (is_parallel(combine_method)) {
/* Free input buffers now that they are not used */
for (i = 0; i < input_count; i++)
free(z->ibuf[i]);
free(z->ibuf);
}
free(z->ilen);
return SOX_SUCCESS;
}
static sox_effect_handler_t const * input_combiner_effect_fn(void)
{
static sox_effect_handler_t handler = { "input", 0, SOX_EFF_MCHAN |
SOX_EFF_MODIFY, 0, combiner_start, 0, combiner_drain,
combiner_stop, 0, sizeof(input_combiner_t)
};
return &handler;
}
static int ostart(sox_effect_t *effp)
{
unsigned prec = effp->out_signal.precision;
if (effp->in_signal.mult && effp->in_signal.precision > prec)
*effp->in_signal.mult *= 1 - (1 << (31 - prec)) * (1. / SOX_SAMPLE_MAX);
return SOX_SUCCESS;
}
static int output_flow(sox_effect_t *effp, sox_sample_t const * ibuf,
sox_sample_t * obuf, size_t * isamp, size_t * osamp)
{
size_t len;
(void)effp, (void)obuf;
if (show_progress) for (len = 0; len < *isamp; len += effp->in_signal.channels) {
omax[0] = max(omax[0], ibuf[len]);
omin[0] = min(omin[0], ibuf[len]);
if (effp->in_signal.channels > 1) {
omax[1] = max(omax[1], ibuf[len + 1]);
omin[1] = min(omin[1], ibuf[len + 1]);
}
else {
omax[1] = omax[0];
omin[1] = omin[0];
}
}
*osamp = 0;
len = *isamp? sox_write(ofile->ft, ibuf, *isamp) : 0;
output_samples += len / ofile->ft->signal.channels;
output_eof = (len != *isamp) ? sox_true: sox_false;
if (len != *isamp) {
if (ofile->ft->sox_errno)
lsx_fail("`%s' %s: %s", ofile->ft->filename,
ofile->ft->sox_errstr, sox_strerror(ofile->ft->sox_errno));
return SOX_EOF;
}
return SOX_SUCCESS;
}
static sox_effect_handler_t const * output_effect_fn(void)
{
static sox_effect_handler_t handler = {"output", 0, SOX_EFF_MCHAN |
SOX_EFF_MODIFY | SOX_EFF_PREC, NULL, ostart, output_flow, NULL, NULL, NULL, 0
};
return &handler;
}
static void auto_effect(sox_effects_chain_t *, char const *, int, char **,
sox_signalinfo_t *, int *);
static int add_effect(sox_effects_chain_t * chain, sox_effect_t * effp,
sox_signalinfo_t * in, sox_signalinfo_t const * out, int * guard) {
int no_guard = -1;
switch (*guard) {
case 0: if (!(effp->handler.flags & SOX_EFF_GAIN)) {
char * arg = "-h";
auto_effect(chain, "gain", 1, &arg, in, &no_guard);
++*guard;
}
break;
case 1: if (effp->handler.flags & SOX_EFF_GAIN) {
char * arg = "-r";
auto_effect(chain, "gain", 1, &arg, in, &no_guard);
--*guard;
}
break;
case 2: if (!(effp->handler.flags & SOX_EFF_MODIFY)) {
lsx_warn("%s: effects that modify audio should not follow dither",
effp->handler.name);
}
break;
}
return sox_add_effect(chain, effp, in, out);
}
static void auto_effect(sox_effects_chain_t *chain, char const *name, int argc,
char *argv[], sox_signalinfo_t *signal, int * guard)
{
sox_effect_t * effp;
effp = sox_create_effect(sox_find_effect(name)); /* Should always succeed. */
if (sox_effect_options(effp, argc, argv) == SOX_EOF)
exit(1); /* The failing effect should have displayed an error message */
if (add_effect(chain, effp, signal, &ofile->ft->signal, guard) != SOX_SUCCESS)
exit(2); /* The effects chain should have displayed an error message */
free(effp);
}
/* add_eff_chain() - NOTE: this only adds memory for one
* additional effects chain beyond value of eff_chain_count. It
* does not unconditionally increase size of effects chain.
*/
static void add_eff_chain(void)
{
lsx_revalloc(user_effargs, eff_chain_count+1);
user_effargs[eff_chain_count] = lsx_malloc(sizeof(**user_effargs));
lsx_revalloc(user_effargs_size, eff_chain_count+1);
user_effargs_size[eff_chain_count] = 0;
lsx_revalloc(nuser_effects, eff_chain_count+1);
nuser_effects[eff_chain_count] = 0;
} /* add_eff_chain */
/* free_eff_chain() - the inverse of add_eff_chain(). Frees
* one effects chain (with index eff_chain_count) such that
* there are eff_chain_count left, the last having index
* eff_chain_count-1.
*/
static void free_eff_chain(void)
{
unsigned j;
int k;
for (j = 0; j < nuser_effects[eff_chain_count]; j++)
{
free(user_effargs[eff_chain_count][j].name);
user_effargs[eff_chain_count][j].name = NULL;
for (k = 0; k < user_effargs[eff_chain_count][j].argc; k++)
{
free(user_effargs[eff_chain_count][j].argv[k]);
user_effargs[eff_chain_count][j].argv[k] = NULL;
}
user_effargs[eff_chain_count][j].argc = 0;
free(user_effargs[eff_chain_count][j].argv);
user_effargs[eff_chain_count][j].argv = NULL;
user_effargs[eff_chain_count][j].argv_size = 0;
}
nuser_effects[eff_chain_count] = 0;
free(user_effargs[eff_chain_count]);
} /* free_eff_chain */
static void delete_eff_chains(void)
{
while (eff_chain_count > 0) {
eff_chain_count--;
free_eff_chain();
}
free(user_effargs);
free(user_effargs_size);
free(nuser_effects);
user_effargs = NULL;
user_effargs_size = NULL;
nuser_effects = NULL;
} /* delete_eff_chains */
static sox_bool is_pseudo_effect(const char *s)
{
if (s)
if (strcmp("newfile", s) == 0 ||
strcmp("restart", s) == 0 ||
strcmp(":", s) == 0)
return sox_true;
return sox_false;
} /* is_pseudo_effect */
static void parse_effects(int argc, char ** argv)
{
while (optstate.ind < argc) {
unsigned eff_offset;
size_t j;
int newline_mode = 0;
eff_offset = nuser_effects[eff_chain_count];
if (eff_offset == user_effargs_size[eff_chain_count]) {
size_t i = user_effargs_size[eff_chain_count];
user_effargs_size[eff_chain_count] += EFFARGS_STEP;
lsx_revalloc(user_effargs[eff_chain_count], user_effargs_size[eff_chain_count]);
for (; i < user_effargs_size[eff_chain_count]; i++) {
user_effargs[eff_chain_count][i].argv = NULL;
user_effargs[eff_chain_count][i].argv_size = 0;
}
}
/* pseudo-effect ":" is used to create a new effects chain */
if (strcmp(argv[optstate.ind], ":") == 0)
{
/* Only create a new chain if current one has effects.
* Error checking will be done when loop is restarted.
*/
if (nuser_effects[eff_chain_count] != 0)
{
eff_chain_count++;
add_eff_chain();
}
optstate.ind++;
continue;
}
if (strcmp(argv[optstate.ind], "newfile") == 0)
{
/* Start a new effect chain for newfile if user doesn't
* manually do it. Restart loop without advancing
* optstate.ind to do error checking.
*/
if (nuser_effects[eff_chain_count] != 0)
{
eff_chain_count++;
add_eff_chain();
continue;
}
newline_mode = 1;
output_method = sox_multiple;
}
else if (strcmp(argv[optstate.ind], "restart") == 0)
{
/* Start a new effect chain for restart if user doesn't
* manually do it. Restart loop without advancing
* optstate.ind to do error checking.
*/
if (nuser_effects[eff_chain_count] != 0)
{
eff_chain_count++;
add_eff_chain();
continue;
}
newline_mode = 1;
}
/* Name should always be correct! */
user_effargs[eff_chain_count][eff_offset].name = lsx_strdup(argv[optstate.ind]);
optstate.ind++;
for (j = 0; j < (size_t)(argc - optstate.ind) && !sox_find_effect(argv[optstate.ind + j]) &&
!is_pseudo_effect(argv[optstate.ind + j]); ++j) {
if (j >= user_effargs[eff_chain_count][eff_offset].argv_size) {
user_effargs[eff_chain_count][eff_offset].argv_size += EFFARGS_STEP;
lsx_revalloc(user_effargs[eff_chain_count][eff_offset].argv,
user_effargs[eff_chain_count][eff_offset].argv_size);
}
user_effargs[eff_chain_count][eff_offset].argv[j] = lsx_strdup(argv[optstate.ind + j]);
}
user_effargs[eff_chain_count][eff_offset].argc = j;
optstate.ind += j; /* Skip past the effect arguments */
nuser_effects[eff_chain_count]++;
if (newline_mode)
{
eff_chain_count++;
add_eff_chain();
}
}
} /* parse_effects */
static char * * strtoargv(char * s, int * argc)
{
sox_bool squote = sox_false; /* Single quote mode (') is in effect. */
sox_bool dquote = sox_false; /* Double quote mode (") is in effect. */
sox_bool esc = sox_false; /* Escape mode (\) is in effect. */
char * t, * * argv = NULL;
for (*argc = 0; *s;) {
for (; isspace(*s); ++s); /* Skip past any (more) white space. */
if (*s) { /* Found an arg. */
lsx_revalloc(argv, *argc + 1);
argv[(*argc)++] = s; /* Store pointer to start of arg. */
/* Find the end of the arg: */
for (t = s; *s && (esc || squote || dquote || !isspace(*s)); ++s)
if (!esc && !squote && *s == '"')
dquote = !dquote; /* Toggle double quote mode. */
else if (!esc && !dquote && *s == '\'')
squote = !squote; /* Toggle single quote mode. */
else if (!(esc = !esc && *s == '\\' && s[1] &&
(!squote && (s[1] == '"' || !dquote))))
*t++ = *s; /* Only copy if not an active ', ", or \ */
s = *s ? s + 1 : s; /* Skip the 1st white space char. */
*t = '\0'; /* Terminate the arg. */
}
}
return argv;
} /* strtoargv */
static void read_user_effects(char const *filename)
{
FILE *file = fopen(filename, "rt");
const size_t buffer_size_step = 1024;
size_t buffer_size = buffer_size_step;
char *s = lsx_malloc(buffer_size); /* buffer for one input line */
int pos = 0;
int argc;
char * * argv;
sox_bool last_was_colon = sox_false; /* last line read consisted of ":" only */
/* Free any command line options and then re-initialize to
* starter user_effargs.
*/
delete_eff_chains();
current_eff_chain = 0;
add_eff_chain();
if (!file) {
lsx_fail("Cannot open effects file `%s': %s", filename, strerror(errno));
exit(1);
}
lsx_report("Reading effects from file `%s'", filename);
while(fgets(s + pos, (int) (buffer_size - pos), file)) {
int len = strlen(s + pos);
if (len && s[pos+len-1] == '\n')
s[pos+len-1] = '\0', pos = 0; /* we've read a complete line */
else if (len == (int)(buffer_size - pos - 1)) {
/* line was longer than buffer size */
buffer_size += buffer_size_step;
s = lsx_realloc(s, buffer_size);
pos += len;
continue; /* read next part */
} else {
/* something strange happened; the file might have ended
without a '\n', might contain '\0', or a read error
occurred */
if (ferror(file))
break; /* use error reporting after loop */
lsx_fail("Error reading effects file `%s' (not a text file?)", filename);
exit(1);
}
last_was_colon = sox_false;
argv = strtoargv(s, &argc);
if (argv && argc == 1 && strcmp(argv[0], ":") == 0)
last_was_colon = sox_true;
if (argv) {
/* Make sure first option is an effect name. */
if (!sox_find_effect(argv[0]) && !is_pseudo_effect(argv[0]))
{
lsx_fail("Cannot find an effect called `%s'.", argv[0]);
exit(1);
}
/* parse_effects normally parses options from command line.
* Reset opt index so it thinks its back at beginning of
* main()'s argv[].
*/
optstate.ind = 0;
parse_effects(argc, argv);
/* Advance to next effect but only if current chain has been
* filled in. This recovers from side affects of pseudo-effects.
*/
if (nuser_effects[eff_chain_count] > 0) {
eff_chain_count++;
add_eff_chain();
}
free(argv);
}
}
if (ferror(file)) {
lsx_fail("Error reading effects file `%s': %s", filename, strerror(errno));
exit(1);
}
fclose(file);
free(s);
if (last_was_colon || eff_chain_count == 0) {
/* user explicitly wanted an empty last effects chain,
or didn't specify any chains at all */
eff_chain_count++;
} else {
/* there's one unneeded effects chain */
free_eff_chain();
}
} /* read_user_effects */
/* Creates users effects and passes in user specified options.
* This is done without putting anything into the effects chain
* because an effect may set the effp->in_format and we may want
* to copy that back into the input/combiner before opening and
* inserting it.
* Similarly, we may want to use effp->out_format to override the
* default values of output file before we open it.
* To keep things simple, we create all user effects. Later, when
* we add them, some may already be in the chain and we will need to free
* them.
*/
static void create_user_effects(void)
{
unsigned i;
sox_effect_t *effp;
size_t num_effects = nuser_effects[current_eff_chain];
/* extend user_efftab, if needed */
if (user_efftab_size < num_effects) {
user_efftab_size = num_effects;
lsx_revalloc(user_efftab, num_effects);