exordium.audio.io

Audio input/output utilities.

Attributes

logger

Module-level logger.

Classes

AudioLoader

Audio loader with caching support for efficient repeated access.

Functions

save_audio(→ None)

Save audio waveform to file using torchaudio.

load_audio(→ tuple[torch.Tensor, int])

Load audio file with optional resampling and preprocessing.

split_audio(→ list[torch.Tensor])

Split audio into fixed-duration segments.

Module Contents

exordium.audio.io.logger

Module-level logger.

exordium.audio.io.save_audio(audio: torch.Tensor, output_path: pathlib.Path | str, sr: int = 16000) → None[source]

Save audio waveform to file using torchaudio.

Parameters:
  • audio – Audio tensor of shape (T,) or (C, T).

  • output_path – Path where audio file will be saved.

  • sr – Sample rate in Hz.

exordium.audio.io.load_audio(audio_path: pathlib.Path | str, target_sample_rate: int | None = 16000, clamp: bool = True, mono: bool = True, squeeze: bool = True) → tuple[torch.Tensor, int][source]

Load audio file with optional resampling and preprocessing.

Parameters:
  • audio_path – Path to audio file.

  • target_sample_rate – Target sample rate in Hz. If None, keeps original sample rate.

  • clamp – If True, clamp waveform values to [-1, 1].

  • mono – If True and audio is stereo, convert to mono by averaging channels.

  • squeeze – If True, remove singleton dimensions from output.

Returns:

  • waveform: Audio tensor of shape (T,) if mono and squeezed,

    (C, T) if stereo or not squeezed.

  • sample_rate: Sample rate in Hz.

Return type:

Tuple of (waveform, sample_rate)

exordium.audio.io.split_audio(audio: torch.Tensor, segment_duration: float, sample_rate: int) → list[torch.Tensor][source]

Split audio into fixed-duration segments.

Parameters:
  • audio – Audio tensor of shape (T,) or (C, T).

  • segment_duration – Duration of each segment in seconds.

  • sample_rate – Sample rate in Hz.

Returns:

List of audio segments. Last segment may be shorter than segment_duration.

class exordium.audio.io.AudioLoader[source]

Audio loader with caching support for efficient repeated access.

Caches loaded audio files to avoid redundant I/O operations.

Initialize AudioLoader with empty cache.

load_audio(audio_file: pathlib.Path | str, start_time_sec: float | None = None, end_time_sec: float | None = None, target_sample_rate: int | None = None, mono: bool = True, squeeze: bool = True, batch_dim: bool = False) → tuple[torch.Tensor, int][source]

Load audio file with caching, optional time slicing, and format support.

Parameters:
  • audio_file – Path to audio or video file with audio.

  • start_time_sec – Start time in seconds for audio slice. Requires end_time_sec.

  • end_time_sec – End time in seconds for audio slice. Requires start_time_sec.

  • target_sample_rate – Target sample rate in Hz. Required for .mp4 files.

  • mono – If True, convert to mono.

  • squeeze – If True, remove singleton dimensions.

  • batch_dim – If True, add batch dimension at index 0.

Returns:

  • waveform: Audio tensor.

  • sample_rate: Sample rate in Hz.

Return type:

Tuple of (waveform, sample_rate)