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
//! Support for Musepack audio files.
//!
//! This module provides support for Musepack (MPC), a lossy audio compression format
//! optimized for transparency at mid-to-high bitrates. Musepack excels at preserving
//! audio quality while achieving smaller file sizes than MP3 at equivalent bitrates.
//!
//! # File Format
//!
//! Musepack supports two major stream versions:
//! - **SV7 (Stream Version 7)**: Legacy format with fixed frame structure
//! - **SV8 (Stream Version 8)**: Modern format with packet-based design
//! - **SV4-6**: Older versions also supported
//!
//! # Audio Characteristics
//!
//! - **Compression**: Lossy (psychoacoustic model)
//! - **Bitrate**: 120-500 kbps (quality levels 4-10)
//! - **Sample Rates**: 32 kHz, 37.8 kHz, 44.1 kHz, 48 kHz
//! - **Channels**: 1-2 (SV4-7 hardcode stereo, SV8 parses from header)
//! - **Quality Focus**: Transparency at 180+ kbps
//! - **File Extension**: `.mpc`
//! - **MIME Type**: `audio/x-musepack`
//!
//! # Tagging
//!
//! Musepack uses APEv2 tags:
//! - **Standard fields**: Title, Artist, Album, Year, Track, Genre
//! - **ReplayGain**: Built-in support for playback normalization
//! - **Binary support**: Embedded cover art
//!
//! # Basic Usage
//!
//! ```no_run
//! use audex::musepack::Musepack;
//! use audex::{FileType, Tags};
//!
//! # fn main() -> audex::Result<()> {
//! let mut mpc = Musepack::load("song.mpc")?;
//!
//! println!("Version: {}", mpc.info.version);
//! println!("Sample Rate: {} Hz", mpc.info.sample_rate);
//! println!("Channels: {}", mpc.info.channels);
//!
//! if let Some(tags) = mpc.tags_mut() {
//!     tags.set_text("Title", "Song Title".to_string())?;
//!     tags.set_text("Artist", "Artist Name".to_string())?;
//! }
//!
//! mpc.save()?;
//! # Ok(())
//! # }
//! ```
//!
//! # Stream Versions
//!
//! - **SV4-SV6**: Legacy versions, limited support
//! - **SV7**: Most common, fixed 1152-sample frames
//! - **SV8**: Latest, variable packet sizes, better efficiency
//!
//! # References
//!
//! - [Musepack Official Site](https://musepack.net/)

use crate::{
    AudexError, FileType, Result, StreamInfo,
    apev2::{APEv2, APEv2Tags},
};
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use std::time::Duration;

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

/// Musepack sample rates (used by stream versions 4-8)
const RATES: [u32; 4] = [44100, 48000, 37800, 32000];

/// Parse a Musepack SV8 variable-length integer from a reader.
///
/// SV8 encodes integers using a variable number of bytes (up to `limit`).
/// Each byte contributes 7 data bits; the MSB signals continuation (1 = more
/// bytes follow, 0 = last byte). Returns `(decoded_value, bytes_consumed)`.
pub fn parse_sv8_int<R: Read>(reader: &mut R, limit: usize) -> Result<(u64, usize)> {
    let mut num = 0u64;
    for i in 0..limit {
        let mut buf = [0u8; 1];
        reader
            .read_exact(&mut buf)
            .map_err(|_| AudexError::MusepackHeaderError("Unexpected end of file".to_string()))?;

        let byte = buf[0];

        // Guard against silent overflow: if the accumulated value already
        // occupies more than 57 bits, shifting left by 7 would discard the
        // upper bits of a u64, producing a silently corrupted result.
        if num > (u64::MAX >> 7) {
            return Err(AudexError::MusepackHeaderError(
                "SV8 variable-length integer overflow".to_string(),
            ));
        }

        num = (num << 7) | (byte as u64 & 0x7F);

        if (byte & 0x80) == 0 {
            return Ok((num, i + 1));
        }
    }

    if limit > 0 {
        Err(AudexError::MusepackHeaderError(
            "Invalid SV8 integer".to_string(),
        ))
    } else {
        Ok((0, 0))
    }
}

