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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2021 Takashi Sakamoto

//! Protocol specific to Focusrite Saffire Pro 24 DSP.
//!
//! The module includes structure, enumeration, and trait and its implementation for protocol
//! defined by Focusrite for Saffire Pro 24 DSP.
//!
//! ## Diagram of internal signal flow for Saffire Pro 24 DSP.
//!
//! I note that optical input interface is available exclusively for ADAT input and S/PDIF input.
//!
//! ```text
//!
//! XLR input 1 ------+---------+
//! Phone input 1-----+         |
//!                             |
//! XLR input 2 ------+---------+
//! Phone input 2 ----+         |
//!                             +------------------> analog-input-1/2
//! Phone input 3/4 -------------------------------> analog-input-3/4
//! Phone input 5/6 -------------------------------> analog-input-5/6
//! Coaxial input 1/2 -----------------------------> spdif-input-1/2
//! Optical input --------------or-----------------> spdif-input-3/4
//!                             +------------------> adat-input-1..8
//!
//!                          ++=============++
//! analog-input-1/2 ------> ||   46 x 46   || ----> analog-output-1/2
//! analog-input-3/4 ------> ||   router    || ----> analog-output-3/4
//! analog-input-5/6 ------> ||   up to     || ----> analog-output-5/6
//! spdif-input-1/2 -------> || 128 entries || ----> spdif-output-1/2
//! spdif-input-3/4 -------> ||             ||
//! adat-input-1/2 --------> ||             ||
//! adat-input-3/4 --------> ||             ||
//! adat-input-5/6 --------> ||             ||
//! adat-input-7/8 --------> ||             ||
//!                          ||             ||
//! stream-input-1/2 ------> ||             || ----> stream-output-1/2
//! stream-input-3/4 ------> ||             || ----> stream-output-3/4
//! stream-input-5/6 ------> ||             || ----> stream-output-5/6
//! stream-input-7/8 ------> ||             || ----> stream-output-7/8
//!                          ||             || ----> stream-output-9/10
//!                          ||             || ----> stream-output-11/12
//!                          ||             || ----> stream-output-13/14
//!                          ||             || ----> stream-output-15/16
//!                          ||             ||
//! mixer-output-1/2 ------> ||             || ----> mixer-input-1/2
//! mixer-output-3/4 ------> ||             || ----> mixer-input-3/4
//! mixer-output-5/6 ------> ||             || ----> mixer-input-5/6
//! mixer-output-7/8 ------> ||             || ----> mixer-input-7/8
//! mixer-output-9/10 -----> ||             || ----> mixer-input-9/10
//! mixer-output-11/12 ----> ||             || ----> mixer-input-11/12
//! mixer-output-13/14 ----> ||             || ----> mixer-input-13/14
//! mixer-output-15/16 ----> ||             || ----> mixer-input-15/16
//!                          ||             || ----> mixer-input-17/18
//!                          ||             ||
//! ch-strip-output-1/2 ---> ||             || ----> ch-strip-input-1/2
//! reverb-output-1/2 -----> ||             || ----> reverb-input-1/2
//!                          ++=============++
//!
//!                          ++=============++
//! mixer-input-1/2 -------> ||             || ----> mixer-output-1/2
//! mixer-input-3/4 -------> ||             || ----> mixer-output-3/4
//! mixer-input-5/6 -------> ||             || ----> mixer-output-5/6
//! mixer-input-7/8 -------> ||    mixer    || ----> mixer-output-7/8
//! mixer-input-9/10 ------> ||             || ----> mixer-output-9/10
//! mixer-input-11/12 -----> ||   18 x 16   || ----> mixer-output-10/12
//! mixer-input-13/14 -----> ||             || ----> mixer-output-12/14
//! mixer-input-15/16 -----> ||             || ----> mixer-output-14/16
//! mixer-input-17/18 -----> ||             ||
//!                          ++=============++
//!
//!                          ++=============++
//!                          ||             || ----> Phone output 1/2
//!                          ||             ||
//! analog-output-1/2 -----> ||   output    || --+-> Phone output 3/4
//! analog-output-3/4 -----> ||             ||   +-> Headphone output 1/2
//! analog-output-5/6 -----> ||   group     ||
//!                          ||             || --+-> Phone output 5/6
//!                          ||             ||   +-> Headphone output 3/4
//!                          ++=============++
//!
//!                          ++=============++
//!                          ||  equalizer  ||
//! ch-strip-input-1/2 ----> ||      &      || ----> ch-strip-output-1/2
//!                          || compressor  ||
//!                          ++=============++
//!
//!                          ++=============++
//! reverb-input-1/2 ------> ||   reverb    || ----> reverb-output-1/2
//!                          ++=============++
//!
//! ```
//!
//! ## Data layout in TCAT application section for DSP effect
//!
//! The offset of TCAT application section is 0x6dd4. Any change by write transaction is firstly
//! effective when software message is written to 0x5ec.
//!
//! ### Data Layout for DSP effect
//!
//! * 0x000: DSP enable/disable (sw msg: 0x1c)
//! * 0x008: flags for channel strip effects (sw msg: 0x05)
//!     * 0x00000001: ch 0 equalizer enable
//!     * 0x00010000: ch 1 equalizer enable
//!     * 0x00000002: ch 0 compressor enable
//!     * 0x00020000: ch 1 compressor enable
//!     * 0x00000004: ch 0 equalizer after compressor
//!     * 0x00040000: ch 1 equalizer after compressor
//!
//! blk 0 | blk 2 | blk 4 | blk 6 | count   | purpose                         | sw msg |
//! ----- | ----- | ----- | ----- | --------| ------------------------------- | ------ |
//! 0x120 | 0x230 | 0x340 | 0x450 | 6 quads | ch 0 comp                       |   0x06 |
//! 0x138 | 0x248 | 0x358 | 0x468 | 2 quads | ch 0/ch 1 eq output             |   0z09 |
//! 0x140 | 0x250 | 0x360 | 0x470 | 5 quads | ch 0 eq low freq filter         |   0x0c |
//! 0x154 | 0x264 | 0x374 | 0x484 | 5 quads | ch 0 eq low-middle freq filter  |   0x0f |
//! 0x168 | 0x278 | 0x388 | 0x498 | 5 quads | ch 0 eq high-middle freq filter |   0x12 |
//! 0x17c | 0x28c | 0x39c | 0x4ac | 5 quads | ch 0 eq high freq filter        |   0x15 |
//! 0x190 | 0x2a0 | 0x3b0 | 0x4c0 | 6 quads | ch 0 reverb                     |   0x1a |
//!
//! blk 1 | blk 3 | blk 5 | blk 7 | count   | purpose                         | sw msg |
//! ----- | ----- | ----- | ----- | --------| ------------------------------- | ------ |
//! 0x1a8 | 0x2b8 | 0x3c8 | 0x4d8 | 6 quads | ch 1 comp                       |   0x07 |
//! 0x1c0 | 0x2d0 | 0x3e0 | 0x4f0 | 2 quads | ch 0/ch 1 eq output             |   0x0a |
//! 0x1c8 | 0x2d8 | 0x3e8 | 0x4f8 | 5 quads | ch 1 eq low freq filter         |   0x0d |
//! 0x1dc | 0x2ec | 0x3fc | 0x50c | 5 quads | ch 1 eq low-middle freq filter  |   0x10 |
//! 0x1f0 | 0x300 | 0x410 | 0x520 | 5 quads | ch 1 eq high-middle freq filter |   0x13 |
//! 0x204 | 0x314 | 0x424 | 0x534 | 5 quads | ch 1 eq high freq filter        |   0x16 |
//! 0x218 | 0x328 | 0x438 | 0x548 | 6 quads | ch 1 reverb                     |   0x1a |
//!
//! ### Compressor coefficients (6 quadlets)
//!
//! Actually change to block 2 is effective.
//!
//! quad | purpose          | min value  | max value  | min repr | max repr |
//! ---- | -------------    | ---------- | ---------- | -------- | -------- |
//!    0 | unknown          | 0x3f800000 | 0x3f800000 |    -     |    -     |
//!    1 | output volume    | 0x00000000 | 0x42800000 | -36.0 dB | +36.0 dB |
//!    2 | threshold        | 0xbfa00000 | 0x00000000 | -80.0 dB |   0.0 dB |
//!    3 | ratio            | 0x3d000000 | 0x3f000000 |  1.1:1   |  inf:1   |
//!    4 | attack           | 0xbf800000 | 0xbf700000 |   2ms    |  100ms   |
//!    5 | release          | 0x3f700000 | 0x3f800000 |  100ms   |   3s     |
//!
//! ### Equalizer output coefficients (2 quadlets)
//!
//! Actually change to block 2 is effective.
//!
//! quad | purpose          | min value  | max value  | min repr | max repr |
//! ---- | ---------------- | ---------- | ---------- | -------- | -------- |
//!    0 | left volume      | 0x00000000 | 0x3f800000 | -36.0 dB | +36.0 dB |
//!    1 | right volume     | 0x00000000 | 0x3f800000 | -36.0 dB | +36.0 dB |
//!
//! ### Equalizer coefficients (5 quadlets)
//!
//! Actually change to block 2 is effective.
//!
//! quad | purpose          | min value  | max value  | min repr | max repr |
//! ---- | ---------------- | ---------- | ---------- | -------- | -------- |
//!    0 | unknown          |     -      |     -      |    -     |    -     |
//!    1 | unknown          |     -      |     -      |    -     |    -     |
//!    2 | unknown          |     -      |     -      |    -     |    -     |
//!    3 | unknown          |     -      |     -      |    -     |    -     |
//!    4 | unknown          |     -      |     -      |    -     |    -     |
//!
//! ### Reverb coefficients (6 quadlets)
//!
//! Actually change to block 3 is effective.
//!
//! quad | purpose          | min value  | max value  | min repr | max repr |
//! ---- | ---------------- | ---------- | ---------- | -------- | -------- |
//!    0 | room size        | 0x00000000 | 0x3f800000 | 0 %      | 100 %    |
//!    1 | air              | 0x00000000 | 0x3f800000 | 100 %    | 0 %      |
//!    2 | enabled          | 0x00000000 | 0x3f800000 | false    | true     |
//!    3 | disabled         | 0x00000000 | 0x3f800000 | false    | true     |
//!    4 | pre filter value | 0x00000000 | 0x3f800000 | 5.0      | 0.0      |
//!    5 | pre filter sign  | 0x00000000 | 0x3f800000 | negative | positive |

