-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclownresampler.h
1253 lines (962 loc) · 48.4 KB
/
clownresampler.h
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
/*
Copyright (c) 2022-2023 Clownacy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
/*
clownresampler
This is a single-file library for resampling audio. It is written in C89 and
licensed under the terms of the Zero-Clause BSD licence.
In particular, this library implements a windowed-sinc resampler, using a
Lanczos window.
https://github.com/Clownacy/clownresampler
*/
/*
Contents:
- 1. Examples
- 2. Configuration
- 3. Header & Documentation
- 4. Implementation
*/
/* 1. Examples */
#if 0
/*
This demonstrates use of clownresampler's low-level API.
The low-level API is ideal for when the entirety of the input data is available
at once, whereas the high-level API is ideal for when the input data is
streamed piece by piece.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define MINIAUDIO_IMPLEMENTATION
#define MA_NO_DECODING
#define MA_NO_ENCODING
#define MA_NO_GENERATION
#define MA_NO_ENGINE
#define MA_NO_NODE_GRAPH
#define MA_API static
#include "libraries/miniaudio.h" /* v0.11.9 */
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_ONLY_MP3
#define DRMP3_API static
#include "libraries/dr_mp3.h" /* v0.6.33 */
#define CLOWNRESAMPLER_IMPLEMENTATION
#define CLOWNRESAMPLER_STATIC
#define CLOWNRESAMPLER_NO_HIGH_LEVEL_API /* We do not need the high-level API. */
#include "../clownresampler.h"
static ClownResampler_Precomputed precomputed;
static ClownResampler_LowLevel_State resampler;
static unsigned int total_channels;
static drmp3_int16 *resampler_input_buffer;
static size_t resampler_input_buffer_total_frames;
static size_t resampler_input_buffer_frames_remaining;
typedef struct ResamplerCallbackData
{
ma_int16 *output_pointer;
ma_uint32 output_buffer_frames_remaining;
} ResamplerCallbackData;
static cc_bool ResamplerOutputCallback(void *user_data, const cc_s32f *frame, cc_u8f total_samples)
{
ResamplerCallbackData* const callback_data = (ResamplerCallbackData*)user_data;
cc_u8f i;
/* Output the frame. */
for (i = 0; i < total_samples; ++i)
{
cc_s32f sample;
sample = frame[i];
/* Clamp the sample to 16-bit. */
if (sample > 0x7FFF)
sample = 0x7FFF;
else if (sample < -0x7FFF)
sample = -0x7FFF;
/* Push the sample to the output buffer. */
*callback_data->output_pointer++ = (ma_int16)sample;
}
/* Signal whether there is more room in the output buffer. */
return --callback_data->output_buffer_frames_remaining != 0;
}
static void AudioCallback(ma_device *device, void *output, const void *input, ma_uint32 frame_count)
{
ResamplerCallbackData callback_data;
(void)device;
(void)input;
callback_data.output_pointer = (ma_int16*)output;
callback_data.output_buffer_frames_remaining = frame_count;
/* Resample the decoded audio data. */
ClownResampler_LowLevel_Resample(&resampler, &precomputed, &resampler_input_buffer[(resampler_input_buffer_total_frames - resampler_input_buffer_frames_remaining) * total_channels], &resampler_input_buffer_frames_remaining, ResamplerOutputCallback, &callback_data);
/* If there are no more samples left, then fill the remaining space in the buffer with 0. */
memset(callback_data.output_pointer, 0, callback_data.output_buffer_frames_remaining * total_channels * sizeof(ma_int16));
}
int main(int argc, char **argv)
{
int exit_code;
drmp3 mp3_decoder;
exit_code = EXIT_FAILURE;
if (argc < 2)
{
fputs("Pass the path to an MP3 file as an argument.\n", stderr);
}
else
{
if (!drmp3_init_file(&mp3_decoder, argv[1], NULL))
{
fputs("Failed to initialise MP3 decoder.\n", stderr);
}
else
{
/******************************/
/* Initialise audio playback. */
/******************************/
ma_device_config miniaudio_config;
ma_device miniaudio_device;
miniaudio_config = ma_device_config_init(ma_device_type_playback);
miniaudio_config.playback.format = ma_format_s16;
miniaudio_config.playback.channels = mp3_decoder.channels;
miniaudio_config.sampleRate = 0; /* Use whatever sample rate the playback device wants. */
miniaudio_config.dataCallback = AudioCallback;
miniaudio_config.pUserData = NULL;
if (ma_device_init(NULL, &miniaudio_config, &miniaudio_device) != MA_SUCCESS)
{
drmp3_uninit(&mp3_decoder);
fputs("Failed to initialise playback device.\n", stderr);
}
else
{
/*****************************************/
/* Finished initialising audio playback. */
/*****************************************/
const size_t size_of_frame = mp3_decoder.channels * sizeof(drmp3_int16);
size_t total_mp3_pcm_frames;
total_mp3_pcm_frames = drmp3_get_pcm_frame_count(&mp3_decoder);
total_channels = mp3_decoder.channels;
/* Inform the user of the input and output sample rates. */
fprintf(stderr, "MP3 Sample Rate: %lu\n", (unsigned long)mp3_decoder.sampleRate);
fprintf(stderr, "Playback Sample Rate: %lu\n", (unsigned long)miniaudio_device.sampleRate);
fflush(stderr);
/******************************/
/* Initialise clownresampler. */
/******************************/
/* Precompute the Lanczos kernel. */
ClownResampler_Precompute(&precomputed);
/* Create a resampler that converts from the sample rate of the MP3 to the sample rate of the playback device. */
/* The low-pass filter is set to 44100Hz since that should allow all human-perceivable frequencies through. */
ClownResampler_LowLevel_Init(&resampler, mp3_decoder.channels, mp3_decoder.sampleRate, miniaudio_device.sampleRate, 44100);
/*****************************************/
/* Finished initialising clownresampler. */
/*****************************************/
/*****************************************/
/* Set up clownresampler's input buffer. */
/*****************************************/
/* Create a buffer to hold the decoded PCM data. */
/* clownresampler's low-level API requires that this buffer have padding at its beginning and end. */
resampler_input_buffer = (drmp3_int16*)malloc((resampler.lowest_level.integer_stretched_kernel_radius * 2 + total_mp3_pcm_frames) * size_of_frame);
if (resampler_input_buffer == NULL)
{
drmp3_uninit(&mp3_decoder);
fputs("Failed to allocate memory for resampler input buffer.\n", stderr);
}
else
{
/* Set the padding samples at the start to 0. */
memset(&resampler_input_buffer[0], 0, resampler.lowest_level.integer_stretched_kernel_radius * size_of_frame);
/* Decode the MP3 to the input buffer. */
drmp3_read_pcm_frames_s16(&mp3_decoder, total_mp3_pcm_frames, &resampler_input_buffer[resampler.lowest_level.integer_stretched_kernel_radius * total_channels]);
drmp3_uninit(&mp3_decoder);
/* Set the padding samples at the end to 0. */
memset(&resampler_input_buffer[(resampler.lowest_level.integer_stretched_kernel_radius + total_mp3_pcm_frames) * total_channels], 0, resampler.lowest_level.integer_stretched_kernel_radius * size_of_frame);
/* Initialise some variables that will be used by the audio callback. */
resampler_input_buffer_total_frames = resampler_input_buffer_frames_remaining = total_mp3_pcm_frames;
/*****************************************************/
/* Finished setting up the resampler's input buffer. */
/*****************************************************/
/* Begin playback. */
ma_device_start(&miniaudio_device);
/* Wait for input from the user before terminating the program. */
fgetc(stdin);
ma_device_stop(&miniaudio_device);
free(resampler_input_buffer);
exit_code = EXIT_SUCCESS;
}
ma_device_uninit(&miniaudio_device);
}
}
}
return exit_code;
}
#endif
#if 0
/*
This demonstrates use of clownresampler's high-level API.
The low-level API is ideal for when the entirety of the input data is available
at once, whereas the high-level API is ideal for when the input data is
streamed piece by piece.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define MINIAUDIO_IMPLEMENTATION
#define MA_NO_DECODING
#define MA_NO_ENCODING
#define MA_NO_GENERATION
#define MA_NO_ENGINE
#define MA_NO_NODE_GRAPH
#define MA_API static
#include "libraries/miniaudio.h" /* v0.11.9 */
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_ONLY_MP3
#define DRMP3_API static
#include "libraries/dr_mp3.h" /* v0.6.33 */
#define CLOWNRESAMPLER_IMPLEMENTATION
#define CLOWNRESAMPLER_STATIC
#include "../clownresampler.h"
static ClownResampler_Precomputed precomputed;
static ClownResampler_HighLevel_State resampler;
static drmp3 mp3_decoder;
static unsigned int total_channels;
typedef struct ResamplerCallbackData
{
ma_int16 *output_pointer;
ma_uint32 output_buffer_frames_remaining;
} ResamplerCallbackData;
static size_t ResamplerInputCallback(void *user_data, cc_s16l *buffer, size_t total_frames)
{
(void)user_data;
/* Obtain samples from the MP3 file. */
return drmp3_read_pcm_frames_s16(&mp3_decoder, total_frames, buffer);
}
static cc_bool ResamplerOutputCallback(void *user_data, const cc_s32f *frame, cc_u8f total_samples)
{
ResamplerCallbackData* const callback_data = (ResamplerCallbackData*)user_data;
cc_u8f i;
/* Output the frame. */
for (i = 0; i < total_samples; ++i)
{
cc_s32f sample;
sample = frame[i];
/* Clamp the sample to 16-bit. */
if (sample > 0x7FFF)
sample = 0x7FFF;
else if (sample < -0x7FFF)
sample = -0x7FFF;
/* Push the sample to the output buffer. */
*callback_data->output_pointer++ = (ma_int16)sample;
}
/* Signal whether there is more room in the output buffer. */
return --callback_data->output_buffer_frames_remaining != 0;
}
static void AudioCallback(ma_device *device, void *output, const void *input, ma_uint32 frame_count)
{
ResamplerCallbackData callback_data;
(void)device;
(void)input;
callback_data.output_pointer = (ma_int16*)output;
callback_data.output_buffer_frames_remaining = frame_count;
/* Resample the decoded audio data. */
ClownResampler_HighLevel_Resample(&resampler, &precomputed, ResamplerInputCallback, ResamplerOutputCallback, &callback_data);
/* If there are no more samples left, then fill the remaining space in the buffer with 0. */
memset(callback_data.output_pointer, 0, callback_data.output_buffer_frames_remaining * total_channels * sizeof(ma_int16));
}
int main(int argc, char **argv)
{
int exit_code;
exit_code = EXIT_FAILURE;
if (argc < 2)
{
fputs("Pass the path to an MP3 file as an argument.\n", stderr);
}
else
{
if (!drmp3_init_file(&mp3_decoder, argv[1], NULL))
{
fputs("Failed to initialise MP3 decoder.\n", stderr);
}
else
{
/******************************/
/* Initialise audio playback. */
/******************************/
ma_device_config miniaudio_config;
ma_device miniaudio_device;
miniaudio_config = ma_device_config_init(ma_device_type_playback);
miniaudio_config.playback.format = ma_format_s16;
miniaudio_config.playback.channels = mp3_decoder.channels;
miniaudio_config.sampleRate = 0; /* Use whatever sample rate the playback device wants. */
miniaudio_config.dataCallback = AudioCallback;
miniaudio_config.pUserData = NULL;
if (ma_device_init(NULL, &miniaudio_config, &miniaudio_device) != MA_SUCCESS)
{
fputs("Failed to initialise playback device.\n", stderr);
}
else
{
/*****************************************/
/* Finished initialising audio playback. */
/*****************************************/
/* Inform the user of the input and output sample rates. */
fprintf(stderr, "MP3 Sample Rate: %lu\n", (unsigned long)mp3_decoder.sampleRate);
fprintf(stderr, "Playback Sample Rate: %lu\n", (unsigned long)miniaudio_device.sampleRate);
fflush(stderr);
/******************************/
/* Initialise clownresampler. */
/******************************/
/* Precompute the Lanczos kernel. */
ClownResampler_Precompute(&precomputed);
/* Create a resampler that converts from the sample rate of the MP3 to the sample rate of the playback device. */
/* The low-pass filter is set to 44100Hz since that should allow all human-perceivable frequencies through. */
ClownResampler_HighLevel_Init(&resampler, mp3_decoder.channels, mp3_decoder.sampleRate, miniaudio_device.sampleRate, 44100);
/*****************************************/
/* Finished initialising clownresampler. */
/*****************************************/
total_channels = mp3_decoder.channels;
/* Begin playback. */
ma_device_start(&miniaudio_device);
/* Wait for input from the user before terminating the program. */
fgetc(stdin);
ma_device_uninit(&miniaudio_device);
exit_code = EXIT_SUCCESS;
}
drmp3_uninit(&mp3_decoder);
}
}
return exit_code;
}
#endif
#ifndef CLOWNRESAMPLER_GUARD_MISC
#define CLOWNRESAMPLER_GUARD_MISC
/* 2. Configuration */
/* Define 'CLOWNRESAMPLER_STATIC' to limit the visibility of public functions. */
/* Alternatively, define 'CLOWNRESAMPLER_API' to control the qualifiers applied to the public functions. */
#ifndef CLOWNRESAMPLER_API
#ifdef CLOWNRESAMPLER_STATIC
#define CLOWNRESAMPLER_API static
#else
#define CLOWNRESAMPLER_API
#endif
#endif
/* Controls the number of 'lobes' of the windowed sinc function.
A higher number results in better audio, but is more expensive. */
#ifndef CLOWNRESAMPLER_KERNEL_RADIUS
#define CLOWNRESAMPLER_KERNEL_RADIUS 3
#endif
/* How many samples to render per lobe for the pre-computed Lanczos kernel.
Higher numbers produce a higher-quality Lanczos kernel, but cause it to take
up more memory and cache. */
#ifndef CLOWNRESAMPLER_KERNEL_RESOLUTION
#define CLOWNRESAMPLER_KERNEL_RESOLUTION 0x400 /* 1024 samples per lobe should be more than good enough */
#endif
/* The maximum number of channels supported by the resampler.
This will likely be removed in the future. */
#ifndef CLOWNRESAMPLER_MAXIMUM_CHANNELS
#define CLOWNRESAMPLER_MAXIMUM_CHANNELS 16 /* As stb_vorbis says, this should be enough for pretty much everyone. */
#endif
/* Disables the low-level API. */
/*#define CLOWNRESAMPLER_NO_LOW_LEVEL_API*/
/* Disables the high-level API. */
/*#define CLOWNRESAMPLER_NO_HIGH_LEVEL_API*/
/* Disables the ClownResampler_HighLevel_Adjust function. */
/*#define CLOWNRESAMPLER_NO_HIGH_LEVEL_ADJUST*/
/* Disables the ClownResampler_HighLevel_ResampleEnd function. */
/*#define CLOWNRESAMPLER_NO_HIGH_LEVEL_RESAMPLE_END*/
/* 3. Header & Documentation */
#include <stddef.h>
/* Integer types. */
#ifndef CC_INTEGERS_DEFINED
#define CC_INTEGERS_DEFINED
#if defined(CC_USE_C99_INTEGERS)
/* Use C99's/C++11's better integer types if available. */
#include <stdint.h>
typedef int_least8_t cc_s8l;
typedef int_least16_t cc_s16l;
typedef int_least32_t cc_s32l;
typedef uint_least8_t cc_u8l;
typedef uint_least16_t cc_u16l;
typedef uint_least32_t cc_u32l;
typedef int_fast8_t cc_s8f;
typedef int_fast16_t cc_s16f;
typedef int_fast32_t cc_s32f;
typedef uint_fast8_t cc_u8f;
typedef uint_fast16_t cc_u16f;
typedef uint_fast32_t cc_u32f;
#define CC_PRIdLEAST8 PRIdLEAST8
#define CC_PRIiLEAST8 PRIiLEAST8
#define CC_PRIuLEAST8 PRIuLEAST8
#define CC_PRIoLEAST8 PRIoLEAST8
#define CC_PRIxLEAST8 PRIxLEAST8
#define CC_PRIXLEAST8 PRIXLEAST8
#define CC_PRIdLEAST16 PRIdLEAST16
#define CC_PRIiLEAST16 PRIiLEAST16
#define CC_PRIuLEAST16 PRIuLEAST16
#define CC_PRIoLEAST16 PRIoLEAST16
#define CC_PRIxLEAST16 PRIxLEAST16
#define CC_PRIXLEAST16 PRIXLEAST16
#define CC_PRIdLEAST32 PRIdLEAST32
#define CC_PRIiLEAST32 PRIiLEAST32
#define CC_PRIuLEAST32 PRIuLEAST32
#define CC_PRIoLEAST32 PRIoLEAST32
#define CC_PRIxLEAST32 PRIxLEAST32
#define CC_PRIXLEAST32 PRIXLEAST32
#define CC_PRIdFAST8 PRIdFAST8
#define CC_PRIiFAST8 PRIiFAST8
#define CC_PRIuFAST8 PRIuFAST8
#define CC_PRIoFAST8 PRIoFAST8
#define CC_PRIxFAST8 PRIxFAST8
#define CC_PRIXFAST8 PRIXFAST8
#define CC_PRIdFAST16 PRIdFAST16
#define CC_PRIiFAST16 PRIiFAST16
#define CC_PRIuFAST16 PRIuFAST16
#define CC_PRIoFAST16 PRIoFAST16
#define CC_PRIxFAST16 PRIxFAST16
#define CC_PRIXFAST16 PRIXFAST16
#define CC_PRIdFAST32 PRIdFAST32
#define CC_PRIiFAST32 PRIiFAST32
#define CC_PRIuFAST32 PRIuFAST32
#define CC_PRIoFAST32 PRIoFAST32
#define CC_PRIxFAST32 PRIxFAST32
#define CC_PRIXFAST32 PRIXFAST32
#else
/* Fall back on C89's/C++98's dumb types. */
typedef signed char cc_s8l;
typedef signed short cc_s16l;
typedef signed long cc_s32l;
typedef unsigned char cc_u8l;
typedef unsigned short cc_u16l;
typedef unsigned long cc_u32l;
typedef signed int cc_s8f;
typedef signed int cc_s16f;
typedef signed long cc_s32f;
typedef unsigned int cc_u8f;
typedef unsigned int cc_u16f;
typedef unsigned long cc_u32f;
#define CC_PRIdLEAST8 "%d"
#define CC_PRIiLEAST8 "%i"
#define CC_PRIuLEAST8 "%u"
#define CC_PRIoLEAST8 "%o"
#define CC_PRIxLEAST8 "%x"
#define CC_PRIXLEAST8 "%X"
#define CC_PRIdLEAST16 "%d"
#define CC_PRIiLEAST16 "%i"
#define CC_PRIuLEAST16 "%u"
#define CC_PRIoLEAST16 "%o"
#define CC_PRIxLEAST16 "%x"
#define CC_PRIXLEAST16 "%X"
#define CC_PRIdLEAST32 "%ld"
#define CC_PRIiLEAST32 "%li"
#define CC_PRIuLEAST32 "%lu"
#define CC_PRIoLEAST32 "%lo"
#define CC_PRIxLEAST32 "%lx"
#define CC_PRIXLEAST32 "%lX"
#define CC_PRIdFAST8 "%d"
#define CC_PRIiFAST8 "%i"
#define CC_PRIuFAST8 "%u"
#define CC_PRIoFAST8 "%o"
#define CC_PRIxFAST8 "%x"
#define CC_PRIXFAST8 "%X"
#define CC_PRIdFAST16 "%d"
#define CC_PRIiFAST16 "%i"
#define CC_PRIuFAST16 "%u"
#define CC_PRIoFAST16 "%o"
#define CC_PRIxFAST16 "%x"
#define CC_PRIXFAST16 "%X"
#define CC_PRIdFAST32 "%ld"
#define CC_PRIiFAST32 "%li"
#define CC_PRIuFAST32 "%lu"
#define CC_PRIoFAST32 "%lo"
#define CC_PRIxFAST32 "%lx"
#define CC_PRIXFAST32 "%lX"
#endif
/* Boolean. */
typedef cc_u8l cc_bool;
enum
{
cc_false = 0,
cc_true = 1
};
#endif
#define CLOWNRESAMPLER_COUNT_OF(x) (sizeof(x) / sizeof(*(x)))
#define CLOWNRESAMPLER_MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLOWNRESAMPLER_MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLOWNRESAMPLER_CLAMP(min, max, x) (CLOWNRESAMPLER_MAX((min), CLOWNRESAMPLER_MIN((max), (x))))
#define CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE (1L << 16) /* For 16.16. This is good because it reduces multiplications and divisions to mere bit-shifts. */
#define CLOWNRESAMPLER_TO_FIXED_POINT_FROM_INTEGER(x) ((x) * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
#define CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_FLOOR(x) ((x) / CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
#define CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_ROUND(x) (((x) + (CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE / 2)) / CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
#define CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_CEILING(x) (((x) + (CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE - 1)) / CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
#define CLOWNRESAMPLER_FIXED_POINT_MULTIPLY(a, b) ((a) * (b) / CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
typedef struct ClownResampler_Precomputed
{
cc_s32l lanczos_kernel_table[CLOWNRESAMPLER_KERNEL_RADIUS * 2 * CLOWNRESAMPLER_KERNEL_RESOLUTION];
} ClownResampler_Precomputed;
typedef struct ClownResampler_LowestLevel_Configuration
{
cc_s32f sample_normaliser; /* 17.15 fixed point. */
size_t stretched_kernel_radius; /* 16.16 fixed point. */
size_t integer_stretched_kernel_radius;
size_t stretched_kernel_radius_delta; /* 16.16 fixed point. */
size_t kernel_step_size;
} ClownResampler_LowestLevel_Configuration;
typedef struct ClownResampler_LowLevel_State
{
ClownResampler_LowestLevel_Configuration lowest_level;
cc_u8f channels;
size_t position_integer;
cc_u32f position_fractional; /* 16.16 fixed point. */
cc_u32f increment; /* 16.16 fixed point. */
} ClownResampler_LowLevel_State;
typedef struct ClownResampler_HighLevel_State
{
ClownResampler_LowLevel_State low_level;
cc_s16l input_buffer[0x1000]; /* TODO: This should be dynamically allocated in accordance with the kernel radius... */
cc_s16l *input_buffer_start;
cc_s16l *input_buffer_end;
size_t maximum_integer_stretched_kernel_radius;
size_t leading_padding_frames_needed, trailing_padding_frames_remaining;
} ClownResampler_HighLevel_State;
typedef size_t (*ClownResampler_InputCallback)(void *user_data, cc_s16l *buffer, size_t total_frames);
typedef cc_bool (*ClownResampler_OutputCallback)(void *user_data, const cc_s32f *frame, cc_u8f total_samples);
#endif /* CLOWNRESAMPLER_GUARD_MISC */
#if !defined(CLOWNRESAMPLER_STATIC) || defined(CLOWNRESAMPLER_IMPLEMENTATION)
#ifdef __cplusplus
extern "C" {
#endif
#ifndef CLOWNRESAMPLER_GUARD_FUNCTION_DECLARATIONS
#define CLOWNRESAMPLER_GUARD_FUNCTION_DECLARATIONS
/* Common API.
This API is used for both the low-level and high-level APIs. */
/* Precomputes some data to improve the performance of the resampler.
Multiple resamplers can use the same 'ClownResampler_Precomputed'.
The output of this function is always the same, so if you want to avoid
calling this function, then you could dump the contents of the struct and
then insert a const 'ClownResampler_Precomputed' in your source code. */
CLOWNRESAMPLER_API void ClownResampler_Precompute(ClownResampler_Precomputed *precomputed);
/* Lowest-level API. */
CLOWNRESAMPLER_API cc_bool ClownResampler_LowestLevel_Configure(ClownResampler_LowestLevel_Configuration *configuration, cc_u32f input_sample_rate, cc_u32f output_sample_rate, cc_u32f low_pass_filter_sample_rate);
CLOWNRESAMPLER_API void ClownResampler_LowestLevel_Resample(const ClownResampler_LowestLevel_Configuration *configuration, const ClownResampler_Precomputed *precomputed, cc_s32f *output_frame, cc_u8f channels, const cc_s16l *input_buffer, size_t position_integer, cc_u32f position_fractional);
#endif /* CLOWNRESAMPLER_GUARD_FUNCTION_DECLARATIONS */
#ifndef CLOWNRESAMPLER_NO_LOW_LEVEL_API
/* Low-level API.
This API has lower overhead, but is more difficult to use, requiring that
audio be pre-processed before resampling.
Do NOT mix low-level API calls with high-level API calls for the same
resampler! */
/* Initialises a low-level resampler. This function must be called before the
state is passed to any other functions. The input and output sample rates do
not actually have to match the sample rates being used - they just need to
provide the ratio between the two (for example, 1 and 2 works just as well
as 22050 and 44100). Remember that a sample rate is double the frequency.
The 'channels' parameter must not be larger than
CLOWNRESAMPLER_MAXIMUM_CHANNELS.
Returns 'cc_false' on failure, and 'cc_true' otherwise. */
CLOWNRESAMPLER_API cc_bool ClownResampler_LowLevel_Init(ClownResampler_LowLevel_State *resampler, cc_u8f channels, cc_u32f input_sample_rate, cc_u32f output_sample_rate, cc_u32f low_pass_filter_sample_rate);
/* Adjusts properties of the resampler. The input and output sample rates do
not actually have to match the sample rates being used - they just need to
provide the ratio between the two (for example, 1 and 2 works just as well
as 22050 and 44100). Remember that a sample rate is double the frequency.
Returns 'cc_false' on failure, and 'cc_true' otherwise. */
CLOWNRESAMPLER_API cc_bool ClownResampler_LowLevel_Adjust(ClownResampler_LowLevel_State *resampler, cc_u32f input_sample_rate, cc_u32f output_sample_rate, cc_u32f low_pass_filter_sample_rate);
/* Resamples (pre-processed) audio. The 'total_input_frames' and
'total_output_frames' parameters measure the size of their respective
buffers in frames, not samples nor bytes.
The input buffer must be specially pre-processed, so that it is padded with
extra frames at the beginning and end. This is needed as the resampler will
unavoidably read past the beginning and the end of the audio data. The
specific number of frames needed at the beginning and end can be found in
the 'resampler->integer_stretched_kernel_radius' variable. If the audio you
are resampling is a chunk of a larger piece of audio, then the 'padding' at
the beginning and end must be the frames from before and after said chunk
of audio, otherwise these frames should just be 0. Note that these padding
frames must not be counted by the 'total_input_frames' parameter.
'output_callback' is a callback for outputting a single completed frame.
'frame' points to a series of samples corresponding a frame of audio.
'total_samples' is the number of samples in the frame, which will always
match the number of channels that was passed to
'ClownResampler_LowLevel_Init'. Must return 0 if no more frames are needed,
in which case this function terminates. The 'user_data' parameter is the
same as the 'user_data' parameter of this function.
After this function returns, the 'total_input_frames' parameter will
contain the number of frames in the input buffer that were not processed.
This function will return 'cc_true' if it terminated because it ran out of
input samples, or 'cc_false' if it terminated because the callback returned
0. */
CLOWNRESAMPLER_API cc_bool ClownResampler_LowLevel_Resample(ClownResampler_LowLevel_State *resampler, const ClownResampler_Precomputed *precomputed, const cc_s16l *input_buffer, size_t *total_input_frames, ClownResampler_OutputCallback output_callback, const void *user_data);
#endif /* CLOWNRESAMPLER_NO_LOW_LEVEL_API */
#ifndef CLOWNRESAMPLER_NO_HIGH_LEVEL_API
/* High-level API.
This API has more overhead, but is easier to use.
Do NOT mix high-level API calls with low-level API calls for the same
resampler! */
/* Initialises a high-level resampler. This function must be called before the
state is passed to any other functions. The input and output sample rates do
not actually have to match the sample rates being used - they just need to
provide the ratio between the two (for example, 1 and 2 works just as well
as 22050 and 44100). Remember that a sample rate is double the frequency.
The 'channels' parameter must not be larger than
CLOWNRESAMPLER_MAXIMUM_CHANNELS.
Returns 'cc_false' on failure, and 'cc_true' otherwise. */
CLOWNRESAMPLER_API cc_bool ClownResampler_HighLevel_Init(ClownResampler_HighLevel_State *resampler, cc_u8f channels, cc_u32f input_sample_rate, cc_u32f output_sample_rate, cc_u32f low_pass_filter_sample_rate);
/* Resamples audio. This function returns when either the output buffer is
full, or the input callback stops providing frames.
This function will return 'cc_true' if it terminated because the input
callback returned 0, or 'cc_false' if it terminated because the output
callback returned 0.
The parameters are as follows:
'resampler'
A pointer to a state struct that was previously initialised with the
'ClownResampler_HighLevel_Init' function.
'output_buffer'
A pointer to a buffer which the resampled audio will be written to.
The size of the audio buffer will be specified by the 'total_output_frames'
variable.
'total_output_frames'
The size of the buffer specified by the 'output_buffer' parameter. The size
is measured in frames, not samples nor bytes.
'input_callback'
A callback for retrieving frames of the input audio. The callback must
write frames to the buffer pointed to by the 'buffer' parameter. The
'total_frames' parameter specifies the maximum number of frames that can be
written to the buffer. The callback must return the number of frames that
were written to the buffer. If the callback returns 0, then this function
terminates. The 'user_data' parameter is the same as the 'user_data'
parameter of this function.
'output_callback'
A callback for outputting a single completed frame. 'frame' points to a
series of samples corresponding a frame of audio. 'total_samples' is the
number of samples in the frame, which will always match the number of
channels that was passed to 'ClownResampler_HighLevel_Init'. Must return 0
if no more frames are needed, in which case this function terminates. The
'user_data' parameter is the same as the 'user_data' parameter of this
function.
'user_data'
An arbitrary pointer that is passed to the callback functions. */
CLOWNRESAMPLER_API cc_bool ClownResampler_HighLevel_Resample(ClownResampler_HighLevel_State *resampler, const ClownResampler_Precomputed *precomputed, ClownResampler_InputCallback input_callback, ClownResampler_OutputCallback output_callback, const void *user_data);
#endif /* CLOWNRESAMPLER_NO_HIGH_LEVEL_API */
#if !defined(CLOWNRESAMPLER_NO_HIGH_LEVEL_ADJUST) && !defined(CLOWNRESAMPLER_NO_HIGH_LEVEL_API)
/* Adjusts properties of the resampler. The input and output sample rates do
not actually have to match the sample rates being used - they just need to
provide the ratio between the two (for example, 1 and 2 works just as well
as 22050 and 44100). Remember that a sample rate is double the frequency.
Unlike in the low-level API, when the input sample rate is higher than the
output sample rate, the ratio between the two MUST NOT be wider than that
of the rates passed to the 'ClownResampler_HighLevel_Init' function.
Returns 'cc_false' on failure, and 'cc_true' otherwise. */
CLOWNRESAMPLER_API cc_bool ClownResampler_HighLevel_Adjust(ClownResampler_HighLevel_State *resampler, cc_u32f input_sample_rate, cc_u32f output_sample_rate, cc_u32f low_pass_filter_sample_rate);
#endif /* CLOWNRESAMPLER_NO_HIGH_LEVEL_ADJUST */
#if !defined(CLOWNRESAMPLER_NO_HIGH_LEVEL_RESAMPLE_END) && !defined(CLOWNRESAMPLER_NO_HIGH_LEVEL_API)
/* This is to be used after the final call to
'ClownResampler_HighLevel_Resample', to output the last few samples.
Returns 'cc_true' when the final sample has been output. */
CLOWNRESAMPLER_API cc_bool ClownResampler_HighLevel_ResampleEnd(ClownResampler_HighLevel_State *resampler, const ClownResampler_Precomputed *precomputed, ClownResampler_OutputCallback output_callback, const void *user_data);
#endif /* CLOWNRESAMPLER_NO_HIGH_LEVEL_RESAMPLE_END */
#ifdef __cplusplus
}
#endif
#endif /* !defined(CLOWNRESAMPLER_STATIC) || defined(CLOWNRESAMPLER_IMPLEMENTATION) */
/* 4. Implementation */
#ifdef CLOWNRESAMPLER_IMPLEMENTATION
#ifndef CLOWNRESAMPLER_GUARD_FUNCTION_DEFINITIONS
#define CLOWNRESAMPLER_GUARD_FUNCTION_DEFINITIONS
/* These can be used to provide your own C standard library functions. */
#ifndef CLOWNRESAMPLER_ASSERT
#include <assert.h>
#define CLOWNRESAMPLER_ASSERT assert
#endif
#ifndef CLOWNRESAMPLER_FABS
#include <math.h>
#define CLOWNRESAMPLER_FABS fabs
#endif
#ifndef CLOWNRESAMPLER_SIN
#include <math.h>
#define CLOWNRESAMPLER_SIN sin
#endif
#ifndef CLOWNRESAMPLER_ZERO
#include <string.h>
#define CLOWNRESAMPLER_ZERO(buffer, size) memset(buffer, 0, size)
#endif
#ifndef CLOWNRESAMPLER_MEMMOVE
#include <string.h>
#define CLOWNRESAMPLER_MEMMOVE memmove
#endif
#include <stddef.h>
static double ClownResampler_LanczosKernel(const double x)
{
const double kernel_radius = (double)CLOWNRESAMPLER_KERNEL_RADIUS;
const double x_times_pi = x * 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; /* 100 digits should be good enough. */
const double x_times_pi_divided_by_radius = x_times_pi / kernel_radius;
/*CLOWNRESAMPLER_ASSERT(x != 0.0);*/
if (x == 0.0)
return 1.0;
CLOWNRESAMPLER_ASSERT(CLOWNRESAMPLER_FABS(x) <= kernel_radius);
/*if (CLOWNRESAMPLER_FABS(x) > kernel_radius)
return 0.0f*/
return (CLOWNRESAMPLER_SIN(x_times_pi) * CLOWNRESAMPLER_SIN(x_times_pi_divided_by_radius)) / (x_times_pi * x_times_pi_divided_by_radius);
}
/* Common API */
static cc_u32f ClownResampler_CalculateRatio(const cc_u32f a, const cc_u32f b)
{
/* HAHAHA, I NEVER THOUGHT LONG DIVISION WOULD ACTUALLY COME IN HANDY! */
cc_u32f upper, middle, lower, result;
/* A hack to prevent crashes when either sample rate is 0. */
if (a == 0 || b == 0)
return 0xFFFFFFFF;
/* As well as splitting the number into chunks of CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE
size, this sneakily also multiplies it by CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE. */
upper = a / CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;
middle = a % CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;
lower = 0;
/* Perform long division. */
middle |= upper % b * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;
upper /= b;
lower |= middle % b * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;
middle /= b;
/*even_lower |= lower % b * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;*/ /* Nothing to feed the remainder into... */
lower /= b;
/* Detect overflow. */
if (upper != 0 || middle >= CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE)
return 0xFFFFFFFF;
/* Merge the chunks back together. */
result = 0;
/*result += upper * ((cc_u32f)CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE);*/
result += middle * CLOWNRESAMPLER_FIXED_POINT_FRACTIONAL_SIZE;
result += lower;
/* Detect underflow. */
if (result == 0)
return 1;
return result;
}
CLOWNRESAMPLER_API void ClownResampler_Precompute(ClownResampler_Precomputed* const precomputed)
{
size_t i;
for (i = 0; i < CLOWNRESAMPLER_COUNT_OF(precomputed->lanczos_kernel_table); ++i)
precomputed->lanczos_kernel_table[i] = (cc_s32l)CLOWNRESAMPLER_TO_FIXED_POINT_FROM_INTEGER(ClownResampler_LanczosKernel(((double)i / (double)CLOWNRESAMPLER_COUNT_OF(precomputed->lanczos_kernel_table) * 2.0 - 1.0) * (double)CLOWNRESAMPLER_KERNEL_RADIUS));
}
CLOWNRESAMPLER_API cc_bool ClownResampler_LowestLevel_Configure(ClownResampler_LowestLevel_Configuration* const configuration, const cc_u32f input_sample_rate, const cc_u32f output_sample_rate, const cc_u32f low_pass_filter_sample_rate)
{
/* Determine the kernel scale. This is used to apply a low-pass filter. Not only is this something that the user may
explicitly request, but it is needed when downsampling to avoid artefacts. */
/* Note that we do not ever want the kernel to be squished, but rather only stretched. */
const cc_u32f actual_low_pass_sample_rate = CLOWNRESAMPLER_MIN(input_sample_rate, CLOWNRESAMPLER_MIN(output_sample_rate, low_pass_filter_sample_rate));
const cc_u32f kernel_scale = ClownResampler_CalculateRatio(input_sample_rate, actual_low_pass_sample_rate);
const cc_u32f inverse_kernel_scale = ClownResampler_CalculateRatio(actual_low_pass_sample_rate, input_sample_rate);
/* Bail on crazy ratios. */
/* TODO: Maybe use better ratio logic to raise this limit? */
if (kernel_scale >= CLOWNRESAMPLER_TO_FIXED_POINT_FROM_INTEGER(0x1000))
return cc_false;
configuration->stretched_kernel_radius = CLOWNRESAMPLER_KERNEL_RADIUS * kernel_scale;
configuration->integer_stretched_kernel_radius = CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_CEILING(configuration->stretched_kernel_radius);
configuration->stretched_kernel_radius_delta = CLOWNRESAMPLER_TO_FIXED_POINT_FROM_INTEGER(configuration->integer_stretched_kernel_radius) - configuration->stretched_kernel_radius;
CLOWNRESAMPLER_ASSERT(configuration->stretched_kernel_radius_delta < CLOWNRESAMPLER_TO_FIXED_POINT_FROM_INTEGER(1));
configuration->kernel_step_size = CLOWNRESAMPLER_FIXED_POINT_MULTIPLY(CLOWNRESAMPLER_KERNEL_RESOLUTION, inverse_kernel_scale);
/* The wider the kernel, the greater the number of taps, the louder the sample. */
/* Note that the scale is cast to 'long' here. This is to prevent samples from being promoted to
'unsigned long' later on, which breaks their sign-extension. Also note that we convert from
16.16 to 17.15 here. */
configuration->sample_normaliser = (cc_s32f)(inverse_kernel_scale >> (16 - 15));
return cc_true;
}
CLOWNRESAMPLER_API void ClownResampler_LowestLevel_Resample(const ClownResampler_LowestLevel_Configuration* const configuration, const ClownResampler_Precomputed* const precomputed, cc_s32f* const output_frame, const cc_u8f channels, const cc_s16l* const input_buffer, const size_t position_integer, const cc_u32f position_fractional)
{
cc_u8f current_channel;
size_t sample_index, kernel_index;
/* Calculate the bounds of the kernel convolution. */
const size_t min_relative = CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_CEILING(position_fractional + configuration->stretched_kernel_radius_delta);
const size_t max_relative = CLOWNRESAMPLER_TO_INTEGER_FROM_FIXED_POINT_FLOOR(position_fractional + configuration->stretched_kernel_radius);