rbox 0.1.7

Rust library for interacting with the local and export data of Pioneers Rekordbox DJ software
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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
// Copyright (C) 2026 Dylan Jones
// SPDX-License-Identifier: GPL-3.0-only

#![allow(unused_parens)] // Supress warnings coming from binrw macros

use binrw::io::{Read, Seek, Write};
use binrw::{binrw, BinRead, BinResult, BinWrite, Endian, NullWideString};
use modular_bitfield::prelude::*;
use std::ffi::OsStr;

use super::xor::XorStream;
use crate::error::{Error, Result};

/// The tag of section.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub enum AnlzTag {
    /// File section that contains all other sections.
    #[brw(magic = b"PMAI")]
    File,
    /// All beats found in the track.
    #[brw(magic = b"PQTZ")]
    BeatGrid,
    /// All extended beats found in the track.
    #[brw(magic = b"PQT2")]
    ExtendedBeatGrid,
    /// Either memory points and loops or hotcues and hot loops of the track.
    ///
    /// *Note:* Since the release of the Nexus 2 series, there also exists the `ExtendedCueList`
    /// section which can carry additional information.
    #[brw(magic = b"PCOB")]
    CueList,
    /// Extended version of the `CueList` section (since Nexus 2 series).
    #[brw(magic = b"PCO2")]
    ExtendedCueList,
    /// Single cue entry inside a `ExtendedCueList` section.
    #[brw(magic = b"PCP2")]
    ExtendedCue,
    /// Single cue entry inside a `CueList` section.
    #[brw(magic = b"PCPT")]
    Cue,
    /// File path of the audio file.
    #[brw(magic = b"PPTH")]
    Path,
    /// Seek information for variable bitrate files.
    #[brw(magic = b"PVBR")]
    VBR,
    /// Fixed-width monochrome preview of the track waveform.
    #[brw(magic = b"PWAV")]
    WaveformPreview,
    /// Smaller version of the fixed-width monochrome preview of the track waveform (for the
    /// CDJ-900).
    #[brw(magic = b"PWV2")]
    TinyWaveformPreview,
    /// Variable-width large monochrome version of the track waveform.
    ///
    /// Used in `.EXT` files.
    #[brw(magic = b"PWV3")]
    WaveformDetail,
    /// Fixed-width colored version of the track waveform.
    ///
    /// Used in `.EXT` files.
    #[brw(magic = b"PWV4")]
    WaveformColorPreview,
    /// Variable-width large colored version of the track waveform.
    ///
    /// Used in `.EXT` files.
    #[brw(magic = b"PWV5")]
    WaveformColorDetail,
    /// Fixed-width three-band preview of the track waveform.
    ///
    /// Used in `.2EX` files.
    #[brw(magic = b"PWV6")]
    Waveform3BandPreview,
    /// Variable-width three-band rendition of the track waveform.
    ///
    /// Used in `.2EX` files.
    #[brw(magic = b"PWV7")]
    Waveform3BandDetail,
    /*
    /// Unknown Waveform data
    ///
    /// Used in `.2EX` files.
    #[brw(magic = b"PWVC")]
    WaveformUnknown,
    */
    /// Describes the structure of a sond (Intro, Chrous, Verse, etc.).
    ///
    /// Used in `.EXT` files.
    #[brw(magic = b"PSSI")]
    SongStructure,
    /// Unknown Kind.
    ///
    /// This allows handling files that contain unknown section types and allows to access later
    /// sections in the file that have a known type instead of failing to parse the whole file.
    Unknown([u8; 4]),
}

impl AnlzTag {
    /// Returns the tag type as a string.
    pub fn to_string(&self) -> String {
        match self {
            AnlzTag::File => "File".to_string(),
            AnlzTag::BeatGrid => "BeatGrid".to_string(),
            AnlzTag::ExtendedBeatGrid => "ExtendedBeatGrid".to_string(),
            AnlzTag::CueList => "CueList".to_string(),
            AnlzTag::ExtendedCueList => "ExtendedCueList".to_string(),
            AnlzTag::ExtendedCue => "ExtendedCue".to_string(),
            AnlzTag::Cue => "Cue".to_string(),
            AnlzTag::Path => "Path".to_string(),
            AnlzTag::VBR => "VBR".to_string(),
            AnlzTag::WaveformPreview => "WaveformPreview".to_string(),
            AnlzTag::TinyWaveformPreview => "TinyWaveformPreview".to_string(),
            AnlzTag::WaveformDetail => "WaveformDetail".to_string(),
            AnlzTag::WaveformColorPreview => "WaveformColorPreview".to_string(),
            AnlzTag::WaveformColorDetail => "WaveformColorDetail".to_string(),
            AnlzTag::Waveform3BandPreview => "Waveform3BandPreview".to_string(),
            AnlzTag::Waveform3BandDetail => "Waveform3BandDetail".to_string(),
            AnlzTag::SongStructure => "SongStructure".to_string(),
            _ => format!("Unknown({:?})", self),
        }
    }
}

impl From<String> for AnlzTag {
    fn from(tag: String) -> Self {
        match tag.as_str() {
            "File" => AnlzTag::File,
            "BeatGrid" => AnlzTag::BeatGrid,
            "ExtendedBeatGrid" => AnlzTag::ExtendedBeatGrid,
            "CueList" => AnlzTag::CueList,
            "ExtendedCueList" => AnlzTag::ExtendedCueList,
            "ExtendedCue" => AnlzTag::ExtendedCue,
            "Cue" => AnlzTag::Cue,
            "Path" => AnlzTag::Path,
            "VBR" => AnlzTag::VBR,
            "WaveformPreview" => AnlzTag::WaveformPreview,
            "TinyWaveformPreview" => AnlzTag::TinyWaveformPreview,
            "WaveformDetail" => AnlzTag::WaveformDetail,
            "WaveformColorPreview" => AnlzTag::WaveformColorPreview,
            "WaveformColorDetail" => AnlzTag::WaveformColorDetail,
            "Waveform3BandPreview" => AnlzTag::Waveform3BandPreview,
            "Waveform3BandDetail" => AnlzTag::Waveform3BandDetail,
            _ => panic!("Unknown tag type: {}", tag),
        }
    }
}

/// Header struct
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct Header {
    /// Kind of content in this item.
    pub tag: AnlzTag,
    /// Length of the header data (including `kind`, `size` and `total_size`).
    pub size: u32,
    /// Length of the section (including the header).
    pub total_size: u32,
}

