medkit.core.audio.audio_buffer#

Classes#

AudioBuffer

Audio buffer base class. Gives access to raw audio samples.

FileAudioBuffer

Audio buffer giving access to audio files stored on the filesystem.

MemoryAudioBuffer

Audio buffer giving access to signals stored in memory.

PlaceholderAudioBuffer

Placeholder for a MemoryAudioBuffer for which we have lost the actual signal.

Module Contents#

class medkit.core.audio.audio_buffer.AudioBuffer(sample_rate: int, nb_samples: int, nb_channels: int)#

Bases: abc.ABC, medkit.core.dict_conv.SubclassMapping

Audio buffer base class. Gives access to raw audio samples.

Parameters:
sample_rate:

Sample rate of the signal, in samples per second.

nb_samples:

Duration of the signal in samples.

nb_channels:

Number of channels in the signal.

sample_rate#
nb_samples#
nb_channels#
property duration: float#

Duration of the signal in seconds.

abstract read(copy: bool = False) numpy.ndarray#

Return the signal in the audio buffer.

Parameters:
copy:

If True, the returned array will be a copy that can be safely mutated.

Returns:
np.ndarray:

Raw audio samples

abstract trim(start: int | None, end: int | None) AudioBuffer#

Return the signal from the original buffer trimmed by start and end indexes.

Parameters:
start: int, optional

Start sample of the new buffer (defaults to 0).

end: int, optional

End sample of the new buffer, excluded (default to full duration).

Returns:
AudioBuffer:

Trimmed audio buffer with new start and end samples, of same type as original audio buffer.

trim_duration(start_time: float | None = None, end_time: float | None = None) AudioBuffer#

Return the signal from the original buffer trimmed by start and end times.

Return a new audio buffer pointing to a portion of the signal in the original buffer, using boundaries in seconds. Since start_time and end_time are in seconds, the exact trim boundaries will be rounded to the nearest sample and will therefore depend on the sampling rate.

Parameters:
start_time: float, optional

Start time of the new buffer (defaults to 0.0).

end_time: float, optional

End time of thew new buffer, excluded (default to full duration).

Returns:
AudioBuffer:

Trimmed audio buffer with new start and end samples, of same type as original audio buffer.

classmethod __init_subclass__()#
classmethod from_dict(data_dict: dict[str, Any]) typing_extensions.Self#
abstract to_dict() dict[str, Any]#
abstract __eq__(other: object) bool#
class medkit.core.audio.audio_buffer.FileAudioBuffer(path: str | pathlib.Path, trim_start: int | None = None, trim_end: int | None = None, sf_info: Any | None = None)#

Bases: AudioBuffer

Audio buffer giving access to audio files stored on the filesystem.

To be used when manipulating unmodified raw audio.

Supports all file formats handled by libsndfile

Parameters:
path: str or Path

Path to the audio file.

trim_start: int, optional

First sample of audio file to consider.

trim_end: int, optional

First sample of audio file to exclude.

sf_info: Any, optional

Optional metadata dict returned by soundfile.

path#
trim_start#
trim_end#
sample_rate#
nb_samples#
nb_channels#
_trim_end#
_trim_start#
_sf_info#
read(copy: bool = False) numpy.ndarray#

Return the signal in the audio buffer.

Parameters:
copy:

If True, the returned array will be a copy that can be safely mutated.

Returns:
np.ndarray:

Raw audio samples

trim(start: int | None = None, end: int | None = None) AudioBuffer#

Return the signal from the original buffer trimmed by start and end indexes.

Parameters:
start: int, optional

Start sample of the new buffer (defaults to 0).

end: int, optional

End sample of the new buffer, excluded (default to full duration).

Returns:
AudioBuffer:

Trimmed audio buffer with new start and end samples, of same type as original audio buffer.

to_dict() dict[str, Any]#
classmethod from_dict(data: dict[str, Any]) typing_extensions.Self#
__eq__(other: object) bool#
class medkit.core.audio.audio_buffer.MemoryAudioBuffer(signal: numpy.ndarray, sample_rate: int)#

Bases: AudioBuffer

Audio buffer giving access to signals stored in memory.

To be used for reading or writing a modified audio signal.

Parameters:
signal: ndarray

Samples constituting the audio signal, with shape (nb_channel, nb_samples).

sample_rate: int

Sample rate of the signal, in samples per second.

_signal#
read(copy: bool = False) numpy.ndarray#

Return the signal in the audio buffer.

Parameters:
copy:

If True, the returned array will be a copy that can be safely mutated.

Returns:
np.ndarray:

Raw audio samples

trim(start: int | None = None, end: int | None = None) AudioBuffer#

Return the signal from the original buffer trimmed by start and end indexes.

Parameters:
start: int, optional

Start sample of the new buffer (defaults to 0).

end: int, optional

End sample of the new buffer, excluded (default to full duration).

Returns:
AudioBuffer:

Trimmed audio buffer with new start and end samples, of same type as original audio buffer.

to_dict() dict[str, Any]#
classmethod from_dict(data: dict[str, Any]) typing_extensions.Self#
__eq__(other: object) bool#
class medkit.core.audio.audio_buffer.PlaceholderAudioBuffer(sample_rate: int, nb_samples: int, nb_channels: int)#

Bases: AudioBuffer

Placeholder for a MemoryAudioBuffer for which we have lost the actual signal.

This class is only here so that MemoryAudioBuffer objects can be converted into json/yaml serializable dicts and then unserialized, but no further processing can be performed since the actual signal is not saved. Calling :meth`~read()` or :meth`~.trim()` will raise.

classmethod from_audio_buffer(audio_buffer: AudioBuffer) PlaceholderAudioBuffer#
read(copy: bool = False) numpy.ndarray#

Return the signal in the audio buffer.

Parameters:
copy:

If True, the returned array will be a copy that can be safely mutated.

Returns:
np.ndarray:

Raw audio samples

trim(start: int | None, end: int | None) AudioBuffer#

Return the signal from the original buffer trimmed by start and end indexes.

Parameters:
start: int, optional

Start sample of the new buffer (defaults to 0).

end: int, optional

End sample of the new buffer, excluded (default to full duration).

Returns:
AudioBuffer:

Trimmed audio buffer with new start and end samples, of same type as original audio buffer.

to_dict() dict[str, Any]#
classmethod from_dict(data: dict[str, Any]) typing_extensions.Self#
__eq__(other: object) bool#