audex 0.2.0

Audio metadata reading and writing library with flexible I/O and easy wrappers
Documentation
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
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
//! MP3 file type implementation and audio stream information.
//!
//! This module contains the main [`MP3`] and [`EasyMP3`] types for working with MP3 files,
//! as well as [`MPEGInfo`] for accessing audio stream properties.
//!
//! The implementation handles:
//! - MPEG frame parsing and synchronization
//! - ID3 tag reading and writing
//! - VBR header detection (Xing/Info/VBRI)
//! - Duration calculation for both CBR and VBR files
//! - Encoder information extraction (LAME, etc.)

use crate::id3::{ID3, ID3Tags};
use crate::mp3::util::{BitrateMode, MPEGFrame};
use crate::mp3::{ChannelMode, Emphasis, MPEGLayer, MPEGVersion};
use crate::{AudexError, FileType, Result, StreamInfo};
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use std::time::Duration;

#[cfg(feature = "async")]
use tokio::fs::File as TokioFile;
#[cfg(feature = "async")]
use tokio::io::{AsyncReadExt, AsyncSeekExt};

/// Represents an MP3 audio file with stream information and metadata tags.
///
/// This struct provides access to both the audio properties (bitrate, sample rate, duration, etc.)
/// and metadata tags (ID3v1, ID3v2) contained in an MP3 file.
///
/// # Structure
///
/// - **`info`**: Audio stream information including bitrate, sample rate, channels, and duration
/// - **`tags`**: Optional ID3 tags (ID3v1 and/or ID3v2)
/// - **`filename`**: Path to the file (used for save operations)
///
/// # Examples
///
/// ## Loading and Reading Tags
///
/// ```no_run
/// use audex::mp3::MP3;
/// use audex::FileType;
///
/// // Load MP3 file using FileType trait method
/// let mp3 = MP3::load("song.mp3").unwrap();
///
/// // Access audio stream information
/// println!("Duration: {:?}", mp3.info.length);
/// println!("Bitrate: {} bps", mp3.info.bitrate);
/// println!("Sample rate: {} Hz", mp3.info.sample_rate);
/// println!("Channels: {}", mp3.info.channels);
///
/// // Read ID3 tags if present
/// if let Some(ref tags) = mp3.tags {
///     if let Some(title) = tags.get_text_values("TIT2") {
///         println!("Title: {:?}", title);
///     }
/// }
/// ```
///
/// ## Modifying and Saving Tags
///
/// ```no_run
/// use audex::mp3::MP3;
/// use audex::{FileType, Tags};
///
/// // Load MP3 file using FileType trait method
/// let mut mp3 = MP3::load("song.mp3").unwrap();
///
/// // Modify ID3 tags using the Tags trait
/// if let Some(ref mut tags) = mp3.tags {
///     tags.set("TIT2", vec!["New Title".to_string()]);
///     tags.set("TPE1", vec!["New Artist".to_string()]);
/// }
///
/// // Save changes back to the original file
/// mp3.save().unwrap();
/// ```
///
/// ## Async Usage
///
/// ```ignore
/// // Note: This example requires the `async` feature to be enabled.
/// // Enable with: audex = { version = "...", features = ["async"] }
/// use audex::mp3::MP3;
/// use audex::Tags;
///
/// # async fn example() {
/// // Load MP3 file asynchronously
/// let mut mp3 = MP3::load_async("song.mp3").await.unwrap();
///
/// // Modify tags using the Tags trait
/// if let Some(ref mut tags) = mp3.tags {
///     tags.set("TIT2", vec!["Async Title".to_string()]);
/// }
///
/// // Save changes asynchronously
/// mp3.save_async().await.unwrap();
/// # }
/// ```
///
/// # See Also
///
/// - [`MPEGInfo`] - Audio stream information
/// - [`ID3Tags`] - ID3 tag access
/// - [`EasyMP3`] - Simplified interface for common tagging operations
#[derive(Debug)]
pub struct MP3 {
    /// Audio stream information (bitrate, sample rate, duration, etc.)
    pub info: MPEGInfo,

    /// ID3 tags (ID3v1 and/or ID3v2), if present in the file
    pub tags: Option<ID3Tags>,

    /// Path to the file (stored for save operations)
    pub filename: Option<String>,
}

impl MP3 {
    /// Extract ReplayGain value from an RVA2 frame.
    /// Returns the gain (plain number) or peak as a string.
    fn get_rva2_replaygain(&self, track_type: &str, is_gain: bool) -> Option<Vec<String>> {
        let tags = self.tags.as_ref()?;
        // Search for the RVA2 frame — try multiple key formats since
        // the identification may be stored in different cases
        let lower_type = track_type.to_lowercase();
        for (key, frame) in tags.dict.iter() {
            if !key.starts_with("RVA2") {
                continue;
            }
            if !key.to_lowercase().contains(&lower_type) {
                continue;
            }
            if let Some(rva2) = frame.as_any().downcast_ref::<crate::id3::frames::RVA2>() {
                if let Some((gain, peak)) = rva2.get_master() {
                    // Round to 2 decimal places for gain (matches text tag precision)
                    // and 7 for peak (f32 has ~7 significant digits)
                    return if is_gain {
                        let rounded = (gain * 100.0).round() / 100.0;
                        Some(vec![format!("{}", rounded)])
                    } else {
                        let rounded = (peak * 10000000.0).round() / 10000000.0;
                        Some(vec![format!("{}", rounded)])
                    };
                }
            }
        }
        None
    }

    /// Creates a new empty MP3 instance with default values.
    ///
    /// This creates an MP3 struct with default audio information and no tags.
    /// Typically you would use [`MP3::from_file`] or [`MP3::load`](FileType::load) instead.
    ///
    /// ```
    /// use audex::mp3::MP3;
    ///
    /// let mp3 = MP3::new();
    /// assert!(mp3.tags.is_none());
    /// assert_eq!(mp3.info.bitrate, 0);
    /// ```
    pub fn new() -> Self {
        Self {
            info: MPEGInfo::default(),
            tags: None,
            filename: None,
        }
    }

    /// Loads an MP3 file from the specified path.
    ///
    /// This method opens the file, parses the MPEG audio stream to extract audio properties,
    /// and loads any ID3 tags (ID3v1, ID3v2) present in the file.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the MP3 file to load
    ///
    /// # Returns
    ///
    /// * `Ok(MP3)` - Successfully loaded MP3 file with audio info and tags
    /// * `Err(AudexError)` - Failed to open file or parse MPEG stream
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The file cannot be opened (doesn't exist, permission denied, etc.)
    /// - The file is not a valid MP3 file (no MPEG frame sync found)
    /// - The MPEG headers are corrupted or invalid
    ///
    /// Note: missing ID3 tags are not considered an error; the `tags` field will be `None`.
    /// The current implementation also treats ID3 parsing failures as `None` rather than
    /// surfacing them separately.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::mp3::MP3;
    /// use audex::Tags;
    ///
    /// let mp3 = MP3::from_file("song.mp3").unwrap();
    /// if let Some(length) = mp3.info.length {
    ///     println!("Duration: {:.2} seconds", length.as_secs_f64());
    /// }
    /// println!("Bitrate: {} kbps", mp3.info.bitrate / 1000);
    ///
    /// // Access tags via the Tags trait
    /// if let Some(tags) = &mp3.tags {
    ///     if let Some(title) = tags.get_text_values("TIT2") {
    ///         println!("Title: {:?}", title);
    ///     }
    /// }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all, fields(path = %path.as_ref().display())))]
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        debug_event!("parsing MP3 file");
        let mut file = File::open(path)?;
        let mut mp3 = Self::new();

        // Store filename for save operations
        mp3.filename = Some(path.to_string_lossy().to_string());

        // Load ID3 tags - use the actual loaded tags
        match ID3::load_from_file(path) {
            Ok(id3) => {
                debug_event!("ID3v2 tags parsed for MP3");
                mp3.tags = Some(id3.tags);
            }
            Err(_) => {
                // If loading fails (e.g., "No ID3 tags found"), start with None
                // Tags can be created later when needed
                mp3.tags = None;
            }
        }

        // Parse MPEG stream info
        mp3.info = MPEGInfo::from_file(&mut file)?;
        debug_event!(
            bitrate = mp3.info.bitrate,
            sample_rate = mp3.info.sample_rate,
            channels = mp3.info.channels,
            "MPEG stream info parsed"
        );

        Ok(mp3)
    }

