aboutsummaryrefslogtreecommitdiffstats
path: root/al/effects/equalizer.cpp
blob: c052db3e3f9edacd628afb2630149388a280081e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035

#include "config.h"

#include "AL/al.h"
#include "AL/efx.h"

#include "alc/effects/base.h"
#include "effects.h"

#if ALSOFT_EAX
#include "alnumeric.h"

#include "al/eax_exception.h"
#include "al/eax_utils.h"
#endif // ALSOFT_EAX


namespace {

void Equalizer_setParami(EffectProps*, ALenum param, int)
{ throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer integer property 0x%04x", param}; }
void Equalizer_setParamiv(EffectProps*, ALenum param, const int*)
{
    throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer integer-vector property 0x%04x",
        param};
}
void Equalizer_setParamf(EffectProps *props, ALenum param, float val)
{
    switch(param)
    {
    case AL_EQUALIZER_LOW_GAIN:
        if(!(val >= AL_EQUALIZER_MIN_LOW_GAIN && val <= AL_EQUALIZER_MAX_LOW_GAIN))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer low-band gain out of range"};
        props->Equalizer.LowGain = val;
        break;

    case AL_EQUALIZER_LOW_CUTOFF:
        if(!(val >= AL_EQUALIZER_MIN_LOW_CUTOFF && val <= AL_EQUALIZER_MAX_LOW_CUTOFF))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer low-band cutoff out of range"};
        props->Equalizer.LowCutoff = val;
        break;

    case AL_EQUALIZER_MID1_GAIN:
        if(!(val >= AL_EQUALIZER_MIN_MID1_GAIN && val <= AL_EQUALIZER_MAX_MID1_GAIN))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid1-band gain out of range"};
        props->Equalizer.Mid1Gain = val;
        break;

    case AL_EQUALIZER_MID1_CENTER:
        if(!(val >= AL_EQUALIZER_MIN_MID1_CENTER && val <= AL_EQUALIZER_MAX_MID1_CENTER))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid1-band center out of range"};
        props->Equalizer.Mid1Center = val;
        break;

    case AL_EQUALIZER_MID1_WIDTH:
        if(!(val >= AL_EQUALIZER_MIN_MID1_WIDTH && val <= AL_EQUALIZER_MAX_MID1_WIDTH))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid1-band width out of range"};
        props->Equalizer.Mid1Width = val;
        break;

    case AL_EQUALIZER_MID2_GAIN:
        if(!(val >= AL_EQUALIZER_MIN_MID2_GAIN && val <= AL_EQUALIZER_MAX_MID2_GAIN))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid2-band gain out of range"};
        props->Equalizer.Mid2Gain = val;
        break;

    case AL_EQUALIZER_MID2_CENTER:
        if(!(val >= AL_EQUALIZER_MIN_MID2_CENTER && val <= AL_EQUALIZER_MAX_MID2_CENTER))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid2-band center out of range"};
        props->Equalizer.Mid2Center = val;
        break;

    case AL_EQUALIZER_MID2_WIDTH:
        if(!(val >= AL_EQUALIZER_MIN_MID2_WIDTH && val <= AL_EQUALIZER_MAX_MID2_WIDTH))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer mid2-band width out of range"};
        props->Equalizer.Mid2Width = val;
        break;

    case AL_EQUALIZER_HIGH_GAIN:
        if(!(val >= AL_EQUALIZER_MIN_HIGH_GAIN && val <= AL_EQUALIZER_MAX_HIGH_GAIN))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer high-band gain out of range"};
        props->Equalizer.HighGain = val;
        break;

    case AL_EQUALIZER_HIGH_CUTOFF:
        if(!(val >= AL_EQUALIZER_MIN_HIGH_CUTOFF && val <= AL_EQUALIZER_MAX_HIGH_CUTOFF))
            throw effect_exception{AL_INVALID_VALUE, "Equalizer high-band cutoff out of range"};
        props->Equalizer.HighCutoff = val;
        break;

    default:
        throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer float property 0x%04x", param};
    }
}
void Equalizer_setParamfv(EffectProps *props, ALenum param, const float *vals)
{ Equalizer_setParamf(props, param, vals[0]); }

void Equalizer_getParami(const EffectProps*, ALenum param, int*)
{ throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer integer property 0x%04x", param}; }
void Equalizer_getParamiv(const EffectProps*, ALenum param, int*)
{
    throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer integer-vector property 0x%04x",
        param};
}
void Equalizer_getParamf(const EffectProps *props, ALenum param, float *val)
{
    switch(param)
    {
    case AL_EQUALIZER_LOW_GAIN:
        *val = props->Equalizer.LowGain;
        break;

    case AL_EQUALIZER_LOW_CUTOFF:
        *val = props->Equalizer.LowCutoff;
        break;

    case AL_EQUALIZER_MID1_GAIN:
        *val = props->Equalizer.Mid1Gain;
        break;

    case AL_EQUALIZER_MID1_CENTER:
        *val = props->Equalizer.Mid1Center;
        break;

    case AL_EQUALIZER_MID1_WIDTH:
        *val = props->Equalizer.Mid1Width;
        break;

    case AL_EQUALIZER_MID2_GAIN:
        *val = props->Equalizer.Mid2Gain;
        break;

    case AL_EQUALIZER_MID2_CENTER:
        *val = props->Equalizer.Mid2Center;
        break;

    case AL_EQUALIZER_MID2_WIDTH:
        *val = props->Equalizer.Mid2Width;
        break;

    case AL_EQUALIZER_HIGH_GAIN:
        *val = props->Equalizer.HighGain;
        break;

    case AL_EQUALIZER_HIGH_CUTOFF:
        *val = props->Equalizer.HighCutoff;
        break;

    default:
        throw effect_exception{AL_INVALID_ENUM, "Invalid equalizer float property 0x%04x", param};
    }
}
void Equalizer_getParamfv(const EffectProps *props, ALenum param, float *vals)
{ Equalizer_getParamf(props, param, vals); }

EffectProps genDefaultProps() noexcept
{
    EffectProps props{};
    props.Equalizer.LowCutoff = AL_EQUALIZER_DEFAULT_LOW_CUTOFF;
    props.Equalizer.LowGain = AL_EQUALIZER_DEFAULT_LOW_GAIN;
    props.Equalizer.Mid1Center = AL_EQUALIZER_DEFAULT_MID1_CENTER;
    props.Equalizer.Mid1Gain = AL_EQUALIZER_DEFAULT_MID1_GAIN;
    props.Equalizer.Mid1Width = AL_EQUALIZER_DEFAULT_MID1_WIDTH;
    props.Equalizer.Mid2Center = AL_EQUALIZER_DEFAULT_MID2_CENTER;
    props.Equalizer.Mid2Gain = AL_EQUALIZER_DEFAULT_MID2_GAIN;
    props.Equalizer.Mid2Width = AL_EQUALIZER_DEFAULT_MID2_WIDTH;
    props.Equalizer.HighCutoff = AL_EQUALIZER_DEFAULT_HIGH_CUTOFF;
    props.Equalizer.HighGain = AL_EQUALIZER_DEFAULT_HIGH_GAIN;
    return props;
}

} // namespace