use super::{tcat::tcd22xx_spec::*, *};

/// Protocol implementation specific to Saffire Pro 24 DSP.
#[derive(Default, Debug)]
pub struct SPro24DspProtocol;

impl TcatOperation for SPro24DspProtocol {}

impl TcatGlobalSectionSpecification for SPro24DspProtocol {}

impl TcatExtensionOperation for SPro24DspProtocol {}

impl Tcd22xxSpecification for SPro24DspProtocol {
    const INPUTS: &'static [Input] = &[
        Input {
            id: SrcBlkId::Ins0,
            offset: 2,
            count: 2,
            label: Some("Mic"),
        },
        Input {
            id: SrcBlkId::Ins0,
            offset: 0,
            count: 2,
            label: Some("Line"),
        },
        Input {
            id: SrcBlkId::Ins0,
            offset: 8,
            count: 2,
            label: Some("Ch-strip"),
        },
        // Input{id: SrcBlkId::Ins0, offset: 4, count: 2, label: Some("Ch-strip")}, at 88.2/96.0 kHz.
        Input {
            id: SrcBlkId::Ins0,
            offset: 14,
            count: 2,
            label: Some("Reverb"),
        },
        // Input{id: SrcBlkId::Ins0, offset: 6, count: 2, label: Some("Reverb")}, at 88.2/96.0 kHz.
        Input {
            id: SrcBlkId::Aes,
            offset: 6,
            count: 2,
            label: Some("S/PDIF-coax"),
        },
        // NOTE: share the same optical interface.
        Input {
            id: SrcBlkId::Adat,
            offset: 0,
            count: 8,
            label: None,
        },
        Input {
            id: SrcBlkId::Aes,
            offset: 4,
            count: 2,
            label: Some("S/PDIF-opt"),
        },
    ];

    const OUTPUTS: &'static [Output] = &[
        Output {
            id: DstBlkId::Ins0,
            offset: 0,
            count: 6,
            label: None,
        },
        Output {
            id: DstBlkId::Aes,
            offset: 6,
            count: 2,
            label: Some("S/PDIF-coax"),
        },
        Output {
            id: DstBlkId::Ins0,
            offset: 8,
            count: 2,
            label: Some("Ch-strip"),
        },
        // Output{id: DstBlkId::Ins0, offset: 4, count: 2, label: Some("Ch-strip")}, at 88.2/96.0 kHz.
        Output {
            id: DstBlkId::Ins0,
            offset: 14,
            count: 2,
            label: Some("Reverb"),
        },
        // Output{id: DstBlkId::Ins0, offset: 6, count: 2, label: Some("Reverb")}, at 88.2/96.0 kHz.
    ];

    // NOTE: The first 4 entries in router section are used to display hardware metering.
    const FIXED: &'static [SrcBlk] = &[
        SrcBlk {
            id: SrcBlkId::Ins0,
            ch: 2,
        },
        SrcBlk {
            id: SrcBlkId::Ins0,
            ch: 3,
        },
        SrcBlk {
            id: SrcBlkId::Ins0,
            ch: 0,
        },
        SrcBlk {
            id: SrcBlkId::Ins0,
            ch: 1,
        },
    ];
}