impl Header {
    fn remaining_size(&self) -> u32 {
        self.size - 12
    }

    fn content_size(&self) -> u32 {
        self.total_size - self.size
    }
}

trait SizedSection {
    fn size(&self) -> u32;
}

// -- Beat Grid Tags ------------------------------------------------------------------------------

/// Single beat grid entry inside a `BeatGrid` section.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct Beat {
    /// Beat number inside the bar (1-4).
    pub beat_number: u16,
    /// Current tempo in centi-BPM (= 1/100 BPM).
    pub tempo: u16,
    /// Time in milliseconds after which this beat would occur (at normal playback speed).
    pub time: u32,
}

/// Beat Grid Tag containing all beats in the track
///
/// Identifier: PQTZ
/// Used in `.DAT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BeatGrid {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Unknown field: Seems to always be `0`.
    #[br(temp)]
    #[br(assert(u1 == 0))]
    #[bw(calc = 0)]
    u1: u32,
    /// Unknown field: Seems to always be `0x80000`.
    #[br(temp)]
    #[br(assert(u2 == 0x80000))]
    #[bw(calc = 0x80000)]
    u2: u32,
    /// Number of beats
    #[br(temp)]
    #[bw(calc = beats.len() as u32)]
    n_beats: u32,
    /// Beats in this beatgrid.
    #[br(count = n_beats)]
    pub beats: Vec<Beat>,
}

impl SizedSection for BeatGrid {
    fn size(&self) -> u32 {
        self.total_size
    }
}

/// Single beat grid entry inside a `BeatGridExtended` section.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct ExtBeat {
    /// Beat number inside the bar (1-4).
    pub beat_number: u8,
    /// Unknown field.
    unknown: u8,
}

/// Extended Beat Grid Tag (PQT2) containing all extended beats in the track
///
/// Identifier: PQT2
/// Used in `.EXT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ExtendedBeatGrid {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 56))]
    #[bw(calc = 56)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Padding: 4bytes.
    #[br(temp)]
    #[br(assert(pad1 == 0))]
    #[bw(calc = 0)]
    pad1: u32,
    /// Unknown field: Seems to always be `0x01000002`.
    #[br(temp)]
    #[br(assert(u1 == 0x01000002))]
    #[bw(calc = 0x01000002)]
    u1: u32,
    /// Padding: 4bytes.
    #[br(temp)]
    #[br(assert(pad2 == 0))]
    #[bw(calc = 0)]
    pad2: u32,
    /// Two 'normal' beats for BPM
    #[br(count = 2)]
    pub bpm: Vec<Beat>,
    /// Number of beats
    #[br(temp)]
    #[bw(calc = beats.len() as u32)]
    n_beats: u32,
    /// Unknow field
    u2: u32,
    /// Unknow field: Seems to always be `0`
    #[br(temp)]
    #[br(assert(u3 == 0))]
    #[bw(calc = 0)]
    u3: u32,
    /// Unknow field: Seems to always be `0`
    #[br(temp)]
    #[br(assert(u4 == 0))]
    #[bw(calc = 0)]
    u4: u32,
    /// Beats in this beatgrid.
    #[br(count = n_beats)]
    pub beats: Vec<ExtBeat>,
}

impl SizedSection for ExtendedBeatGrid {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Cue List Tags -------------------------------------------------------------------------------

/// Indicates if the cue is point or a loop.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(repr = u8)]
pub enum CueType {
    /// Cue point.
    Point = 1,
    /// Loop.
    Loop = 2,
}

impl Into<u16> for CueType {
    fn into(self) -> u16 {
        match self {
            CueType::Point => 1,
            CueType::Loop => 2,
        }
    }
}

/// Describes the types of entries found in a Cue List section.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big, repr = u32)]
pub enum CueListType {
    /// Memory cues or loops.
    MemoryCues = 0,
    /// Hot cues or loops.
    HotCues = 1,
}

impl Into<u16> for CueListType {
    fn into(self) -> u16 {
        match self {
            CueListType::MemoryCues => 0,
            CueListType::HotCues => 1,
        }
    }
}

/// Indicates if the cue is point or a loop.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(repr = u32)]
pub enum CueStatus {
    /// Cue point diabled.
    Disabled = 0,
    /// Cue point enabled
    Active = 4,
}

impl Into<u16> for CueStatus {
    fn into(self) -> u16 {
        match self {
            CueStatus::Disabled => 0,
            CueStatus::Active => 4,
        }
    }
}

/// A memory or hot cue (or loop).
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct Cue {
    /// Cue entry header.
    header: Header,
    /// Hot cue number.
    pub hot_cue: u32,
    /// Loop status. `4` if this cue is an active loop, `0` otherwise.
    pub status: CueStatus,
    /// Unknown field. Seems to always have the value `0x10000`.
    #[br(temp)]
    #[br(assert(u1 == 0x10000))]
    #[bw(calc = 0x10000)]
    u1: u32,
    /// Somehow used for sorting cues.
    /// 0xffff for first cue, 0,1,2 for next
    pub order_first: u16,
    /// Somehow used for sorting cues.
    /// 1,2,3 for first, second, third cue, 0xffff for last
    pub order_last: u16,
    /// Type of this cue
    pub cue_type: CueType,
    /// Unknown field. Seems always have the value `0`.
    #[br(temp)]
    #[br(assert(u2 == 0))]
    #[bw(calc = 0)]
    u2: u8,
    /// Unknown field. Seems always have the value `0x03E8` (= decimal 1000).
    #[br(temp)]
    #[br(assert(u3 == 1000))]
    #[bw(calc = 1000)]
    u3: u16,
    /// Time in milliseconds after which this cue would occur (at normal playback speed).
    pub time: u32,
    /// Time in milliseconds after which this the loop would jump back to `time` (at normal playback speed).
    pub loop_time: u32,
    /// Unknown field.
    u4: u32,
    /// Unknown field.
    u5: u32,
    /// Unknown field.
    u6: u32,
    /// Unknown field.
    u7: u32,
}

/// A extended memory or hot cue (or loop).
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct ExtendedCue {
    /// Cue entry header.
    header: Header,
    /// Hot cue number.
    pub hot_cue: u32,
    /// Type of this cue (`2` if this cue is a loop).
    pub cue_type: CueType,
    /// Unknown field. Seems always have the value `0`.
    #[br(temp)]
    #[br(assert(u1 == 0))]
    #[bw(calc = 0)]
    u1: u8,
    /// Unknown field. Seems always have the value `0x03E8` (= decimal 1000).
    #[br(temp)]
    #[br(assert(u2 == 1000))]
    #[bw(calc = 1000)]
    u2: u16,
    /// Time in milliseconds after which this cue would occur (at normal playback speed).
    pub time: u32,
    /// Time in milliseconds after which this the loop would jump back to `time` (at normal playback speed).
    pub loop_time: u32,
    /// Color assigned to this cue.
    ///
    /// Only used by memory cues, hot cues use a different value (see below).
    pub color: u8,
    /// Unknown field.
    u3: u8,
    /// Unknown field.
    u4: u16,
    /// Unknown field.
    u5: u32,
    /// Represents the loop size numerator (if this is a quantized loop).
    pub loop_numerator: u16,
    /// Represents the loop size denominator (if this is a quantized loop).
    pub loop_denominator: u16,
    /// Length of the comment string in bytes.
    #[br(temp)]
    #[bw(calc = (comment.len() as u32 + 1) * 2)]
    len_comment: u32,
    /// An UTF-16BE encoded string, followed by a trailing  `0x0000`.
    #[br(assert((comment.len() as u32 + 1) * 2 == len_comment))]
    pub comment: NullWideString,
    /// Rekordbox hotcue color index.
    pub hot_cue_color_index: u8,
    /// Rekordbot hot cue color RGB value.
    ///
    /// This color is similar but not identical to the color that Rekordbox displays, and possibly
    /// used to illuminate the RGB LEDs in a player that has loaded the cue. If not color is
    /// associated with this hot cue, the value is `(0, 0, 0)`.
    pub hot_cue_color_rgb: (u8, u8, u8),
    /// Unknown field.
    u6: u32,
    /// Unknown field.
    u7: u32,
    /// Unknown field.
    u8: u32,
    /// Unknown field.
    u9: u32,
    /// Unknown field.
    u10: u32,
}

/// List of cue points or loops.
///
/// Identifier: PCOB
/// Used in `.DAT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CueList {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// The types of cues (memory or hot) that this list contains.
    pub list_type: CueListType,
    /// Unknown field: Seems to always be `0`.
    #[br(temp)]
    #[br(assert(u1 == 0))]
    #[bw(calc = 0)]
    u1: u16,
    /// Number of cues.
    #[br(temp)]
    #[bw(calc = cues.len() as u16)]
    len_cues: u16,
    /// Unknown field: Seems to always be `0xFFFFFFFF`
    #[br(temp)]
    #[br(assert(u2 == 0xFFFFFFFF))]
    #[bw(calc = 0xFFFFFFFF)]
    u2: u32,
    /// Cues
    #[br(count = usize::from(len_cues))]
    pub cues: Vec<Cue>,
}