/// Calculate SV8 gain value
fn calc_sv8_gain(gain: i16) -> f64 {
    // 64.82 taken from mpcdec
    64.82 - (gain as f64) / 256.0
}

/// Calculate SV8 peak value
fn calc_sv8_peak(peak: i16) -> f64 {
    10_f64.powf((peak as f64) / (256.0 * 20.0)) / 65535.0
}

/// Musepack stream information
#[derive(Debug, Default)]
pub struct MusepackStreamInfo {
    pub length: Option<Duration>,
    pub bitrate: Option<u32>,
    pub channels: u16,
    pub sample_rate: u32,
    pub version: u8,
    pub samples: u64,

    // Optional replay gain fields (SV7/SV8 only)
    pub title_gain: Option<f64>,
    pub title_peak: Option<f64>,
    pub album_gain: Option<f64>,
    pub album_peak: Option<f64>,
}

impl StreamInfo for MusepackStreamInfo {
    fn length(&self) -> Option<Duration> {
        self.length
    }
    fn bitrate(&self) -> Option<u32> {
        self.bitrate
    }
    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
    }
}

impl MusepackStreamInfo {
    /// Parse Musepack file and extract stream information
    pub fn from_reader<R: Read + Seek>(reader: &mut R) -> Result<Self> {
        reader.seek(SeekFrom::Start(0))?;

        let mut header = [0u8; 4];
        reader
            .read_exact(&mut header)
            .map_err(|_| AudexError::MusepackHeaderError("Not a Musepack file".to_string()))?;

        // Skip ID3v2 tags if present
        if &header[0..3] == b"ID3" {
            let mut id3_header = [0u8; 6];
            reader
                .read_exact(&mut id3_header)
                .map_err(|_| AudexError::MusepackHeaderError("Not a Musepack file".to_string()))?;

            // Parse ID3v2 size (syncsafe integer - each byte uses only lower 7 bits)
            let size = (((id3_header[2] & 0x7F) as u32) << 21)
                | (((id3_header[3] & 0x7F) as u32) << 14)
                | (((id3_header[4] & 0x7F) as u32) << 7)
                | ((id3_header[5] & 0x7F) as u32);

            reader.seek(SeekFrom::Start(10 + size as u64))?;
            reader
                .read_exact(&mut header)
                .map_err(|_| AudexError::MusepackHeaderError("Not a Musepack file".to_string()))?;
        }

        // Determine Musepack version and parse accordingly
        if &header == b"MPCK" {
            Self::parse_sv8(reader)
        } else {
            Self::parse_sv467(reader, &header)
        }
    }