    /// Saves tag modifications back to the MP3 file with default options.
    ///
    /// This method writes any changes made to the tags back to the file. Only the tags
    /// are modified; the audio data remains unchanged. By default, this:
    /// - Saves the current ID3 tags if `self.tags` is present
    /// - Updates existing ID3v1 tags or creates them if tags are present
    /// - Preserves the original file's audio data
    ///
    /// The file path used is the one stored in the `filename` field, which is set
    /// automatically when loading via [`MP3::from_file`].
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Tags successfully saved to file
    /// * `Err(AudexError)` - Failed to save (file not writable, no filename stored, etc.)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - No filename is stored and none was provided
    /// - The file cannot be written (permission denied, disk full, etc.)
    /// - The file was deleted or moved since loading
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::mp3::MP3;
    /// use audex::Tags;
    ///
    /// let mut mp3 = MP3::from_file("song.mp3").unwrap();
    ///
    /// // Modify tags
    /// if let Some(ref mut tags) = mp3.tags {
    ///     tags.set("TIT2", vec!["New Title".to_string()]);
    /// }
    ///
    /// // Save changes
    /// mp3.save().unwrap();
    /// ```
    ///
    /// # See Also
    ///
    /// - [`MP3::save_with_options`] - Save with specific ID3 version and options
    pub fn save(&mut self) -> Result<()> {
        debug_event!("saving MP3 tags");
        self.save_with_options(None, None, None, None)
    }

    /// Saves tag modifications with format-specific options.
    ///
    /// This method provides fine-grained control over how ID3 tags are saved, including:
    /// - Target ID3v2 version (2.3 or 2.4)
    /// - ID3v1 tag handling (remove, update, or create)
    /// - Custom separator for multi-value fields in ID3v2.3
    /// - Optional different file path
    ///
    /// # Arguments
    ///
    /// * `file_path` - Optional alternative path to save to (uses stored filename if `None`)
    /// * `v1` - ID3v1 option:
    ///   - `0` = Remove ID3v1 tags
    ///   - `1` = Update existing ID3v1 tags only
    ///   - `2` = Create ID3v1 tags if missing (default)
    /// * `v2_version` - Target ID3v2 version:
    ///   - `3` = ID3v2.3 (default, most compatible)
    ///   - `4` = ID3v2.4 (newer features, less compatible)
    /// * `v23_sep` - Separator for multi-value text frames in ID3v2.3 (default: "/")
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Tags successfully saved with specified options
    /// * `Err(AudexError)` - Failed to save
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::mp3::MP3;
    /// use audex::Tags;
    ///
    /// let mut mp3 = MP3::from_file("song.mp3").unwrap();
    ///
    /// // Modify tags
    /// if let Some(ref mut tags) = mp3.tags {
    ///     tags.set("TIT2", vec!["New Title".to_string()]);
    /// }
    ///
    /// // Save as ID3v2.4 without ID3v1 tags
    /// mp3.save_with_options(None, Some(0), Some(4), None).unwrap();
    /// ```
    ///
    /// ```no_run
    /// use audex::mp3::MP3;
    ///
    /// let mut mp3 = MP3::from_file("song.mp3").unwrap();
    ///
    /// // Save to a different file
    /// mp3.save_with_options(Some("output.mp3"), None, None, None).unwrap();
    /// ```
    pub fn save_with_options(
        &mut self,
        file_path: Option<&str>,
        v1: Option<u8>,
        v2_version: Option<u8>,
        v23_sep: Option<&str>,
    ) -> Result<()> {
        // Use provided file_path or fall back to stored filename
        let target_path = match file_path {
            Some(path) => path,
            None => self.filename.as_deref().ok_or_else(|| {
                AudexError::InvalidData("No file path provided and no filename stored".to_string())
            })?,
        };

        // Set default values for format compatibility
        let v1_option = v1.unwrap_or(2); // Default to CREATE (2)
        let v2_version_option = v2_version.unwrap_or(3); // Default to v2.3
        let v23_sep_string = v23_sep.map(|s| s.to_string()); // Convert Option<&str> to Option<String>

        trace_event!(
            path = target_path,
            id3v1_option = v1_option,
            id3v2_version = v2_version_option,
            "writing MP3 ID3 tags to file"
        );

        if let Some(ref mut tags) = self.tags {
            // Use the new in-place ID3 modification - performs efficient byte manipulation
            // instead of rebuilding the entire file
            tags.save(
                target_path,
                v1_option,
                v2_version_option,
                v23_sep_string,
                None,
            )?;
        }

        Ok(())
    }
}