impl SizedSection for CueList {
    fn size(&self) -> u32 {
        self.total_size
    }
}

/// List of cue points or loops PCO2
///
/// Identifier: PCO2
/// Used in `.DAT` files.
/// Variation of the original `CueList` that also adds support for more metadata such as
/// comments and colors. Introduces with the Nexus 2 series players.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ExtendedCueList {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 20))]
    #[bw(calc = 20)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// The types of cues (memory or hot) that this list contains.
    pub list_type: CueListType,
    /// Number of cues.
    #[br(temp)]
    #[bw(calc = cues.len() as u16)]
    len_cues: u16,
    /// Unknown field (apparently always `0`)
    #[br(temp)]
    #[br(assert(u1 == 0))]
    #[bw(calc = 0)]
    u1: u16,
    /// Cues
    #[br(count = usize::from(len_cues))]
    pub cues: Vec<ExtendedCue>,
}

impl SizedSection for ExtendedCueList {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Path Tag ------------------------------------------------------------------------------------

/// Path tag containing the file path that this analysis belongs to.
///
/// Identifier: PPTH
/// Used in `.DAT`, `.EXT` and `.2EX` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Path {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 16))]
    #[bw(calc = 16)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Length of the path field in bytes.
    #[br(temp)]
    #[br(assert(len_path == total_size - header_size))]
    #[bw(calc = ((path.len() as u32) + 1) * 2)]
    len_path: u32,
    /// Path of the audio file.
    #[br(assert(len_path == total_size - header_size))]
    #[br(assert((path.len() as u32 + 1) * 2 == len_path))]
    pub path: NullWideString,
}

impl SizedSection for Path {
    fn size(&self) -> u32 {
        self.total_size
    }
}

impl Path {
    pub fn new<P: AsRef<std::path::Path> + AsRef<OsStr>>(path: P) -> Self {
        let string = NullWideString::from("");
        let mut item = Self {
            total_size: 0,
            path: string,
        };
        item.set(path);
        item
    }

    pub fn set<P: AsRef<std::path::Path> + AsRef<OsStr>>(&mut self, path: P) {
        let p = std::path::Path::new(&path);
        let string = NullWideString::from(p.as_os_str().to_str().unwrap());
        self.path = string.clone();
        self.total_size = ((string.len() as u32) + 1) * 2 + 16;
    }
}

// -- VBR Tag -------------------------------------------------------------------------------------

/// Variable bitrate tag (PVBR) containing seek information.
///
/// Identifier: PVBR
/// Used in `.DAT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct VBR {
    /// Length of the header data (including `kind`, `size` and `total_size`).
    #[br(assert(header_size == 16))]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Unknown field (apparently always `0`)
    #[br(temp)]
    #[br(assert(unknown == 0))]
    #[bw(calc = 0)]
    unknown: u32,
    /// Unknown data.
    #[br(count = total_size - header_size)]
    pub data: Vec<u8>,
}

impl SizedSection for VBR {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Tiny Waveform Preview Tag -------------------------------------------------------------------

/// Single Column value in a Tiny Waveform Preview.
#[bitfield]
#[derive(BinRead, BinWrite, Debug, PartialEq, Eq, Clone, Copy)]
#[br(big, map = Self::from_bytes)]
#[bw(big, map = |x: &TinyWaveformColumn| x.into_bytes())]
pub struct TinyWaveformColumn {
    #[allow(dead_code)]
    unused: B4,
    /// Height of the Column in pixels.
    pub height: B4,
}

impl Default for TinyWaveformColumn {
    fn default() -> Self {
        Self::new()
    }
}

/// Smaller version of the fixed-width monochrome preview of the track waveform.
///
/// Identifier: PWAV
/// Used in `.DAT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TinyWaveformPreview {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 20))]
    #[bw(calc = 20)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Unknown field.
    #[br(temp)]
    #[br(assert(len_preview == total_size - header_size))]
    #[bw(calc = data.len() as u32)]
    len_preview: u32,
    /// Unknown field (apparently always `0x000100000`)
    #[br(temp)]
    #[br(assert(unknown == 0x00010000))]
    #[bw(calc = 0x00010000)]
    unknown: u32,
    /// Waveform preview column data.
    #[br(count = len_preview)]
    pub data: Vec<TinyWaveformColumn>,
}