    /// Parse SV8 format
    fn parse_sv8<R: Read + Seek>(reader: &mut R) -> Result<Self> {
        let mut info = MusepackStreamInfo {
            version: 8,
            ..Default::default()
        };

        let key_size = 2;
        let mandatory_packets = vec![b"SH".to_vec(), b"RG".to_vec()];
        let mut found_packets = Vec::new();

        loop {
            let mut frame_type = [0u8; 2];
            match reader.read_exact(&mut frame_type) {
                Ok(_) => {}
                Err(_) => break, // End of stream
            }

            // Validate frame key
            if frame_type[0] < b'A'
                || frame_type[0] > b'Z'
                || frame_type[1] < b'A'
                || frame_type[1] > b'Z'
            {
                return Err(AudexError::MusepackHeaderError(
                    "Invalid frame key".to_string(),
                ));
            }

            // Stop at AP (APEv2) or SE (Stream End) packets
            if &frame_type == b"AP" || &frame_type == b"SE" {
                break;
            }

            let (frame_size, size_len) = parse_sv8_int(reader, 9)?;

            // A zero-length frame cannot even contain its own key bytes,
            // so it is always malformed. Reject it before computing
            // data_size to avoid a zero-byte seek that causes an infinite loop.
            if frame_size == 0 {
                return Err(AudexError::MusepackHeaderError(
                    "Malformed packet: zero-length frame".to_string(),
                ));
            }

            let data_size = frame_size
                .saturating_sub(key_size as u64)
                .saturating_sub(size_len as u64);

            // A zero data_size means the frame_size was smaller than its
            // own header overhead. Seeking by 0 bytes would make no forward
            // progress, so reject the malformed packet immediately.
            if data_size == 0 {
                return Err(AudexError::MusepackHeaderError(
                    "Malformed packet: frame_size smaller than header overhead".to_string(),
                ));
            }

            // Reject absurdly large packets to prevent OOM
            if data_size > 4 * 1024 * 1024 {
                return Err(AudexError::MusepackHeaderError(
                    "Packet size too large".to_string(),
                ));
            }

            match &frame_type {
                b"SH" => {
                    if found_packets.contains(&b"SH".to_vec()) {
                        return Err(AudexError::MusepackHeaderError(
                            "Duplicate SH packet".to_string(),
                        ));
                    }
                    found_packets.push(b"SH".to_vec());
                    Self::parse_stream_header(reader, data_size, &mut info)?;
                }
                b"RG" => {
                    if found_packets.contains(&b"RG".to_vec()) {
                        return Err(AudexError::MusepackHeaderError(
                            "Duplicate RG packet".to_string(),
                        ));
                    }
                    found_packets.push(b"RG".to_vec());
                    Self::parse_replaygain_packet(reader, data_size, &mut info)?;
                }
                _ => {
                    // Skip unknown packets — use checked cast to guard
                    // against future cap changes that might exceed i64
                    let seek_offset = i64::try_from(data_size).map_err(|_| {
                        AudexError::MusepackHeaderError(
                            "Packet data_size too large for seek offset".to_string(),
                        )
                    })?;
                    reader.seek(SeekFrom::Current(seek_offset))?;
                }
            }
        }

        // Check for mandatory packets
        for packet in &mandatory_packets {
            if !found_packets.contains(packet) {
                return Err(AudexError::MusepackHeaderError(format!(
                    "Missing mandatory packet: {:?}",
                    String::from_utf8_lossy(packet)
                )));
            }
        }

        // Calculate length and bitrate
        if info.sample_rate > 0 {
            info.length =
                Duration::try_from_secs_f64(info.samples as f64 / info.sample_rate as f64).ok();
        }

        Ok(info)
    }

