audiofile¶
This module contains the following classes:
- AudioFile, representing an audio file;
- AudioFileConverterError,
- AudioFileNotInitializedError,
- AudioFileProbeError, and
- AudioFileUnsupportedFormatError, representing errors generated by audio files.
- 
class aeneas.audiofile.AudioFile(file_path=None, file_format=None, rconf=None, logger=None)[source]¶
- A class representing an audio file. - This class can be used either to extract properties from an audio file on disk, or to load/edit/save a monoaural (single channel) audio file, represented as an array of audio samples. - The properties of the audio file (length, format, etc.) can set by invoking the - read_properties()function, which calls an audio file probe. (Currently, the probe is- FFPROBEWrapper)- Moreover, this class can read the audio data, by converting the original file format into a temporary PCM16 Mono WAVE (RIFF) file, which is deleted as soon as audio data is read in memory. (Currently, the converter is - FFMPEGWrapper)- The internal representation of the wave is a a NumPy 1D array of - float64values in- [-1.0, 1.0]. It supports append, reverse, and trim operations. Audio samples can be written to file. Memory can be pre-allocated to speed append operations up. Allocated memory is doubled when an append operation requires more memory than what is available; this leads to an amortized linear complexity (in the number of audio samples) for append operations.- Note - Support for stereo WAVE files might be implemented in a future version - Parameters: - file_path (string) – the path of the audio file
- file_format (tuple) – the format of the audio file, if known in advance: (codec, channels, rate)orNone
- rconf (RuntimeConfiguration) – a runtime configuration
- logger (Logger) – the logger object
 - 
FILE_EXTENSIONS= ['3g2', '3gp', 'aa', 'aa3', 'aac', 'aax', 'aiff', 'alac', 'amr', 'ape', 'asf', 'at3', 'at9', 'au', 'avi', 'awb', 'celt', 'dct', 'dss', 'dvf', 'eac', 'flac', 'flv', 'gsm', 'm4a', 'm4b', 'm4p', 'm4v', 'mid', 'midi', 'mkv', 'mmf', 'mov', 'mp2', 'mp3', 'mp4', 'mpc', 'mpeg', 'mpg', 'mpv', 'msv', 'oga', 'ogg', 'ogv', 'oma', 'opus', 'pcm', 'qt', 'ra', 'ram', 'raw', 'riff', 'rm', 'rmvb', 'shn', 'sln', 'theora', 'tta', 'vob', 'vorbis', 'vox', 'wav', 'webm', 'wma', 'wmv', 'wv', 'yuv']¶
- Extensions of common formats for audio (and video) files. 
 - 
add_samples(samples, reverse=False)[source]¶
- Concatenate the given new samples to the current audio data. - This function initializes the memory if no audio data is present already. - If - reverseis- True, the new samples will be reversed and then concatenated.- Parameters: - samples (numpy.ndarray(1D)) – the new samples to be concatenated
- reverse (bool) – if True, concatenate new samples after reversing them
 - New in version 1.2.1. 
- samples (
 - 
audio_channels¶
- The number of channels of the audio file. - Return type: - int 
 - 
audio_format¶
- The format of the audio file. - Return type: - string 
 - 
audio_sample_rate¶
- The sample rate of the audio file, in samples per second. - Return type: - int 
 - 
audio_samples¶
- The audio audio_samples, that is, an array of - float64values, each representing an audio sample in- [-1.0, 1.0].- Note that this function returns a view into the first - self.__samples_lengthelements of- self.__samples. If you want to clone the values, you must use e.g.- numpy.array(audiofile.audio_samples).- Return type: - numpy.ndarray(1D, view)- Raises: - AudioFileNotInitializedError: if the audio file is not initialized yet
 - 
file_path¶
- The path of the audio file. - Return type: - string 
 - 
file_size¶
- The size of the audio file, in bytes. - Return type: - int 
 - 
minimize_memory()[source]¶
- Reduce the allocated memory to the minimum required to store the current audio samples. - This function is meant to be called when building a wave incrementally, after the last append operation. - New in version 1.5.0. 
 - 
preallocate_memory(capacity)[source]¶
- Preallocate memory to store audio samples, to avoid repeated new allocations and copies while performing several consecutive append operations. - If - self.__samplesis not initialized, it will become an array of- capacityzeros.- If - capacityis larger than the current capacity, the current- self.__sampleswill be extended with zeros.- If - capacityis smaller than the current capacity, the first- capacityvalues of- self.__sampleswill be retained.- Parameters: - capacity (int) – the new capacity, in number of samples - Raises: - ValueError: if - capacityis negative- New in version 1.5.0. 
 - 
read_properties()[source]¶
- Populate this object by reading the audio properties of the file at the given path. - Currently this function uses - FFPROBEWrapperto get the audio file properties.- Raises: - AudioFileProbeError: if the path to the- ffprobeexecutable cannot be called- Raises: - AudioFileUnsupportedFormatError: if the audio file has a format not supported- Raises: - OSError: if the audio file cannot be read 
 - 
read_samples_from_file()[source]¶
- Load the audio samples from file into memory. - If - self.file_formatis- Noneor it is not- ("pcm_s16le", 1, self.rconf.sample_rate), the file will be first converted to a temporary PCM16 mono WAVE file. Audio data will be read from this temporary file, which will be then deleted from disk immediately.- Otherwise, the audio data will be read directly from the given file, which will not be deleted from disk. - Raises: - AudioFileConverterError: if the path to the- ffmpegexecutable cannot be called- Raises: - AudioFileUnsupportedFormatError: if the audio file has a format not supported- Raises: - OSError: if the audio file cannot be read 
 - 
reverse()[source]¶
- Reverse the audio data. - Raises: - AudioFileNotInitializedError: if the audio file is not initialized yet- New in version 1.2.0. 
 - 
trim(begin=None, length=None)[source]¶
- Get a slice of the audio data of - lengthseconds, starting from- beginseconds.- If audio data is not loaded, load it and then slice it. - Parameters: - Raises: - TypeError: if one of the arguments is not - Noneor- TimeValue- New in version 1.2.0. 
 - 
write(file_path)[source]¶
- Write the audio data to file. Return - Trueon success, or- Falseotherwise.- Parameters: - file_path (string) – the path of the output file to be written - Raises: - AudioFileNotInitializedError: if the audio file is not initialized yet- New in version 1.2.0. 
 
- 
exception aeneas.audiofile.AudioFileConverterError[source]¶
- Error raised when the audio converter executable cannot be executed. 
- 
exception aeneas.audiofile.AudioFileNotInitializedError[source]¶
- Error raised when trying to access audio samples from an - AudioFileobject which has not been initialized yet.