iracema.core.audio

This module contains the implementation of the class Audio.

class iracema.core.audio.Audio(fs, data, start_time=None, caption=None)[source]

Bases: iracema.core.timeseries.TimeSeries

Class for storing and manipulating audio data.

Hint

This class is also available at the main package level as iracema.Audio.

Example

There are two ways to instantiate an Audio object: from audio files or from NumPy arrays. To initialize it using an audio file, you must use the method Audio.load(), specifying the location from which the file must be loaded:

>>> ir.Audio.load('02 - Clarinet - Long Notes.wav')
Audio: (1, 1785244), fs=44100.0, unit=amplitude, label=waveform

Alternatively the location can be specified trough an http URL:

>>> url = (
... 'https://raw.githubusercontent.com/cegeme/iracema-audio/master/03 - '
... 'Clarinet - Fast Excerpt.wav'
... )
>>> ir.Audio.load(url)
Audio: (1, 390060), fs=44100.0, unit=amplitude, label=waveform

To initialize the object using a NumPy array, you can use the initializer of the class Audio(). In this case, two arguments are necessary: the sampling frequency fs of the data and the data array.

>>> ir.Audio(44100, clarinet_data)
Audio: (1, 390060), fs=44100.0, unit=amplitude, label=waveform
Parameters
  • fs (int, optional) – Sampling frequency for the data.

  • data (np.array, optional) – Data array containing the audio samples.

  • start_time (Decimal, optional) – The time in seconds the time series start, relative to the original time reference.

  • unit (str, optional) – Unit name for plotting the data of the time series.

  • caption (str, optional) – Text caption for the time series.

  • data – Data vector containing the audio data to be loaded.

  • fs – Sampling frequency of the data.

  • caption – Textual description used for plotting and displaying reports about the audio excerpt.

classmethod load(file_location, caption=None)[source]

Load an audio file into an Audio object.

Parameters
  • file_location (str) – Location from which the file will be loaded. The string might contain a path pointing to a local file or an http URL referencing a remote file

  • caption (str, optional) – Caption for the audio file loaded (optional). If this argument is not provided, the base name of the loaded file will be used.

Returns

audio – An object of the class Audio, containing the data loaded from the specified location.

Return type

Audio

resample(new_fs)[source]

Resample time series to new_fs.

pitch_shift(n_steps, **kwargs)[source]

Shift the pitch of the audio time series and return the new object.

This method is a wrapper over librosa’s pitch_shift method.

time_stretch(rate, **kwargs)[source]

Time-stretch the audio time series and return the new object.

This method is a wrapper over librosa’s time_stretch method.

add_noise(db=- 50.0)[source]

Add noise to the audio time series and return the new object.

Parameters

db (float) – Peak amplitude (in dB) of the generated noise, with an amplitude of 1.0 correspoding to 0 dB.

plot(linewidth=0.1, alpha=0.9, **kwargs)[source]

Plot the time series. The linewidth and alpha (opacity) values can be set using keyword arguments.

play()[source]

Play audio from Audio object.

play_with_clicks(points)[source]

Play audio mixed with click sounds in the instants corresponding to points.

mix_clicks(points)[source]

Return an audio time series with clicks mixed in the instants corresponding to points.

play_from_time(from_time)[source]

Play audio from Audio object start at time from_time.

play_segment(segment)[source]

Play segment from Audio obejct.

static stop()[source]

Stop playing audio.