// Async methods for MP3 - feature-gated for async runtime support
#[cfg(feature = "async")]
impl MP3 {
    /// Load MP3 from file asynchronously
    ///
    /// Reads MPEG stream information and ID3 tags from the specified file
    /// using non-blocking I/O operations for improved concurrency.
    ///
    /// # Arguments
    /// * `path` - Path to the MP3 file
    ///
    /// # Returns
    /// * `Ok(Self)` - Successfully loaded MP3 with metadata
    /// * `Err(AudexError)` - Error occurred during file reading or parsing
    pub async fn load_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::from_file_async(path).await
    }

    /// Load MP3 from file asynchronously (alias for load_async)
    ///
    /// This is the primary async loading method that mirrors the synchronous
    /// `from_file` method behavior.
    pub async fn from_file_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let mut file = TokioFile::open(path).await?;
        let mut mp3 = Self::new();

        // Store filename for subsequent save operations
        mp3.filename = Some(path.to_string_lossy().to_string());

        // Load ID3 tags asynchronously using consolidated ID3 async methods
        match ID3::load_from_file_async(path).await {
            Ok(id3) => {
                mp3.tags = Some(id3.tags);
            }
            Err(_) => {
                // If loading fails (e.g., no ID3 tags present), start with None
                // Tags can be created and added later when needed
                mp3.tags = None;
            }
        }

        // Parse MPEG stream information from audio data
        mp3.info = Self::parse_mpeg_info_async(&mut file).await?;

        Ok(mp3)
    }

    /// Parse MPEG stream information from async file reader
    ///
    /// Analyzes the MPEG audio stream to extract duration, bitrate, sample rate,
    /// and other audio metadata using async I/O operations.
    async fn parse_mpeg_info_async(file: &mut TokioFile) -> Result<MPEGInfo> {
        // Skip ID3v2 tag if present at the start of file
        Self::skip_id3v2_async(file).await?;

        // Find and parse first valid MPEG frame using frame synchronization
        let (frame, overall_sketchy) = Self::find_and_parse_frame_async(file).await?;

        // Build MPEGInfo from parsed frame data
        let mut info = MPEGInfo {
            length: frame.length,
            bitrate: frame.bitrate,
            sample_rate: frame.sample_rate,
            channels: frame.channels(),
            version: frame.version,
            layer: frame.layer,
            channel_mode: frame.channel_mode,
            emphasis: frame.emphasis,
            protected: frame.protected,
            padding: frame.padding,
            private: frame.private,
            copyright: frame.copyright,
            original: frame.original,
            mode_extension: frame.mode_extension,
            sketchy: overall_sketchy,
            bitrate_mode: frame.bitrate_mode,
            encoder_info: frame.encoder_info,
            encoder_settings: frame.encoder_settings,
            track_gain: frame.track_gain,
            track_peak: frame.track_peak,
            album_gain: frame.album_gain,
            album_peak: None,
        };

        // Estimate duration from file size if VBR header not available
        if info.length.is_none() {
            Self::estimate_length_async(&mut info, file, frame.frame_offset).await?;
        }

        Ok(info)
    }

    /// Skip ID3v2 tag if present at file start asynchronously
    ///
    /// Windows Media Player and other software may write multiple ID3 tags,
    /// so this method skips all consecutive ID3v2 tags found.
    async fn skip_id3v2_async(reader: &mut TokioFile) -> Result<()> {
        reader.seek(SeekFrom::Start(0)).await?;
        let file_size = reader.seek(SeekFrom::End(0)).await?;
        reader.seek(SeekFrom::Start(0)).await?;

        // Skip multiple consecutive ID3v2 tags (some software writes multiple).
        // Cap iterations to prevent unbounded looping on pathological files.
        const MAX_ID3_SKIP_ITERATIONS: usize = 1000;
        let mut id3_iterations = 0usize;
        loop {
            id3_iterations += 1;
            if id3_iterations > MAX_ID3_SKIP_ITERATIONS {
                break;
            }

            let mut id3_header = [0u8; 10];
            let mut bytes_read = 0usize;
            while bytes_read < id3_header.len() {
                let read_now = reader.read(&mut id3_header[bytes_read..]).await?;
                if read_now == 0 {
                    break;
                }
                bytes_read += read_now;
            }

            if bytes_read < 10 {
                reader.seek(SeekFrom::Start(0)).await?;
                break;
            }

            if &id3_header[0..3] == b"ID3" {
                let tag_size =
                    crate::id3::util::decode_synchsafe_int_checked(&id3_header[6..10])? as u64;
                let current_pos = reader.stream_position().await?;
                if tag_size > 0 && current_pos + tag_size <= file_size {
                    let skip = i64::try_from(tag_size).map_err(|_| {
                        AudexError::InvalidData("ID3 tag size exceeds i64 range".to_string())
                    })?;
                    reader.seek(SeekFrom::Current(skip)).await?;
                    continue;
                }
            }

            // No more ID3 tags found, seek back to audio data start
            reader.seek(SeekFrom::Current(-(bytes_read as i64))).await?;
            break;
        }

        Ok(())
    }

    /// Find and parse first valid MPEG frame asynchronously
    ///
    /// Uses frame synchronization to locate valid MPEG audio frames and
    /// returns frame data along with confidence indicator.
    async fn find_and_parse_frame_async(reader: &mut TokioFile) -> Result<(MPEGFrame, bool)> {
        const MAX_READ: u64 = 1024 * 1024; // 1MB maximum search range
        const MAX_SYNCS: usize = 1500; // Maximum sync word attempts
        const ENOUGH_FRAMES: usize = 4; // Frames needed for high confidence
        const MIN_FRAMES: usize = 2; // Minimum acceptable frame count

        let mut max_syncs = MAX_SYNCS;
        let mut first_frame: Option<MPEGFrame> = None;
        let mut overall_sketchy = true;

        // Get all potential sync word positions
        let sync_positions = Self::iter_sync_async(reader, MAX_READ).await?;

        for sync_offset in sync_positions {
            if max_syncs == 0 {
                break;
            }
            max_syncs -= 1;

            reader.seek(SeekFrom::Start(sync_offset)).await?;
            let mut frames = Vec::new();

            // Attempt to parse consecutive frames from this sync position
            for _ in 0..ENOUGH_FRAMES {
                match Self::parse_frame_async(reader).await {
                    Ok(frame) => {
                        frames.push(frame);
                        // Non-sketchy frame (has valid VBR header) is definitive
                        if !frames
                            .last()
                            .expect("frames is non-empty after push")
                            .sketchy
                        {
                            break;
                        }
                    }
                    Err(_) => break,
                }
            }

            // Save first valid frame sequence as fallback
            if frames.len() >= MIN_FRAMES && first_frame.is_none() {
                first_frame = Some(frames[0].clone());
            }

            // Prefer non-sketchy frame with VBR header information
            if let Some(last_frame) = frames.last() {
                if !last_frame.sketchy {
                    overall_sketchy = false;
                    return Ok((last_frame.clone(), overall_sketchy));
                }
            }

            // Sufficient consecutive frames indicate valid sync
            if frames.len() >= ENOUGH_FRAMES {
                overall_sketchy = false;
                return Ok((frames[0].clone(), overall_sketchy));
            }
        }

        // Return best available frame or error if none found
        if let Some(frame) = first_frame {
            Ok((frame, overall_sketchy))
        } else {
            Err(AudexError::InvalidData(
                "can't sync to MPEG frame".to_string(),
            ))
        }
    }

    /// Iterate over potential sync word positions asynchronously
    ///
    /// Scans file for MPEG sync word patterns (0xFF followed by 0xE0+)
    /// and returns byte offsets of all potential frame starts.
    async fn iter_sync_async(reader: &mut TokioFile, max_read: u64) -> Result<Vec<u64>> {
        let mut positions = Vec::new();
        let start_pos = reader.stream_position().await?;

        // Clamp the read buffer to the remaining file size to avoid
        // allocating more memory than the file contains
        let file_size = reader.seek(SeekFrom::End(0)).await?;
        reader.seek(SeekFrom::Start(start_pos)).await?;
        let remaining = file_size.saturating_sub(start_pos);
        let read_size = max_read.min(remaining) as usize;

        let mut buffer = vec![0u8; read_size];
        let bytes_read = reader.read(&mut buffer).await?;
        buffer.truncate(bytes_read);

        // Locate all potential MPEG sync word positions.
        // Cap the number of positions to prevent excessive memory usage.
        const MAX_SYNC_POSITIONS: usize = 100_000;
        for i in 0..buffer.len().saturating_sub(1) {
            // MPEG sync word: 0xFF followed by byte with top 3 bits set
            if buffer[i] == 0xFF && (buffer[i + 1] & 0xE0) == 0xE0 {
                positions.push(start_pos + i as u64);
                if positions.len() >= MAX_SYNC_POSITIONS {
                    break;
                }
            }
        }

        Ok(positions)
    }

    /// Parse single MPEG frame from current position asynchronously
    ///
    /// Reads the full frame data via async I/O, then uses a Cursor to delegate
    /// to the sync `from_reader` which correctly parses VBR headers (Xing/VBRI).
    async fn parse_frame_async(reader: &mut TokioFile) -> Result<MPEGFrame> {
        let offset = reader.stream_position().await?;

        // Read enough data for the frame header + VBR header parsing.
        // Xing header can be at offset 36 + up to 512 bytes = 548 bytes minimum.
        // Use 1024 bytes for safety to cover all VBR header variants.
        let mut buf = vec![0u8; 1024];
        let bytes_read = reader.read(&mut buf).await?;
        buf.truncate(bytes_read);

        if bytes_read < 4 {
            return Err(AudexError::InvalidData(
                "Not enough data for MPEG frame".to_string(),
            ));
        }

        // Parse via Cursor at position 0 — from_reader will set frame_offset=0
        let mut cursor = std::io::Cursor::new(&buf[..]);
        let mut frame = MPEGFrame::from_reader(&mut cursor)?;

        // Restore the actual file offset so downstream code has the real position
        frame.frame_offset = offset;

        // Advance the async reader past this frame
        reader
            .seek(SeekFrom::Start(offset + frame.frame_size as u64))
            .await?;

        Ok(frame)
    }

    /// Estimate audio duration from file size when VBR header unavailable
    async fn estimate_length_async(
        info: &mut MPEGInfo,
        reader: &mut TokioFile,
        audio_start: u64,
    ) -> Result<()> {
        let file_size = reader.seek(SeekFrom::End(0)).await?;
        let content_size = file_size.saturating_sub(audio_start);

        if info.bitrate > 0 && content_size > 0 {
            // Calculate duration: (file_size * 8 bits) / bitrate
            let seconds = content_size as f64 * 8.0 / info.bitrate as f64;
            info.length = Some(Duration::from_secs_f64(seconds));
        }

        Ok(())
    }

    /// Save MP3 tags asynchronously with default options
    ///
    /// Writes ID3 tag modifications back to the file using efficient
    /// in-place byte manipulation when possible.
    pub async fn save_async(&mut self) -> Result<()> {
        self.save_with_options_async(None, None, None, None).await
    }

    /// Save MP3 tags asynchronously with format-specific options
    ///
    /// # Arguments
    /// * `file_path` - Optional target path (uses stored filename if None)
    /// * `v1` - ID3v1 save option (0=REMOVE, 1=UPDATE, 2=CREATE)
    /// * `v2_version` - Target ID3v2 version (3 or 4)
    /// * `v23_sep` - Separator for multiple values in v2.3 format
    pub async fn save_with_options_async(
        &mut self,
        file_path: Option<&str>,
        v1: Option<u8>,
        v2_version: Option<u8>,
        v23_sep: Option<&str>,
    ) -> Result<()> {
        // Determine target file path
        let target_path = match file_path {
            Some(path) => path.to_string(),
            None => self.filename.clone().ok_or_else(|| {
                AudexError::InvalidData("No file path provided and no filename stored".to_string())
            })?,
        };

        // Apply default format options for compatibility
        let v1_option = v1.unwrap_or(2);
        let v2_version_option = v2_version.unwrap_or(3);
        let v23_sep_string = v23_sep.map(|s| s.to_string());

        if let Some(ref tags) = self.tags {
            let config = crate::id3::tags::ID3SaveConfig {
                v2_version: v2_version_option,
                v2_minor: 0,
                v23_sep: v23_sep_string.clone().unwrap_or_else(|| "/".to_string()),
                v23_separator: v23_sep_string
                    .as_deref()
                    .and_then(|s| s.as_bytes().first().copied())
                    .unwrap_or(b'/'),
                padding: None,
                merge_frames: true,
                preserve_unknown: true,
                compress_frames: false,
                write_v1: match v1_option {
                    0 => crate::id3::file::ID3v1SaveOptions::REMOVE,
                    1 => crate::id3::file::ID3v1SaveOptions::UPDATE,
                    _ => crate::id3::file::ID3v1SaveOptions::CREATE,
                },
                unsync: false,
                extended_header: false,
                convert_v24_frames: true,
            };

            tags.save_to_file_async(&target_path, &config).await?;
        }

        Ok(())
    }

    /// Clear all ID3 tags asynchronously
    ///
    /// Removes tag data from memory and saves the cleared state to disk.
    pub async fn clear_async(&mut self) -> Result<()> {
        if let Some(ref mut tags) = self.tags {
            tags.dict.clear();
            tags.frames_by_id.clear();
        }
        self.save_async().await
    }

    /// Delete all ID3 tags from file asynchronously
    ///
    /// Removes both ID3v1 and ID3v2 tags from the file on disk.
    /// This is a static method that operates directly on the file.
    pub async fn delete_async<P: AsRef<Path>>(path: P) -> Result<()> {
        crate::id3::file::clear_async(path.as_ref(), true, true).await
    }
}

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

