pub struct AudioFormatConverter;Expand description
High-level audio format converter.
Detects common audio container formats and converts WAV/PCM input to the standard format: mono, 16kHz, normalized f32 samples.
§Pipeline Architecture
Input Bytes
↓
[Format Detection] ← 6 formats: WAV, MP3, FLAC, Opus, WebM, AAC
↓
[WAV Decoding] ← 16/24-bit PCM normalization
↓
[Resampling] ← Arbitrary rate → 16kHz (linear interpolation)
↓
[Channel Mixing] ← Multi-channel → Mono (simple averaging)
↓
StandardAudio (mono, 16kHz, f32)§Current Scope Limitations
- Formats: Only WAV decoding implemented; other formats detected but not decoded
- Channel Counts: 1, 2, 4, 6 channels supported
- Bit Depths: 16-bit and 24-bit PCM only
- Resampling: Linear interpolation (sinc reserved for future)
§Example
use speech_prep::converter::AudioFormatConverter;
let wav_bytes = std::fs::read("audio.wav")?;
let standard = AudioFormatConverter::convert_to_standard(&wav_bytes)?;
assert!(standard.metadata.original_sample_rate > 0);
assert!(standard.metadata.original_channels > 0);Implementations§
Source§impl AudioFormatConverter
impl AudioFormatConverter
Sourcepub fn convert_to_standard(audio_bytes: &[u8]) -> Result<StandardAudio>
pub fn convert_to_standard(audio_bytes: &[u8]) -> Result<StandardAudio>
Convert WAV audio bytes to standard format: mono, 16kHz, f32.
This is the primary entry point for the audio normalization pipeline. It composes all 4 stages: format detection, decoding, resampling, and channel mixing.
§Arguments
audio_bytes- Raw audio file bytes
§Returns
StandardAudio with mono 16kHz samples and complete conversion
metadata.
§Errors
Returns Error::InvalidInput if:
- Format detection fails (not a recognized audio format)
- Format is detected but not WAV (only WAV decoding supported)
- WAV decoding fails (malformed file, unsupported codec)
- Resampling fails (invalid sample rates)
- Channel mixing fails (unsupported channel count)
§Performance
Target: <10ms for 3-second audio clip on reference hardware.
Actual timing captured in ConversionMetadata.conversion_time_ms.
§Example
use speech_prep::converter::AudioFormatConverter;
let audio_bytes = std::fs::read("recording.wav")?;
let standard = AudioFormatConverter::convert_to_standard(&audio_bytes)?;
assert_eq!(standard.metadata.original_format.as_str(), "wav");
assert!(standard.samples.len() > 0);Trait Implementations§
Source§impl Clone for AudioFormatConverter
impl Clone for AudioFormatConverter
Source§fn clone(&self) -> AudioFormatConverter
fn clone(&self) -> AudioFormatConverter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AudioFormatConverter
impl Debug for AudioFormatConverter
Source§impl Default for AudioFormatConverter
impl Default for AudioFormatConverter
Source§fn default() -> AudioFormatConverter
fn default() -> AudioFormatConverter
Returns the “default value” for a type. Read more
impl Copy for AudioFormatConverter
Auto Trait Implementations§
impl Freeze for AudioFormatConverter
impl RefUnwindSafe for AudioFormatConverter
impl Send for AudioFormatConverter
impl Sync for AudioFormatConverter
impl Unpin for AudioFormatConverter
impl UnsafeUnpin for AudioFormatConverter
impl UnwindSafe for AudioFormatConverter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more