Audio Loader

Audio file loading and handling.

This module provides utilities for loading audio files with proper format handling and channel conversion.

mixref.audio.loader.load_audio(path, sample_rate=None, channel_mode='stereo')[source]

Load audio file and return as numpy array.

Loads audio files in various formats (WAV, FLAC, MP3) and handles mono/stereo conversion automatically.

Parameters:
  • path (str | Path) – Path to audio file

  • sample_rate (int | None) – Target sample rate. If None, uses file’s native SR. If specified, resamples to target SR.

  • channel_mode (Literal['mono', 'stereo', 'auto']) – Channel handling mode: - “mono”: Convert to mono (average channels) - “stereo”: Convert to stereo (duplicate if mono) - “auto”: Keep original channel configuration

Returns:

  • audio_data: Float32 array with shape (samples,) for mono or (samples, 2) for stereo

  • sample_rate: Sample rate in Hz

Return type:

Tuple of (audio_data, sample_rate) where

Raises:
  • AudioFileNotFoundError – If audio file doesn’t exist

  • UnsupportedFormatError – If file format is not supported

  • CorruptFileError – If file cannot be read or is corrupt

Example

>>> # Load stereo audio
>>> audio, sr = load_audio("track.wav")
>>> audio.shape
(441000, 2)
>>> sr
44100
>>> # Load and force mono
>>> audio_mono, sr = load_audio("track.wav", channel_mode="mono")
>>> audio_mono.shape
(441000,)