DEFINE_ALEFFECT_VTABLE(Equalizer);

const EffectProps EqualizerEffectProps{genDefaultProps()};

#if ALSOFT_EAX
namespace
{


using EaxEqualizerEffectDirtyFlagsValue = std::uint_least16_t;

struct EaxEqualizerEffectDirtyFlags
{
    using EaxIsBitFieldStruct = bool;

    EaxEqualizerEffectDirtyFlagsValue lLowGain : 1;
    EaxEqualizerEffectDirtyFlagsValue flLowCutOff : 1;
    EaxEqualizerEffectDirtyFlagsValue lMid1Gain : 1;
    EaxEqualizerEffectDirtyFlagsValue flMid1Center : 1;
    EaxEqualizerEffectDirtyFlagsValue flMid1Width : 1;
    EaxEqualizerEffectDirtyFlagsValue lMid2Gain : 1;
    EaxEqualizerEffectDirtyFlagsValue flMid2Center : 1;
    EaxEqualizerEffectDirtyFlagsValue flMid2Width : 1;
    EaxEqualizerEffectDirtyFlagsValue lHighGain : 1;
    EaxEqualizerEffectDirtyFlagsValue flHighCutOff : 1;
}; // EaxEqualizerEffectDirtyFlags


class EaxEqualizerEffect final :
    public EaxEffect
{
public:
    EaxEqualizerEffect(
        EffectProps& al_effect_props);


    // [[nodiscard]]
    bool dispatch(
        const EaxEaxCall& eax_call) override;


private:
    EffectProps& al_effect_props_;

    EAXEQUALIZERPROPERTIES eax_{};
    EAXEQUALIZERPROPERTIES eax_d_{};
    EaxEqualizerEffectDirtyFlags eax_dirty_flags_{};


    void set_eax_defaults();


    void set_efx_low_gain();

    void set_efx_low_cutoff();

    void set_efx_mid1_gain();

    void set_efx_mid1_center();

    void set_efx_mid1_width();

    void set_efx_mid2_gain();

    void set_efx_mid2_center();

    void set_efx_mid2_width();

    void set_efx_high_gain();

    void set_efx_high_cutoff();

    void set_efx_defaults();


    // [[nodiscard]]
    bool get(
        const EaxEaxCall& eax_call);


    void validate_low_gain(
        long lLowGain);

    void validate_low_cutoff(
        float flLowCutOff);

    void validate_mid1_gain(
        long lMid1Gain);

    void validate_mid1_center(
        float flMid1Center);

    void validate_mid1_width(
        float flMid1Width);

    void validate_mid2_gain(
        long lMid2Gain);

    void validate_mid2_center(
        float flMid2Center);

    void validate_mid2_width(
        float flMid2Width);

    void validate_high_gain(
        long lHighGain);

    void validate_high_cutoff(
        float flHighCutOff);

    void validate_all(
        const EAXEQUALIZERPROPERTIES& all);


    void defer_low_gain(
        long lLowGain);

    void defer_low_cutoff(
        float flLowCutOff);

    void defer_mid1_gain(
        long lMid1Gain);

    void defer_mid1_center(
        float flMid1Center);

    void defer_mid1_width(
        float flMid1Width);

    void defer_mid2_gain(
        long lMid2Gain);

    void defer_mid2_center(
        float flMid2Center);

    void defer_mid2_width(
        float flMid2Width);

    void defer_high_gain(
        long lHighGain);

    void defer_high_cutoff(
        float flHighCutOff);

    void defer_all(
        const EAXEQUALIZERPROPERTIES& all);


    void defer_low_gain(
        const EaxEaxCall& eax_call);

    void defer_low_cutoff(
        const EaxEaxCall& eax_call);

    void defer_mid1_gain(
        const EaxEaxCall& eax_call);

    void defer_mid1_center(
        const EaxEaxCall& eax_call);

    void defer_mid1_width(
        const EaxEaxCall& eax_call);

    void defer_mid2_gain(
        const EaxEaxCall& eax_call);

    void defer_mid2_center(
        const EaxEaxCall& eax_call);

    void defer_mid2_width(
        const EaxEaxCall& eax_call);

    void defer_high_gain(
        const EaxEaxCall& eax_call);

    void defer_high_cutoff(
        const EaxEaxCall& eax_call);

    void defer_all(
        const EaxEaxCall& eax_call);


    // [[nodiscard]]
    bool apply_deferred();

    // [[nodiscard]]
    bool set(
        const EaxEaxCall& eax_call);
}; // EaxEqualizerEffect


class EaxEqualizerEffectException :
    public EaxException
{
public:
    explicit EaxEqualizerEffectException(
        const char* message)
        :
        EaxException{"EAX_EQUALIZER_EFFECT", message}
    {
    }
}; // EaxEqualizerEffectException


EaxEqualizerEffect::EaxEqualizerEffect(
    EffectProps& al_effect_props)
    :
    al_effect_props_{al_effect_props}
{
    set_eax_defaults();
    set_efx_defaults();
}

// [[nodiscard]]
bool EaxEqualizerEffect::dispatch(
    const EaxEaxCall& eax_call)
{
    return eax_call.is_get() ? get(eax_call) : set(eax_call);
}

void EaxEqualizerEffect::set_eax_defaults()
{
    eax_.lLowGain = EAXEQUALIZER_DEFAULTLOWGAIN;
    eax_.flLowCutOff = EAXEQUALIZER_DEFAULTLOWCUTOFF;
    eax_.lMid1Gain = EAXEQUALIZER_DEFAULTMID1GAIN;
    eax_.flMid1Center = EAXEQUALIZER_DEFAULTMID1CENTER;
    eax_.flMid1Width = EAXEQUALIZER_DEFAULTMID1WIDTH;
    eax_.lMid2Gain = EAXEQUALIZER_DEFAULTMID2GAIN;
    eax_.flMid2Center = EAXEQUALIZER_DEFAULTMID2CENTER;
    eax_.flMid2Width = EAXEQUALIZER_DEFAULTMID2WIDTH;
    eax_.lHighGain = EAXEQUALIZER_DEFAULTHIGHGAIN;
    eax_.flHighCutOff = EAXEQUALIZER_DEFAULTHIGHCUTOFF;

    eax_d_ = eax_;
}

void EaxEqualizerEffect::set_efx_low_gain()
{
    const auto low_gain = clamp(
        level_mb_to_gain(static_cast<float>(eax_.lLowGain)),
        AL_EQUALIZER_MIN_LOW_GAIN,
        AL_EQUALIZER_MAX_LOW_GAIN);

    al_effect_props_.Equalizer.LowGain = low_gain;
}

void EaxEqualizerEffect::set_efx_low_cutoff()
{
    const auto low_cutoff = clamp(
        eax_.flLowCutOff,
        AL_EQUALIZER_MIN_LOW_CUTOFF,
        AL_EQUALIZER_MAX_LOW_CUTOFF);

    al_effect_props_.Equalizer.LowCutoff = low_cutoff;
}

void EaxEqualizerEffect::set_efx_mid1_gain()
{
    const auto mid1_gain = clamp(
        level_mb_to_gain(static_cast<float>(eax_.lMid1Gain)),
        AL_EQUALIZER_MIN_MID1_GAIN,
        AL_EQUALIZER_MAX_MID1_GAIN);

    al_effect_props_.Equalizer.Mid1Gain = mid1_gain;
}

void EaxEqualizerEffect::set_efx_mid1_center()
{
    const auto mid1_center = clamp(
        eax_.flMid1Center,
        AL_EQUALIZER_MIN_MID1_CENTER,
        AL_EQUALIZER_MAX_MID1_CENTER);

    al_effect_props_.Equalizer.Mid1Center = mid1_center;
}

void EaxEqualizerEffect::set_efx_mid1_width()
{
    const auto mid1_width = clamp(
        eax_.flMid1Width,
        AL_EQUALIZER_MIN_MID1_WIDTH,
        AL_EQUALIZER_MAX_MID1_WIDTH);

    al_effect_props_.Equalizer.Mid1Width = mid1_width;
}

void EaxEqualizerEffect::set_efx_mid2_gain()
{
    const auto mid2_gain = clamp(
        level_mb_to_gain(static_cast<float>(eax_.lMid2Gain)),
        AL_EQUALIZER_MIN_MID2_GAIN,
        AL_EQUALIZER_MAX_MID2_GAIN);

    al_effect_props_.Equalizer.Mid2Gain = mid2_gain;
}

void EaxEqualizerEffect::set_efx_mid2_center()
{
    const auto mid2_center = clamp(
        eax_.flMid2Center,
        AL_EQUALIZER_MIN_MID2_CENTER,
        AL_EQUALIZER_MAX_MID2_CENTER);

    al_effect_props_.Equalizer.Mid2Center = mid2_center;
}

void EaxEqualizerEffect::set_efx_mid2_width()
{
    const auto mid2_width = clamp(
        eax_.flMid2Width,
        AL_EQUALIZER_MIN_MID2_WIDTH,
        AL_EQUALIZER_MAX_MID2_WIDTH);

    al_effect_props_.Equalizer.Mid2Width = mid2_width;
}

void EaxEqualizerEffect::set_efx_high_gain()
{
    const auto high_gain = clamp(
        level_mb_to_gain(static_cast<float>(eax_.lHighGain)),
        AL_EQUALIZER_MIN_HIGH_GAIN,
        AL_EQUALIZER_MAX_HIGH_GAIN);

    al_effect_props_.Equalizer.HighGain = high_gain;
}

void EaxEqualizerEffect::set_efx_high_cutoff()
{
    const auto high_cutoff = clamp(
        eax_.flHighCutOff,
        AL_EQUALIZER_MIN_HIGH_CUTOFF,
        AL_EQUALIZER_MAX_HIGH_CUTOFF);

    al_effect_props_.Equalizer.HighCutoff = high_cutoff;
}

void EaxEqualizerEffect::set_efx_defaults()
{
    set_efx_low_gain();
    set_efx_low_cutoff();
    set_efx_mid1_gain();
    set_efx_mid1_center();
    set_efx_mid1_width();
    set_efx_mid2_gain();
    set_efx_mid2_center();
    set_efx_mid2_width();
    set_efx_high_gain();
    set_efx_high_cutoff();
}

// [[nodiscard]]
bool EaxEqualizerEffect::get(
    const EaxEaxCall& eax_call)
{
    switch (eax_call.get_property_id())
    {
        case EAXEQUALIZER_NONE:
            break;

        case EAXEQUALIZER_ALLPARAMETERS:
            eax_call.set_value<EaxEqualizerEffectException>(eax_);
            break;

        case EAXEQUALIZER_LOWGAIN:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.lLowGain);
            break;

        case EAXEQUALIZER_LOWCUTOFF:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flLowCutOff);
            break;