impl SizedSection for TinyWaveformPreview {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Waveform Tags -------------------------------------------------------------------------------

/// Single Column value in a Waveform Preview.
#[bitfield]
#[derive(BinRead, BinWrite, Debug, PartialEq, Eq, Clone, Copy)]
#[br(big, map = Self::from_bytes)]
#[bw(big, map = |x: &WaveformColumn| x.into_bytes())]
pub struct WaveformColumn {
    /// Height of the Column in pixels.
    pub height: B5,
    /// Shade of white.
    pub whiteness: B3,
}

/// Fixed-width monochrome preview of the track waveform.
///
/// Identifier: PWV2
/// Used in `.DAT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WaveformPreview {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 20))]
    #[bw(calc = 20)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Unknown field.
    #[br(temp)]
    #[br(assert(len_preview == total_size - header_size))]
    #[bw(calc = data.len() as u32)]
    len_preview: u32,
    /// Unknown field (apparently always `0x00010000`)
    #[br(temp)]
    #[br(assert(unknown == 0x00010000))]
    #[bw(calc = 0x00010000)]
    unknown: u32,
    /// Waveform preview column data.
    #[br(count = len_preview)]
    pub data: Vec<WaveformColumn>,
}

impl SizedSection for WaveformPreview {
    fn size(&self) -> u32 {
        self.total_size
    }
}

/// Variable-width large monochrome version of the track waveform.
///
/// Identifier: PWV3
/// Used in `.EXT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WaveformDetail {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 1.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 1))]
    #[bw(calc = 1u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[bw(calc = data.len() as u32)]
    #[br(assert((len_entry_bytes * len_entries) == total_size - header_size))]
    len_entries: u32,
    /// Unknown field (apparently always `0x00960000`)
    #[br(temp)]
    #[br(assert(unknown == 0x00960000))]
    #[bw(calc = 0x00960000)]
    unknown: u32,
    /// Waveform preview column data.
    ///
    /// Each entry represents one half-frame of audio data, and there are 75 frames per second,
    /// so for each second of track audio there are 150 waveform detail entries.
    #[br(count = len_entries)]
    pub data: Vec<WaveformColumn>,
}

impl SizedSection for WaveformDetail {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Waveform Color Tag ------------------------------------------------------------------

/// Single Column value in a Waveform Color Preview.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[brw(big)]
pub struct WaveformColorPreviewColumn {
    /// Unknown field (somehow encodes the "whiteness").
    u1: u8,
    /// Unknown field (somehow encodes the "whiteness").
    u2: u8,
    /// Sound energy in the bottom half of the frequency range (<10 KHz).
    pub energy_bottom_half_freq: u8,
    /// Sound energy in the bottom third of the frequency range.
    pub energy_bottom_third_freq: u8,
    /// Sound energy in the mid of the frequency range.
    pub energy_mid_third_freq: u8,
    /// Sound energy in the top of the frequency range.
    pub energy_top_third_freq: u8,
}

/// Variable-width large monochrome version of the track waveform.
///
/// Identifier: PWV4
/// Used in `.EXT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WaveformColorPreview {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 6.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 6))]
    #[bw(calc = 6u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[bw(calc = data.len() as u32)]
    #[br(assert((len_entry_bytes * len_entries) == total_size - header_size))]
    len_entries: u32,
    /// Unknown field (apparently always `0`)
    #[br(temp)]
    #[br(assert(unknown == 0))]
    #[bw(calc = 0)]
    unknown: u32,
    /// Waveform preview column data.
    ///
    /// Each entry represents one half-frame of audio data, and there are 75 frames per second,
    /// so for each second of track audio there are 150 waveform detail entries.
    #[br(count = len_entries)]
    pub data: Vec<WaveformColorPreviewColumn>,
}

impl SizedSection for WaveformColorPreview {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Waveform Color Detail Tag -------------------------------------------------------------------

/// Single Column value in a Waveform Color Detail section.
#[bitfield]
#[derive(BinRead, BinWrite, Debug, PartialEq, Eq, Clone, Copy)]
#[br(map = Self::from_bytes)]
#[bw(big, map = |x: &WaveformColorDetailColumn| x.into_bytes())]
pub struct WaveformColorDetailColumn {
    /// Red color component.
    pub red: B3,
    /// Green color component.
    pub green: B3,
    /// Blue color component.
    pub blue: B3,
    /// Height of the column.
    pub height: B5,
    /// Unknown field
    #[allow(dead_code)]
    unknown: B2,
}

/// Variable-width large colored version of the track waveform.
///
/// Identifier: PWV5
/// Used in `.EXT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WaveformColorDetail {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 2.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 2))]
    #[bw(calc = 2u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[bw(calc = data.len() as u32)]
    #[br(assert((len_entry_bytes * len_entries) == total_size - header_size))]
    len_entries: u32,
    /// Unknown field (apparently always `0x00960305`)
    #[br(temp)]
    #[br(assert(unknown == 0x00960305))]
    #[bw(calc = 0x00960305)]
    unknown: u32,
    /// Waveform detail column data.
    #[br(count = len_entries)]
    pub data: Vec<WaveformColorDetailColumn>,
}

impl SizedSection for WaveformColorDetail {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Waveform 3-Band Tags ------------------------------------------------------------------------

/// Single Column value in a Waveform 3-Band Preview section.
#[bitfield]
#[derive(BinRead, BinWrite, Debug, PartialEq, Eq, Clone, Copy)]
#[br(map = Self::from_bytes)]
#[bw(big, map = |x: &Waveform3BandColumn| x.into_bytes())]
pub struct Waveform3BandColumn {
    /// Mid-range component (amber).
    pub mid: B8,
    /// High frequencies (white).
    pub high: B8,
    /// Low frequencies (dark blue).
    pub low: B8,
}

/// Fixed-width three-band preview of the track waveform.
///
/// Identifier: PWV6
/// Used in `.2EX` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Waveform3BandPreview {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 20))]
    #[bw(calc = 20)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 3.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 3))]
    #[bw(calc = 3u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[bw(calc = data.len() as u32)]
    #[br(assert((len_entry_bytes * len_entries) == total_size - header_size))]
    len_entries: u32,
    /// Waveform detail column data.
    #[br(count = len_entries)]
    pub data: Vec<Waveform3BandColumn>,
}

