-
Notifications
You must be signed in to change notification settings - Fork 3
/
equalizer.c
1157 lines (920 loc) · 22.8 KB
/
equalizer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MOC - music on console
* Copyright (C) 2004-2008 Damian Pietras <daper@daper.net>
*
* Equalizer-extension Copyright (C) 2008 Hendrik Iben <hiben@tzi.de>
* Provides a parametric biquadratic equalizer.
*
* This code is based on the 'Cookbook formulae for audio EQ biquad filter
* coefficients' by Robert Bristow-Johnson.
* http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
*
* TODO:
* - Merge somehow with softmixer code to avoid multiple endianness
* conversions.
* - Implement equalizer routines for integer samples... conversion
* to float (and back) is lazy...
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <locale.h>
#include "common.h"
#include "audio.h"
#include "audio_helper.h"
#include "options.h"
#include "log.h"
#include "files.h"
#include "equalizer.h"
#define TWOPI (2.0 * M_PI)
#define NEWLINE 0x0A
#define CRETURN 0x0D
#define SPACE 0x20
#define EQSET_HEADER "EQSET"
#define EQUALIZER_CFG_ACTIVE "Active:"
#define EQUALIZER_CFG_PRESET "Preset:"
#define EQUALIZER_CFG_MIXIN "Mixin:"
#define EQUALIZER_SAVE_FILE "equalizer"
#define EQUALIZER_SAVE_OPTION "Equalizer_SaveState"
typedef struct t_biquad t_biquad;
struct t_biquad
{
float a0, a1, a2, a3, a4;
float x1, x2, y1, y2;
float cf, bw, gain, srate;
int israte;
};
typedef struct t_eq_setup t_eq_setup;
struct t_eq_setup
{
char *name;
float preamp;
int bcount;
float *cf;
float *bw;
float *dg;
};
typedef struct t_eq_set t_eq_set;
struct t_eq_set
{
char *name;
int channels;
float preamp;
int bcount;
t_biquad *b;
};
typedef struct t_eq_set_list t_eq_set_list;
struct t_eq_set_list
{
t_eq_set *set;
t_eq_set_list *prev, *next;
};
typedef struct t_active_set t_active_set;
struct t_active_set
{
int srate;
t_eq_set *set;
};
typedef struct t_eq_settings t_eq_settings;
struct t_eq_settings
{
char *preset_name;
int bcount;
float *gain;
t_eq_settings *next;
};
/* config processing */
static char *skip_line(char *s);
static char *skip_whitespace(char *s);
static int read_float(char *s, float *f, char **endp);
static int read_setup(char *name, char *desc, t_eq_setup **sp);
static void equalizer_adjust_preamp();
static void equalizer_read_config();
static void equalizer_write_config();
/* biquad application */
static inline void apply_biquads(float *src, float *dst, int channels, int len, t_biquad *b, int blen);
/* biquad filter creation */
static t_biquad *mk_biquad(float dbgain, float cf, float srate, float bw, t_biquad *b);
/* equalizer list processing */
static t_eq_set_list *append_eq_set(t_eq_set *eqs, t_eq_set_list *l);
static void clear_eq_set(t_eq_set_list *l);
/* sound processing */
static void equ_process_buffer_u8(uint8_t *buf, size_t size);
static void equ_process_buffer_s8(int8_t *buf, size_t size);
static void equ_process_buffer_u16(uint16_t *buf, size_t size);
static void equ_process_buffer_s16(int16_t *buf, size_t size);
static void equ_process_buffer_u32(uint32_t *buf, size_t size);
static void equ_process_buffer_s32(int32_t *buf, size_t size);
static void equ_process_buffer_float(float *buf, size_t size);
/* static global variables */
static t_eq_set_list equ_list, *current_equ;
static int sample_rate, equ_active, equ_channels;
static float mixin_rate, r_mixin_rate;
static float preamp, preampf;
static char *eqsetdir;
static char *config_preset_name;
/* public functions */
int equalizer_is_active()
{
return equ_active?1:0;
}
int equalizer_set_active(int active)
{
return equ_active = active?1:0;
}
char *equalizer_current_eqname()
{
if(equ_active && current_equ && current_equ->set)
{
return xstrdup(current_equ->set->name);
}
return xstrdup("off");
}
void equalizer_next()
{
if(current_equ)
{
if(current_equ->next)
{
current_equ = current_equ->next;
}
else
{
current_equ = &equ_list;
}
if(!current_equ->set && !(current_equ == &equ_list && !current_equ->next))
equalizer_next();
}
equalizer_adjust_preamp();
}
void equalizer_prev()
{
if(current_equ)
{
if(current_equ->prev)
{
current_equ = current_equ->prev;
}
else
{
while(current_equ->next)
current_equ = current_equ->next;
}
if(!current_equ->set && !(current_equ == &equ_list && !current_equ->next))
equalizer_prev();
}
equalizer_adjust_preamp();
}
/* biquad functions */
/* Create a Peaking EQ Filter.
* See 'Audio EQ Cookbook' for more information
*/
static t_biquad *mk_biquad(float dbgain, float cf, float srate, float bw, t_biquad *b)
{
if(b==NULL)
b = (t_biquad *)xmalloc(sizeof(t_biquad));
float A = powf(10.0f, dbgain / 40.0f);
float omega = TWOPI * cf / srate;
float sn = sin(omega);
float cs = cos(omega);
float alpha = sn * sinh(M_LN2 / 2.0f * bw * omega / sn);
float alpha_m_A = alpha * A;
float alpha_d_A = alpha / A;
float b0 = 1.0f + alpha_m_A;
float b1 = -2.0f * cs;
float b2 = 1.0f - alpha_m_A;
float a0 = 1.0f + alpha_d_A;
float a1 = b1;
float a2 = 1.0f - alpha_d_A;
b->a0 = b0 / a0;
b->a1 = b1 / a0;
b->a2 = b2 / a0;
b->a3 = a1 / a0;
b->a4 = a2 / a0;
b->x1 = 0.0f;
b->x2 = 0.0f;
b->y1 = 0.0f;
b->y2 = 0.0f;
b->cf = cf;
b->bw = bw;
b->srate = srate;
b->israte = (int)srate;
b->gain = dbgain;
return b;
};
/*
* not used but keep as example use for biquad filter
static inline void biquad(float *src, float *dst, int len, t_biquad *b)
{
while(len-->0)
{
float s = *src++;
float f = s * b->a0 + b->a1 * b->x1 + b->a2 * b->x2 - b->a3 * b->y1 - b->a4 * b->y2;
*dst++=f;
b->x2 = b->x1;
b->x1 = s;
b->y2 = b->y1;
b->y1 = f;
}
};
*/
/* Applies a set of biquadratic filters to a buffer of floating point
* samples.
* It is safe to have the same input and output buffer.
*
* blen is the sample-count ignoring channels (samples per channel * channels)
*/
static inline void apply_biquads(float *src, float *dst, int channels, int len, t_biquad *b, int blen)
{
int bi, ci, boffs, idx;
while(len>0)
{
boffs = 0;
for(ci=0; ci<channels; ci++)
{
float s = *src++;
float f = s;
for(bi=0; bi<blen; bi++)
{
idx = boffs + bi;
f =
s * b[idx].a0 \
+ b[idx].a1 * b[idx].x1 \
+ b[idx].a2 * b[idx].x2 \
- b[idx].a3 * b[idx].y1 \
- b[idx].a4 * b[idx].y2;
b[idx].x2 = b[idx].x1;
b[idx].x1 = s;
b[idx].y2 = b[idx].y1;
b[idx].y1 = f;
s = f;
}
*dst++=f;
boffs += blen;
len--;
};
};
};
/*
preamping
XMMS / Beep Media Player / Audacious use all the same code but
do something I do not understand for preamping...
actually preamping by X dB should be like
sample * 10^(X/20)
they do:
sample * (( 1.0 + 0.0932471 * X + 0.00279033 * X^2 ) / 2)
what are these constants ?
the equations are not even close to each other in their results...
- hiben
*/
static void equalizer_adjust_preamp()
{
if(current_equ && current_equ->set)
{
preamp = current_equ->set->preamp;
preampf = powf(10.0f, current_equ->set->preamp / 20.0f);
}
}
static void equalizer_read_config()
{
char *curloc = xstrdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C"); // posix decimal point
char *sfile = xstrdup(create_file_name("equalizer"));
FILE *cf = fopen(sfile, "r");
free (sfile);
if(cf==NULL)
{
logit ("Unable to read equalizer configuration");
if (curloc)
free (curloc);
return;
}
char *linebuffer = NULL;
char presetbuf[128];
presetbuf[0] = 0;
int tmp;
float ftmp;
while((linebuffer=read_line(cf)))
{
if(
strncasecmp
(
linebuffer
, EQUALIZER_CFG_ACTIVE
, strlen(EQUALIZER_CFG_ACTIVE)
) == 0
)
{
if(sscanf(linebuffer, "%*s %i", &tmp)>0)
{
if(tmp>0)
{
equ_active = 1;
}
else
{
equ_active = 0;
}
}
}
if(
strncasecmp
(
linebuffer
, EQUALIZER_CFG_MIXIN
, strlen(EQUALIZER_CFG_MIXIN)
) == 0
)
{
if(sscanf(linebuffer, "%*s %f", &ftmp)>0)
{
if(RANGE(0.0f, ftmp, 1.0f))
{
mixin_rate = ftmp;
}
}
}
if(
strncasecmp
(
linebuffer
, EQUALIZER_CFG_PRESET
, strlen(EQUALIZER_CFG_PRESET)
) == 0
)
{
if(sscanf(linebuffer, "%*s %127s", presetbuf)>0)
{
/* ignore too large strings... */
if(strlen(presetbuf)<127)
{
if(config_preset_name)
free(config_preset_name);
config_preset_name = xstrdup(presetbuf);
}
}
}
free(linebuffer);
}
fclose(cf);
if (curloc) {
setlocale(LC_NUMERIC, curloc);
free (curloc);
}
}
static void equalizer_write_config()
{
char *curloc = xstrdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C"); /* posix decimal point */
char *cfname = create_file_name(EQUALIZER_SAVE_FILE);
FILE *cf = fopen(cfname, "w");
if(cf==NULL)
{
logit ("Unable to write equalizer configuration");
if (curloc)
free (curloc);
return;
}
fprintf(cf, "%s %i\n", EQUALIZER_CFG_ACTIVE, equ_active);
if(current_equ && current_equ->set)
fprintf(cf, "%s %s\n", EQUALIZER_CFG_PRESET, current_equ->set->name);
fprintf(cf, "%s %f\n", EQUALIZER_CFG_MIXIN, mixin_rate);
fclose(cf);
if (curloc) {
setlocale(LC_NUMERIC, curloc);
free (curloc);
}
logit ("Equalizer configuration written");
}
void equalizer_init()
{
equ_active = 1;
equ_list.set = NULL;
equ_list.next = NULL;
equ_list.prev = NULL;
sample_rate = 44100;
equ_channels = 2;
preamp = 0.0f;
preampf = powf(10.0f, preamp / 20.0f);
eqsetdir = xstrdup(create_file_name("eqsets"));
config_preset_name = NULL;
mixin_rate = 0.25f;
equalizer_read_config();
r_mixin_rate = 1.0f - mixin_rate;
equalizer_refresh();
logit ("Equalizer initialized");
}
void equalizer_shutdown()
{
if(options_get_int(EQUALIZER_SAVE_OPTION))
equalizer_write_config();
clear_eq_set(&equ_list);
logit ("Equalizer stopped");
}
void equalizer_refresh()
{
t_eq_setup *eqs = NULL;
char buf[1024];
char *current_set_name = NULL;
if(current_equ && current_equ->set)
{
current_set_name = xstrdup(current_equ->set->name);
}
else
{
if(config_preset_name)
current_set_name = config_preset_name;
}
clear_eq_set(&equ_list);
current_equ = NULL;
DIR *d = opendir(eqsetdir);
if(!d)
{
return;
}
struct dirent *de = readdir(d);
struct stat st;
t_eq_set_list *last_elem;
last_elem = &equ_list;
while(de)
{
sprintf(buf, "eqsets/%s", de->d_name);
char *filename = xstrdup(create_file_name(buf));
stat(filename, &st);
if( S_ISREG(st.st_mode) )
{
FILE *f = fopen(filename, "r");
if(f)
{
char filebuffer[4096];
char *fb = filebuffer;
int maxread = 4095 - (fb - filebuffer);
// read in whole file
while(!feof(f) && maxread>0)
{
maxread = 4095 - (fb - filebuffer);
int rb = fread(fb, sizeof(char), maxread, f);
fb+=rb;
}
fclose(f);
*fb = 0;
int r = read_setup(de->d_name, filebuffer, &eqs);
if(r==0)
{
int i, channel;
t_eq_set *eqset = (t_eq_set *)xmalloc(sizeof(t_eq_set));
eqset->b = (t_biquad *)xmalloc(sizeof(t_biquad)*eqs->bcount*equ_channels);
eqset->name = xstrdup(eqs->name);
eqset->preamp = eqs->preamp;
eqset->bcount = eqs->bcount;
eqset->channels = equ_channels;
for(i=0; i<eqs->bcount; i++)
{
mk_biquad(eqs->dg[i], eqs->cf[i], sample_rate, eqs->bw[i], &eqset->b[i]);
for(channel=1; channel<equ_channels; channel++)
{
eqset->b[channel*eqset->bcount + i] = eqset->b[i];
}
}
last_elem = append_eq_set(eqset, last_elem);
free(eqs->name);
free(eqs->cf);
free(eqs->bw);
free(eqs->dg);
}
else
{
switch(r)
{
case 0:
logit ("This should not happen: No error but no EQSET was parsed: %s", filename);
break;
case -1:
logit ("Not an EQSET (empty file): %s", filename);
break;
case -2:
logit ("Not an EQSET (invalid header): %s", filename);
break;
case -3:
logit ("Error while parsing settings from EQSET: %s", filename);
break;
default:
logit ("Unknown error while parsing EQSET: %s", filename);
break;
}
}
if(eqs)
free(eqs);
eqs = NULL;
}
}
free(filename);
de = readdir(d);
}
closedir(d);
current_equ = &equ_list;
if(current_set_name)
{
current_equ = &equ_list;
while(current_equ)
{
if(current_equ->set)
{
if(strcmp(current_set_name, current_equ->set->name)==0)
break;
}
current_equ = current_equ->next;
}
free(current_set_name);
}
if(current_equ && !current_equ->set)
equalizer_next();
equalizer_adjust_preamp();
}
/* sound processing code */
void equalizer_process_buffer(char *buf, size_t size, const struct sound_params *sound_params)
{
debug ("EQ Processing %u bytes...", size);
if(!equ_active || !current_equ || !current_equ->set)
return;
if(sound_params->rate != current_equ->set->b->israte || sound_params->channels != equ_channels)
{
logit ("Recreating filters due to sound parameter changes...");
sample_rate = sound_params->rate;
equ_channels = sound_params->channels;
equalizer_refresh();
}
long sound_endianness = sound_params->fmt & SFMT_MASK_ENDIANNESS;
long sound_format = sound_params->fmt & SFMT_MASK_FORMAT;
int samplesize = sample_size(sound_format);
int is_float = (sound_params->fmt & SFMT_MASK_FORMAT) == SFMT_FLOAT;
int need_endianness_swap = 0;
if((sound_endianness != SFMT_NE) && (samplesize > 1) && (!is_float))
{
need_endianness_swap = 1;
}
/* setup samples to perform arithmetic */
if(need_endianness_swap)
{
debug ("Converting endianness before mixing");
if(samplesize == 4)
swap_endianness_32((int32_t *)buf, size / sizeof(int32_t));
else
swap_endianness_16((int16_t *)buf, size / sizeof(int16_t));
}
switch(sound_format)
{
case SFMT_U8:
equ_process_buffer_u8((uint8_t *)buf, size);
break;
case SFMT_S8:
equ_process_buffer_s8((int8_t *)buf, size);
break;
case SFMT_U16:
equ_process_buffer_u16((uint16_t *)buf, size / sizeof(uint16_t));
break;
case SFMT_S16:
equ_process_buffer_s16((int16_t *)buf, size / sizeof(int16_t));
break;
case SFMT_U32:
equ_process_buffer_u32((uint32_t *)buf, size / sizeof(uint32_t));
break;
case SFMT_S32:
equ_process_buffer_s32((int32_t *)buf, size / sizeof(int32_t));
break;
case SFMT_FLOAT:
equ_process_buffer_float((float *)buf, size / sizeof(float));
break;
}
/* restore sample-endianness */
if(need_endianness_swap)
{
debug ("Restoring endianness after mixing");
if(samplesize == 4)
swap_endianness_32((int32_t *)buf, size / sizeof(int32_t));
else
swap_endianness_16((int16_t *)buf, size / sizeof(int16_t));
}
}
static void equ_process_buffer_u8(uint8_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(0, tmp[i], UINT8_MAX);
buf[i] = (uint8_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_s8(int8_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(INT8_MIN, tmp[i], INT8_MAX);
buf[i] = (int8_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_u16(uint16_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(0, tmp[i], UINT16_MAX);
buf[i] = (uint16_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_s16(int16_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(INT16_MIN, tmp[i], INT16_MAX);
buf[i] = (int16_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_u32(uint32_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(0, tmp[i], UINT32_MAX);
buf[i] = (uint32_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_s32(int32_t *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(INT32_MIN, tmp[i], INT32_MAX);
buf[i] = (int32_t)tmp[i];
}
free(tmp);
}
static void equ_process_buffer_float(float *buf, size_t size)
{
size_t i;
float *tmp;
debug ("equalizing");
tmp = (float *)xmalloc (size * sizeof (float));
for(i=0; i<size; i++)
tmp[i] = preampf * (float)buf[i];
apply_biquads(tmp, tmp, equ_channels, size, current_equ->set->b, current_equ->set->bcount);
for(i=0; i<size; i++)
{
tmp[i] = r_mixin_rate * tmp[i] + mixin_rate * buf[i];
tmp[i] = CLAMP(-1.0f, tmp[i], 1.0f);
buf[i] = tmp[i];
}
free(tmp);
}
/* equalizer list maintenance */
static t_eq_set_list *append_eq_set(t_eq_set *eqs, t_eq_set_list *l)
{
if(l->set == NULL)
{
l->set = eqs;
}
else
{
if(l->next)
{
append_eq_set(eqs, l->next);
}
else
{
l->next = (t_eq_set_list *)xmalloc(sizeof(t_eq_set_list));
l->next->set = NULL;
l->next->next = NULL;
l->next->prev = l;
l = append_eq_set(eqs, l->next);
}
};
return l;
};
static void clear_eq_set(t_eq_set_list *l)
{
if(l->set)
{
free(l->set->name);
free(l->set->b);
free(l->set);
l->set = NULL;
}
if(l->next)
{
clear_eq_set(l->next);
free(l->next);
l->next = NULL;
}
};
/* parsing stuff */
static int read_setup(char *name, char *desc, t_eq_setup **sp)
{
char *curloc = xstrdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C"); // posix decimal point
t_eq_setup *s = *sp;
desc = skip_whitespace(desc);
if(!*desc)
{
if (curloc)
free (curloc);
return -1;
}
if(strncasecmp(desc, EQSET_HEADER, sizeof(EQSET_HEADER)-1))
{
if (curloc)
free (curloc);
return -2;
}
desc+=5;