        case EAXEQUALIZER_MID1GAIN:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.lMid1Gain);
            break;

        case EAXEQUALIZER_MID1CENTER:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flMid1Center);
            break;

        case EAXEQUALIZER_MID1WIDTH:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flMid1Width);
            break;

        case EAXEQUALIZER_MID2GAIN:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.lMid2Gain);
            break;

        case EAXEQUALIZER_MID2CENTER:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flMid2Center);
            break;

        case EAXEQUALIZER_MID2WIDTH:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flMid2Width);
            break;

        case EAXEQUALIZER_HIGHGAIN:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.lHighGain);
            break;

        case EAXEQUALIZER_HIGHCUTOFF:
            eax_call.set_value<EaxEqualizerEffectException>(eax_.flHighCutOff);
            break;

        default:
            throw EaxEqualizerEffectException{"Unsupported property id."};
    }

    return false;
}

void EaxEqualizerEffect::validate_low_gain(
    long lLowGain)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Low Gain",
        lLowGain,
        EAXEQUALIZER_MINLOWGAIN,
        EAXEQUALIZER_MAXLOWGAIN);
}

void EaxEqualizerEffect::validate_low_cutoff(
    float flLowCutOff)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Low Cutoff",
        flLowCutOff,
        EAXEQUALIZER_MINLOWCUTOFF,
        EAXEQUALIZER_MAXLOWCUTOFF);
}

