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
//! # MP3 Format Support
//!
//! This module provides comprehensive support for reading and writing MPEG Layer III (MP3) audio files.
//!
//! ## Overview
//!
//! The MP3 format is one of the most widely used audio compression formats. This module supports:
//! - **MPEG-1, MPEG-2, and MPEG-2.5** audio versions
//! - **Layer I, II, and III** encoding (with primary focus on Layer III/MP3)
//! - **Multiple tagging formats**:
//! - ID3v2 (versions 2.2, 2.3, and 2.4) - Primary tagging format
//! - ID3v1 and ID3v1.1 - Legacy tagging format
//! - **Bitrate detection**: Constant Bitrate (CBR), Variable Bitrate (VBR), and Average Bitrate (ABR)
//! - **VBR header support**:
//! - Xing/Info headers (used by LAME and other encoders)
//! - VBRI headers (Fraunhofer encoder format)
//! - Both provide accurate duration calculation and seeking support
//! - **LAME encoder information** extraction including ReplayGain values
//!
//! ## Supported Features
//!
//! - Read/write audio properties (bitrate, sample rate, channels, duration)
//! - Read/write metadata tags (artist, title, album, etc.)
//! - Support for embedded artwork via ID3v2 APIC frames
//! - Accurate duration calculation for VBR files using Xing/VBRI headers
//! - Multiple tag format handling in a single file
//!
//! ## Basic Usage
//!
//! ```no_run
//! use audex::mp3::MP3;
//! use audex::{FileType, Tags};
//!
//! // Load an MP3 file
//! let mut mp3 = MP3::load("song.mp3").unwrap();
//!
//! // Access audio information
//! if let Some(length) = mp3.info.length {
//! println!("Duration: {:.2} seconds", length.as_secs_f64());
//! }
//! println!("Bitrate: {} kbps", mp3.info.bitrate / 1000);
//! println!("Sample rate: {} Hz", mp3.info.sample_rate);
//!
//! // Read ID3 tags using the Tags trait
//! if let Some(ref tags) = mp3.tags {
//! if let Some(title) = tags.get_text_values("TIT2") {
//! println!("Title: {:?}", title);
//! }
//! }
//!
//! // Modify tags and save
//! if let Some(ref mut tags) = mp3.tags {
//! tags.set("TIT2", vec!["New Title".to_string()]);
//! }
//! mp3.save().unwrap();
//! ```
//!
//! ## Limitations and Known Issues
//!
//! - **Read-only audio properties**: This library does not support re-encoding audio data.
//! Only metadata can be modified.
//! - **Tag preservation**: When saving, the library preserves all existing tag formats by default.
//! Use the `clear()` method to remove specific tag types if needed.
//! - **Memory usage**: Large MP3 files with extensive ID3v2 tags (especially with embedded artwork)
//! will be fully loaded into memory.
//! - **MPEG Layer I/II**: While basic support exists, this module is primarily tested with Layer III (MP3) files.
//!
//! ## VBR Header Types
//!
//! This module supports two VBR header formats for accurate duration and seeking:
//!
//! - **Xing/Info headers** (`XingHeader`): Used by LAME and most encoders. The "Xing"
//! identifier is used for VBR files, while "Info" is used for CBR files encoded by LAME.
//! - **VBRI headers** (`VBRIHeader`): Fraunhofer's alternative format. Less common but
//! still encountered in files from Fraunhofer-based encoders.
//!
//! Both header types are automatically detected and parsed. The higher-level `MP3` type
//! handles this transparently.
//!
//! ## See Also
//!
//! - `MP3` - Main struct for MP3 file handling
//! - `MPEGInfo` - Audio stream information
//! - `EasyMP3` - Simplified interface for common tagging operations
//! - `XingHeader` - Xing/Info VBR header structure
//! - `VBRIHeader` - VBRI VBR header structure (Fraunhofer format)
//! - `LAMEHeader` - Extended LAME encoder information
//! - `MPEGFrame` - Low-level MPEG frame structure
//! - `BitrateMode` - CBR/VBR/ABR detection
//! - [`crate::id3`] - ID3 tag format support
use crateAudexError;
use fmt;
pub use ;
pub use ;
/// MP3-specific error type for handling MP3 format errors.
///
/// This error type represents general MP3 format errors that can occur during
/// file parsing, validation, or processing. It automatically converts to
/// [`AudexError`] for use with the library's error handling system.
///
/// # Examples
///
/// ```
/// use audex::mp3::Error;
/// use audex::AudexError;
///
/// let mp3_error = Error {
/// message: "Invalid frame sync".to_string(),
/// };
///
/// // Automatically converts to AudexError
/// let audex_error: AudexError = mp3_error.into();
/// ```
/// Error indicating that a required MPEG header could not be found.
///
/// This error occurs when the parser cannot locate a valid MPEG frame header
/// in the expected position within the file. This typically indicates either:
/// - The file is not a valid MP3 file
/// - The file is corrupted
/// - The file contains excessive leading data before the first MPEG frame
///
/// # Examples
///
/// ```
/// use audex::mp3::HeaderNotFoundError;
///
/// let error = HeaderNotFoundError {
/// message: "No valid MPEG header found in first 8KB".to_string(),
/// };
/// ```
/// Error indicating that an MPEG header was found but contains invalid data.
///
/// This error occurs when a potential MPEG header is detected (valid sync bits)
/// but the header data itself is malformed or contains invalid values. Common causes:
/// - Invalid bitrate index (all 1s or all 0s in bitrate field)
/// - Invalid sample rate index
/// - Reserved MPEG version or layer values
/// - Inconsistent header values across frames
///
/// # Examples
///
/// ```
/// use audex::mp3::InvalidMPEGHeader;
///
/// let error = InvalidMPEGHeader {
/// message: "Invalid bitrate index: 15".to_string(),
/// };
/// ```
/// Channel mode constants as defined in the MPEG audio specification.
///
/// These constants represent the four possible channel modes encoded in MPEG audio frame headers.
/// Stereo mode - Two independent channels (left and right).
///
/// Both channels contain different audio data with no inter-channel compression.
pub const STEREO: i32 = 0;
/// Joint stereo mode - Channels share some information for better compression.
///
/// Uses mid/side stereo or intensity stereo encoding to exploit inter-channel redundancy.
/// Common in modern MP3 encoders for improved compression efficiency.
pub const JOINTSTEREO: i32 = 1;
/// Dual channel mode - Two independent mono channels.
///
/// Similar to stereo but typically used for bilingual or multi-language audio
/// where each channel contains a separate mono program.
pub const DUALCHANNEL: i32 = 2;
/// Mono mode - Single channel audio.
///
/// The audio stream contains only one channel of audio data.
pub const MONO: i32 = 3;
/// Determines the bitrate mode (CBR/VBR/ABR) from a Xing/Info header.
///
/// This function analyzes the Xing or Info header to determine how the MP3 file
/// was encoded. The detection logic uses several heuristics:
///
/// 1. **LAME header VBR method field** (if present) - Most reliable indicator
/// 2. **Info vs Xing tag** - Info tags are only written for CBR files
/// 3. **VBR scale presence** - Indicates variable bitrate encoding
/// 4. **LAME version string** - Older LAME versions using VBR
///
/// # Arguments
///
/// * `xing` - Reference to a parsed Xing or Info header
///
/// # Returns
///
/// Returns the detected [`BitrateMode`], or `BitrateMode::Unknown` if the mode
/// cannot be reliably determined.
///
/// # Implementation Notes
///
/// The LAME encoder uses different VBR method codes:
/// - 1, 8: Constant Bitrate (CBR)
/// - 2, 9: Average Bitrate (ABR)
/// - 3-6: Variable Bitrate (VBR) with different quality settings
pub
/// Clear (remove) all ID3 tags from an MP3 file
///
/// Removes both ID3v1 and ID3v2 tags from the file.
///
/// # Arguments
/// * `filething` - Path to the MP3 file
///
/// # Example
/// ```no_run
/// use audex::mp3;
///
/// mp3::clear("song.mp3").unwrap();
/// ```
/// Clear (remove) ID3 tags from an MP3 file with options
///
/// # Arguments
/// * `filething` - Path to the MP3 file
/// * `clear_v1` - Whether to clear ID3v1 tags
/// * `clear_v2` - Whether to clear ID3v2 tags
///
/// # Example
/// ```no_run
/// use audex::mp3;
///
/// // Clear only ID3v2 tags, keep ID3v1
/// mp3::clear_with_options("song.mp3", false, true).unwrap();
/// ```
/// Native async version of [`clear`]. Removes both ID3v1 and ID3v2 tags
/// from an MP3 file using tokio I/O.
pub async
/// MPEG audio version as specified in the frame header.
///
/// MPEG audio has three standardized versions, each with different
/// supported sample rates and features:
///
/// - **MPEG-1**: Original standard, supports 32/44.1/48 kHz
/// - **MPEG-2**: Extension for lower sample rates (16/22.05/24 kHz)
/// - **MPEG-2.5**: Unofficial extension for even lower rates (8/11.025/12 kHz)
///
/// The version affects the supported sample rates, bit reservoir size,
/// and some technical encoding parameters.
/// MPEG audio layer as specified in the frame header.
///
/// MPEG audio defines three layers, each representing a different
/// complexity/quality tradeoff:
///
/// - **Layer I**: Simplest, lowest compression, rarely used today
/// - **Layer II**: Medium complexity, used in broadcasting (DAB, DVB)
/// - **Layer III**: Most complex, best compression, the "MP3" format
///
/// Higher layer numbers provide better compression but require more
/// processing power to encode and decode.
/// Audio channel configuration mode.
///
/// Specifies how audio channels are encoded in the MPEG stream.
/// The channel mode affects both the number of channels and how
/// they are compressed.
/// Pre-emphasis filter applied to the audio signal.
///
/// Emphasis refers to a pre-processing filter that boosts high frequencies
/// during encoding. The decoder applies the inverse filter (de-emphasis) to
/// restore the original frequency balance. This technique reduces high-frequency
/// noise in analog recording systems.
///
/// In practice, emphasis is rarely used in modern digital audio and most
/// MP3 files use `None`.