impl SaffireproSwNoticeOperation for SPro24DspProtocol {
    const SW_NOTICE_OFFSET: usize = 0x05ec;
}

impl SaffireproOutGroupSpecification for SPro24DspProtocol {
    const OUT_GROUP_STATE_OFFSET: usize = 0x000c;

    const ENTRY_COUNT: usize = 6;
    const HAS_VOL_HWCTL: bool = false;

    const SRC_NOTICE: u32 = 0x00000001;
    const DIM_MUTE_NOTICE: u32 = 0x00000002;
}

impl SaffireproInputSpecification for SPro24DspProtocol {
    const INPUT_PARAMS_OFFSET: usize = 0x0058;
}

// When VRM mode is enabled, write 0x00000001 to the offset
#[allow(dead_code)]
const DSP_ENABLE_OFFSET: usize = 0x0070; // sw notice: 0x1c.
const CH_STRIP_FLAG_OFFSET: usize = 0x0078;
const CH_STRIP_FLAG_EQ_ENABLE: u16 = 0x0001;
const CH_STRIP_FLAG_COMP_ENABLE: u16 = 0x0002;
const CH_STRIP_FLAG_EQ_AFTER_COMP: u16 = 0x0004;

const CH_STRIP_FLAG_SW_NOTICE: u32 = 0x00000005;