void EaxEqualizerEffect::validate_mid1_gain(
    long lMid1Gain)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid1 Gain",
        lMid1Gain,
        EAXEQUALIZER_MINMID1GAIN,
        EAXEQUALIZER_MAXMID1GAIN);
}

void EaxEqualizerEffect::validate_mid1_center(
    float flMid1Center)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid1 Center",
        flMid1Center,
        EAXEQUALIZER_MINMID1CENTER,
        EAXEQUALIZER_MAXMID1CENTER);
}

void EaxEqualizerEffect::validate_mid1_width(
    float flMid1Width)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid1 Width",
        flMid1Width,
        EAXEQUALIZER_MINMID1WIDTH,
        EAXEQUALIZER_MAXMID1WIDTH);
}

void EaxEqualizerEffect::validate_mid2_gain(
    long lMid2Gain)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid2 Gain",
        lMid2Gain,
        EAXEQUALIZER_MINMID2GAIN,
        EAXEQUALIZER_MAXMID2GAIN);
}

void EaxEqualizerEffect::validate_mid2_center(
    float flMid2Center)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid2 Center",
        flMid2Center,
        EAXEQUALIZER_MINMID2CENTER,
        EAXEQUALIZER_MAXMID2CENTER);
}

void EaxEqualizerEffect::validate_mid2_width(
    float flMid2Width)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "Mid2 Width",
        flMid2Width,
        EAXEQUALIZER_MINMID2WIDTH,
        EAXEQUALIZER_MAXMID2WIDTH);
}