    /// Parse SV8 Stream Header packet
    fn parse_stream_header<R: Read>(
        reader: &mut R,
        data_size: u64,
        info: &mut MusepackStreamInfo,
    ) -> Result<()> {
        // Skip CRC
        let mut crc = [0u8; 4];
        reader.read_exact(&mut crc)?;
        let remaining_size = data_size
            .checked_sub(4)
            .ok_or_else(|| AudexError::MusepackHeaderError("SH packet too small".to_string()))?;

        // Read version
        let mut version_buf = [0u8; 1];
        reader.read_exact(&mut version_buf).map_err(|_| {
            AudexError::MusepackHeaderError("SH packet ended unexpectedly".to_string())
        })?;
        info.version = version_buf[0];
        let remaining_size = remaining_size.checked_sub(1).ok_or_else(|| {
            AudexError::MusepackHeaderError("SH packet ended unexpectedly".to_string())
        })?;

        // Read sample counts
        let (samples, len1) = parse_sv8_int(reader, 9).map_err(|_| {
            AudexError::MusepackHeaderError("SH packet: Invalid sample counts".to_string())
        })?;
        let (samples_skip, len2) = parse_sv8_int(reader, 9).map_err(|_| {
            AudexError::MusepackHeaderError("SH packet: Invalid sample counts".to_string())
        })?;

        info.samples = samples.saturating_sub(samples_skip);
        let remaining_size = remaining_size
            .checked_sub(len1 as u64 + len2 as u64)
            .ok_or_else(|| {
                AudexError::MusepackHeaderError("SH packet ended unexpectedly".to_string())
            })?;

        // Read rate and channel info
        if remaining_size < 2 {
            return Err(AudexError::MusepackHeaderError(
                "SH packet ended unexpectedly".to_string(),
            ));
        }

        // Only read the 2 bytes needed for rate and channel data,
        // then discard the rest. Allocating the full remaining_size
        // (potentially megabytes) wastes memory for just 2 bytes.
        let mut rate_chan_data = [0u8; 2];
        reader.read_exact(&mut rate_chan_data).map_err(|_| {
            AudexError::MusepackHeaderError("SH packet ended unexpectedly".to_string())
        })?;

        // Discard remaining bytes in the SH packet by reading in
        // small chunks (reader may not support Seek)
        let mut to_skip = remaining_size.saturating_sub(2);
        let mut skip_buf = [0u8; 1024];
        while to_skip > 0 {
            let n = (to_skip as usize).min(skip_buf.len());
            reader.read_exact(&mut skip_buf[..n]).map_err(|_| {
                AudexError::MusepackHeaderError("SH packet ended unexpectedly".to_string())
            })?;
            to_skip -= n as u64;
        }

        let rate_index = (rate_chan_data[0] >> 5) as usize;
        if rate_index >= RATES.len() {
            return Err(AudexError::MusepackHeaderError(
                "Invalid sample rate".to_string(),
            ));
        }
        info.sample_rate = RATES[rate_index];
        let channels = ((rate_chan_data[1] >> 4) + 1) as u16;

        // Musepack only supports mono (1) and stereo (2) configurations
        if channels > 2 {
            return Err(AudexError::MusepackHeaderError(format!(
                "Unsupported channel count {}: Musepack supports at most 2 channels",
                channels
            )));
        }
        info.channels = channels;

        Ok(())
    }

    /// Parse SV8 Replay Gain packet.
    ///
    /// `data_size` is validated independently of the caller so that this
    /// function remains safe to use even if invoked from a different context
    /// that does not enforce the same upper bound.
    fn parse_replaygain_packet<R: Read>(
        reader: &mut R,
        data_size: u64,
        info: &mut MusepackStreamInfo,
    ) -> Result<()> {
        // Reject oversized packets to prevent unbounded allocation.
        // 4 MB matches the cap enforced by the SV8 packet loop, but is
        // checked here as well for defense-in-depth.
        const MAX_RG_PACKET_SIZE: u64 = 4 * 1024 * 1024;
        if data_size > MAX_RG_PACKET_SIZE {
            return Err(AudexError::MusepackHeaderError(
                "RG packet data_size too large".to_string(),
            ));
        }

        if data_size < 9 {
            return Err(AudexError::MusepackHeaderError(
                "Invalid RG packet size".to_string(),
            ));
        }

        let mut data = vec![0u8; data_size as usize];
        reader.read_exact(&mut data).map_err(|_| {
            AudexError::MusepackHeaderError("RG packet ended unexpectedly".to_string())
        })?;

        // Parse gain and peak values (big-endian)
        let title_gain = i16::from_be_bytes([data[1], data[2]]);
        let title_peak = i16::from_be_bytes([data[3], data[4]]);
        let album_gain = i16::from_be_bytes([data[5], data[6]]);
        let album_peak = i16::from_be_bytes([data[7], data[8]]);

        if title_gain != 0 {
            info.title_gain = Some(calc_sv8_gain(title_gain));
        }
        if title_peak != 0 {
            info.title_peak = Some(calc_sv8_peak(title_peak));
        }
        if album_gain != 0 {
            info.album_gain = Some(calc_sv8_gain(album_gain));
        }
        if album_peak != 0 {
            info.album_peak = Some(calc_sv8_peak(album_peak));
        }

        Ok(())
    }