const COEF_OFFSET: usize = 0x0190;
const COEF_BLOCK_SIZE: usize = 0x88;
//const COEF_BLOCK_COUNT: usize = 8;

const EQ_COEF_COUNT: usize = 5;

const COMP_OUTPUT_OFFSET: usize = 0x04;
const COMP_THRESHOLD_OFFSET: usize = 0x08;
const COMP_RATIO_OFFSET: usize = 0x0c;
const COMP_ATTACK_OFFSET: usize = 0x10;
const COMP_RELEASE_OFFSET: usize = 0x14;

const COMP_CH0_SW_NOTICE: u32 = 0x00000006;
const COMP_CH1_SW_NOTICE: u32 = 0x00000007;

const EQ_OUTPUT_OFFSET: usize = 0x18;
const EQ_LOW_FREQ_OFFSET: usize = 0x20;
//const EQ_LOW_MIDDLE_FREQ_OFFSET: usize = 0x34;
//const EQ_HIGH_MIDDLE_FREQ_OFFSET: usize = 0x48;
//const EQ_HIGH_FREQ_OFFSET: usize = 0x5c;

const EQ_OUTPUT_CH0_SW_NOTICE: u32 = 0x09;
const EQ_OUTPUT_CH1_SW_NOTICE: u32 = 0x0a;
const EQ_LOW_FREQ_CH0_SW_NOTICE: u32 = 0x0c;
const EQ_LOW_FREQ_CH1_SW_NOTICE: u32 = 0x0c;
const EQ_LOW_MIDDLE_FREQ_CH0_SW_NOTICE: u32 = 0x0f;
const EQ_LOW_MIDDLE_FREQ_CH1_SW_NOTICE: u32 = 0x10;
const EQ_HIGH_MIDDLE_FREQ_CH0_SW_NOTICE: u32 = 0x12;
const EQ_HIGH_MIDDLE_FREQ_CH1_SW_NOTICE: u32 = 0x13;
const EQ_HIGH_FREQ_CH0_SW_NOTICE: u32 = 0x15;
const EQ_HIGH_FREQ_CH1_SW_NOTICE: u32 = 0x16;

const REVERB_SIZE_OFFSET: usize = 0x70;
const REVERB_AIR_OFFSET: usize = 0x74;
const REVERB_ENABLE_OFFSET: usize = 0x78;
const REVERB_DISABLE_OFFSET: usize = 0x7c;
const REVERB_PRE_FILTER_VALUE_OFFSET: usize = 0x80;
const REVERB_PRE_FILTER_SIGN_OFFSET: usize = 0x84;

const REVERB_SW_NOTICE: u32 = 0x0000001a;

fn serialize_f32(val: &f32, raw: &mut [u8]) -> Result<(), String> {
    assert!(raw.len() >= 4);

    raw[..4].copy_from_slice(&val.to_be_bytes());

    Ok(())
}

fn deserialize_f32(val: &mut f32, raw: &[u8]) -> Result<(), String> {
    assert!(raw.len() >= 4);

    let mut quadlet = [0; 4];
    quadlet.copy_from_slice(&raw[..4]);
    *val = f32::from_be_bytes(quadlet);

    Ok(())
}

/// State of compressor effect.
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Spro24DspCompressorState {
    /// The volume of output, between 0.0 to 64.0.
    pub output: [f32; 2],
    /// The threshold, between -1.25 to 0.0.
    pub threshold: [f32; 2],
    /// The ratio, between 0.03125 to 0.5.
    pub ratio: [f32; 2],
    /// The attack, between -0.9375 to -1.0.
    pub attack: [f32; 2],
    /// The release, between 0.9375 to 1.0.
    pub release: [f32; 2],
}

/// Coefficients per frequency band.
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Spro24DspEqualizerFrequencyBandState([f32; EQ_COEF_COUNT]);

/// State of equalizer effect.
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Spro24DspEqualizerState {
    /// The volume of output, between 0.0 to 1.0.
    pub output: [f32; 2],
    // TODO: how to convert these coefficients to friendly parameter.
    pub low_coef: [Spro24DspEqualizerFrequencyBandState; 2],
    pub low_middle_coef: [Spro24DspEqualizerFrequencyBandState; 2],
    pub high_middle_coef: [Spro24DspEqualizerFrequencyBandState; 2],
    pub high_coef: [Spro24DspEqualizerFrequencyBandState; 2],
}

/// State of reverb effect.
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Spro24DspReverbState {
    /// The size of room, between 0.0 to 1.0.
    pub size: f32,
    /// The amount to reduce dumping, between 0.0 to 1.0.
    pub air: f32,
    /// Whether the reverb effect is enabled or not.
    pub enabled: bool,
    /// The ratio of high-pass/low-pass filter, between -1.0 to 1.0.
    pub pre_filter: f32,
}

