espnet2.fileio.sound_scp.soundfile_read
About 1 min
espnet2.fileio.sound_scp.soundfile_read
espnet2.fileio.sound_scp.soundfile_read(wavs: str | List[str], dtype=None, always_2d: bool = False, concat_axis: int = 1, start: int = 0, end: int | None = None, return_subtype: bool = False) → Tuple[array, int]
Read audio files using the soundfile library.
This function reads one or more audio files and returns their audio data as a NumPy array along with the sample rate. It can handle both single and multi-channel audio files, and allows for optional concatenation of audio data along a specified axis.
- Parameters:
- wavs (Union *[*str , List *[*str ] ]) – A single audio file path or a list of audio file paths to read.
- dtype – The desired data type of the output array (default is None).
- always_2d (bool , optional) – If True, ensures that the output array is always 2-dimensional (default is False).
- concat_axis (int , optional) – The axis along which to concatenate multiple audio arrays (default is 1).
- start (int , optional) – The starting frame to read from each audio file (default is 0).
- end (int , optional) – The ending frame to read from each audio file. If None, reads until the end of the file (default is None).
- return_subtype (bool , optional) – If True, returns the subtype of the audio files along with the data and sample rate (default is False).
- Returns: A tuple containing the audio data as a NumPy array and the sample rate of the audio files. If return_subtype is True, it returns a tuple of the form (array, rate, subtypes).
- Return type: Tuple[np.array, int]
- Raises:
- RuntimeError – If the sampling rates of the audio files do not
- match or if the shapes of the arrays do not align along the –
- specified concatenation axis. –
Examples
>>> data, rate = soundfile_read('audio.wav')
>>> data, rate = soundfile_read(['audio1.wav', 'audio2.wav'],
... concat_axis=0)
>>> data, rate, subtypes = soundfile_read('audio.wav',
... return_subtype=True)
NOTE
This function uses the soundfile library, which needs to be installed separately. Ensure that the audio files are in a supported format.