void EaxEqualizerEffect::validate_high_gain(
    long lHighGain)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "High Gain",
        lHighGain,
        EAXEQUALIZER_MINHIGHGAIN,
        EAXEQUALIZER_MAXHIGHGAIN);
}

void EaxEqualizerEffect::validate_high_cutoff(
    float flHighCutOff)
{
    eax_validate_range<EaxEqualizerEffectException>(
        "High Cutoff",
        flHighCutOff,
        EAXEQUALIZER_MINHIGHCUTOFF,
        EAXEQUALIZER_MAXHIGHCUTOFF);
}

void EaxEqualizerEffect::validate_all(
    const EAXEQUALIZERPROPERTIES& all)
{
    validate_low_gain(all.lLowGain);
    validate_low_cutoff(all.flLowCutOff);
    validate_mid1_gain(all.lMid1Gain);
    validate_mid1_center(all.flMid1Center);
    validate_mid1_width(all.flMid1Width);
    validate_mid2_gain(all.lMid2Gain);
    validate_mid2_center(all.flMid2Center);
    validate_mid2_width(all.flMid2Width);
    validate_high_gain(all.lHighGain);
    validate_high_cutoff(all.flHighCutOff);
}

void EaxEqualizerEffect::defer_low_gain(
    long lLowGain)
{
    eax_d_.lLowGain = lLowGain;
    eax_dirty_flags_.lLowGain = (eax_.lLowGain != eax_d_.lLowGain);
}

