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 guessed from file’s extension. filename can be a compressed audio or video file. This will require installing pydub (https://github.com/jiaaro/pydub).

The normal behavior is to load all audio data to memory from which a BufferAudioSource object is created. This should be convenient most of the time unless audio file is very large. In that case, and in order to load audio data in lazy manner (i.e. read data from disk each time AudioSource.read() is called), large_file should be True.

Note that the current implementation supports only wave and raw formats for lazy audio loading.

If an 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()

Parameters:
  • filename (str) – path to input audio or video file.
  • audio_format (str) – audio format used to save data (e.g. raw, webm, wav, ogg).
  • large_file (bool, default: False) – if True, audio won’t fully be loaded to memory but only when a window is read from disk.
Other Parameters:
 
  • sampling_rate, sr (int) – sampling rate of audio data
  • sample_width (int) – sample width (i.e. number of bytes used to represent one audio sample)
  • channels (int) – number of channels of audio data
Returns:

audio_source – an AudioSource object that reads data from input file.

Return type:

AudioSource

Raises:

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