    /// Parse SV4, SV5, SV6, SV7 formats  
    fn parse_sv467<R: Read + Seek>(reader: &mut R, _initial_header: &[u8]) -> Result<Self> {
        reader.seek(SeekFrom::Current(-4))?; // Go back to start of header

        let mut header = [0u8; 32];
        reader
            .read_exact(&mut header)
            .map_err(|_| AudexError::MusepackHeaderError("Not a Musepack file".to_string()))?;

        let mut info = MusepackStreamInfo {
            channels: 2, // All SV4-7 are stereo
            ..Default::default()
        };

        if &header[0..3] == b"MP+" {
            // SV7 format
            info.version = header[3] & 0xF;
            if info.version < 7 {
                return Err(AudexError::MusepackHeaderError(
                    "Not a Musepack file".to_string(),
                ));
            }

            let frames = u32::from_le_bytes([header[4], header[5], header[6], header[7]]);
            let flags = u32::from_le_bytes([header[8], header[9], header[10], header[11]]);

            // Parse replay gain data
            let title_peak = u16::from_le_bytes([header[12], header[13]]);
            let title_gain = i16::from_le_bytes([header[14], header[15]]);
            let album_peak = u16::from_le_bytes([header[16], header[17]]);
            let album_gain = i16::from_le_bytes([header[18], header[19]]);

            // Only populate replay gain fields when non-zero; zero means
            // the encoder did not write a value (consistent with SV8 handling).
            if title_gain != 0 {
                info.title_gain = Some(title_gain as f64 / 100.0);
            }
            if album_gain != 0 {
                info.album_gain = Some(album_gain as f64 / 100.0);
            }
            if title_peak != 0 {
                info.title_peak = Some(title_peak as f64 / 65535.0);
            }
            if album_peak != 0 {
                info.album_peak = Some(album_peak as f64 / 65535.0);
            }

            let rate_index = ((flags >> 16) & 0x0003) as usize;
            if rate_index >= RATES.len() {
                return Err(AudexError::MusepackHeaderError(
                    "Invalid sample rate".to_string(),
                ));
            }
            info.sample_rate = RATES[rate_index];
            info.bitrate = Some(0); // Will be calculated later

            info.samples = (frames as u64).saturating_mul(1152).saturating_sub(576);
        } else {
            // SV4-SV6 format
            let header_dword = u32::from_le_bytes([header[0], header[1], header[2], header[3]]);
            // Extract the 10-bit version field and validate before
            // truncating to u8 — raw values 256+ would wrap around
            // and could land in the valid 4-6 range by accident
            let raw_version = (header_dword >> 11) & 0x03FF;
            if !(4..=6).contains(&raw_version) {
                return Err(AudexError::MusepackHeaderError(
                    "Not a Musepack file".to_string(),
                ));
            }
            // Safe to truncate: validated to be in range 4-6
            info.version = raw_version as u8;

            // The 9-bit field stores bitrate in kbps; convert to bps
            // for consistency with other formats and the StreamInfo trait.
            let raw_kbps = (header_dword >> 23) & 0x01FF;
            info.bitrate = Some(raw_kbps.saturating_mul(1000));
            info.sample_rate = 44100;

            let frames = if info.version >= 5 {
                u32::from_le_bytes([header[4], header[5], header[6], header[7]])
            } else {
                u16::from_le_bytes([header[6], header[7]]) as u32
            };

            let actual_frames = if info.version < 6 {
                frames.saturating_sub(1)
            } else {
                frames
            };
            info.samples = (actual_frames as u64)
                .saturating_mul(1152)
                .saturating_sub(576);
        }

        if info.sample_rate > 0 {
            info.length =
                Duration::try_from_secs_f64(info.samples as f64 / info.sample_rate as f64).ok();
        }

        Ok(info)
    }