impl SizedSection for Waveform3BandPreview {
    fn size(&self) -> u32 {
        self.total_size
    }
}

/// Variable-width three-band rendition of the track waveform, used for CDJ-3000 and in Rekordbox.
///
/// Identifier: PWV7
/// Used in `.2EX` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Waveform3BandDetail {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 24))]
    #[bw(calc = 24)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 2.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 3))]
    #[bw(calc = 3u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[bw(calc = data.len() as u32)]
    #[br(assert((len_entry_bytes * len_entries) == total_size - header_size))]
    len_entries: u32,
    /// Unknown field (apparently always `0x00960000`)
    #[br(temp)]
    #[br(assert(unknown == 0x00960000))]
    #[bw(calc = 0x00960000)]
    unknown: u32,
    /// Waveform detail column data.
    #[br(count = len_entries)]
    pub data: Vec<Waveform3BandColumn>,
}

impl SizedSection for Waveform3BandDetail {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Song Structure Tag --------------------------------------------------------------------------

/// Music classification that is used for Lightnight mode and based on rhythm, tempo kick drum and
/// sound density.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[brw(big, repr = u16)]
pub enum Mood {
    /// Phrase types consist of "Intro", "Up", "Down", "Chorus", and "Outro". Other values in each
    /// phrase entry cause the intro, chorus, and outro phrases to have their labels subdivided
    /// into styles "1" or "2" (for example, "Intro 1"), and "up" is subdivided into style "Up 1",
    /// "Up 2", or "Up 3".
    High = 1,
    /// Phrase types are labeled "Intro", "Verse 1" through "Verse 6", "Chorus", "Bridge", and
    /// "Outro".
    Mid,
    /// Phrase types are labeled "Intro", "Verse 1", "Verse 2", "Chorus", "Bridge", and "Outro".
    /// There are three different phrase type values for each of "Verse 1" and "Verse 2", but
    /// rekordbox makes no distinction between them.
    Low,
}

impl Into<u16> for Mood {
    fn into(self) -> u16 {
        match self {
            Mood::High => 1,
            Mood::Mid => 2,
            Mood::Low => 3,
        }
    }
}

/// Stylistic track bank for Lightning mode.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[brw(repr = u8)]
pub enum Bank {
    /// Default bank variant, treated as `Cool`.
    Default = 0,
    /// "Cool" bank variant.
    Cool,
    /// "Natural" bank variant.
    Natural,
    /// "Hot" bank variant.
    Hot,
    /// "Subtle" bank variant.
    Subtle,
    /// "Warm" bank variant.
    Warm,
    /// "Vivid" bank variant.
    Vivid,
    /// "Club 1" bank variant.
    Club1,
    /// "Club 2" bank variant.
    Club2,
}

impl Into<u16> for Bank {
    fn into(self) -> u16 {
        match self {
            Bank::Default => 0,
            Bank::Cool => 1,
            Bank::Natural => 2,
            Bank::Hot => 3,
            Bank::Subtle => 4,
            Bank::Warm => 5,
            Bank::Vivid => 6,
            Bank::Club1 => 7,
            Bank::Club2 => 8,
        }
    }
}
/// A song structure entry that represents a phrase in the track.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[brw(big)]
pub struct Phrase {
    /// Phrase number (starting at 1).
    pub index: u16,
    /// Beat number where this phrase begins.
    pub beat: u16,
    /// Kind of phrase that rekordbox has identified (?).
    pub kind: u16,
    /// Unknown field.
    #[allow(dead_code)]
    u1: u8,
    /// Flag byte used for numbered variations (in case of the `High` mood).
    ///
    /// See the documentation for details:
    /// <https://djl-analysis.deepsymmetry.org/rekordbox-export-analysis/anlz.html#high-phrase-variants>
    pub k1: u8,
    /// Unknown field.
    #[allow(dead_code)]
    u2: u8,
    /// Flag byte used for numbered variations (in case of the `High` mood).
    ///
    /// See the documentation for details:
    /// <https://djl-analysis.deepsymmetry.org/rekordbox-export-analysis/anlz.html#high-phrase-variants>
    pub k2: u8,
    /// Unknown field.
    #[allow(dead_code)]
    u3: u8,
    /// Flag that determined if only `beat2` is used (0), or if `beat2`, `beat3` and `beat4` are
    /// used (1).
    pub b: u8,
    /// Beat number.
    pub beat2: u16,
    /// Beat number.
    pub beat3: u16,
    /// Beat number.
    pub beat4: u16,
    /// Unknown field.
    #[allow(dead_code)]
    u4: u8,
    /// Flag byte used for numbered variations (in case of the `High` mood).
    ///
    /// See the documentation for details:
    /// <https://djl-analysis.deepsymmetry.org/rekordbox-export-analysis/anlz.html#high-phrase-variants>
    pub k3: u8,
    /// Unknown field.
    #[allow(dead_code)]
    u5: u8,
    /// Indicates if there are fill (non-phrase) beats at the end of the phrase.
    pub fill: u8,
    /// Beat number where the fill begins (if `fill` is non-zero).
    pub beat_fill: u16,
}

/// The data part of the [`SongStructure`] section that may be encrypted (RB6+).
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[br(import(len_entries: u16))]
pub struct SongStructureData {
    /// Overall type of phrase structure.
    pub mood: Mood,
    /// Unknown field.
    u1: u32,
    /// Unknown field.
    u2: u16,
    /// Number of the beat at which the last recognized phrase ends.
    pub end_beat: u16,
    /// Unknown field.
    u3: u16,
    /// Stylistic bank assigned in Lightning Mode.
    pub bank: Bank,
    /// Unknown field.
    u4: u8,
    /// Phrase entry data.
    #[br(count = usize::from(len_entries))]
    pub phrases: Vec<Phrase>,
}

impl SongStructureData {
    const KEY_DATA: [u8; 19] = [
        0xCB, 0xE1, 0xEE, 0xFA, 0xE5, 0xEE, 0xAD, 0xEE, 0xE9, 0xD2, 0xE9, 0xEB, 0xE1, 0xE9, 0xF3,
        0xE8, 0xE9, 0xF4, 0xE1,
    ];