/// General parameters of DSP effects.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Spro24DspEffectGeneralParams {
    /// Use equalizer after compressor.
    pub eq_after_comp: [bool; 2],
    /// Whether to enable compressor.
    pub comp_enable: [bool; 2],
    /// Whether to enable equalizer.
    pub eq_enable: [bool; 2],
}

const COEF_BLOCK_COMP: usize = 2;
const COEF_BLOCK_EQ: usize = 2;
const COEF_BLOCK_REVERB: usize = 3;

// Serialize to a pair of coefficient block.
fn serialize_compressor_state(
    state: &Spro24DspCompressorState,
    raw: &mut [u8],
) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE * 2);

    (0..2).try_for_each(|ch| {
        let base_offset = COEF_BLOCK_SIZE * ch;

        let pos = base_offset + COMP_OUTPUT_OFFSET;
        serialize_f32(&state.output[ch], &mut raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_THRESHOLD_OFFSET;
        serialize_f32(&state.threshold[ch], &mut raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_RATIO_OFFSET;
        serialize_f32(&state.ratio[ch], &mut raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_ATTACK_OFFSET;
        serialize_f32(&state.attack[ch], &mut raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_RELEASE_OFFSET;
        serialize_f32(&state.release[ch], &mut raw[pos..(pos + 4)])
    })
}

// Deserialize from a pair of coefficient block.
fn deserialize_compressor_state(
    state: &mut Spro24DspCompressorState,
    raw: &[u8],
) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE * 2);

    (0..2).try_for_each(|ch| {
        let base_offset = COEF_BLOCK_SIZE * ch;

        let pos = base_offset + COMP_OUTPUT_OFFSET;
        deserialize_f32(&mut state.output[ch], &raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_THRESHOLD_OFFSET;
        deserialize_f32(&mut state.threshold[ch], &raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_RATIO_OFFSET;
        deserialize_f32(&mut state.ratio[ch], &raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_ATTACK_OFFSET;
        deserialize_f32(&mut state.attack[ch], &raw[pos..(pos + 4)])?;

        let pos = base_offset + COMP_RELEASE_OFFSET;
        deserialize_f32(&mut state.release[ch], &raw[pos..(pos + 4)])
    })
}

// Serialize to a pair of coefficient block.
fn serialize_equalizer_state(
    state: &Spro24DspEqualizerState,
    raw: &mut [u8],
) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE * 2);

    (0..2).try_for_each(|ch| {
        let base_offset = COEF_BLOCK_SIZE * ch;

        let pos = base_offset + EQ_OUTPUT_OFFSET;
        serialize_f32(&state.output[ch], &mut raw[pos..(pos + 4)])?;

        state.low_coef[ch]
            .0
            .iter()
            .chain(state.low_middle_coef[ch].0.iter())
            .chain(state.high_middle_coef[ch].0.iter())
            .chain(state.high_coef[ch].0.iter())
            .enumerate()
            .try_for_each(|(i, coef)| {
                let pos = base_offset + EQ_LOW_FREQ_OFFSET + i * 4;
                serialize_f32(coef, &mut raw[pos..(pos + 4)])
            })
    })
}

// Deserialize from a pair of coefficient block.
fn deserialize_equalizer_state(
    state: &mut Spro24DspEqualizerState,
    raw: &[u8],
) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE * 2);

    (0..2).try_for_each(|ch| {
        let base_offset = COEF_BLOCK_SIZE * ch;

        let pos = base_offset + EQ_OUTPUT_OFFSET;
        deserialize_f32(&mut state.output[ch], &raw[pos..(pos + 4)])?;

        state.low_coef[ch]
            .0
            .iter_mut()
            .chain(state.low_middle_coef[ch].0.iter_mut())
            .chain(state.high_middle_coef[ch].0.iter_mut())
            .chain(state.high_coef[ch].0.iter_mut())
            .enumerate()
            .try_for_each(|(i, coef)| {
                let pos = base_offset + EQ_LOW_FREQ_OFFSET + i * 4;
                deserialize_f32(coef, &raw[pos..(pos + 4)])
            })
    })
}

// Serialize to a coefficient block.
fn serialize_reverb_state(state: &Spro24DspReverbState, raw: &mut [u8]) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE);

    let pos = REVERB_SIZE_OFFSET;
    serialize_f32(&state.size, &mut raw[pos..(pos + 4)])?;

    let pos = REVERB_AIR_OFFSET;
    serialize_f32(&state.air, &mut raw[pos..(pos + 4)])?;

    let enabled_pos = REVERB_ENABLE_OFFSET;
    let disabled_pos = REVERB_DISABLE_OFFSET;
    let vals = if state.enabled {
        [1.0, 0.0]
    } else {
        [0.0, 1.0]
    };
    serialize_f32(&vals[0], &mut raw[enabled_pos..(enabled_pos + 4)])?;
    serialize_f32(&vals[1], &mut raw[disabled_pos..(disabled_pos + 4)])?;

    let pos = REVERB_ENABLE_OFFSET;
    let val = if state.enabled { 1.0 } else { 0.0 };
    serialize_f32(&val, &mut raw[pos..(pos + 4)])?;

    let pos = REVERB_PRE_FILTER_VALUE_OFFSET;
    let val = state.pre_filter.abs();
    serialize_f32(&val, &mut raw[pos..(pos + 4)])?;

    let pos = REVERB_PRE_FILTER_SIGN_OFFSET;
    let sign = if state.pre_filter > 0.0 { 1.0 } else { 0.0 };
    serialize_f32(&sign, &mut raw[pos..(pos + 4)])?;

    Ok(())
}