void EaxEqualizerEffect::defer_low_cutoff(
    float flLowCutOff)
{
    eax_d_.flLowCutOff = flLowCutOff;
    eax_dirty_flags_.flLowCutOff = (eax_.flLowCutOff != eax_d_.flLowCutOff);
}

void EaxEqualizerEffect::defer_mid1_gain(
    long lMid1Gain)
{
    eax_d_.lMid1Gain = lMid1Gain;
    eax_dirty_flags_.lMid1Gain = (eax_.lMid1Gain != eax_d_.lMid1Gain);
}

void EaxEqualizerEffect::defer_mid1_center(
    float flMid1Center)
{
    eax_d_.flMid1Center = flMid1Center;
    eax_dirty_flags_.flMid1Center = (eax_.flMid1Center != eax_d_.flMid1Center);
}

void EaxEqualizerEffect::defer_mid1_width(
    float flMid1Width)
{
    eax_d_.flMid1Width = flMid1Width;
    eax_dirty_flags_.flMid1Width = (eax_.flMid1Width != eax_d_.flMid1Width);
}

void EaxEqualizerEffect::defer_mid2_gain(
    long lMid2Gain)
{
    eax_d_.lMid2Gain = lMid2Gain;
    eax_dirty_flags_.lMid2Gain = (eax_.lMid2Gain != eax_d_.lMid2Gain);
}