    /// Pretty print stream info
    pub fn pprint(&self) -> String {
        let mut rg_data = Vec::new();

        if let Some(title_gain) = self.title_gain {
            rg_data.push(format!("{:+0.2} (title)", title_gain));
        }
        if let Some(album_gain) = self.album_gain {
            rg_data.push(format!("{:+0.2} (album)", album_gain));
        }

        let rg_str = if rg_data.is_empty() {
            String::new()
        } else {
            format!(", Gain: {}", rg_data.join(", "))
        };

        format!(
            "Musepack SV{}, {:.2} seconds, {} Hz, {} bps{}",
            self.version,
            self.length.map(|d| d.as_secs_f64()).unwrap_or(0.0),
            self.sample_rate,
            self.bitrate.unwrap_or(0),
            rg_str
        )
    }
}

/// Musepack file with APEv2 tags
#[derive(Debug)]
pub struct Musepack {
    pub info: MusepackStreamInfo,
    pub tags: Option<APEv2Tags>,
    pub filename: Option<String>,
}

impl Musepack {
    /// Create a new empty Musepack instance with default stream info and no tags.
    pub fn new() -> Self {
        Self {
            info: MusepackStreamInfo::default(),
            tags: None,
            filename: None,
        }
    }

    /// Parse Musepack file and extract information
    fn parse_file<R: Read + Seek>(&mut self, reader: &mut R) -> Result<()> {
        // Parse stream info
        self.info = MusepackStreamInfo::from_reader(reader)?;

        // Calculate file-based bitrate if not already set
        if self.info.bitrate.is_none() || self.info.bitrate == Some(0) {
            let current_pos = reader.stream_position()?;
            reader.seek(SeekFrom::End(0))?;
            let file_size = reader.stream_position()?;
            reader.seek(SeekFrom::Start(current_pos))?;

            if let Some(length) = self.info.length {
                if length.as_secs_f64() > 0.0 {
                    // Clamp before casting to avoid truncation on extreme values
                    let bitrate_f64 =
                        (file_size.saturating_mul(8) as f64 / length.as_secs_f64()).round();
                    let bitrate = if bitrate_f64 > u32::MAX as f64 {
                        u32::MAX
                    } else {
                        bitrate_f64 as u32
                    };
                    self.info.bitrate = Some(bitrate);
                }
            }
        }

        // Parse APEv2 tags
        if let Some(filename) = &self.filename {
            match APEv2::load(filename) {
                Ok(ape) => self.tags = Some(ape.tags),
                Err(_) => self.tags = None, // No APE tags or parsing failed
            }
        }

        Ok(())
    }

    /// Add empty APEv2 tags
    pub fn add_tags(&mut self) -> Result<()> {
        if self.tags.is_some() {
            return Err(AudexError::MusepackHeaderError(
                "APEv2 tag already exists".to_string(),
            ));
        }
        self.tags = Some(APEv2Tags::new());
        Ok(())
    }

    /// Clear APEv2 tags
    pub fn clear(&mut self) -> Result<()> {
        if let Some(ref filename) = self.filename {
            // Use APEv2::clear to properly remove tags from the file
            crate::apev2::clear(filename)?;
        }
        self.tags = None;
        Ok(())
    }