impl FileType for MP3 {
    type Tags = ID3Tags;
    type Info = MPEGInfo;

    fn format_id() -> &'static str {
        "MP3"
    }

    fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::from_file(path)
    }

    fn load_from_reader(reader: &mut dyn crate::ReadSeek) -> Result<Self> {
        debug_event!("parsing MP3 file from reader");
        let mut mp3 = Self::new();

        // Load ID3 tags from the reader
        match <ID3 as FileType>::load_from_reader(reader) {
            Ok(id3) => {
                debug_event!("ID3v2 tags parsed for MP3");
                mp3.tags = Some(id3.tags);
            }
            Err(_) => {
                mp3.tags = None;
            }
        }

        // Seek back and parse MPEG stream info
        reader.seek(std::io::SeekFrom::Start(0))?;
        let mut reader = reader;
        mp3.info = MPEGInfo::from_file(&mut reader)?;
        debug_event!(
            bitrate = mp3.info.bitrate,
            sample_rate = mp3.info.sample_rate,
            channels = mp3.info.channels,
            "MPEG stream info parsed"
        );

        Ok(mp3)
    }

    fn save(&mut self) -> Result<()> {
        // Delegate to the MP3 save method with default options
        MP3::save(self)
    }

    fn clear(&mut self) -> Result<()> {
        // Clear all ID3 tags
        if let Some(ref mut tags) = self.tags {
            tags.dict.clear();
            tags.frames_by_id.clear();
        }
        self.save()
    }

    fn save_to_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        if let Some(ref tags) = self.tags {
            // Construct an ID3 instance from the MP3's tags and delegate saving
            let mut id3_file = ID3::new();
            id3_file.tags = tags.clone();
            id3_file.save_to_writer(writer)
        } else {
            Err(AudexError::InvalidData("No tags to save".to_string()))
        }
    }

    fn clear_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        crate::id3::file::clear_from_writer(writer, true, true)?;
        self.tags = None;
        Ok(())
    }

    fn save_to_path(&mut self, path: &Path) -> Result<()> {
        self.save_with_options(path.to_str(), None, None, None)
    }

    fn tags(&self) -> Option<&Self::Tags> {
        self.tags.as_ref()
    }

    fn tags_mut(&mut self) -> Option<&mut Self::Tags> {
        self.tags.as_mut()
    }

    fn info(&self) -> &Self::Info {
        &self.info
    }

    fn add_tags(&mut self) -> Result<()> {
        if self.tags.is_some() {
            return Err(AudexError::InvalidOperation(
                "Tags already exist".to_string(),
            ));
        }
        let mut tags = ID3Tags::new();
        // Set the filename so tags can be saved
        if let Some(ref filename) = self.filename {
            tags.filename = Some(std::path::PathBuf::from(filename));
        }
        self.tags = Some(tags);
        Ok(())
    }

    fn get(&self, key: &str) -> Option<Vec<String>> {
        // Check for ReplayGain keys — EasyID3 stores these in RVA2 frames,
        // so the raw ID3Tags won't find them as text. Use EasyID3's reader.
        if key.eq_ignore_ascii_case("REPLAYGAIN_TRACK_GAIN") || key == "TXXX:REPLAYGAIN_TRACK_GAIN"
        {
            return self.get_rva2_replaygain("track", true);
        }
        if key.eq_ignore_ascii_case("REPLAYGAIN_TRACK_PEAK") || key == "TXXX:REPLAYGAIN_TRACK_PEAK"
        {
            return self.get_rva2_replaygain("track", false);
        }
        if key.eq_ignore_ascii_case("REPLAYGAIN_ALBUM_GAIN") || key == "TXXX:REPLAYGAIN_ALBUM_GAIN"
        {
            return self.get_rva2_replaygain("album", true);
        }
        if key.eq_ignore_ascii_case("REPLAYGAIN_ALBUM_PEAK") || key == "TXXX:REPLAYGAIN_ALBUM_PEAK"
        {
            return self.get_rva2_replaygain("album", false);
        }
        // ID3Tags has a special get_text_values method that handles the mapping
        self.tags.as_ref()?.get_text_values(key)
    }

    fn keys(&self) -> Vec<String> {
        let mut keys: Vec<String> = self
            .tags
            .as_ref()
            .map(|t| t.dict.keys().cloned().collect())
            .unwrap_or_default();
        // Expose RVA2-based ReplayGain as TXXX keys so they participate in
        // normalized diffs alongside text-based ReplayGain in other formats.
        // Check for any RVA2 key in dict (e.g. "RVA2:track", "RVA2:album")
        let has_track_rva2 = keys
            .iter()
            .any(|k| k.starts_with("RVA2") && k.to_lowercase().contains("track"));
        let has_album_rva2 = keys
            .iter()
            .any(|k| k.starts_with("RVA2") && k.to_lowercase().contains("album"));
        if has_track_rva2 {
            if !keys.iter().any(|k| k == "TXXX:REPLAYGAIN_TRACK_GAIN") {
                keys.push("TXXX:REPLAYGAIN_TRACK_GAIN".to_string());
            }
            if !keys.iter().any(|k| k == "TXXX:REPLAYGAIN_TRACK_PEAK") {
                keys.push("TXXX:REPLAYGAIN_TRACK_PEAK".to_string());
            }
        }
        if has_album_rva2 {
            if !keys.iter().any(|k| k == "TXXX:REPLAYGAIN_ALBUM_GAIN") {
                keys.push("TXXX:REPLAYGAIN_ALBUM_GAIN".to_string());
            }
            if !keys.iter().any(|k| k == "TXXX:REPLAYGAIN_ALBUM_PEAK") {
                keys.push("TXXX:REPLAYGAIN_ALBUM_PEAK".to_string());
            }
        }
        keys
    }

    fn score(filename: &str, header: &[u8]) -> i32 {
        let mut score = 0;
        let filename_lower = filename.to_lowercase();

        // Check for specific MPEG sync patterns - matches specification exactly
        if header.len() >= 2 {
            let _sync_word = (header[0] as u16) << 8 | header[1] as u16;

            // Check specific sync patterns that specification looks for
            if header.starts_with(&[0xFF, 0xF2]) ||  // MPEG-2 Layer III
               header.starts_with(&[0xFF, 0xF3]) ||  // MPEG-2 Layer III
               header.starts_with(&[0xFF, 0xFA]) ||  // MPEG-1 Layer III
               header.starts_with(&[0xFF, 0xFB])
            {
                // MPEG-1 Layer III
                score += 2;
            }
        }

        // Check for ID3v2 header
        if header.len() >= 3 && header.starts_with(b"ID3") {
            score += 2;
        }

        // Check file extensions - exact scoring from specification
        if filename_lower.ends_with(".mp3")
            || filename_lower.ends_with(".mp2")
            || filename_lower.ends_with(".mpg")
            || filename_lower.ends_with(".mpeg")
        {
            score += 1;
        }

        score
    }

    fn mime_types() -> &'static [&'static str] {
        &["audio/mpeg", "audio/mp3", "audio/mpg", "audio/mpeg3"]
    }
}

