Audio Validation

Audio file validation and inspection utilities.

This module provides functions to validate audio files and extract metadata like duration, sample rate, channel count, and format.

class mixref.audio.validation.AudioInfo[source]

Bases: NamedTuple

Audio file information.

duration

Duration in seconds

Type:

float

sample_rate

Sample rate in Hz

Type:

int

channels

Number of channels (1=mono, 2=stereo)

Type:

int

format

File format (e.g., ‘WAV’, ‘FLAC’, ‘MP3’)

Type:

str

subtype

Format subtype (e.g., ‘PCM_16’, ‘FLOAT’)

Type:

str

duration: float

Alias for field number 0

sample_rate: int

Alias for field number 1

channels: int

Alias for field number 2

format: str

Alias for field number 3

subtype: str

Alias for field number 4

static __new__(_cls, duration, sample_rate, channels, format, subtype)

Create new instance of AudioInfo(duration, sample_rate, channels, format, subtype)

mixref.audio.validation.get_audio_info(path)[source]

Get detailed information about an audio file.

Parameters:

path (str | Path) – Path to audio file

Returns:

AudioInfo object with duration, sample rate, channels, format, and subtype

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

  • CorruptFileError – If the file cannot be read or is corrupted

Return type:

AudioInfo

Example

>>> info = get_audio_info("my_track.wav")
>>> print(f"Duration: {info.duration:.2f}s at {info.sample_rate}Hz")
Duration: 180.50s at 44100Hz
mixref.audio.validation.validate_duration(path, min_duration=0.0, max_duration=None)[source]

Validate audio file duration is within acceptable range.

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

  • min_duration (float) – Minimum acceptable duration in seconds (default: 0.0)

  • max_duration (float | None) – Maximum acceptable duration in seconds (None = no limit)

Returns:

Tuple of (is_valid, message). If valid, message is empty string.

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

  • CorruptFileError – If the file cannot be read

Return type:

tuple[bool, str]

Example

>>> is_valid, msg = validate_duration("track.wav", min_duration=30.0)
>>> if not is_valid:
...     print(f"Warning: {msg}")
mixref.audio.validation.validate_sample_rate(path, expected_sr, tolerance=0)[source]

Validate audio file sample rate matches expected value.

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

  • expected_sr (int) – Expected sample rate in Hz

  • tolerance (int) – Allowed deviation in Hz (default: 0 = exact match)

Returns:

Tuple of (is_valid, message). If valid, message is empty string.

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

  • CorruptFileError – If the file cannot be read

Return type:

tuple[bool, str]

Example

>>> is_valid, msg = validate_sample_rate("track.wav", 44100)
>>> if not is_valid:
...     print(f"Sample rate mismatch: {msg}")