    /// Returns an iterator over the key bytes (RB6+).
    fn get_key(len_entries: u16) -> impl Iterator<Item = u8> {
        Self::KEY_DATA.into_iter().map(move |x: u8| -> u8 {
            let value = u16::from(x) + len_entries;
            (value % 256) as u8
        })
    }

    /// Returns `true` if the [`SongStructureData`] is encrypted.
    ///
    /// The method tries to decrypt the `raw_mood` field and checking if the result is valid.
    fn check_if_encrypted(raw_mood: [u8; 2], len_entries: u16) -> bool {
        let buffer: Vec<u8> = raw_mood
            .iter()
            .zip(Self::get_key(len_entries).take(2))
            .map(|(byte, key)| byte ^ key)
            .collect();
        let mut reader = binrw::io::Cursor::new(buffer);
        Mood::read(&mut reader).is_ok()
    }

    /// Read a [`SongStructureData`] section that may be encrypted, depending on the `is_encrypted`
    /// value.
    fn read_encrypted<R: Read + Seek>(
        reader: &mut R,
        endian: Endian,
        (is_encrypted, len_entries): (bool, u16),
    ) -> BinResult<Self> {
        if is_encrypted {
            let key: Vec<u8> = Self::get_key(len_entries).collect();
            let mut xor_reader = XorStream::with_key(reader, key);
            Self::read_options(&mut xor_reader, endian, (len_entries,))
        } else {
            Self::read_options(reader, endian, (len_entries,))
        }
    }

    /// Write a [`SongStructureData`] section that may be encrypted, depending on the
    /// `is_encrypted` value.
    fn write_encrypted<W: Write + Seek>(
        &self,
        writer: &mut W,
        endian: Endian,
        (is_encrypted, len_entries): (bool, u16),
    ) -> BinResult<()> {
        if is_encrypted {
            let key: Vec<u8> = Self::get_key(len_entries).collect();
            let mut xor_writer = XorStream::with_key(writer, key);
            self.write_options(&mut xor_writer, endian, ())
        } else {
            self.write_options(writer, endian, ())
        }
    }
}

/// Describes the structure of a song (Intro, Chrous, Verse, etc.).
///
/// Identifier: PSSI
/// Used in `.EXT` files.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SongStructure {
    /// Length of the header data (including `kind`, `header_size` and `total_size`).
    #[br(temp)]
    #[br(assert(header_size == 32))]
    #[bw(calc = 32)]
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Size of a single entry, always 24.
    #[br(temp)]
    #[br(assert(len_entry_bytes == 24))]
    #[bw(calc = 24u32)]
    len_entry_bytes: u32,
    /// Number of entries in this section.
    #[br(temp)]
    #[br(assert((len_entry_bytes * (len_entries as u32)) == total_size - header_size))]
    #[bw(calc = data.phrases.len() as u16)]
    len_entries: u16,
    /// Indicates if the remaining parts of the song structure section are encrypted.
    ///
    /// This is a virtual field and not actually present in the file.
    #[br(restore_position, map = |raw_mood: [u8; 2]| SongStructureData::check_if_encrypted(raw_mood, len_entries))]
    #[bw(ignore)]
    pub is_encrypted: bool,
    /// Song structure data.
    #[br(args(is_encrypted, len_entries), parse_with = SongStructureData::read_encrypted)]
    #[bw(args(*is_encrypted, len_entries), write_with = SongStructureData::write_encrypted)]
    pub data: SongStructureData,
}

impl SizedSection for SongStructure {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Unknown -------------------------------------------------------------------------------------

/// Unknown content.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Unknown {
    /// Length of the header data (including `kind`, `size` and `total_size`).
    header_size: u32,
    /// Length of the section (including the header).
    total_size: u32,
    /// Unknown header data.
    #[br(count = header_size - 12)]
    header_data: Vec<u8>,
    /// Unknown content data.
    #[br(count = total_size - header_size)]
    content_data: Vec<u8>,
}

impl SizedSection for Unknown {
    fn size(&self) -> u32 {
        self.total_size
    }
}

// -- Anlz data -----------------------------------------------------------------------------------

/// Section content which differs depending on the section type.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[br(import(tag: AnlzTag))]
pub enum Content {
    /// All beats in the track.
    #[br(pre_assert(tag == AnlzTag::BeatGrid))]
    BeatGrid(BeatGrid),
    /// Extended beats in the track
    #[br(pre_assert(tag == AnlzTag::ExtendedBeatGrid))]
    ExtendedBeatGrid(ExtendedBeatGrid),
    /// List of cue points or loops (either hot cues or memory cues).
    #[br(pre_assert(tag == AnlzTag::CueList))]
    CueList(CueList),
    /// List of cue points or loops (either hot cues or memory cues, extended version).
    #[br(pre_assert(tag == AnlzTag::ExtendedCueList))]
    ExtendedCueList(ExtendedCueList),
    /// Path of the audio file that this analysis belongs to.
    #[br(pre_assert(tag == AnlzTag::Path))]
    Path(Path),
    /// Seek information for variable bitrate files (probably).
    #[br(pre_assert(tag == AnlzTag::VBR))]
    VBR(VBR),
    /// Fixed-width monochrome preview of the track waveform.
    #[br(pre_assert(tag == AnlzTag::WaveformPreview))]
    WaveformPreview(WaveformPreview),
    /// Smaller version of the fixed-width monochrome preview of the track waveform.
    #[br(pre_assert(tag == AnlzTag::TinyWaveformPreview))]
    TinyWaveformPreview(TinyWaveformPreview),
    /// Variable-width large monochrome version of the track waveform.
    #[br(pre_assert(tag == AnlzTag::WaveformDetail))]
    WaveformDetail(WaveformDetail),
    /// Variable-width large monochrome version of the track waveform.
    #[br(pre_assert(tag == AnlzTag::WaveformColorPreview))]
    WaveformColorPreview(WaveformColorPreview),
    /// Variable-width large colored version of the track waveform.
    #[br(pre_assert(tag == AnlzTag::WaveformColorDetail))]
    WaveformColorDetail(WaveformColorDetail),
    /// Fixed-width three-band preview of the track waveform.
    #[br(pre_assert(tag == AnlzTag::Waveform3BandPreview))]
    Waveform3BandPreview(Waveform3BandPreview),
    /// Variable-width three-band rendition of the track waveform.
    #[br(pre_assert(tag == AnlzTag::Waveform3BandDetail))]
    Waveform3BandDetail(Waveform3BandDetail),
    /// Describes the structure of a song (Intro, Chrous, Verse, etc.).
    #[br(pre_assert(tag == AnlzTag::SongStructure))]
    SongStructure(SongStructure),
    /// Unknown content.
    ///
    /// This allows handling files that contain unknown section types and allows to access later
    /// sections in the file that have a known type instead of failing to parse the whole file.
    #[br(pre_assert(matches!(tag, AnlzTag::Unknown(_))))]
    Unknown(Unknown),
}