/// Detailed information about an MPEG audio stream.
///
/// This struct contains comprehensive technical information extracted from the MPEG audio
/// frames, including audio properties, encoder information, and ReplayGain data.
///
/// # Field Categories
///
/// ## Basic Audio Properties
/// - **`length`**: Duration of the audio stream
/// - **`bitrate`**: Bitrate in bits per second (bps, NOT kbps)
/// - **`sample_rate`**: Sample rate in Hz (e.g., 44100, 48000)
/// - **`channels`**: Number of audio channels (1=mono, 2=stereo)
///
/// ## MPEG Technical Details
/// - **`version`**: MPEG version (MPEG-1, MPEG-2, or MPEG-2.5)
/// - **`layer`**: MPEG layer (I, II, or III/MP3)
/// - **`channel_mode`**: Stereo mode (stereo, joint stereo, dual channel, mono)
/// - **`emphasis`**: Pre-emphasis filter applied
///
/// ## Frame Flags
/// - **`protected`**: CRC error protection enabled
/// - **`padding`**: Frame padding bit set
/// - **`private`**: Private bit set (application-specific)
/// - **`copyright`**: Copyright bit set
/// - **`original`**: Original media bit set
/// - **`mode_extension`**: Joint stereo mode extension data
///
/// ## Encoding Information
/// - **`sketchy`**: `true` if duration/bitrate is estimated (no VBR header found)
/// - **`bitrate_mode`**: CBR, VBR, ABR, or Unknown
/// - **`encoder_info`**: Encoder name/version (e.g., "LAME3.99r")
/// - **`encoder_settings`**: Encoder settings string
///
/// ## ReplayGain
/// - **`track_gain`**: Track-level ReplayGain adjustment in dB
/// - **`track_peak`**: Peak sample value for the track (0.0-1.0)
/// - **`album_gain`**: Album-level ReplayGain adjustment in dB
/// - **`album_peak`**: Peak sample value for the album (0.0-1.0)
///
/// # Examples
///
/// ```no_run
/// use audex::mp3::{MP3, MPEGInfo};
///
/// let mp3 = MP3::from_file("song.mp3").unwrap();
/// let info: &MPEGInfo = &mp3.info;
///
/// // Basic audio properties
/// println!("Duration: {:?}", info.length);
/// println!("Bitrate: {} kbps", info.bitrate / 1000);
/// println!("Sample rate: {} Hz", info.sample_rate);
/// println!("Channels: {}", info.channels);
///
/// // Encoding information
/// println!("Bitrate mode: {:?}", info.bitrate_mode);
/// if let Some(encoder) = &info.encoder_info {
///     println!("Encoder: {}", encoder);
/// }
///
/// // Quality indicator
/// if info.sketchy {
///     println!("Warning: Duration/bitrate is estimated (no VBR header)");
/// }
/// ```
///
/// # Notes
///
/// - **Bitrate units**: The `bitrate` field is in **bits per second** (bps), not kilobits per second.
///   Divide by 1000 to get kbps (e.g., 320000 bps = 320 kbps).
/// - **Sketchy flag**: When `true`, the duration and bitrate are estimated from file size rather
///   than read from a VBR header. This is less accurate but still usable.
/// - **VBR files**: For Variable Bitrate files, the `bitrate` represents the average bitrate.
///
/// # See Also
///
/// - [`MP3`] - The main MP3 file type that contains this info
/// - [`BitrateMode`] - Bitrate encoding mode
/// - [`MPEGVersion`] - MPEG version enum
/// - [`MPEGLayer`] - MPEG layer enum
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MPEGInfo {
    /// Duration of the audio stream, if determinable
    #[cfg_attr(
        feature = "serde",
        serde(with = "crate::serde_helpers::duration_as_secs_f64")
    )]
    pub length: Option<Duration>,

    /// Bitrate in bits per second (bps, NOT kbps)
    ///
    /// For CBR files, this is the constant bitrate.
    /// For VBR files, this is the average bitrate.
    /// Divide by 1000 to convert to kbps (e.g., 320000 bps = 320 kbps)
    pub bitrate: u32,

    /// Sample rate in Hz (e.g., 44100, 48000, 22050)
    pub sample_rate: u32,

    /// Number of audio channels (1 = mono, 2 = stereo)
    pub channels: u16,

    /// MPEG version (MPEG-1, MPEG-2, or MPEG-2.5)
    pub version: MPEGVersion,

    /// MPEG layer (I, II, or III/MP3)
    pub layer: MPEGLayer,

    /// Channel mode (stereo, joint stereo, dual channel, or mono)
    pub channel_mode: ChannelMode,

    /// Pre-emphasis filter applied to the audio
    pub emphasis: Emphasis,

    /// CRC error protection is enabled
    pub protected: bool,

    /// Frame padding bit is set
    pub padding: bool,

    /// Private bit (application-specific use)
    pub private: bool,

    /// Copyright bit indicating copyrighted material
    pub copyright: bool,

    /// Original media bit (vs. copy)
    pub original: bool,

    /// Mode extension for joint stereo encoding
    pub mode_extension: u8,

    /// `true` if duration/bitrate is estimated (no VBR header found)
    ///
    /// When `true`, the `length` and `bitrate` values are calculated from file size
    /// rather than read from a VBR header (Xing/Info/VBRI). This makes them less
    /// accurate but still usable for most purposes.
    pub sketchy: bool,

    /// Bitrate encoding mode (CBR, VBR, ABR, or Unknown)
    pub bitrate_mode: BitrateMode,

    /// Encoder name and version (e.g., "LAME3.99r", "FhG"), if available
    pub encoder_info: Option<String>,

    /// Encoder settings string, if available
    pub encoder_settings: Option<String>,

    /// Track-level ReplayGain adjustment in dB
    pub track_gain: Option<f32>,

    /// Peak sample value for the track (0.0 to 1.0+)
    pub track_peak: Option<f32>,

    /// Album-level ReplayGain adjustment in dB
    pub album_gain: Option<f32>,

    /// Peak sample value for the album (0.0 to 1.0+)
    pub album_peak: Option<f32>,
}

