Skip to main content

rpfm_lib/files/audio/
mod.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Audio file passthrough handler.
12//!
13//! This module provides the [`Audio`] type for handling audio files in Total War PackFiles.
14//! It acts as a passthrough, storing raw audio data without decoding or encoding the actual
15//! audio formats.
16//!
17//! # Supported Extensions
18//!
19//! The following audio file extensions are recognized:
20//! - **`.mp3`** - MPEG Audio Layer III
21//! - **`.wem`** - Wwise Encoded Media (proprietary audio format)
22//! - **`.wav`** - Waveform Audio File Format
23//!
24//! # Limitations
25//!
26//! This module does not decode or encode audio data. It only provides raw byte access,
27//! allowing external audio libraries or tools to process the data. The files are stored
28//! and written back in their original binary format.
29//!
30//! # Use Cases
31//!
32//! - Extracting audio files from PackFiles for editing in external tools
33//! - Re-packing modified audio files
34//! - Analyzing audio file metadata
35//! - Custom audio processing with specialized libraries
36//!
37//! # Example
38//!
39//! ```
40//! use rpfm_lib::files::{Decodeable, audio::Audio};
41//! use std::io::Cursor;
42//!
43//! // Read raw audio data
44//! let audio_bytes = vec![/* MP3 or WEM data */];
45//! let mut reader = Cursor::new(audio_bytes);
46//! let audio = Audio::decode(&mut reader, &None).unwrap();
47//!
48//! // Access raw data for external processing
49//! let raw_data = audio.data();
50//! ```
51
52use getset::*;
53use serde_derive::{Serialize, Deserialize};
54
55use crate::binary::{ReadBytes, WriteBytes};
56use crate::error::Result;
57use crate::files::{DecodeableExtraData, Decodeable, EncodeableExtraData, Encodeable};
58
59/// Extension used by audio files.
60pub const EXTENSIONS: [&str; 3] = [".mp3", ".wem", ".wav"];
61
62//---------------------------------------------------------------------------//
63//                              Enum & Structs
64//---------------------------------------------------------------------------//
65
66/// Container for raw audio file data.
67///
68/// Stores audio data in its original binary format without decoding. This allows
69/// audio files to be extracted, modified with external tools, and re-packed.
70///
71/// # Fields
72///
73/// * `data` - Raw binary audio data (MP3, WEM, or WAV format)
74///
75/// # Getters
76///
77/// Fields have public getters via the `getset` crate:
78/// - `data()` - Get reference to the raw audio bytes
79///
80/// # Example
81///
82/// ```
83/// use rpfm_lib::files::{Decodeable, Encodeable, audio::Audio};
84/// use std::io::Cursor;
85///
86/// # let mp3_data = vec![0xFF, 0xFB]; // MP3 header bytes
87/// let mut reader = Cursor::new(mp3_data.clone());
88/// let audio = Audio::decode(&mut reader, &None).unwrap();
89///
90/// assert_eq!(audio.data(), &mp3_data);
91/// ```
92#[derive(Clone, Debug, PartialEq, Eq, Getters, Setters, Serialize, Deserialize)]
93#[getset(get = "pub")]
94pub struct Audio {
95    /// Raw binary audio data in original format.
96    data: Vec<u8>,
97}
98
99//---------------------------------------------------------------------------//
100//                              Implementations
101//---------------------------------------------------------------------------//
102
103impl Decodeable for Audio {
104
105    fn decode<R: ReadBytes>(data: &mut R, _extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
106        let len = data.len()?;
107        let data = data.read_slice(len as usize, false)?;
108        Ok(Self {
109            data,
110        })
111    }
112}
113
114impl Encodeable for Audio {
115
116    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData>) -> Result<()> {
117        buffer.write_all(&self.data).map_err(From::from)
118    }
119}