void EaxEqualizerEffect::defer_mid2_center(
    float flMid2Center)
{
    eax_d_.flMid2Center = flMid2Center;
    eax_dirty_flags_.flMid2Center = (eax_.flMid2Center != eax_d_.flMid2Center);
}

void EaxEqualizerEffect::defer_mid2_width(
    float flMid2Width)
{
    eax_d_.flMid2Width = flMid2Width;
    eax_dirty_flags_.flMid2Width = (eax_.flMid2Width != eax_d_.flMid2Width);
}

void EaxEqualizerEffect::defer_high_gain(
    long lHighGain)
{
    eax_d_.lHighGain = lHighGain;
    eax_dirty_flags_.lHighGain = (eax_.lHighGain != eax_d_.lHighGain);
}

void EaxEqualizerEffect::defer_high_cutoff(
    float flHighCutOff)
{
    eax_d_.flHighCutOff = flHighCutOff;
    eax_dirty_flags_.flHighCutOff = (eax_.flHighCutOff != eax_d_.flHighCutOff);
}

void EaxEqualizerEffect::defer_all(
    const EAXEQUALIZERPROPERTIES& all)
{
    defer_low_gain(all.lLowGain);
    defer_low_cutoff(all.flLowCutOff);
    defer_mid1_gain(all.lMid1Gain);
    defer_mid1_center(all.flMid1Center);
    defer_mid1_width(all.flMid1Width);
    defer_mid2_gain(all.lMid2Gain);
    defer_mid2_center(all.flMid2Center);
    defer_mid2_width(all.flMid2Width);
    defer_high_gain(all.lHighGain);
    defer_high_cutoff(all.flHighCutOff);
}

void EaxEqualizerEffect::defer_low_gain(
    const EaxEaxCall& eax_call)
{
    const auto& low_gain =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::lLowGain)>();

    validate_low_gain(low_gain);
    defer_low_gain(low_gain);
}

void EaxEqualizerEffect::defer_low_cutoff(
    const EaxEaxCall& eax_call)
{
    const auto& low_cutoff =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flLowCutOff)>();

    validate_low_cutoff(low_cutoff);
    defer_low_cutoff(low_cutoff);
}

void EaxEqualizerEffect::defer_mid1_gain(
    const EaxEaxCall& eax_call)
{
    const auto& mid1_gain =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::lMid1Gain)>();

    validate_mid1_gain(mid1_gain);
    defer_mid1_gain(mid1_gain);
}

void EaxEqualizerEffect::defer_mid1_center(
    const EaxEaxCall& eax_call)
{
    const auto& mid1_center =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flMid1Center)>();

    validate_mid1_center(mid1_center);
    defer_mid1_center(mid1_center);
}

void EaxEqualizerEffect::defer_mid1_width(
    const EaxEaxCall& eax_call)
{
    const auto& mid1_width =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flMid1Width)>();

    validate_mid1_width(mid1_width);
    defer_mid1_width(mid1_width);
}

void EaxEqualizerEffect::defer_mid2_gain(
    const EaxEaxCall& eax_call)
{
    const auto& mid2_gain =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::lMid2Gain)>();

    validate_mid2_gain(mid2_gain);
    defer_mid2_gain(mid2_gain);
}

void EaxEqualizerEffect::defer_mid2_center(
    const EaxEaxCall& eax_call)
{
    const auto& mid2_center =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flMid2Center)>();

    validate_mid2_center(mid2_center);
    defer_mid2_center(mid2_center);
}

void EaxEqualizerEffect::defer_mid2_width(
    const EaxEaxCall& eax_call)
{
    const auto& mid2_width =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flMid2Width)>();

    validate_mid2_width(mid2_width);
    defer_mid2_width(mid2_width);
}

void EaxEqualizerEffect::defer_high_gain(
    const EaxEaxCall& eax_call)
{
    const auto& high_gain =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::lHighGain)>();

    validate_high_gain(high_gain);
    defer_high_gain(high_gain);
}