impl MPEGInfo {
    /// Create new MPEG info
    pub fn new() -> Self {
        Self::default()
    }

    /// Parse MPEG info from file
    pub fn from_file<R: Read + Seek>(reader: &mut R) -> Result<Self> {
        // Skip ID3v2 tag if present
        Self::skip_id3v2(reader)?;

        // Find and parse first valid MPEG frame using frame synchronization
        let (frame, overall_sketchy) = Self::find_and_parse_frame(reader)?;

        // Create MPEGInfo from frame data
        let mut info = MPEGInfo {
            length: frame.length,
            bitrate: frame.bitrate,
            sample_rate: frame.sample_rate,
            channels: frame.channels(),
            version: frame.version,
            layer: frame.layer,
            channel_mode: frame.channel_mode,
            emphasis: frame.emphasis,
            protected: frame.protected,
            padding: frame.padding,
            private: frame.private,
            copyright: frame.copyright,
            original: frame.original,
            mode_extension: frame.mode_extension,
            sketchy: overall_sketchy, // Use overall sketchy status, not frame's
            bitrate_mode: frame.bitrate_mode,
            encoder_info: frame.encoder_info,
            encoder_settings: frame.encoder_settings,
            track_gain: frame.track_gain,
            track_peak: frame.track_peak,
            album_gain: frame.album_gain,
            album_peak: None, // Not available in frame data
        };

        // Estimate length if not found in VBR header
        if info.length.is_none() {
            info.estimate_length_from_file_size(reader, frame.frame_offset)?;
        }

        Ok(info)
    }

    /// Skip ID3v2 tag if present - matches specification implementation
    fn skip_id3v2<R: Read + Seek>(reader: &mut R) -> Result<()> {
        reader.seek(SeekFrom::Start(0))?;
        let file_size = reader.seek(SeekFrom::End(0))?;
        reader.seek(SeekFrom::Start(0))?;

        // Windows Media Player writes multiple ID3 tags, so skip as many as we find.
        // Cap iterations to prevent unbounded looping on pathological files.
        const MAX_ID3_SKIP_ITERATIONS: usize = 1000;
        let mut id3_iterations = 0usize;
        loop {
            id3_iterations += 1;
            if id3_iterations > MAX_ID3_SKIP_ITERATIONS {
                break;
            }

            let mut id3_header = [0u8; 10];
            let mut bytes_read = 0usize;
            while bytes_read < id3_header.len() {
                let read_now = reader.read(&mut id3_header[bytes_read..])?;
                if read_now == 0 {
                    break;
                }
                bytes_read += read_now;
            }

            if bytes_read < 10 {
                reader.seek(SeekFrom::Start(0))?;
                break;
            }

            if &id3_header[0..3] == b"ID3" {
                let tag_size =
                    crate::id3::util::decode_synchsafe_int_checked(&id3_header[6..10])? as u64;
                // Validate that the tag doesn't extend past the actual file
                let current_pos = reader.stream_position()?;
                if tag_size > 0 && current_pos + tag_size <= file_size {
                    let skip = i64::try_from(tag_size).map_err(|_| {
                        AudexError::InvalidData("ID3 tag size exceeds i64 range".to_string())
                    })?;
                    reader.seek(SeekFrom::Current(skip))?;
                    continue;
                }
            }

            // No more ID3 tags, seek back to start of non-ID3 data
            reader.seek(SeekFrom::Current(-(bytes_read as i64)))?;
            break;
        }

        Ok(())
    }

    /// Find and parse first valid MPEG frame using synchronization
    fn find_and_parse_frame<R: Read + Seek>(reader: &mut R) -> Result<(MPEGFrame, bool)> {
        const MAX_READ: u64 = 1024 * 1024; // 1MB maximum search
        const MAX_SYNCS: usize = 1500; // Maximum sync attempts
        const ENOUGH_FRAMES: usize = 4; // Frames needed for confidence
        const MIN_FRAMES: usize = 2; // Minimum acceptable frames

        let mut max_syncs = MAX_SYNCS;
        let mut first_frame: Option<MPEGFrame> = None;
        let mut overall_sketchy = true; // Overall sketchy status - separate from frame's sketchy

        for sync_offset in crate::mp3::util::iter_sync(reader, MAX_READ)? {
            if max_syncs == 0 {
                break;
            }
            max_syncs -= 1;

            reader.seek(SeekFrom::Start(sync_offset))?;
            let mut frames = Vec::new();

            // Try to parse multiple consecutive frames
            for _ in 0..ENOUGH_FRAMES {
                match MPEGFrame::from_reader(reader) {
                    Ok(frame) => {
                        frames.push(frame);
                        // If frame is non-sketchy (has valid VBR header), use it immediately
                        if !frames
                            .last()
                            .expect("frames is non-empty after push")
                            .sketchy
                        {
                            break;
                        }
                    }
                    Err(_) => break,
                }
            }

            // Save first valid frame sequence in case this is all we get
            if frames.len() >= MIN_FRAMES && first_frame.is_none() {
                first_frame = Some(frames[0].clone());
            }

            // If the last frame was non-sketchy (has valid VBR header), use that
            if let Some(last_frame) = frames.last() {
                if !last_frame.sketchy {
                    overall_sketchy = false; // Found non-sketchy frame
                    return Ok((last_frame.clone(), overall_sketchy));
                }
            }

            // If we have enough valid frames, use the first one
            if frames.len() >= ENOUGH_FRAMES {
                overall_sketchy = false; // Found enough frames to be confident
                return Ok((frames[0].clone(), overall_sketchy));
            }
        }

        // Return first_frame if found - overall_sketchy remains true
        if let Some(frame) = first_frame {
            Ok((frame, overall_sketchy))
        } else {
            Err(AudexError::InvalidData(
                "can't sync to MPEG frame".to_string(),
            ))
        }
    }

    /// Estimate length from file size when VBR header is not available
    fn estimate_length_from_file_size<R: Read + Seek>(
        &mut self,
        reader: &mut R,
        audio_start: u64,
    ) -> Result<()> {
        let file_size = reader.seek(SeekFrom::End(0))?;
        let content_size = file_size.saturating_sub(audio_start);

        if self.bitrate > 0 && content_size > 0 {
            // Bitrate is already in bps
            let seconds = content_size as f64 * 8.0 / self.bitrate as f64;
            self.length = Some(Duration::from_secs_f64(seconds));
            // Don't modify sketchy flag here - it's already set correctly from frame parsing
        }

        Ok(())
    }
}

