-
Notifications
You must be signed in to change notification settings - Fork 5
/
art.c
953 lines (755 loc) · 31.8 KB
/
art.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
////////////////////////////////////////////////////////////////////////////
// **** ART **** //
// Audio Resampling Tool //
// Copyright (c) 2006-2023 David Bryant //
// All Rights Reserved //
// Distributed under the BSD Software License (see license.txt) //
////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <math.h>
#include "resampler.h"
#include "biquad.h"
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x0100)
static const char *sign_on = "\n"
" ART Audio Resampling Tool Version 0.2\n"
" Copyright (c) 2006 - 2023 David Bryant.\n\n";
static const char *usage =
" Usage: ART [-options] infile.wav outfile.wav\n\n"
" Options: -1|2|3|4 = quality presets, default = 3\n"
" -r<Hz> = resample to specified rate\n"
" -g<dB> = apply gain (default = 0 dB)\n"
" -s<degrees> = add specified phase shift (+/-360 degrees)\n"
" -l<Hz> = specify alternate lowpass frequency\n"
" -f<num> = number of sinc filters (2-1024)\n"
" -t<num> = number of sinc taps (4-1024, multiples of 4)\n"
" -o<bits> = change output file bitdepth (4-24 or 32)\n"
" -n = use nearest filter (don't interpolate)\n"
" -b = Blackman-Harris windowing (best stopband)\n"
" -h = Hann windowing (fastest transition)\n"
" -p = pre/post filtering (cascaded biquads)\n"
" -q = quiet mode (display errors only)\n"
" -v = verbose (display lots of info)\n"
" -y = overwrite outfile if it exists\n\n"
" Web: Visit www.github.com/dbry/audio-resampler for latest version and info\n\n";
static int wav_process (char *infilename, char *outfilename);
static int bh4_window, hann_window, num_taps = 256, num_filters = 256;
static int verbosity, interpolate = 1, pre_post_filter, outbits;
static unsigned long resample_rate, lowpass_freq;
static double phase_shift, gain = 1.0;
int main (argc, argv) int argc; char **argv;
{
int overwrite = 0;
char *infilename = NULL, *outfilename = NULL;
FILE *outfile;
// loop through command-line arguments
while (--argc) {
#if defined (_WIN32)
if ((**++argv == '-' || **argv == '/') && (*argv)[1])
#else
if ((**++argv == '-') && (*argv)[1])
#endif
while (*++*argv)
switch (**argv) {
case '1':
num_filters = num_taps = 16;
break;
case '2':
num_filters = num_taps = 64;
break;
case '3':
num_filters = num_taps = 256;
break;
case '4':
num_filters = num_taps = 1024;
break;
case 'P': case 'p':
pre_post_filter = 1;
break;
case 'Q': case 'q':
verbosity = -1;
break;
case 'V': case 'v':
verbosity = 1;
break;
case 'Y': case 'y':
overwrite = 1;
break;
case 'R': case 'r':
resample_rate = strtod (++*argv, argv);
--*argv;
break;
case 'S': case 's':
phase_shift = strtod (++*argv, argv) / 360.0;
if (phase_shift <= -1.0 || phase_shift >= 1.0) {
fprintf (stderr, "\nphase shift must be less than +/- 1 sample!\n");
return 1;
}
--*argv;
break;
case 'G': case 'g':
gain = pow (10.0, strtod (++*argv, argv) / 20.0);
--*argv;
break;
case 'L': case 'l':
lowpass_freq = strtod (++*argv, argv);
--*argv;
break;
case 'F': case 'f':
num_filters = strtod (++*argv, argv);
if (num_filters < 2 || num_filters > 1024) {
fprintf (stderr, "\nnum of filters must be 2 - 1024!\n");
return 1;
}
--*argv;
break;
case 'O': case 'o':
outbits = strtod (++*argv, argv);
if (outbits != 32 && (outbits < 4 || outbits > 24)) {
fprintf (stderr, "\noutbits must be 4 - 24 (for integer) or 32 (for float)!\n");
return 1;
}
--*argv;
break;
case 'T': case 't':
num_taps = strtod (++*argv, argv);
if ((num_taps & 3) || num_taps < 4 || num_taps > 1024) {
fprintf (stderr, "\nnum of taps must be 4 - 1024 and a multiple of 4!\n");
return 1;
}
--*argv;
break;
case 'N': case 'n':
interpolate = 0;
break;
case 'B': case 'b':
bh4_window = 1;
break;
case 'H': case 'h':
hann_window = 1;
break;
default:
fprintf (stderr, "\nillegal option: %c !\n", **argv);
return 1;
}
else if (!infilename) {
infilename = malloc (strlen (*argv) + 10);
strcpy (infilename, *argv);
}
else if (!outfilename) {
outfilename = malloc (strlen (*argv) + 10);
strcpy (outfilename, *argv);
}
else {
fprintf (stderr, "\nextra unknown argument: %s !\n", *argv);
return 1;
}
}
if (verbosity >= 0)
fprintf (stderr, "%s", sign_on);
if (!outfilename) {
printf ("%s", usage);
return 0;
}
if (!strcmp (infilename, outfilename)) {
fprintf (stderr, "can't overwrite input file (specify different/new output file name)\n");
return -1;
}
if (!overwrite && (outfile = fopen (outfilename, "r"))) {
fclose (outfile);
fprintf (stderr, "output file \"%s\" exists (use -y to overwrite)\n", outfilename);
return -1;
}
int res = wav_process (infilename, outfilename);
free (infilename);
free (outfilename);
return res;
}
typedef struct {
char ckID [4];
uint32_t ckSize;
char formType [4];
} RiffChunkHeader;
typedef struct {
char ckID [4];
uint32_t ckSize;
} ChunkHeader;
#define ChunkHeaderFormat "4L"
typedef struct {
uint16_t FormatTag, NumChannels;
uint32_t SampleRate, BytesPerSecond;
uint16_t BlockAlign, BitsPerSample;
uint16_t cbSize;
union {
uint16_t ValidBitsPerSample;
uint16_t SamplesPerBlock;
uint16_t Reserved;
} Samples;
int32_t ChannelMask;
uint16_t SubFormat;
char GUID [14];
} WaveHeader;
#define WaveHeaderFormat "SSLLSSSSLS"
#define WAVE_FORMAT_PCM 0x1
#define WAVE_FORMAT_IEEE_FLOAT 0x3
#define WAVE_FORMAT_EXTENSIBLE 0xfffe
static unsigned int process_audio (FILE *infile, FILE *outfile, unsigned long sample_rate,
unsigned long num_samples, int num_channels, int inbits);
static int write_pcm_wav_header (FILE *outfile, int bps, int num_channels, unsigned long num_samples, unsigned long sample_rate, uint32_t channel_mask);
static void little_endian_to_native (void *data, char *format);
static void native_to_little_endian (void *data, char *format);
static int wav_process (char *infilename, char *outfilename)
{
int format = 0, res = 0, inbits = 0, num_channels = 0;
unsigned long num_samples = 0, sample_rate = 0;
uint32_t channel_mask = 0;
FILE *infile, *outfile;
RiffChunkHeader riff_chunk_header;
ChunkHeader chunk_header;
WaveHeader WaveHeader;
// open both input and output files
if (!(infile = fopen (infilename, "rb"))) {
fprintf (stderr, "can't open file \"%s\" for reading!\n", infilename);
return -1;
}
if (!(outfile = fopen (outfilename, "wb"))) {
fprintf (stderr, "can't open file \"%s\" for writing!\n", outfilename);
fclose (infile);
return -1;
}
// read (and write) initial RIFF form header
if (!fread (&riff_chunk_header, sizeof (RiffChunkHeader), 1, infile) ||
strncmp (riff_chunk_header.ckID, "RIFF", 4) ||
strncmp (riff_chunk_header.formType, "WAVE", 4)) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
// loop through all elements of the RIFF wav header (until the data chuck)
while (1) {
if (!fread (&chunk_header, sizeof (ChunkHeader), 1, infile)) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
little_endian_to_native (&chunk_header, ChunkHeaderFormat);
// if it's the format chunk, we want to get some info out of there and
// make sure it's a .wav file we can handle
if (!strncmp (chunk_header.ckID, "fmt ", 4)) {
int supported = 1;
if (chunk_header.ckSize < 16 || chunk_header.ckSize > sizeof (WaveHeader) ||
!fread (&WaveHeader, chunk_header.ckSize, 1, infile)) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
little_endian_to_native (&WaveHeader, WaveHeaderFormat);
format = (WaveHeader.FormatTag == WAVE_FORMAT_EXTENSIBLE && chunk_header.ckSize == 40) ?
WaveHeader.SubFormat : WaveHeader.FormatTag;
if (WaveHeader.FormatTag == WAVE_FORMAT_EXTENSIBLE && chunk_header.ckSize == 40)
channel_mask = WaveHeader.ChannelMask;
else if (WaveHeader.NumChannels <= 2)
channel_mask = 0x5 - WaveHeader.NumChannels;
else if (WaveHeader.NumChannels < 32)
channel_mask = (1U << WaveHeader.NumChannels) - 1;
else
channel_mask = 0xffffffff;
inbits = (chunk_header.ckSize == 40 && WaveHeader.Samples.ValidBitsPerSample) ?
WaveHeader.Samples.ValidBitsPerSample : WaveHeader.BitsPerSample;
if (WaveHeader.NumChannels < 1 || WaveHeader.NumChannels > 32)
supported = 0;
else if (format == WAVE_FORMAT_PCM) {
if (inbits < 4 || inbits > 24)
supported = 0;
if (WaveHeader.BlockAlign != WaveHeader.NumChannels * ((inbits + 7) / 8))
supported = 0;
}
else if (format == WAVE_FORMAT_IEEE_FLOAT) {
if (inbits != 32)
supported = 0;
if (WaveHeader.BlockAlign != WaveHeader.NumChannels * 4)
supported = 0;
}
else
supported = 0;
if (!supported) {
fprintf (stderr, "\"%s\" is an unsupported .WAV format!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
if (verbosity > 0) {
fprintf (stderr, "format tag size = %d\n", chunk_header.ckSize);
fprintf (stderr, "FormatTag = 0x%x, NumChannels = %u, BitsPerSample = %u\n",
WaveHeader.FormatTag, WaveHeader.NumChannels, WaveHeader.BitsPerSample);
fprintf (stderr, "BlockAlign = %u, SampleRate = %lu, BytesPerSecond = %lu\n",
WaveHeader.BlockAlign, (unsigned long) WaveHeader.SampleRate, (unsigned long) WaveHeader.BytesPerSecond);
if (chunk_header.ckSize > 16)
fprintf (stderr, "cbSize = %d, ValidBitsPerSample = %d\n", WaveHeader.cbSize,
WaveHeader.Samples.ValidBitsPerSample);
if (chunk_header.ckSize > 20)
fprintf (stderr, "ChannelMask = %x, SubFormat = %d\n",
WaveHeader.ChannelMask, WaveHeader.SubFormat);
}
}
else if (!strncmp (chunk_header.ckID, "data", 4)) {
// on the data chunk, get size and exit parsing loop
if (!WaveHeader.NumChannels) { // make sure we saw a "fmt" chunk...
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
if (!chunk_header.ckSize) {
fprintf (stderr, "this .WAV file has no audio samples, probably is corrupt!\n");
fclose (outfile);
fclose (infile);
return -1;
}
if (chunk_header.ckSize % WaveHeader.BlockAlign) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
num_samples = chunk_header.ckSize / WaveHeader.BlockAlign;
if (!num_samples) {
fprintf (stderr, "this .WAV file has no audio samples, probably is corrupt!\n");
fclose (outfile);
fclose (infile);
return -1;
}
if (verbosity > 0)
fprintf (stderr, "num samples = %lu\n", num_samples);
num_channels = WaveHeader.NumChannels;
sample_rate = WaveHeader.SampleRate;
break;
}
else { // just ignore/copy unknown chunks
unsigned int bytes_to_copy = (chunk_header.ckSize + 1) & ~1L;
if (verbosity > 0)
fprintf (stderr, "extra unknown chunk \"%c%c%c%c\" of %u bytes\n",
chunk_header.ckID [0], chunk_header.ckID [1], chunk_header.ckID [2],
chunk_header.ckID [3], bytes_to_copy);
while (bytes_to_copy) {
unsigned int bytes_to_read = bytes_to_copy, bytes_read;
char temp_buffer [256];
if (bytes_to_read > sizeof (temp_buffer))
bytes_to_read = sizeof (temp_buffer);
bytes_read = fread (temp_buffer, 1, bytes_to_read, infile);
if (bytes_read != bytes_to_read) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
bytes_to_copy -= bytes_read;
}
}
}
if (!num_channels || !sample_rate || !inbits || !num_samples) {
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename);
fclose (outfile);
fclose (infile);
return -1;
}
// if not specified, preserve sample rate and bitdepth of input
if (!resample_rate) resample_rate = sample_rate;
if (!outbits) outbits = inbits;
if (verbosity >= 0)
fprintf (stderr, "resampling %d-channel file \"%s\" (%db/%dk) to \"%s\" (%db/%dk)...\n",
num_channels, infilename, inbits, (int)((sample_rate + 500) / 1000),
outfilename, outbits, (int)((resample_rate + 500) / 1000));
if (!write_pcm_wav_header (outfile, outbits, num_channels, num_samples, resample_rate, channel_mask)) {
fprintf (stderr, "can't write to file \"%s\"!\n", outfilename);
fclose (outfile);
fclose (infile);
return -1;
}
unsigned int output_samples = process_audio (infile, outfile, sample_rate, num_samples, num_channels, inbits);
// write an extra padding zero byte if the data chunk is not an even size
if ((output_samples * num_channels * ((outbits + 7) / 8)) & 1)
fwrite ("", 1, 1, outfile);
rewind (outfile);
if (!write_pcm_wav_header (outfile, outbits, num_channels, output_samples, resample_rate, channel_mask)) {
fprintf (stderr, "can't write to file \"%s\"!\n", outfilename);
fclose (outfile);
fclose (infile);
return -1;
}
fclose (outfile);
fclose (infile);
return res;
}
// Return a tpdf random value in the range: -1.0 <= n < 1.0
// type: -1: negative intersample correlation (HF boost)
// 0: no correlation (independent samples, flat spectrum)
// 1: positive intersample correlation (LF boost)
// Note: not thread-safe on the same channel
static uint32_t *tpdf_generators;
static void tpdf_dither_init (int num_channels)
{
int generator_bytes = num_channels * sizeof (uint32_t);
unsigned char *seed = malloc (generator_bytes);
uint32_t random = 0x31415926;
tpdf_generators = (uint32_t *) seed;
while (generator_bytes--) {
*seed++ = random >> 24;
random = ((random << 4) - random) ^ 1;
random = ((random << 4) - random) ^ 1;
random = ((random << 4) - random) ^ 1;
}
}
static inline double tpdf_dither (int channel, int type)
{
uint32_t random = tpdf_generators [channel];
random = ((random << 4) - random) ^ 1;
random = ((random << 4) - random) ^ 1;
uint32_t first = type ? tpdf_generators [channel] ^ ((int32_t) type >> 31) : ~random;
random = ((random << 4) - random) ^ 1;
random = ((random << 4) - random) ^ 1;
random = ((random << 4) - random) ^ 1;
tpdf_generators [channel] = random;
return (((first >> 1) + (random >> 1)) / 2147483648.0) - 1.0;
}
static void tpdf_dither_free (void)
{
free (tpdf_generators);
}
#define BUFFER_SAMPLES 4096
static unsigned int process_audio (FILE *infile, FILE *outfile, unsigned long sample_rate,
unsigned long num_samples, int num_channels, int inbits)
{
double sample_ratio = (double) resample_rate / sample_rate, lowpass_ratio = 1.0;
unsigned int outbuffer_samples = (int) floor (BUFFER_SAMPLES * sample_ratio * 1.1 + 100.0);
unsigned long remaining_samples = num_samples, output_samples = 0, clipped_samples = 0;
float *outbuffer = malloc (outbuffer_samples * num_channels * sizeof (float));
float *inbuffer = malloc (BUFFER_SAMPLES * num_channels * sizeof (float));
int flags = interpolate ? SUBSAMPLE_INTERPOLATE : 0;
int samples_to_append = num_taps / 2;
int pre_filter = 0, post_filter = 0;
Biquad lowpass [num_channels] [2];
BiquadCoefficients lowpass_coeff;
unsigned char *tmpbuffer = NULL;
void *readbuffer = inbuffer;
float error [num_channels];
Resample *resampler;
// when downsampling, calculate the optimum lowpass based on resample filter
// length (i.e., more taps allow us to lowpass closer to Nyquist)
if (sample_ratio < 1.0) {
lowpass_ratio -= (10.24 / num_taps);
if (lowpass_ratio < 0.84) // limit the lowpass for very short filters
lowpass_ratio = 0.84;
if (lowpass_ratio < sample_ratio) // avoid discontinuities near unity sample ratios
lowpass_ratio = sample_ratio;
}
if (lowpass_freq) {
double user_lowpass_ratio;
if (sample_ratio < 1.0)
user_lowpass_ratio = lowpass_freq / (resample_rate / 2.0);
else
user_lowpass_ratio = lowpass_freq / (sample_rate / 2.0);
if (user_lowpass_ratio >= 1.0)
fprintf (stderr, "warning: ignoring invalid lowpass frequency specification (at or over Nyquist)\n");
else
lowpass_ratio = user_lowpass_ratio;
}
if (bh4_window || !hann_window)
flags |= BLACKMAN_HARRIS;
if (lowpass_ratio * sample_ratio < 0.98 && pre_post_filter) {
double cutoff = lowpass_ratio * sample_ratio / 2.0;
biquad_lowpass (&lowpass_coeff, cutoff);
pre_filter = 1;
if (verbosity > 0)
fprintf (stderr, "cascaded biquad pre-filter at %g Hz\n", sample_rate * cutoff);
}
if (sample_ratio < 1.0) {
resampler = resampleInit (num_channels, num_taps, num_filters, sample_ratio * lowpass_ratio, flags | INCLUDE_LOWPASS);
if (verbosity > 0)
fprintf (stderr, "%d-tap sinc downsampler with lowpass at %g Hz\n", num_taps, sample_ratio * lowpass_ratio * sample_rate / 2.0);
}
else if (lowpass_ratio < 1.0) {
resampler = resampleInit (num_channels, num_taps, num_filters, lowpass_ratio, flags | INCLUDE_LOWPASS);
if (verbosity > 0)
fprintf (stderr, "%d-tap sinc resampler with lowpass at %g Hz\n", num_taps, lowpass_ratio * sample_rate / 2.0);
}
else {
resampler = resampleInit (num_channels, num_taps, num_filters, 1.0, flags);
if (verbosity > 0)
fprintf (stderr, "%d-tap pure sinc resampler (no lowpass), %g Hz Nyquist\n", num_taps, sample_rate / 2.0);
}
if (lowpass_ratio / sample_ratio < 0.98 && pre_post_filter && !pre_filter) {
double cutoff = lowpass_ratio / sample_ratio / 2.0;
biquad_lowpass (&lowpass_coeff, cutoff);
post_filter = 1;
if (verbosity > 0)
fprintf (stderr, "cascaded biquad post-filter at %g Hz\n", resample_rate * cutoff);
}
if (pre_filter || post_filter)
for (int i = 0; i < num_channels; ++i) {
biquad_init (&lowpass [i] [0], &lowpass_coeff, 1.0);
biquad_init (&lowpass [i] [1], &lowpass_coeff, 1.0);
}
if (outbits != 32) {
memset (error, 0, sizeof (error));
tpdf_dither_init (num_channels);
}
if (inbits != 32 || outbits != 32) {
int max_samples = BUFFER_SAMPLES, max_bytes = 2;
if (outbuffer_samples > BUFFER_SAMPLES)
max_samples = outbuffer_samples;
if (inbits > 16 || outbits > 16)
max_bytes = 3;
tmpbuffer = malloc (max_samples * num_channels * max_bytes);
if (inbits != 32)
readbuffer = tmpbuffer;
}
// this takes care of the filter delay and any user-specified phase shift
resampleAdvancePosition (resampler, num_taps / 2.0 + phase_shift);
uint32_t progress_divider = 0, percent;
if (verbosity >= 0 && remaining_samples > 1000) {
progress_divider = (remaining_samples + 50) / 100;
fprintf (stderr, "\rprogress: %d%% ", percent = 0); fflush (stderr);
}
while (remaining_samples + samples_to_append) {
// first we read the audio data, converting to 32-bit float (if not already) and applying gain
unsigned long samples_to_read = remaining_samples, samples_read, samples_generated;
ResampleResult res;
if (samples_to_read > BUFFER_SAMPLES)
samples_to_read = BUFFER_SAMPLES;
samples_read = fread (readbuffer, num_channels * ((inbits + 7) / 8), samples_to_read, infile);
remaining_samples -= samples_read;
if (!samples_read) {
int samples_to_append_now = samples_to_append;
if (!samples_to_append_now)
break;
if (samples_to_append_now > BUFFER_SAMPLES)
samples_to_append_now = BUFFER_SAMPLES;
memset (readbuffer, (inbits <= 8) * 128, samples_to_append_now * num_channels * ((inbits + 7) / 8));
samples_read = samples_to_append_now;
samples_to_append -= samples_to_append_now;
}
if (inbits <= 8) {
float gain_factor = gain / 128.0;
int i;
for (i = 0; i < samples_read * num_channels; ++i)
inbuffer [i] = ((int) tmpbuffer [i] - 128) * gain_factor;
}
else if (inbits <= 16) {
float gain_factor = gain / 32768.0;
int i, j;
for (i = j = 0; i < samples_read * num_channels; ++i) {
int16_t value = tmpbuffer [j++];
value += tmpbuffer [j++] << 8;
inbuffer [i] = value * gain_factor;
}
}
else if (inbits <= 24) {
float gain_factor = gain / 8388608.0;
int i, j;
for (i = j = 0; i < samples_read * num_channels; ++i) {
int32_t value = tmpbuffer [j++];
value += tmpbuffer [j++] << 8;
value += (int32_t) (signed char) tmpbuffer [j++] << 16;
inbuffer [i] = value * gain_factor;
}
}
else {
if (IS_BIG_ENDIAN) {
unsigned char *bptr = (unsigned char *) inbuffer, word [4];
int wcount = samples_read * num_channels;
while (wcount--) {
memcpy (word, bptr, 4);
*bptr++ = word [3];
*bptr++ = word [2];
*bptr++ = word [1];
*bptr++ = word [0];
}
}
if (gain != 1.0)
for (int i = 0; i < samples_read * num_channels; ++i)
inbuffer [i] *= gain;
}
// common code to process the audio in 32-bit floats
if (pre_filter)
for (int i = 0; i < num_channels; ++i) {
biquad_apply_buffer (&lowpass [i] [0], inbuffer + i, samples_read, num_channels);
biquad_apply_buffer (&lowpass [i] [1], inbuffer + i, samples_read, num_channels);
}
res = resampleProcessInterleaved (resampler, inbuffer, samples_read, outbuffer, outbuffer_samples, sample_ratio);
samples_generated = res.output_generated;
if (post_filter)
for (int i = 0; i < num_channels; ++i) {
biquad_apply_buffer (&lowpass [i] [0], outbuffer + i, samples_generated, num_channels);
biquad_apply_buffer (&lowpass [i] [1], outbuffer + i, samples_generated, num_channels);
}
// finally write the audio, converting to appropriate integer format if requested
if (outbits != 32) {
float scaler = (1 << outbits) / 2.0;
int32_t offset = (outbits <= 8) * 128;
int32_t highclip = (1 << (outbits - 1)) - 1;
int32_t lowclip = ~highclip;
int leftshift = (24 - outbits) % 8;
int i, j;
for (i = j = 0; i < samples_generated * num_channels; ++i) {
int chan = i % num_channels;
int32_t output = floor ((outbuffer [i] *= scaler) - error [chan] + tpdf_dither (chan, -1) + 0.5);
if (output > highclip) {
clipped_samples++;
output = highclip;
}
else if (output < lowclip) {
clipped_samples++;
output = lowclip;
}
error [chan] += output - outbuffer [i];
tmpbuffer [j++] = output = (output << leftshift) + offset;
if (outbits > 8) {
tmpbuffer [j++] = output >> 8;
if (outbits > 16)
tmpbuffer [j++] = output >> 16;
}
}
fwrite (tmpbuffer, num_channels * ((outbits + 7) / 8), samples_generated, outfile);
}
else {
if (IS_BIG_ENDIAN) {
unsigned char *bptr = (unsigned char *) outbuffer, word [4];
int wcount = samples_generated * num_channels;
while (wcount--) {
memcpy (word, bptr, 4);
*bptr++ = word [3];
*bptr++ = word [2];
*bptr++ = word [1];
*bptr++ = word [0];
}
}
fwrite (outbuffer, num_channels * sizeof (float), samples_generated, outfile);
}
output_samples += samples_generated;
if (progress_divider) {
int new_percent = 100 - remaining_samples / progress_divider;
if (new_percent != percent) {
fprintf (stderr, "\rprogress: %d%% ", percent = new_percent);
fflush (stderr);
}
}
}
if (verbosity >= 0)
fprintf (stderr, "\r...completed successfully\n");
resampleFree (resampler);
tpdf_dither_free ();
free (inbuffer);
free (outbuffer);
free (tmpbuffer);
if (clipped_samples)
fprintf (stderr, "warning: %lu samples were clipped, suggest reducing gain!\n", clipped_samples);
if (remaining_samples)
fprintf (stderr, "warning: file terminated early!\n");
return output_samples;
}
static int write_pcm_wav_header (FILE *outfile, int bps, int num_channels, unsigned long num_samples, unsigned long sample_rate, uint32_t channel_mask)
{
RiffChunkHeader riffhdr;
ChunkHeader datahdr, fmthdr;
WaveHeader wavhdr;
int wavhdrsize = 16;
int bytes_per_sample = (bps + 7) / 8;
int format = (bps == 32) ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
uint32_t total_data_bytes = num_samples * bytes_per_sample * num_channels;
memset (&wavhdr, 0, sizeof (wavhdr));
wavhdr.FormatTag = format;
wavhdr.NumChannels = num_channels;
wavhdr.SampleRate = sample_rate;
wavhdr.BytesPerSecond = sample_rate * num_channels * bytes_per_sample;
wavhdr.BlockAlign = bytes_per_sample * num_channels;
wavhdr.BitsPerSample = bps;
// write an extended header if more than 2 channels or a non-standard channel-mask
if (num_channels > 2 || channel_mask != 0x5 - num_channels) {
wavhdrsize = sizeof (wavhdr);
wavhdr.cbSize = 22;
wavhdr.Samples.ValidBitsPerSample = bps;
wavhdr.SubFormat = format;
wavhdr.ChannelMask = channel_mask;
wavhdr.FormatTag = WAVE_FORMAT_EXTENSIBLE;
wavhdr.BitsPerSample = bps;
wavhdr.GUID [4] = 0x10;
wavhdr.GUID [6] = 0x80;
wavhdr.GUID [9] = 0xaa;
wavhdr.GUID [11] = 0x38;
wavhdr.GUID [12] = 0x9b;
wavhdr.GUID [13] = 0x71;
}
memcpy (riffhdr.ckID, "RIFF", sizeof (riffhdr.ckID));
memcpy (riffhdr.formType, "WAVE", sizeof (riffhdr.formType));
riffhdr.ckSize = (sizeof (riffhdr) + wavhdrsize + sizeof (datahdr) + total_data_bytes + 1) & ~1U;
memcpy (fmthdr.ckID, "fmt ", sizeof (fmthdr.ckID));
fmthdr.ckSize = wavhdrsize;
memcpy (datahdr.ckID, "data", sizeof (datahdr.ckID));
datahdr.ckSize = total_data_bytes;
// write the RIFF chunks up to just before the data starts
native_to_little_endian (&riffhdr, ChunkHeaderFormat);
native_to_little_endian (&fmthdr, ChunkHeaderFormat);
native_to_little_endian (&wavhdr, WaveHeaderFormat);
native_to_little_endian (&datahdr, ChunkHeaderFormat);
return fwrite (&riffhdr, sizeof (riffhdr), 1, outfile) &&
fwrite (&fmthdr, sizeof (fmthdr), 1, outfile) &&
fwrite (&wavhdr, wavhdrsize, 1, outfile) &&
fwrite (&datahdr, sizeof (datahdr), 1, outfile);
}
static void little_endian_to_native (void *data, char *format)
{
unsigned char *cp = (unsigned char *) data;
int32_t temp;
while (*format) {
switch (*format) {
case 'L':
temp = cp [0] + ((int32_t) cp [1] << 8) + ((int32_t) cp [2] << 16) + ((int32_t) cp [3] << 24);
* (int32_t *) cp = temp;
cp += 4;
break;
case 'S':
temp = cp [0] + (cp [1] << 8);
* (short *) cp = (short) temp;
cp += 2;
break;
default:
if (isdigit ((unsigned char) *format))
cp += *format - '0';
break;
}
format++;
}
}
static void native_to_little_endian (void *data, char *format)
{
unsigned char *cp = (unsigned char *) data;
int32_t temp;
while (*format) {
switch (*format) {
case 'L':
temp = * (int32_t *) cp;
*cp++ = (unsigned char) temp;
*cp++ = (unsigned char) (temp >> 8);
*cp++ = (unsigned char) (temp >> 16);
*cp++ = (unsigned char) (temp >> 24);
break;
case 'S':
temp = * (short *) cp;
*cp++ = (unsigned char) temp;
*cp++ = (unsigned char) (temp >> 8);
break;
default:
if (isdigit ((unsigned char) *format))
cp += *format - '0';
break;
}
format++;
}
}