// Deserialize from a coefficient block.
fn deserialize_reverb_state(state: &mut Spro24DspReverbState, raw: &[u8]) -> Result<(), String> {
    assert!(raw.len() >= COEF_BLOCK_SIZE);

    let pos = REVERB_SIZE_OFFSET;
    deserialize_f32(&mut state.size, &raw[pos..(pos + 4)])?;

    let pos = REVERB_AIR_OFFSET;
    deserialize_f32(&mut state.air, &raw[pos..(pos + 4)])?;

    let mut val = 0.0;
    let pos = REVERB_ENABLE_OFFSET;
    deserialize_f32(&mut val, &raw[pos..(pos + 4)])?;
    state.enabled = val > 0.0;

    let mut val = 0.0;
    let pos = REVERB_PRE_FILTER_VALUE_OFFSET;
    deserialize_f32(&mut val, &raw[pos..(pos + 4)])?;

    let mut sign = 0.0;
    let pos = REVERB_PRE_FILTER_SIGN_OFFSET;
    deserialize_f32(&mut sign, &raw[pos..(pos + 4)])?;
    if sign == 0.0 {
        val *= -1.0;
    }
    state.pre_filter = val;

    Ok(())
}

fn serialize_effect_general_params(
    params: &Spro24DspEffectGeneralParams,
    raw: &mut [u8],
) -> Result<(), String> {
    assert!(raw.len() >= 4);

    let mut val = 0u32;

    (0..2).for_each(|i| {
        let mut flags = 0u16;
        if params.eq_after_comp[i] {
            flags |= CH_STRIP_FLAG_EQ_AFTER_COMP;
        }
        if params.comp_enable[i] {
            flags |= CH_STRIP_FLAG_COMP_ENABLE;
        }
        if params.eq_enable[i] {
            flags |= CH_STRIP_FLAG_EQ_ENABLE;
        }

        val |= (flags as u32) << (16 * i);
    });

    serialize_u32(&val, raw);

    Ok(())
}

fn deserialize_effect_general_params(
    params: &mut Spro24DspEffectGeneralParams,
    raw: &[u8],
) -> Result<(), String> {
    assert!(raw.len() >= 4);

    let mut val = 0u32;
    deserialize_u32(&mut val, raw);
    (0..2).for_each(|i| {
        let flags = (val >> (i * 16)) as u16;
        params.eq_after_comp[i] = flags & CH_STRIP_FLAG_EQ_AFTER_COMP > 0;
        params.comp_enable[i] = flags & CH_STRIP_FLAG_COMP_ENABLE > 0;
        params.eq_enable[i] = flags & CH_STRIP_FLAG_EQ_ENABLE > 0;
    });

    Ok(())
}