impl Default for MPEGInfo {
    fn default() -> Self {
        Self {
            length: None,
            bitrate: 0,
            sample_rate: 0,
            channels: 0,
            version: MPEGVersion::MPEG1,
            layer: MPEGLayer::Layer3,
            channel_mode: ChannelMode::Stereo,
            emphasis: Emphasis::None,
            protected: false,
            padding: false,
            private: false,
            copyright: false,
            original: false,
            mode_extension: 0,
            sketchy: false,
            bitrate_mode: BitrateMode::Unknown,
            encoder_info: None,
            encoder_settings: None,
            track_gain: None,
            track_peak: None,
            album_gain: None,
            album_peak: None,
        }
    }
}

impl StreamInfo for MPEGInfo {
    fn length(&self) -> Option<Duration> {
        self.length
    }

    fn bitrate(&self) -> Option<u32> {
        Some(self.bitrate) // Already in bps
    }

    fn sample_rate(&self) -> Option<u32> {
        Some(self.sample_rate)
    }

    fn channels(&self) -> Option<u16> {
        Some(self.channels)
    }

    fn bits_per_sample(&self) -> Option<u16> {
        None // MP3 is lossy, no meaningful bits per sample
    }
}

/// MP3 file with a simplified tag interface.
///
/// This is a convenience wrapper around [`MP3`] that uses the simplified
/// [`EasyID3`](crate::easyid3::EasyID3) tag interface instead of the full ID3 API.
/// EasyID3 provides a key-value interface for common tags, making it easier
/// to work with standard metadata fields.
///
/// # When to Use EasyMP3
///
/// Use `EasyMP3` when you:
/// - Only need to work with common tag fields (title, artist, album, etc.)
/// - Want a simpler, more intuitive API
/// - Don't need access to advanced ID3 frames
///
/// Use [`MP3`] when you:
/// - Need access to all ID3 frame types
/// - Want to work with embedded artwork (APIC frames)
/// - Need fine control over ID3 versions and frame formats
///
/// # Examples
///
/// ```no_run
/// use audex::mp3::EasyMP3;
///
/// let mut mp3 = EasyMP3::from_file("song.mp3").unwrap();
///
/// // Simple tag access using the EasyID3 interface
/// if let Some(ref mut tags) = mp3.tags {
///     tags.set("title", &["My Song".to_string()])?;
///     tags.set("artist", &["Artist Name".to_string()])?;
///     tags.set("album", &["Album Title".to_string()])?;
/// }
///
/// // Save changes
/// mp3.save().unwrap();
/// # Ok::<(), audex::AudexError>(())
/// ```
///
/// # See Also
///
/// - [`MP3`] - Full-featured MP3 file type
/// - [`EasyID3`](crate::easyid3::EasyID3) - Simplified ID3 tag interface
/// - [`MPEGInfo`] - Audio stream information
#[derive(Debug)]
pub struct EasyMP3 {
    /// Audio stream information (same as in MP3)
    pub info: MPEGInfo,

    /// Simplified EasyID3 tag interface
    pub tags: Option<crate::easyid3::EasyID3>,

    /// Path to the file (stored for save operations)
    pub filename: Option<String>,
}

impl EasyMP3 {
    fn parse_language_code(lang: &str) -> Result<[u8; 3]> {
        let bytes = lang.as_bytes();
        if bytes.len() != 3 || !bytes.iter().all(|b| b.is_ascii_alphabetic()) {
            return Err(AudexError::InvalidData(format!(
                "Language code must be a 3-letter ASCII identifier, got '{}'",
                lang
            )));
        }

        Ok([
            bytes[0].to_ascii_lowercase(),
            bytes[1].to_ascii_lowercase(),
            bytes[2].to_ascii_lowercase(),
        ])
    }

    fn ensure_easy_tags_mut(&mut self) -> Result<&mut crate::easyid3::EasyID3> {
        if self.tags.is_none() {
            self.add_tags()?;
        }

        self.tags
            .as_mut()
            .ok_or_else(|| AudexError::InvalidData("No tags available".to_string()))
    }

    fn load_easy_tags(path: &Path) -> Result<Option<crate::easyid3::EasyID3>> {
        match crate::easyid3::EasyID3::load(path) {
            Ok(tags) => Ok(Some(tags)),
            Err(AudexError::ID3NoHeaderError) | Err(AudexError::HeaderNotFound) => Ok(None),
            // No ID3v2 header AND no ID3v1 tags — treat as empty, tags can be created later
            Err(AudexError::InvalidData(ref msg)) if msg.contains("No ID3 tags found") => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// Create new EasyMP3 instance
    pub fn new() -> Self {
        Self {
            info: MPEGInfo::default(),
            tags: None,
            filename: None,
        }
    }

    /// Load EasyMP3 from file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let path_str = path.to_string_lossy().to_string();

        // Preserve actual parse failures instead of silently treating them as
        // "no tags present".
        let easy_tags = Self::load_easy_tags(path)?;

        // Parse MPEG stream info using the MP3 loader
        let mp3 = MP3::from_file(path)?;

        Ok(Self {
            info: mp3.info,
            tags: easy_tags,
            filename: Some(path_str),
        })
    }

    /// Register a text key mapping for dynamic key registration
    ///
    /// This allows mapping custom keys to ID3 text frames at runtime.
    pub fn register_text_key(&mut self, _key: &str, _frame_id: &str) -> Result<()> {
        self.ensure_easy_tags_mut()?
            .register_text_key(_key, _frame_id)
    }

    /// Register a TXXX key mapping for user-defined text frames
    ///
    /// This allows mapping custom keys to ID3 TXXX frames with descriptions.
    pub fn register_txxx_key(&mut self, _key: &str, _description: &str) -> Result<()> {
        self.ensure_easy_tags_mut()?
            .register_txxx_key(_key, _description)
    }

    /// Set a generic ID3 frame directly
    ///
    /// This provides direct frame manipulation for advanced use cases.
    pub fn set_frame(&mut self, frame_id: &str, frame_data: Vec<String>) -> Result<()> {
        self.ensure_easy_tags_mut()?
            .id3
            .add_text_frame(frame_id, frame_data)
    }

    /// Set TDAT frame for date information
    pub fn set_tdat_frame(&mut self, date_ddmm: &str) -> Result<()> {
        self.set_frame("TDAT", vec![date_ddmm.to_string()])
    }

    /// Set TPUB frame for publisher information
    pub fn set_tpub_frame(&mut self, publisher: &str) -> Result<()> {
        self.set_frame("TPUB", vec![publisher.to_string()])
    }

    /// Set TXXX frame for user-defined text information
    pub fn set_txxx_frame(&mut self, description: &str, text: &str) -> Result<()> {
        let frame_key = format!("TXXX:{}", description);
        self.set_frame(&frame_key, vec![text.to_string()])
    }

    /// Set COMM frame for comments
    pub fn set_comm_frame(&mut self, text: &str, _lang: &str) -> Result<()> {
        use crate::id3::{COMM, specs::TextEncoding};

        let lang = Self::parse_language_code(_lang)?;
        let frame = COMM::new(TextEncoding::Utf16, lang, String::new(), text.to_string());
        self.ensure_easy_tags_mut()?.id3.add(Box::new(frame))
    }

    /// Set USLT frame for unsynchronized lyrics
    pub fn set_uslt_frame(&mut self, lyrics: &str, _lang: &str) -> Result<()> {
        use crate::id3::{USLT, specs::TextEncoding};

        let lang = Self::parse_language_code(_lang)?;
        let frame = USLT::new(TextEncoding::Utf16, lang, String::new(), lyrics.to_string());
        self.ensure_easy_tags_mut()?.id3.add(Box::new(frame))
    }

    /// Set APIC frame for attached pictures
    pub fn set_apic_frame(
        &mut self,
        _data: &[u8],
        _mime: &str,
        _pic_type: u8,
        _description: &str,
    ) -> Result<()> {
        use crate::id3::{APIC, PictureType, specs::TextEncoding};

        let frame = APIC::new(
            TextEncoding::Utf16,
            _mime.to_string(),
            PictureType::from(_pic_type),
            _description.to_string(),
            _data.to_vec(),
        );
        self.ensure_easy_tags_mut()?.id3.add(Box::new(frame))
    }

    /// Save EasyMP3 with default options
    pub fn save(&mut self) -> Result<()> {
        debug_event!("saving EasyMP3 tags");
        self.save_with_options(None, None, None, None)
    }

    /// Save EasyMP3 with format-specific options
    pub fn save_with_options(
        &mut self,
        file_path: Option<&str>,
        v1: Option<u8>,
        v2_version: Option<u8>,
        v23_sep: Option<&str>,
    ) -> Result<()> {
        // Use provided file_path or fall back to stored filename
        let target_path = match file_path {
            Some(path) => path,
            None => self.filename.as_deref().ok_or_else(|| {
                AudexError::InvalidData("No file path provided and no filename stored".to_string())
            })?,
        };

        // Set default values for format compatibility
        let v1_option = v1.unwrap_or(2); // Default to CREATE (2)
        let v2_version_option = v2_version.unwrap_or(3); // Default to v2.3
        let v23_sep_string = v23_sep.map(|s| s.to_string()); // Convert Option<&str> to Option<String>

        let tags = self
            .tags
            .as_mut()
            .ok_or_else(|| AudexError::InvalidData("No tags available for saving".to_string()))?;

        // Use the new in-place ID3 modification - performs efficient byte manipulation
        // instead of rebuilding the entire file.
        tags.id3.save(
            target_path,
            v1_option,
            v2_version_option,
            v23_sep_string,
            None,
        )?;

        Ok(())
    }
}

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

// Async methods for EasyMP3 - feature-gated for async runtime support
#[cfg(feature = "async")]
impl EasyMP3 {
    /// Load EasyMP3 from file asynchronously
    ///
    /// Reads MPEG stream information and EasyID3 tags from the specified file
    /// using non-blocking I/O operations for improved concurrency.
    ///
    /// # Arguments
    /// * `path` - Path to the MP3 file
    ///
    /// # Returns
    /// * `Ok(Self)` - Successfully loaded EasyMP3 with simplified tag interface
    /// * `Err(AudexError)` - Error occurred during file reading or parsing
    pub async fn load_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::from_file_async(path).await
    }