void EaxEqualizerEffect::defer_high_cutoff(
    const EaxEaxCall& eax_call)
{
    const auto& high_cutoff =
        eax_call.get_value<EaxEqualizerEffectException, const decltype(EAXEQUALIZERPROPERTIES::flHighCutOff)>();

    validate_high_cutoff(high_cutoff);
    defer_high_cutoff(high_cutoff);
}

void EaxEqualizerEffect::defer_all(
    const EaxEaxCall& eax_call)
{
    const auto& all =
        eax_call.get_value<EaxEqualizerEffectException, const EAXEQUALIZERPROPERTIES>();

    validate_all(all);
    defer_all(all);
}

// [[nodiscard]]
bool EaxEqualizerEffect::apply_deferred()
{
    if (eax_dirty_flags_ == EaxEqualizerEffectDirtyFlags{})
    {
        return false;
    }

    eax_ = eax_d_;

    if (eax_dirty_flags_.lLowGain)
    {
        set_efx_low_gain();
    }

    if (eax_dirty_flags_.flLowCutOff)
    {
        set_efx_low_cutoff();
    }

    if (eax_dirty_flags_.lMid1Gain)
    {
        set_efx_mid1_gain();
    }

    if (eax_dirty_flags_.flMid1Center)
    {
        set_efx_mid1_center();
    }

    if (eax_dirty_flags_.flMid1Width)
    {
        set_efx_mid1_width();
    }

    if (eax_dirty_flags_.lMid2Gain)
    {
        set_efx_mid2_gain();
    }

    if (eax_dirty_flags_.flMid2Center)
    {
        set_efx_mid2_center();
    }

    if (eax_dirty_flags_.flMid2Width)
    {
        set_efx_mid2_width();
    }

    if (eax_dirty_flags_.lHighGain)
    {
        set_efx_high_gain();
    }

    if (eax_dirty_flags_.flHighCutOff)
    {
        set_efx_high_cutoff();
    }

    eax_dirty_flags_ = EaxEqualizerEffectDirtyFlags{};

    return true;
}

// [[nodiscard]]
bool EaxEqualizerEffect::set(
    const EaxEaxCall& eax_call)
{
    switch (eax_call.get_property_id())
    {
        case EAXEQUALIZER_NONE:
            break;

        case EAXEQUALIZER_ALLPARAMETERS:
            defer_all(eax_call);
            break;

        case EAXEQUALIZER_LOWGAIN:
            defer_low_gain(eax_call);
            break;

        case EAXEQUALIZER_LOWCUTOFF:
            defer_low_cutoff(eax_call);
            break;

        case EAXEQUALIZER_MID1GAIN:
            defer_mid1_gain(eax_call);
            break;

        case EAXEQUALIZER_MID1CENTER:
            defer_mid1_center(eax_call);
            break;

        case EAXEQUALIZER_MID1WIDTH:
            defer_mid1_width(eax_call);
            break;

        case EAXEQUALIZER_MID2GAIN:
            defer_mid2_gain(eax_call);
            break;

        case EAXEQUALIZER_MID2CENTER:
            defer_mid2_center(eax_call);
            break;

        case EAXEQUALIZER_MID2WIDTH:
            defer_mid2_width(eax_call);
            break;

        case EAXEQUALIZER_HIGHGAIN:
            defer_high_gain(eax_call);
            break;

        case EAXEQUALIZER_HIGHCUTOFF:
            defer_high_cutoff(eax_call);
            break;

        default:
            throw EaxEqualizerEffectException{"Unsupported property id."};
    }

    if (!eax_call.is_deferred())
    {
        return apply_deferred();
    }

    return false;
}


} // namespace


EaxEffectUPtr eax_create_eax_equalizer_effect(
    EffectProps& al_effect_props)
{
    return std::make_unique<EaxEqualizerEffect>(al_effect_props);
}


#endif // ALSOFT_EAX