impl TcatExtensionSectionParamsOperation<Spro24DspEffectGeneralParams> for SPro24DspProtocol {
    fn cache_extension_whole_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &mut Spro24DspEffectGeneralParams,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut raw = vec![0u8; 4];
        Self::read_extension(
            req,
            node,
            &sections.application,
            CH_STRIP_FLAG_OFFSET,
            &mut raw,
            timeout_ms,
        )?;
        deserialize_effect_general_params(params, &raw)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionPartialMutableParamsOperation<Spro24DspEffectGeneralParams>
    for SPro24DspProtocol
{
    fn update_extension_partial_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &Spro24DspEffectGeneralParams,
        prev: &mut Spro24DspEffectGeneralParams,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut new = vec![0u8; 4];
        serialize_effect_general_params(params, &mut new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        let mut old = vec![0u8; 4];
        serialize_effect_general_params(prev, &mut old)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        if new != old {
            Self::write_extension(
                req,
                node,
                &sections.application,
                CH_STRIP_FLAG_OFFSET,
                &mut new,
                timeout_ms,
            )?;
            Self::write_sw_notice(req, node, sections, CH_STRIP_FLAG_SW_NOTICE, timeout_ms)?;
        }
        deserialize_effect_general_params(prev, &new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionParamsOperation<Spro24DspCompressorState> for SPro24DspProtocol {
    fn cache_extension_whole_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &mut Spro24DspCompressorState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut raw = vec![0u8; COEF_BLOCK_SIZE * 2];
        Self::read_extension(
            req,
            node,
            &sections.application,
            COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_COMP,
            &mut raw,
            timeout_ms,
        )?;
        deserialize_compressor_state(params, &raw)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionPartialMutableParamsOperation<Spro24DspCompressorState>
    for SPro24DspProtocol
{
    fn update_extension_partial_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &Spro24DspCompressorState,
        prev: &mut Spro24DspCompressorState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut new = vec![0u8; COEF_BLOCK_SIZE * 2];
        serialize_compressor_state(params, &mut new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        let mut old = vec![0u8; COEF_BLOCK_SIZE * 2];
        serialize_compressor_state(prev, &mut old)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        (0..(COEF_BLOCK_SIZE * 2)).step_by(4).try_for_each(|pos| {
            if new[pos..(pos + 4)] != old[pos..(pos + 4)] {
                Self::write_extension(
                    req,
                    node,
                    &sections.application,
                    COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_COMP + pos,
                    &mut new[pos..(pos + 4)],
                    timeout_ms,
                )
            } else {
                Ok(())
            }
        })?;
        Self::write_sw_notice(req, node, sections, COMP_CH0_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(req, node, sections, COMP_CH1_SW_NOTICE, timeout_ms)?;

        deserialize_compressor_state(prev, &new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionParamsOperation<Spro24DspEqualizerState> for SPro24DspProtocol {
    fn cache_extension_whole_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &mut Spro24DspEqualizerState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut raw = vec![0u8; COEF_BLOCK_SIZE * 2];
        Self::read_extension(
            req,
            node,
            &sections.application,
            COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_EQ,
            &mut raw,
            timeout_ms,
        )?;
        deserialize_equalizer_state(params, &raw)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionPartialMutableParamsOperation<Spro24DspEqualizerState>
    for SPro24DspProtocol
{
    fn update_extension_partial_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &Spro24DspEqualizerState,
        prev: &mut Spro24DspEqualizerState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut new = vec![0u8; COEF_BLOCK_SIZE * 2];
        serialize_equalizer_state(params, &mut new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        let mut old = vec![0u8; COEF_BLOCK_SIZE * 2];
        serialize_equalizer_state(prev, &mut old)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        (0..(COEF_BLOCK_SIZE * 2)).step_by(4).try_for_each(|pos| {
            if new[pos..(pos + 4)] != old[pos..(pos + 4)] {
                Self::write_extension(
                    req,
                    node,
                    &sections.application,
                    COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_EQ + pos,
                    &mut new[pos..(pos + 4)],
                    timeout_ms,
                )
            } else {
                Ok(())
            }
        })?;
        Self::write_sw_notice(req, node, sections, EQ_OUTPUT_CH0_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(req, node, sections, EQ_OUTPUT_CH1_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(req, node, sections, EQ_LOW_FREQ_CH0_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(req, node, sections, EQ_LOW_FREQ_CH1_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(
            req,
            node,
            sections,
            EQ_LOW_MIDDLE_FREQ_CH0_SW_NOTICE,
            timeout_ms,
        )?;
        Self::write_sw_notice(
            req,
            node,
            sections,
            EQ_LOW_MIDDLE_FREQ_CH1_SW_NOTICE,
            timeout_ms,
        )?;
        Self::write_sw_notice(
            req,
            node,
            sections,
            EQ_HIGH_MIDDLE_FREQ_CH0_SW_NOTICE,
            timeout_ms,
        )?;
        Self::write_sw_notice(
            req,
            node,
            sections,
            EQ_HIGH_MIDDLE_FREQ_CH1_SW_NOTICE,
            timeout_ms,
        )?;
        Self::write_sw_notice(req, node, sections, EQ_HIGH_FREQ_CH0_SW_NOTICE, timeout_ms)?;
        Self::write_sw_notice(req, node, sections, EQ_HIGH_FREQ_CH1_SW_NOTICE, timeout_ms)?;

        deserialize_equalizer_state(prev, &new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionParamsOperation<Spro24DspReverbState> for SPro24DspProtocol {
    fn cache_extension_whole_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &mut Spro24DspReverbState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut raw = vec![0u8; COEF_BLOCK_SIZE];
        Self::read_extension(
            req,
            node,
            &sections.application,
            COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_REVERB,
            &mut raw,
            timeout_ms,
        )?;
        deserialize_reverb_state(params, &raw)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl TcatExtensionSectionPartialMutableParamsOperation<Spro24DspReverbState> for SPro24DspProtocol {
    fn update_extension_partial_params(
        req: &FwReq,
        node: &FwNode,
        sections: &ExtensionSections,
        _: &ExtensionCaps,
        params: &Spro24DspReverbState,
        prev: &mut Spro24DspReverbState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut new = vec![0u8; COEF_BLOCK_SIZE];
        serialize_reverb_state(params, &mut new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        let mut old = vec![0u8; COEF_BLOCK_SIZE];
        serialize_reverb_state(prev, &mut old)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))?;

        (0..(COEF_BLOCK_SIZE * 2)).step_by(4).try_for_each(|pos| {
            if new[pos..(pos + 4)] != old[pos..(pos + 4)] {
                Self::write_extension(
                    req,
                    node,
                    &sections.application,
                    COEF_OFFSET + COEF_BLOCK_SIZE * COEF_BLOCK_REVERB + pos,
                    &mut new[pos..(pos + 4)],
                    timeout_ms,
                )
            } else {
                Ok(())
            }
        })?;
        Self::write_sw_notice(req, node, sections, REVERB_SW_NOTICE, timeout_ms)?;
        deserialize_reverb_state(prev, &new)
            .map_err(|cause| Error::new(ProtocolExtensionError::Appl, &cause))
    }
}

impl SPro24DspProtocol {
    pub const COMPRESSOR_OUTPUT_MIN: f32 = 0.0;
    pub const COMPRESSOR_OUTPUT_MAX: f32 = 64.0;

    pub const COMPRESSOR_THRESHOLD_MIN: f32 = -1.25;
    pub const COMPRESSOR_THRESHOLD_MAX: f32 = 0.0;

    pub const COMPRESSOR_RATIO_MIN: f32 = 0.03125;
    pub const COMPRESSOR_RATIO_MAX: f32 = 0.5;

    pub const COMPRESSOR_ATTACK_MIN: f32 = -1.0;
    pub const COMPRESSOR_ATTACK_MAX: f32 = -0.9375;

    pub const COMPRESSOR_RELEASE_MIN: f32 = 0.9375;
    pub const COMPRESSOR_RELEASE_MAX: f32 = 1.0;

    pub const EQUALIZER_OUTPUT_MIN: f32 = 0.0;
    pub const EQUALIZER_OUTPUT_MAX: f32 = 1.0;

    pub const REVERB_SIZE_MIN: f32 = 0.0;
    pub const REVERB_SIZE_MAX: f32 = 1.0;

    pub const REVERB_AIR_MIN: f32 = 0.0;
    pub const REVERB_AIR_MAX: f32 = 1.0;

    pub const REVERB_PRE_FILTER_MIN: f32 = -1.0;
    pub const REVERB_PRE_FILTER_MAX: f32 = 1.0;
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn compressor_state_serdes() {
        let state = Spro24DspCompressorState {
            output: [0.04, 0.05],
            threshold: [0.16, 0.17],
            ratio: [0.20, 0.21],
            attack: [0.32, 0.33],
            release: [0.44, 0.45],
        };

        let mut raw = [0u8; COEF_BLOCK_SIZE * 2];
        serialize_compressor_state(&state, &mut raw).unwrap();

        let mut s = Spro24DspCompressorState::default();
        deserialize_compressor_state(&mut s, &raw).unwrap();

        assert_eq!(state, s);
    }

    #[test]
    fn equalizer_state_serdes() {
        let state = Spro24DspEqualizerState {
            output: [0.06, 0.07],
            low_coef: [
                Spro24DspEqualizerFrequencyBandState([0.00, 0.01, 0.02, 0.03, 0.04]),
                Spro24DspEqualizerFrequencyBandState([0.10, 0.11, 0.12, 0.13, 0.14]),
            ],
            low_middle_coef: [
                Spro24DspEqualizerFrequencyBandState([0.20, 0.21, 0.22, 0.23, 0.24]),
                Spro24DspEqualizerFrequencyBandState([0.30, 0.31, 0.32, 0.33, 0.34]),
            ],
            high_middle_coef: [
                Spro24DspEqualizerFrequencyBandState([0.40, 0.41, 0.42, 0.43, 0.44]),
                Spro24DspEqualizerFrequencyBandState([0.50, 0.51, 0.52, 0.53, 0.54]),
            ],
            high_coef: [
                Spro24DspEqualizerFrequencyBandState([0.60, 0.61, 0.62, 0.63, 0.64]),
                Spro24DspEqualizerFrequencyBandState([0.70, 0.71, 0.72, 0.73, 0.74]),
            ],
        };

        let mut raw = [0u8; COEF_BLOCK_SIZE * 2];
        serialize_equalizer_state(&state, &mut raw).unwrap();

        let mut s = Spro24DspEqualizerState::default();
        deserialize_equalizer_state(&mut s, &raw).unwrap();

        assert_eq!(state, s);
    }

    #[test]
    fn reverb_state_serdes() {
        let state = Spro24DspReverbState {
            size: 0.04,
            air: 0.14,
            enabled: false,
            pre_filter: -0.1,
        };
        let mut raw = [0u8; COEF_BLOCK_SIZE];
        serialize_reverb_state(&state, &mut raw).unwrap();

        let mut s = Spro24DspReverbState::default();
        deserialize_reverb_state(&mut s, &raw).unwrap();

        assert_eq!(state, s);
    }

    #[test]
    fn effect_general_params_serdes() {
        let params = Spro24DspEffectGeneralParams {
            eq_after_comp: [false, true],
            comp_enable: [true, false],
            eq_enable: [false, true],
        };
        let mut raw = [0u8; 4];
        serialize_effect_general_params(&params, &mut raw).unwrap();

        let mut p = Spro24DspEffectGeneralParams::default();
        deserialize_effect_general_params(&mut p, &raw).unwrap();

        assert_eq!(params, p);
    }
}