    /// Load EasyMP3 from file asynchronously (alias for load_async)
    ///
    /// This is the primary async loading method that mirrors the synchronous
    /// `from_file` method behavior with the simplified EasyID3 tag interface.
    pub async fn from_file_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let path_str = path.to_string_lossy().to_string();

        let easy_tags = match crate::easyid3::EasyID3::load_async(path).await {
            Ok(tags) => Some(tags),
            Err(AudexError::ID3NoHeaderError) | Err(AudexError::HeaderNotFound) => None,
            // No ID3v2 header AND no ID3v1 tags — treat as empty, tags can be created later
            Err(AudexError::InvalidData(ref msg)) if msg.contains("No ID3 tags found") => None,
            Err(err) => return Err(err),
        };

        // Parse MPEG stream info using async MP3 loader
        let mp3 = MP3::from_file_async(path).await?;

        Ok(Self {
            info: mp3.info,
            tags: easy_tags,
            filename: Some(path_str),
        })
    }

    /// Save EasyMP3 tags asynchronously with default options
    ///
    /// Writes tag modifications back to the file using efficient
    /// in-place byte manipulation when possible.
    pub async fn save_async(&mut self) -> Result<()> {
        self.save_with_options_async(None, None, None, None).await
    }

    /// Save EasyMP3 tags asynchronously with format-specific options
    ///
    /// # Arguments
    /// * `file_path` - Optional target path (uses stored filename if None)
    /// * `v1` - ID3v1 save option (0=REMOVE, 1=UPDATE, 2=CREATE)
    /// * `v2_version` - Target ID3v2 version (3 or 4)
    /// * `v23_sep` - Separator for multiple values in v2.3 format
    pub async fn save_with_options_async(
        &mut self,
        file_path: Option<&str>,
        v1: Option<u8>,
        v2_version: Option<u8>,
        v23_sep: Option<&str>,
    ) -> Result<()> {
        // Determine target file path
        let target_path = match file_path {
            Some(path) => path.to_string(),
            None => self.filename.clone().ok_or_else(|| {
                AudexError::InvalidData("No file path provided and no filename stored".to_string())
            })?,
        };

        // Apply default format options for compatibility
        let v1_option = v1.unwrap_or(2);
        let v2_version_option = v2_version.unwrap_or(3);
        let v23_sep_string = v23_sep.map(|s| s.to_string());

        let tags = self
            .tags
            .as_ref()
            .ok_or_else(|| AudexError::InvalidData("No tags available for saving".to_string()))?;

        let config = crate::id3::tags::ID3SaveConfig {
            v2_version: v2_version_option,
            v2_minor: 0,
            v23_sep: v23_sep_string.clone().unwrap_or_else(|| "/".to_string()),
            v23_separator: v23_sep_string
                .as_deref()
                .and_then(|s| s.as_bytes().first().copied())
                .unwrap_or(b'/'),
            padding: None,
            merge_frames: true,
            preserve_unknown: true,
            compress_frames: false,
            write_v1: match v1_option {
                0 => crate::id3::file::ID3v1SaveOptions::REMOVE,
                1 => crate::id3::file::ID3v1SaveOptions::UPDATE,
                _ => crate::id3::file::ID3v1SaveOptions::CREATE,
            },
            unsync: false,
            extended_header: false,
            convert_v24_frames: true,
        };

        tags.id3.save_to_file_async(&target_path, &config).await?;

        Ok(())
    }

    /// Clear all tags asynchronously
    ///
    /// Removes tag data and saves the cleared state to disk.
    pub async fn clear_async(&mut self) -> Result<()> {
        if let Some(ref mut tags) = self.tags {
            tags.id3.dict.clear();
            tags.id3.frames_by_id.clear();
        }
        self.save_async().await
    }
}

impl FileType for EasyMP3 {
    type Tags = crate::easyid3::EasyID3;
    type Info = MPEGInfo;

    fn format_id() -> &'static str {
        "EasyMP3"
    }

    fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::from_file(path)
    }

    fn save(&mut self) -> Result<()> {
        // Delegate to the EasyMP3 save method with default options
        EasyMP3::save(self)
    }

    fn clear(&mut self) -> Result<()> {
        // Clear all ID3 tags
        if let Some(ref mut tags) = self.tags {
            tags.clear()?;
        }
        self.save()
    }

    fn tags(&self) -> Option<&Self::Tags> {
        self.tags.as_ref()
    }

    fn tags_mut(&mut self) -> Option<&mut Self::Tags> {
        self.tags.as_mut()
    }

    fn info(&self) -> &Self::Info {
        &self.info
    }

    fn add_tags(&mut self) -> Result<()> {
        // Check if tags already exist
        if self.tags.is_some() {
            return Err(AudexError::InvalidOperation(
                "Tags already exist".to_string(),
            ));
        }

        // Create new EasyID3 tags with filename propagation
        let mut tags = crate::easyid3::EasyID3::new();
        if let Some(ref filename) = self.filename {
            tags.filename = Some(filename.clone());
        }

        self.tags = Some(tags);
        Ok(())
    }

    fn score(filename: &str, header: &[u8]) -> i32 {
        // Same scoring as MP3
        MP3::score(filename, header)
    }

    fn mime_types() -> &'static [&'static str] {
        MP3::mime_types()
    }
}