    /// Get MIME types
    pub fn mime(&self) -> Vec<&'static str> {
        vec!["audio/x-musepack", "audio/x-mpc"]
    }

    /// Pretty print file info
    pub fn pprint(&self) -> String {
        self.info.pprint()
    }

    /// Load Musepack file asynchronously
    #[cfg(feature = "async")]
    pub async fn load_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        let mut file = loadfile_read_async(&path).await?;
        let mut mpc = Musepack::new();
        mpc.filename = Some(path.as_ref().to_string_lossy().to_string());

        // Parse stream info
        mpc.info = Self::parse_info_async(&mut file).await?;

        // Load APEv2 tags
        match crate::apev2::APEv2::load_async(&path).await {
            Ok(ape) => mpc.tags = Some(ape.tags),
            Err(AudexError::APENoHeader) => mpc.tags = None,
            Err(e) => return Err(e),
        }

        Ok(mpc)
    }

    /// Parse stream information asynchronously.
    ///
    /// Musepack headers are small — SV4-7 uses a fixed 32-byte header, SV8
    /// reads sequential packets that typically total under 1 KB. This reads
    /// at most 64 KB of header data via async I/O (far more than needed),
    /// then delegates to the sync parser. Bitrate is recalculated from the
    /// real file size to match the sync `parse_file` logic.
    #[cfg(feature = "async")]
    async fn parse_info_async(file: &mut TokioFile) -> Result<MusepackStreamInfo> {
        let file_size = file.seek(SeekFrom::End(0)).await?;

        // Musepack headers live at the start and are tiny. 64 KB is a very
        // generous cap — the actual header is typically well under 1 KB.
        const MAX_HEADER_READ: u64 = 64 * 1024;
        let read_size = std::cmp::min(file_size, MAX_HEADER_READ) as usize;

        file.seek(SeekFrom::Start(0)).await?;
        let mut data = vec![0u8; read_size];
        file.read_exact(&mut data).await?;

        let mut cursor = std::io::Cursor::new(&data[..]);
        let mut info = MusepackStreamInfo::from_reader(&mut cursor)?;

        // Recalculate bitrate from the real file size (the Cursor only saw
        // the header portion, so any file-size-based calculations need the
        // true value).
        if info.bitrate.is_none() || info.bitrate == Some(0) {
            if let Some(length) = info.length {
                if length.as_secs_f64() > 0.0 {
                    // Clamp before casting to avoid truncation on extreme values
                    let bitrate_f64 =
                        (file_size.saturating_mul(8) as f64 / length.as_secs_f64()).round();
                    let bitrate = if bitrate_f64 > u32::MAX as f64 {
                        u32::MAX
                    } else {
                        bitrate_f64 as u32
                    };
                    info.bitrate = Some(bitrate);
                }
            }
        }

        Ok(info)
    }

    /// Save tags asynchronously
    #[cfg(feature = "async")]
    pub async fn save_async(&mut self) -> Result<()> {
        let filename = self
            .filename
            .clone()
            .ok_or(AudexError::InvalidData("No filename set".to_string()))?;

        if let Some(ref tags) = self.tags {
            let mut ape = crate::apev2::APEv2::new();
            ape.filename = Some(filename);
            ape.tags = tags.clone();
            ape.save_async().await
        } else {
            Ok(())
        }
    }

    /// Clear tags asynchronously
    #[cfg(feature = "async")]
    pub async fn clear_async(&mut self) -> Result<()> {
        if let Some(filename) = &self.filename {
            crate::apev2::clear_async(filename).await?;
        }
        self.tags = None;
        Ok(())
    }
}

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

impl FileType for Musepack {
    type Tags = APEv2Tags;
    type Info = MusepackStreamInfo;

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

    fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        debug_event!("parsing Musepack file");
        let mut file = std::fs::File::open(&path)?;
        let mut musepack = Musepack::new();
        musepack.filename = Some(path.as_ref().to_string_lossy().to_string());