impl SizedSection for Content {
    fn size(&self) -> u32 {
        match *self {
            Content::BeatGrid(ref x) => x.size(),
            Content::ExtendedBeatGrid(ref x) => x.size(),
            Content::CueList(ref x) => x.size(),
            Content::ExtendedCueList(ref x) => x.size(),
            Content::Path(ref x) => x.size(),
            Content::VBR(ref x) => x.size(),
            Content::WaveformPreview(ref x) => x.size(),
            Content::TinyWaveformPreview(ref x) => x.size(),
            Content::WaveformDetail(ref x) => x.size(),
            Content::WaveformColorPreview(ref x) => x.size(),
            Content::WaveformColorDetail(ref x) => x.size(),
            Content::Waveform3BandPreview(ref x) => x.size(),
            Content::Waveform3BandDetail(ref x) => x.size(),
            Content::SongStructure(ref x) => x.size(),
            Content::Unknown(ref x) => x.size(),
        }
    }
}

/// ANLZ Section.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Section {
    /// Kind of content in this item.
    pub tag: AnlzTag,
    /// The section content.
    #[br(args(tag.clone()))]
    pub content: Content,
}

/// ANLZ file contents
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[brw(big)]
pub struct AnlzData {
    /// The file header.
    #[br(assert(header.tag == AnlzTag::File))]
    pub header: Header,
    /// The header data.
    #[br(count = header.remaining_size())]
    pub header_data: Vec<u8>,
    /// The content sections.
    #[br(parse_with = Self::parse_sections, args(header.content_size()))]
    pub sections: Vec<Section>,
}

impl AnlzData {
    fn parse_sections<R: Read + Seek>(
        reader: &mut R,
        endian: Endian,
        args: (u32,),
    ) -> BinResult<Vec<Section>> {
        let (content_size,) = args;
        let final_position = reader.stream_position()? + u64::from(content_size);

        let mut sections: Vec<Section> = vec![];
        while reader.stream_position()? < final_position {
            let section = Section::read_options(reader, endian, ())?;
            sections.push(section);
        }
        Ok(sections)
    }

    pub fn update_total_size(&mut self) {
        let mut total = self.header.size;
        for section in self.sections.iter_mut() {
            total += section.content.size();
        }
        self.header.total_size = total;
    }
}

// -- Anlz file handler ---------------------------------------------------------------------------

/// Rekordbox ANLZ file handler.
pub struct Anlz {
    /// Path to the XML file
    path: std::path::PathBuf,
    /// XML document
    pub data: AnlzData,
}

impl Anlz {
    /// Read a Rekordbox masterPlaylist6 XML file.
    pub fn load<P: AsRef<std::path::Path> + AsRef<OsStr>>(path: P) -> Result<Self> {
        let p = std::path::Path::new(&path).to_path_buf();
        let mut file = std::fs::File::open(&p).expect("File not found");
        let data = AnlzData::read(&mut file).expect("Can't read ANLZ");
        Ok(Anlz { path: p, data })
    }

    /// Write the XML document to a file.
    pub fn dump_copy<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<()> {
        self.data.update_total_size();
        let mut file = std::fs::File::create(path).expect("Failed to create file");
        self.data.write(&mut file)?;
        Ok(())
    }

    /// Write the XML document to the original file.
    pub fn dump(&mut self) -> Result<()> {
        let path = &self.path.clone();
        self.dump_copy(path)?;
        Ok(())
    }

    /// Return a reference to the *first* section with the given tag type.
    pub fn find_section_by_tag(&mut self, tag: AnlzTag) -> Option<&mut Section> {
        self.data.sections.iter_mut().find(|s| s.tag == tag)
    }

    /// Return all references to the sections with the given tag type.
    pub fn find_sections_by_tag(&mut self, tag: AnlzTag) -> Vec<&mut Section> {
        // self.data.sections.iter_mut().find(|s| s.tag == tag)
        self.data
            .sections
            .iter_mut()
            .filter(|s| s.tag == tag)
            .collect()
    }

    /// Check if the file contains a section
    pub fn contains(&self, tag: AnlzTag) -> bool {
        self.data.sections.iter().any(|s| s.tag == tag)
    }

    /// Return a list of all tag types present in the file
    pub fn get_tags(&self) -> Result<Vec<String>> {
        let mut names = Vec::new();
        for section in &self.data.sections {
            let name = &section.tag;
            names.push(name.to_string());
        }
        Ok(names)
    }

    /// Return the beat grid entries (PQTZ) if present
    pub fn get_beat_grid(&mut self) -> Option<&mut BeatGrid> {
        let section = self.find_section_by_tag(AnlzTag::BeatGrid);
        if let Some(section) = section {
            if let Content::BeatGrid(content) = &mut section.content {
                return Some(content);
                // return Some(content.beats.iter_mut().collect());
            }
        }
        None
    }

    /// Return the extended beat grid entries (PQT2) if present
    pub fn get_extended_beat_grid(&mut self) -> Option<&mut ExtendedBeatGrid> {
        let section = self.find_section_by_tag(AnlzTag::ExtendedBeatGrid);
        if let Some(section) = section {
            if let Content::ExtendedBeatGrid(content) = &mut section.content {
                // return Some(content.beats.iter_mut().collect());
                return Some(content);
            }
        }
        None
    }

    /// Return the hot cue list entries (PCOB) if present
    pub fn get_hot_cues(&mut self) -> Option<&mut CueList> {
        let sections = self.find_sections_by_tag(AnlzTag::CueList);
        for section in sections {
            if let Content::CueList(content) = &mut section.content {
                if content.list_type == CueListType::HotCues {
                    // return Some(content.cues.iter_mut().collect());
                    return Some(content);
                }
            }
        }
        None
    }

    /// Return the memory cue list entries (PCOB) if present
    pub fn get_memory_cues(&mut self) -> Option<&mut CueList> {
        let sections = self.find_sections_by_tag(AnlzTag::CueList);
        for section in sections {
            if let Content::CueList(content) = &mut section.content {
                if content.list_type == CueListType::MemoryCues {
                    // return Some(content.cues.iter_mut().collect());
                    return Some(content);
                }
            }
        }
        None
    }

