-
Notifications
You must be signed in to change notification settings - Fork 0
/
px_audio.h
2641 lines (2137 loc) · 80.3 KB
/
px_audio.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
#include <stdarg.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#ifndef PX_GLOBALS_H
#define PX_GLOBALS_H
// Math Constants
// ---------------------------------
#define DC_OFFSET 1.0E-25
#define PI 3.141592653589793
#define sgn(val) ((0 < val) - (val < 0))
// ---------------------------------
// Gain
// linear -> dB conversion
static inline float lin2dB(float lin) {
static const float LOG_2_DB = 8.6858896380650365530225783783321; // 20 / ln( 10 )
return log(lin) * LOG_2_DB;
}
// dB -> linear conversion
static inline float dB2lin(float dB) {
static const float DB_2_LOG = 0.11512925464970228420089957273422; // ln( 10 ) / 20
return exp(dB * DB_2_LOG);
}
// -------------------------------------
//
// Mid Side
//
//
typedef enum
{
BOTH = 0,
LEFT,
RIGHT,
MID,
SIDE
} CHANNEL_FLAG;
typedef struct
{
float left;
float right;
} px_ms_decoded;
typedef struct
{
float mid;
float side;
} px_ms_encoded;
static inline px_ms_encoded px_ms_encode(px_ms_decoded decoded)
{
px_ms_encoded encoded;
encoded.mid = 0.5f * (decoded.left + decoded.right);
encoded.side = 0.5f * (decoded.left - decoded.right);
return encoded;
}
static inline px_ms_decoded px_ms_decode(px_ms_encoded encoded)
{
px_ms_decoded decoded;
decoded.left = encoded.mid + encoded.side;
decoded.right = encoded.mid - encoded.side;
return decoded;
}
// assert for process functions
// ------------------------------------------------------------------------------------------------------
static void px_assert_mono(void* control_pointer, float* value_pointer)
{
// check if DSP object passed is NULL or uninitialized
assert(control_pointer);
// check if value is valid float pointer
assert(value_pointer);
}
static void px_assert_stereo(void* control_pointer, float* channel_one_pointer, float* channel_two_pointer)
{
// check if DSP object passed is NULL or uninitialized
assert(control_pointer);
// check if value is valid float pointer
assert(channel_one_pointer);
assert(channel_two_pointer);
}
#define EXPAND(x) x
#define GET_ASSERT_MACRO(_1, _2, _3, NAME, ...) NAME
#define px_assert(...) EXPAND(GET_ASSERT_MACRO(__VA_ARGS__, px_assert_stereo, px_assert_mono)(__VA_ARGS__))
#endif
#ifndef PX_MEMORY_H
#define PX_MEMORY_H
#define px_malloc(a) \
malloc(a); \
printf("malloc at %s line: %d in %s \n", __func__, __LINE__, __FILE__); \
#define px_free(a) \
free(a); \
printf("free at %s line: %d in %s \n", __func__, __LINE__, __FILE__); \
#endif
#ifndef PX_VECTOR_H
#define PX_VECTOR_H
/* -------------------------------------------------------------------------
type generic vector
-------------------------------------------------------------------------*/
typedef struct {
size_t size;
size_t capacity;
void** data;
} px_vector;
static px_vector* px_vector_create();
static void px_vector_initialize(px_vector* vector);
static void px_vector_destroy(px_vector* vector);
static void px_vector_push(px_vector* vector, void* value);
static void px_vector_pop(px_vector* vector);
static void* px_vector_get(px_vector* vector, size_t index);
static void px_vector_remove(px_vector* vector, size_t index);
static void px_vector_copy(px_vector* dest_vector, px_vector* source_vector);
static void px_vector_resize(px_vector* vector, const size_t new_size);
// ----------------------------------------------------------------------------
static px_vector* px_vector_create()
{
px_vector* vector = (px_vector*)px_malloc(sizeof(px_vector));
px_vector_initialize(vector);
return vector;
}
static void px_vector_initialize(px_vector* vector)
{
assert(vector);
vector->data = NULL;
vector->size = 0;
vector->capacity = 0;
}
static void px_vector_destroy(px_vector* vector)
{
if (vector)
{
px_free(vector->data);
px_free(vector);
}
}
static void px_vector_push(px_vector* vector, void* value)
{
assert(vector);
if (vector->size == vector->capacity)
{
size_t new_capacity = (vector->capacity == 0) ? 1 : vector->capacity * 2;
void** new_data = (void**)realloc(vector->data, sizeof(void*) * new_capacity);
if (new_data)
{
vector->data = new_data;
vector->capacity = new_capacity;
}
}
vector->data[vector->size] = value;
vector->size++;
}
static void px_vector_pop(px_vector* vector)
{
assert(vector);
if (vector->size > 0)
{
vector->size--;
vector->data[vector->size] = NULL;
}
}
static void* px_vector_get(px_vector* vector, size_t index)
{
assert(vector);
void* ptr;
if (index < vector->size)
{
ptr = vector->data[index];
}
else
{
printf("OUT OF RANGE");
ptr = NULL;
}
return ptr;
}
static void px_vector_remove(px_vector* vector , size_t index)
{
assert(vector);
if (index < vector->size)
{
vector->data[index] = NULL;
for (size_t i = index; i < (vector->size - 1); ++i)
{
vector->data[i] = vector->data[i+1];
}
vector->size--;
}
else
{
printf("OUT OF RANGE");
}
}
static void px_vector_copy(px_vector* dest_vector, px_vector* source_vector)
{
assert(dest_vector && source_vector);
dest_vector->size = source_vector->size;
dest_vector->capacity = source_vector->capacity;
void** new_data = (void**)px_malloc(sizeof(void*) * dest_vector->capacity);
if (new_data)
{
memcpy(new_data, source_vector->data, sizeof(void*) * source_vector->size);
dest_vector->data = new_data;
}
}
static void px_vector_resize(px_vector* vector, const size_t new_size)
{
if (!vector)
return;
if (new_size == vector->size)
return;
if (new_size > vector->capacity)
{
size_t new_capacity = new_size * 2;
void** new_data = (void**)realloc(vector->data, sizeof(void*) * new_capacity);
if (new_data)
{
vector->data = new_data;
vector->capacity = new_capacity;
}
}
vector->size = new_size;
}
#endif
#ifndef PX_BUFFER_H
#define PX_BUFFER_H
/*
Somewhat type generic (double/float) buffer for working with px_. Includes internal array of type generic (void*) px_vector.
//include
#define PX_FLOAT_BUFFER
or:
#define PX_DOUBLE_BUFFER
//init
stack initialize:
px_buffer buffer;
px_buffer_initialize(&buffer, (int)num_samples, (int)num_channels);
heap:
px_buffer* buffer = px_buffer_create( (int)num_samples, (int)num_channels);
// initialize called within create()
free:
px_buffer_destroy(buffer);
// use
get_sample:
for (int i = 0; i < buffer.num_samples; ++i)
{
float leftValue = px_buffer_get_sample(&buffer, 0, i);
float rightValue = px_buffer_get_sample(&buffer, 1, i);
}
for (int channel = 0; i < buffer.num_channels; ++channel)
{
for (int sample = 0; i < buffer.num_samples; ++sample)
{
float value = px_buffer_get_sample(&buffer, channel, i);
}
}
get_pointer:
for (int i = 0; i < buffer.num_samples; ++i)
{
float* left_value = px_buffer_get_pointer(&buffer, 0, i);
float* right_value = px_buffer_get_pointer(&buffer, 1, i);
}
for (int channel = 0; i < buffer.num_channels; ++channel)
{
for (int sample = 0; i < buffer.num_samples; ++sample)
{
float* value = px_buffer_get_pointer(&buffer, channel, i);
}
}
*/
#ifndef MAX_CHANNELS
#define MAX_CHANNELS 4
#endif
typedef struct
{
int num_samples;
int num_channels;
px_vector vector[MAX_CHANNELS];
bool is_filled;
} px_buffer;
#ifdef PX_FLOAT_BUFFER
#define BUFFER_TYPE float
#elif PX_DOUBLE_BUFFER
#define BUFFER_TYPE double
#endif
// --------------------------------------------------------------------------------------------------------
static px_buffer* px_buffer_create(int num_samples, int num_channels);
static void px_buffer_destroy(px_buffer* buffer);
static void px_buffer_initialize(px_buffer* buffer, int num_samples, int num_channels);
static void px_buffer_set_sample(px_buffer* buffer, int channel, int sample_position, BUFFER_TYPE value);
static BUFFER_TYPE px_buffer_get_sample(px_buffer* buffer, int channel, int sample_position);
static BUFFER_TYPE* px_buffer_get_pointer(px_buffer* buffer, int channel, int sample_position);
static void px_buffer_gain(px_buffer* buffer, BUFFER_TYPE in_gain);
// --------------------------------------------------------------------------------------------------------
static px_buffer* px_buffer_create(int num_samples, int num_channels)
{
px_buffer* buffer = (px_buffer*)px_malloc(sizeof(px_buffer));
px_buffer_initialize(buffer, num_samples, num_channels);
return buffer;
}
static void px_buffer_destroy(px_buffer* buffer)
{
if (buffer)
{
for (int channel = 0; channel < buffer->num_channels; ++channel)
{
if (buffer->vector[channel].data)
{
px_free(buffer->vector[channel].data);
buffer->vector[channel].data = NULL;
}
}
px_free(buffer);
}
}
static void px_buffer_initialize(px_buffer* buffer, int num_samples, int num_channels)
{
assert(buffer);
buffer->num_samples = num_samples;
buffer->num_channels = num_channels;
for (int channel = 0; channel < num_channels; ++channel)
{
px_vector_initialize(&buffer->vector[channel]);
buffer->vector[channel].data = (void**)px_malloc(num_samples * sizeof(BUFFER_TYPE*));
}
}
static void px_buffer_set_sample(px_buffer* buffer, int channel, int sample_position, BUFFER_TYPE value)
{
if ( channel > buffer->num_channels || sample_position > buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return;
}
BUFFER_TYPE* ptr = &value;
buffer->vector[channel].data[sample_position] = ptr;
}
static BUFFER_TYPE px_buffer_get_sample(px_buffer* buffer, int channel, int sample_position)
{
if ( channel > buffer->num_channels || sample_position > buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return 0.f;
}
BUFFER_TYPE* ptr = (BUFFER_TYPE*)buffer->vector[channel].data[sample_position];
return *ptr;
}
static BUFFER_TYPE* px_buffer_get_pointer(px_buffer* buffer, int channel, int sample_position)
{
if ( channel > buffer->num_channels || sample_position > buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return NULL;
}
BUFFER_TYPE* ptr = (BUFFER_TYPE*)buffer->vector[channel].data[sample_position];
if (ptr)
return ptr;
else
return NULL;
}
static void px_buffer_gain(px_buffer* buffer, BUFFER_TYPE in_gain)
{
assert(buffer);
for (int channel = 0; channel < buffer->num_channels; ++channel)
{
for (int i = 0; i < buffer->num_samples; ++i)
{
BUFFER_TYPE* ptr = (float*)buffer->vector[channel].data[i];
*ptr *= in_gain;
buffer->vector[channel].data[i] = ptr;
}
}
}
typedef struct {
float* data;
int head;
int tail;
int max_length;
} px_circular_buffer;
static void px_circular_push(px_circular_buffer* buffer, float value)
{
int next = (buffer->head + 1) % buffer->max_length;
if (next == buffer->tail)
buffer->tail = (buffer->tail + 1) % buffer->max_length;
buffer->data[buffer->head] = value;
buffer->head = next;
}
static float px_circular_pop(px_circular_buffer* buffer)
{
assert(buffer->head != buffer->tail); // Buffer is not empty
float value = buffer->data[buffer->tail];
int next = buffer->tail + 1;
if (next >= buffer->max_length)
next = 0;
buffer->tail = next;
return value;
}
static float px_circular_get_sample(px_circular_buffer* buffer, size_t index)
{
assert(index >= 0 && index < buffer->max_length);
return buffer->data[index % buffer->max_length];
}
static void px_circular_initialize(px_circular_buffer* buffer, int max_length)
{
assert(max_length > 0);
buffer->data = (float*)px_malloc(sizeof(float) * max_length);
memset(buffer->data, 0, sizeof(float) * max_length);
buffer->head = 0;
buffer->tail = 0;
buffer->max_length = max_length;
}
static void px_circular_resize(px_circular_buffer* buffer, int new_size)
{
float* new_data = (float*)px_malloc(sizeof(float) * new_size);
int i = 0;
int j = buffer->head;
while (i < buffer->max_length) {
new_data[i] = buffer->data[j];
j = (j+1) % buffer->max_length;
i++;
}
px_free(buffer->data);
buffer->data = new_data;
buffer->head = 0;
buffer->tail = buffer->max_length-1;
buffer->max_length = new_size;
}
#endif
#ifndef PX_DELAY_H
#define PX_DELAY_H
typedef struct
{
float seconds;
// split for sample interpolation
float fraction;
int whole;
} delay_time;
typedef struct
{
float sample_rate;
float feedback;
delay_time time;
float max_time; // in seconds
float dry_wet;
} px_delay_parameters;
typedef struct
{
px_delay_parameters parameters;
px_circular_buffer buffer;
} px_delay_line;
typedef struct
{
bool ping_pong;
px_delay_line left;
px_delay_line right;
} px_stereo_delay;
static px_delay_line* px_create_mono_delay(float sample_rate, float max_time);
static void px_destroy_mono_delay(px_delay_line* delay);
static void px_delay_mono_free_buffer(px_delay_line* delay);
static void px_delay_mono_initialize(px_delay_line* delay, float sample_rate, float max_time);
static void px_delay_mono_prepare(px_delay_line* delay, float sample_rate);
static void px_delay_mono_set_time(px_delay_line* delay, float time);
static void px_delay_mono_set_feedback(px_delay_line* delay, float feedback);
static void px_delay_mono_process(px_delay_line* delay, float* input);
static px_stereo_delay* px_create_stereo_delay(float sample_rate, float max_time, bool ping_pong);
static void px_destroy_stereo_delay(px_stereo_delay* delay);
static void px_delay_stereo_free_buffer(px_stereo_delay* delay);
static void px_delay_stereo_initialize(px_stereo_delay* delay, float sample_rate, float max_time, bool ping_pong);
static void px_delay_stereo_prepare(px_stereo_delay* delay, float sample_rate);
static void px_delay_stereo_set_time(px_stereo_delay* delay, float time, CHANNEL_FLAG channel);
static void px_delay_stereo_set_feedback(px_stereo_delay* delay, float feedback, CHANNEL_FLAG channel);
static void px_delay_stereo_set_ping_pong(px_stereo_delay* delay, bool ping_pong);
static void px_delay_stereo_process(px_stereo_delay* delay, float* input_left, float* input_right);
static px_delay_line* px_create_mono_delay(float sample_rate, float max_time)
{
px_delay_line* delay = (px_delay_line*) px_malloc(sizeof(px_delay_line));
assert(delay);
px_delay_mono_initialize(delay, sample_rate, max_time);
return delay;
}
static void px_destroy_mono_delay(px_delay_line* delay)
{
if (delay)
{
px_free(delay->buffer.data);
px_free(delay);
}
}
static px_stereo_delay* px_create_stereo_delay(float sample_rate, float max_time, bool ping_pong)
{
px_stereo_delay* delay = (px_stereo_delay*) px_malloc(sizeof(px_stereo_delay));
assert(delay);
px_delay_stereo_initialize(delay, sample_rate, max_time, ping_pong);
return delay;
}
static void px_destroy_stereo_delay(px_stereo_delay* delay)
{
if (delay)
{
px_free(delay->left.buffer.data);
px_free(delay->right.buffer.data);
px_free(delay);
}
}
static void px_delay_mono_initialize(px_delay_line* delay, float sample_rate, float max_time)
{
assert(delay);
// TODO REMOVE THIS
if (delay->parameters.max_time == max_time)
{
px_delay_mono_prepare(delay, sample_rate);
return;
}
delay_time time = { 1.f, 0.f, 1 };
px_delay_parameters parameters = { sample_rate, 0.5f, time, max_time, 0.5f };
delay->parameters = parameters;
int max_samples = sample_rate * max_time;
px_circular_initialize(&delay->buffer, max_samples);
}
static void px_delay_stereo_initialize(px_stereo_delay* delay, float sample_rate, float max_time, bool ping_pong)
{
assert(delay);
delay_time time = {1.f, 0.f, 1 };
px_delay_parameters parameters = { sample_rate, 0.5f, time, max_time, 0.5f };
delay->ping_pong = ping_pong;
delay->left.parameters = parameters;
delay->right.parameters = parameters;
int max_samples = sample_rate * max_time;
px_circular_initialize(&delay->left.buffer, max_samples);
px_circular_initialize(&delay->right.buffer, max_samples);
}
static void px_delay_mono_free_buffer(px_delay_line* delay)
{
if (delay)
{
px_free(delay->buffer.data);
}
}
static void px_delay_stereo_free_buffer(px_stereo_delay* delay)
{
if (delay)
{
px_free(delay->left.buffer.data);
px_free(delay->right.buffer.data);
}
}
static void px_delay_mono_prepare(px_delay_line* delay, float sample_rate)
{
assert(delay);
delay->parameters.sample_rate = sample_rate;
int max_samples = sample_rate * delay->parameters.max_time;
px_circular_initialize(&delay->buffer, max_samples);
}
static void px_delay_stereo_prepare(px_stereo_delay* delay, float sample_rate)
{
assert(delay);
delay->left.parameters.sample_rate = sample_rate;
delay->right.parameters.sample_rate = sample_rate;
//error with initialization
assert(delay->left.parameters.max_time == delay->right.parameters.max_time);
int max_samples = sample_rate * delay->left.parameters.max_time;
px_circular_initialize(&delay->left.buffer, max_samples);
px_circular_initialize(&delay->right.buffer, max_samples);
}
static void px_delay_mono_set_time(px_delay_line* delay, float time)
{
assert(delay);
assert(time > 0 && time < delay->parameters.max_time);
delay->parameters.time.seconds = time;
float time_in_samples = delay->parameters.sample_rate * time;
delay->parameters.time.whole = (int)floorf(time_in_samples);
delay->parameters.time.fraction = time_in_samples - delay->parameters.time.whole;
}
static void px_delay_stereo_set_time(px_stereo_delay* delay, float time, CHANNEL_FLAG channel)
{
assert(delay);
assert(time>0 && time < delay->left.parameters.max_time);
switch (channel)
{
case BOTH:
{
px_delay_mono_set_time(&delay->left, time);
px_delay_mono_set_time(&delay->right, time);
}
case LEFT:
{
px_delay_mono_set_time(&delay->left, time);
}
case RIGHT:
{
px_delay_mono_set_time(&delay->right, time);
}
}
}
static void px_delay_mono_set_feedback(px_delay_line* delay, float feedback)
{
assert(delay);
assert(feedback < 1.01f);
delay->parameters.feedback = feedback;
}
static void px_delay_stereo_set_feedback(px_stereo_delay* delay, float feedback, CHANNEL_FLAG channel)
{
assert(delay);
assert(feedback < 1.01f);
switch (channel)
{
case BOTH:
{
px_delay_mono_set_feedback(&delay->left, feedback);
px_delay_mono_set_feedback(&delay->right, feedback);
}
case LEFT:
{
px_delay_mono_set_feedback(&delay->left, feedback);
}
case RIGHT:
{
px_delay_mono_set_feedback(&delay->right, feedback);
}
}
}
static void px_delay_stereo_set_ping_pong(px_stereo_delay* delay, bool ping_pong)
{
assert(delay);
delay->ping_pong = ping_pong;
}
static void px_delay_mono_process(px_delay_line* delay, float* input)
{
px_assert(delay, input);
int read1 = (delay->buffer.head - delay->parameters.time.whole + delay->buffer.max_length) % delay->buffer.max_length;
int read2 = (read1 + 1) % delay->buffer.max_length;
float delayed1 = px_circular_get_sample(&delay->buffer, (size_t) read1);
float delayed2 = px_circular_get_sample(&delay->buffer, (size_t) read2);
// linear interpolation
float delayed_interp = delayed1 + delay->parameters.time.fraction * (delayed2 - delayed1);
float feedback = (*input) + (delay->parameters.feedback * delayed_interp);
px_circular_push(&delay->buffer, feedback);
float output = ((1.0f - delay->parameters.dry_wet) * (*input)) + (delay->parameters.dry_wet * delayed_interp);
*input = output;
}
static void px_delay_stereo_process(px_stereo_delay* delay, float* input_left, float* input_right)
{
px_assert(delay, input_left, input_right);
if (delay->ping_pong)
{
int read_left1 = (delay->left.buffer.head - delay->left.parameters.time.whole + delay->left.buffer.max_length) % delay->left.buffer.max_length;
int read_right1 = (delay->right.buffer.head - (delay->right.parameters.time.whole + (delay->left.parameters.time.whole / 2)) + delay->right.buffer.max_length) % delay->left.buffer.max_length;
int read_left2 = (read_left1 + 1) % delay->left.buffer.max_length;
int read_right2 = (read_right1 + 1) % delay->right.buffer.max_length;
float delayed_left1 = px_circular_get_sample(&delay->left.buffer, (size_t) read_left1);
float delayed_left2 = px_circular_get_sample(&delay->left.buffer, (size_t) read_left2);
float delayed_right1 = px_circular_get_sample(&delay->right.buffer, (size_t) read_right1);
float delayed_right2 = px_circular_get_sample(&delay->right.buffer, (size_t) read_right2);
float delayed_interp_left = delayed_left1 + delay->left.parameters.time.fraction * (delayed_left2 - delayed_left1);
float delayed_interp_right = delayed_right1 + delay->right.parameters.time.fraction * (delayed_right2 - delayed_right1);
float feedback_left = (*input_left) + (delay->left.parameters.feedback * delayed_interp_right); // Right feedback to left
float feedback_right = (*input_right) + (delay->right.parameters.feedback * delayed_interp_left); // Left feedback to right
// Push feedback into respective buffers
px_circular_push(&delay->left.buffer, feedback_left);
px_circular_push(&delay->right.buffer, feedback_right);
*input_left = ((1.0f - delay->left.parameters.dry_wet) * (*input_left)) + (delay->left.parameters.dry_wet * delayed_interp_left);
*input_right = ((1.0f - delay->right.parameters.dry_wet) * (*input_right)) + (delay->right.parameters.dry_wet * delayed_interp_right);
}
else
{
px_delay_mono_process(&delay->left, input_left);
px_delay_mono_process(&delay->right, input_right);
}
}
#endif
#ifndef PX_BIQUAD_H
#define PX_BIQUAD_H
// PX_BIQUAD filter
//
//
typedef enum {
BIQUAD_NONE,
BIQUAD_LOWPASS,
BIQUAD_HIGHPASS,
BIQUAD_BANDPASS,
BIQUAD_NOTCH,
BIQUAD_PEAK,
BIQUAD_LOWSHELF,
BIQUAD_HIGHSHELF,
BIQUAD_LOWSHELF_NOQ,
BIQUAD_HIGHSHELF_NOQ,
BIQUAD_ALLPASS
} BIQUAD_FILTER_TYPE;
typedef struct
{
float a0;
float a1;
float a2;
float b1;
float b2;
float z1;
float z2;
} px_biquad_coefficients;
typedef struct
{
float sample_rate;
float frequency;
float quality;
float gain;
BIQUAD_FILTER_TYPE type;
} px_biquad_parameters;
typedef struct
{
px_biquad_coefficients coefficients;
px_biquad_parameters parameters;
} px_biquad;
// api functions
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
static px_biquad* px_biquad_create(float sample_rate, BIQUAD_FILTER_TYPE type);
static void px_biquad_destroy(px_biquad* biquad);
static void px_biquad_process(px_biquad* biquad, float* input);
static void px_biquad_initialize(px_biquad* biquad, float sample_rate, BIQUAD_FILTER_TYPE type);
static void px_biquad_set_frequency(px_biquad* biquad, float in_frequency);
static void px_biquad_set_quality(px_biquad* biquad, float in_quality);
static void px_biquad_set_gain(px_biquad* biquad, float in_gain);
static void px_biquad_set_type(px_biquad* biquad, BIQUAD_FILTER_TYPE in_type);
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// inline functions
// ----------------------------------------------------------------------------------
static inline float px_biquad_filter(px_biquad* biquad, float input);
static inline void px_biquad_update_coefficients(const px_biquad_parameters parameters, px_biquad_coefficients* coefficients);
// ---------------------------------------------------------------------------------------
static void px_biquad_process(px_biquad* biquad, float* input)
{
px_assert(biquad, input);
float mono = *input;
*input = px_biquad_filter(biquad, mono);
}
static void px_biquad_initialize(px_biquad* biquad, float sample_rate, BIQUAD_FILTER_TYPE type)
{
assert(biquad);
px_biquad_parameters parameters = { sample_rate, 100.f, 0.5f, 0.f, type };
px_biquad_coefficients coefficients = { 1.f, 0.f, 0.0f, 0.0f, 0.0f };
px_biquad_update_coefficients(parameters, &coefficients);
biquad->parameters = parameters;
biquad->coefficients = coefficients;
}
static px_biquad* px_biquad_create(float sample_rate, BIQUAD_FILTER_TYPE type)
{
px_biquad* biquad = (px_biquad*)malloc(sizeof(px_biquad));
px_biquad_initialize(biquad, sample_rate, type);
return biquad;
}
static void px_biquad_destroy(px_biquad* biquad)
{
if (biquad)
free(biquad);
}
static void px_biquad_set_frequency(px_biquad* biquad, float in_frequency)
{
assert(biquad);
biquad->parameters.frequency = in_frequency;
px_biquad_update_coefficients(biquad->parameters, &biquad->coefficients);
}
static void px_biquad_set_quality(px_biquad* biquad, float in_quality)
{
assert(biquad);
if (in_quality > 0.0f)
{
biquad->parameters.quality = in_quality;
px_biquad_update_coefficients(biquad->parameters, &biquad->coefficients);
}
}
static void px_biquad_set_gain(px_biquad* biquad, float in_gain)
{
assert(biquad);
biquad->parameters.gain = in_gain;
px_biquad_update_coefficients(biquad->parameters, &biquad->coefficients);
}
static void px_biquad_set_type(px_biquad* biquad, BIQUAD_FILTER_TYPE in_type)
{
assert(biquad);
biquad->parameters.type = in_type;
px_biquad_update_coefficients(biquad->parameters, &biquad->coefficients);
}
// ------------------------------------------------------------------------------------------------------------------------------
static inline float px_biquad_filter(px_biquad* biquad, float input)
{
float out = input * biquad->coefficients.a0 + biquad->coefficients.z1;
biquad->coefficients.z1 = input * biquad->coefficients.a1 + biquad->coefficients.z2 - biquad->coefficients.b1 * out;
biquad->coefficients.z2 = input * biquad->coefficients.a2 - biquad->coefficients.b2 * out;
return (float)out;
}
static inline void px_biquad_update_coefficients(const px_biquad_parameters parameters, px_biquad_coefficients* coefficients)