auditok.io.from_file

auditok.io.from_file(filename, audio_format=None, large_file=False, **kwargs)[source]

Read audio data from filename and return an AudioSource object.

If audio_format is None, the appropriate AudioSource class is inferred from the file extension. The filename can refer to a compressed audio or video file; if a video file is provided, its audio track(s) are extracted. This functionality requires pydub (https://github.com/jiaaro/pydub).

By default, all audio data is loaded into memory to create a BufferAudioSource object, suitable for most cases. For very large files, set large_file=True to enable lazy loading, which reads audio data from disk each time AudioSource.read is called. Currently, lazy loading supports only wave and raw formats.

If audio_format is raw, the following keyword arguments are required:

  • sampling_rate, sr: int, sampling rate of audio data.

  • sample_width, sw: int, size in bytes of one audio sample.

  • channels, ch: int, number of channels of audio data.

See also

to_file

A related function for saving audio data to a file.

Parameters:
  • filename (str or Path) – The path to the input audio or video file.

  • audio_format (str, optional) – The audio format (e.g., raw, webm, wav, ogg).

  • large_file (bool, optional, default=False) – If True, the audio data is read lazily from disk rather than being fully loaded into memory.

  • sampling_rate (int) – The sampling rate of the audio data.

  • sr (int) – The sampling rate of the audio data.

  • sample_width (int) – The sample width in bytes (i.e., number of bytes per audio sample).

  • channels (int) – The number of audio channels.

Returns:

An AudioSource object that reads data from the specified file.

Return type:

AudioSource

Raises:

AudioIOError – If audio data cannot be read in the given format or if audio_format is raw and one or more required audio parameters are missing.