    /// Return the extended hot cue list entries (PCOB) if present
    pub fn get_extended_hot_cues(&mut self) -> Option<&mut ExtendedCueList> {
        let sections = self.find_sections_by_tag(AnlzTag::ExtendedCueList);
        for section in sections {
            if let Content::ExtendedCueList(content) = &mut section.content {
                if content.list_type == CueListType::HotCues {
                    // return Some(content.cues.iter_mut().collect());
                    return Some(content);
                }
            }
        }
        None
    }

    /// Return the extended memory cue list entries (PCOB) if present
    pub fn get_extended_memory_cues(&mut self) -> Option<&mut ExtendedCueList> {
        let sections = self.find_sections_by_tag(AnlzTag::ExtendedCueList);
        for section in sections {
            if let Content::ExtendedCueList(content) = &mut section.content {
                if content.list_type == CueListType::MemoryCues {
                    // return Some(content.cues.iter_mut().collect());
                    return Some(content);
                }
            }
        }
        None
    }

    /// Returns the path value (PPTH) if present
    pub fn get_path(&mut self) -> Option<String> {
        let section = self.find_section_by_tag(AnlzTag::Path);
        if let Some(section) = section {
            if let Content::Path(content) = &section.content {
                return Some(content.path.to_string());
            }
        }
        None
    }

    /// Sets the path value (PPTH)
    ///
    /// If no PPTH tag is present, a new one is created
    pub fn set_path<P: AsRef<std::path::Path> + AsRef<OsStr>>(&mut self, path: P) -> Result<()> {
        let section = self.find_section_by_tag(AnlzTag::Path);
        if let Some(section) = section {
            // Found PPTH tag
            if let Content::Path(content) = &mut section.content {
                content.set(path);
            } else {
                return Err(Error::AnlzError("Path not found".into()));
            }
        } else {
            // No PPTH tag, create a new one
            let content = Path::new(path);
            let section = Section {
                tag: AnlzTag::Path,
                content: Content::Path(content),
            };
            self.data.sections.push(section);
        }
        self.data.update_total_size();
        Ok(())
    }

    /// Returns the (unknown) VBR data (PVBR) if present
    pub fn get_vbr_data(&mut self) -> Option<Vec<u8>> {
        let section = self.find_section_by_tag(AnlzTag::VBR);
        if let Some(section) = section {
            if let Content::VBR(content) = &section.content {
                return Some(content.data.clone());
            }
        }
        None
    }

    /// Returns the tiny waveform columns (PWAV) if present
    pub fn get_tiny_waveform_preview(&mut self) -> Option<&mut TinyWaveformPreview> {
        let section = self.find_section_by_tag(AnlzTag::TinyWaveformPreview);
        if let Some(section) = section {
            if let Content::TinyWaveformPreview(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the fixed-width monochrome waveform preview columns (PWV2) if present
    pub fn get_waveform_preview(&mut self) -> Option<&mut WaveformPreview> {
        let section = self.find_section_by_tag(AnlzTag::WaveformPreview);
        if let Some(section) = section {
            if let Content::WaveformPreview(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the variable-width monochrome waveform columns (PWV3) if present
    pub fn get_waveform_detail(&mut self) -> Option<&mut WaveformDetail> {
        let section = self.find_section_by_tag(AnlzTag::WaveformDetail);
        if let Some(section) = section {
            if let Content::WaveformDetail(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the variable-width color waveform preview columns (PWV4) if present
    pub fn get_waveform_color_preview(&mut self) -> Option<&mut WaveformColorPreview> {
        let section = self.find_section_by_tag(AnlzTag::WaveformColorPreview);
        if let Some(section) = section {
            if let Content::WaveformColorPreview(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the variable-width color waveform columns (PWV5) if present
    pub fn get_waveform_color_detail(&mut self) -> Option<&mut WaveformColorDetail> {
        let section = self.find_section_by_tag(AnlzTag::WaveformColorDetail);
        if let Some(section) = section {
            if let Content::WaveformColorDetail(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the fixed-width three-band waveform preview columns (PWV6) if present
    pub fn get_waveform_3band_preview(&mut self) -> Option<&mut Waveform3BandPreview> {
        let section = self.find_section_by_tag(AnlzTag::Waveform3BandPreview);
        if let Some(section) = section {
            if let Content::Waveform3BandPreview(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the fixed-width three-band waveform columns (PWV7) if present
    pub fn get_waveform_3band_detail(&mut self) -> Option<&mut Waveform3BandDetail> {
        let section = self.find_section_by_tag(AnlzTag::Waveform3BandDetail);
        if let Some(section) = section {
            if let Content::Waveform3BandDetail(content) = &mut section.content {
                return Some(content);
            }
        }
        None
    }

    /// Returns the song structure data (PSSI) if present
    pub fn get_song_structure(&mut self) -> Option<SongStructureData> {
        let section = self.find_section_by_tag(AnlzTag::SongStructure);
        if let Some(section) = section {
            if let Content::SongStructure(content) = &mut section.content {
                let data = content.data.clone();
                return Some(data);
            }
        }
        None
    }
}

/// Collection of ANLZ file paths.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct AnlzPaths {
    pub dat: std::path::PathBuf,
    pub ext: Option<std::path::PathBuf>,
    pub ex2: Option<std::path::PathBuf>,
}

/// Collection of ANLZ files.
pub struct AnlzFiles {
    pub dat: Anlz,
    pub ext: Option<Anlz>,
    pub ex2: Option<Anlz>,
}

/// Scan a directory for ANLZ files.
pub fn find_anlz_files<P: AsRef<std::path::Path> + AsRef<OsStr>>(
    root: P,
) -> Result<Option<AnlzPaths>> {
    let root = std::path::Path::new(&root).to_path_buf();
    let mut dat: Option<std::path::PathBuf> = None;
    let mut ext: Option<std::path::PathBuf> = None;
    let mut ex2: Option<std::path::PathBuf> = None;

    let paths = std::fs::read_dir(root)?;
    for path in paths {
        let file = path?.path();
        let extension = file.extension();
        if let Some(extension) = extension {
            match extension.to_ascii_uppercase().to_str().unwrap() {
                "DAT" => dat = Some(file.clone()),
                "EXT" => ext = Some(file.clone()),
                "2EX" => ex2 = Some(file.clone()),
                &_ => {}
            }
        }
    }
    if dat.is_none() {
        return Ok(None);
    }
    let dat = dat.unwrap();
    let files = AnlzPaths { dat, ext, ex2 };
    Ok(Some(files))
}