        musepack.parse_file(&mut file)?;
        Ok(musepack)
    }

    fn load_from_reader(reader: &mut dyn crate::ReadSeek) -> Result<Self> {
        let mut instance = Self::new();
        let mut reader = reader;
        instance.parse_file(&mut reader)?;

        // Parse APEv2 tags directly from the reader
        reader.seek(std::io::SeekFrom::Start(0))?;
        if let Ok(ape) = <crate::apev2::APEv2 as FileType>::load_from_reader(&mut reader) {
            instance.tags = Some(ape.tags);
        }

        Ok(instance)
    }

    fn save(&mut self) -> Result<()> {
        let filename = self
            .filename
            .as_ref()
            .ok_or(AudexError::InvalidData("No filename set".to_string()))?;

        // Save APEv2 tags if they exist
        if let Some(ref tags) = self.tags {
            // Create an APEv2 instance to handle the saving
            let mut apev2 = crate::apev2::APEv2::new();
            apev2.filename = Some(filename.clone());

            // Copy tags from our Musepack tags to the APEv2 tags
            for (key, value) in tags.items() {
                let _ = apev2.tags.set(&key, value.clone());
            }

            // Save the tags
            apev2.save()?;
        }

        Ok(())
    }

    fn clear(&mut self) -> Result<()> {
        if let Some(ref filename) = self.filename {
            // Use APEv2::clear to properly remove tags from the file
            crate::apev2::clear(filename)?;
        }
        self.tags = None;
        Ok(())
    }

    fn save_to_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        if let Some(ref tags) = self.tags {
            let mut apev2 = crate::apev2::APEv2::new();
            for (key, value) in tags.items() {
                let _ = apev2.tags.set(&key, value.clone());
            }
            apev2.save_to_writer(writer)?;
        }
        Ok(())
    }

    fn clear_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        let mut apev2 = crate::apev2::APEv2::new();
        apev2.clear_writer(writer)?;
        self.tags = None;
        Ok(())
    }

    fn save_to_path(&mut self, path: &Path) -> Result<()> {
        if let Some(ref tags) = self.tags {
            let mut apev2 = crate::apev2::APEv2::new();
            apev2.filename = Some(path.to_string_lossy().to_string());
            for (key, value) in tags.items() {
                let _ = apev2.tags.set(&key, value.clone());
            }
            apev2.save()?;
        }
        Ok(())
    }

    /// Adds empty APEv2 tags to the file.
    ///
    /// Creates a new empty tag structure if none exists. If tags already exist,
    /// returns an error.
    ///
    /// Note: the inherent method `Musepack::add_tags()` returns
    /// `AudexError::MusepackHeaderError` on failure. This trait method
    /// returns `AudexError::InvalidOperation` and is reached via
    /// `FileType::add_tags(&mut mpc)`.
    ///
    /// # Errors
    ///
    /// Returns `AudexError::InvalidOperation` if tags already exist.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::musepack::Musepack;
    /// use audex::FileType;
    ///
    /// let mut mpc = Musepack::load("song.mpc")?;
    /// if mpc.tags.is_none() {
    ///     mpc.add_tags()?;
    /// }
    /// # Ok::<(), audex::AudexError>(())
    /// ```
    fn add_tags(&mut self) -> Result<()> {
        if self.tags.is_some() {
            return Err(AudexError::InvalidOperation(
                "APE tags already exist".to_string(),
            ));
        }
        self.tags = Some(APEv2Tags::new());
        Ok(())
    }

    fn get(&self, key: &str) -> Option<Vec<String>> {
        // APEv2Tags stores values as APEValue, need to convert to Vec<String>
        self.tags.as_ref()?.get(key)?.as_text_list().ok()
    }

    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 score(filename: &str, header: &[u8]) -> i32 {
        let mut score = 0;

        // Check for Musepack signatures
        if header.len() >= 4 {
            if &header[0..3] == b"MP+" {
                score += 2;
            }
            if &header[0..4] == b"MPCK" {
                score += 2;
            }
        }

        // Check file extension
        let lower_filename = filename.to_lowercase();
        if lower_filename.ends_with(".mpc") {
            score += 1;
        }

        score
    }

    fn mime_types() -> &'static [&'static str] {
        &["audio/x-musepack", "audio/x-mpc"]
    }
}

/// Standalone functions for Musepack operations
pub fn clear<P: AsRef<Path>>(path: P) -> Result<()> {
    let mut musepack = Musepack::load(path)?;
    musepack.clear()
}