Helper functions#
These are helper functions that are used throughout the package.
- thebeat.helpers.all_possibilities(numbers, target)[source]#
Use a deep-first search to find all possible combinations of ‘numbers’ that sum to ‘target’. Returns a NumPy array of NumPy arrays (with dtype=object to allow nested array).
- Return type:
- thebeat.helpers.check_sound_properties_sameness(objects)[source]#
This helper function checks whether the objects in the passed iterable have the same sound properties. Raises errors if properties are not the same.
- thebeat.helpers.get_sound_with_metronome(samples, fs, metronome_ioi, metronome_amplitude)[source]#
This helper function adds a metronome sound to a NumPy array of sound samples. It works for both mono and stereo sounds.
- Return type:
- thebeat.helpers.make_ramps(samples, fs, onramp_ms, offramp_ms, ramp_type)[source]#
Internal function used to create on- and offramps. Supports ‘linear’ and ‘raised-cosine’ ramps.
- thebeat.helpers.normalize_audio(samples)[source]#
This helper function normalizes audio based on the absolute max amplitude from the provided sound samples.
- Return type:
- thebeat.helpers.overlay_samples(samples_arrays)[source]#
Overlay all the samples in the iterable
- Return type:
- thebeat.helpers.play_samples(samples, fs, mean_ioi, loop, metronome, metronome_amplitude)[source]#
This helper function uses the sounddevice library to play a sound, either with or without metronome.
- Return type:
- thebeat.helpers.plot_lp(lp, filepath=None, suppress_display=False, title=None, figsize=None, dpi=300, ax=None)[source]#
This function plots a LilyPond object.
- Parameters:
lp – A LilyPond string.
filepath (
PathLike
|str
, default:None
) – If provided, the plot will be saved to this path. Has to end with either .png or .pdf.suppress_display (
bool
, default:False
) – If True, the plot will not be displayed usingmatplotlib.figure.Figure.show()
.figsize (
Optional
[tuple
[float
,float
]], default:None
) – A tuple containing the desired output size of the plot in inches, e.g.(4, 1)
. This refers to thefigsize
parameter inmatplotlib.pyplot.figure()
.dpi (
int
, default:300
) – The resolution of the plot in dots per inch.ax (
Optional
[Axes
], default:None
) – If provided, the plot will be drawn on this axes.
- Return type:
- thebeat.helpers.plot_single_sequence(onsets, end_with_interval, final_ioi=None, style='seaborn-v0_8', title=None, x_axis_label='Time', linewidths=None, figsize=None, dpi=100, suppress_display=False, ax=None)[source]#
This function plots the onsets of a Sequence or SoundSequence object in an event plot.
- Parameters:
onsets (
list
|ndarray
) – The onsets (i.e. t values) of the sequence.end_with_interval (
bool
) – Indicates whether sequence ends with an interval (True
) or event (False
).final_ioi (
Optional
[float
], default:None
) – The final inter-onset interval of the sequence. This is only used if the sequence ends with an interval.linewidths (
UnionType
[list
[float
],ndarray
[Any
,dtype
[float
]],float
,None
], default:None
) – The desired width of the bars (events). Defaults to 1/10th of the smallest inter-onset interval (IOI). Can be a single value that will be used for each onset, or a list or array of values (i.e with a value for each respective onsets).style (
str
, default:'seaborn-v0_8'
) – Matplotlib style to use for the plot. Defaults to ‘seaborn’. See matplotlib style sheets reference.title (
Optional
[str
], default:None
) – If desired, one can provide a title for the plot. This takes precedence over using the name of the object as the title of the plot (if the object has one).x_axis_label (
str
, default:'Time'
) – A label for the x axis.figsize (
Optional
[tuple
], default:None
) – A tuple containing the desired output size of the plot in inches, e.g.(4, 1)
. This refers to thefigsize
parameter inmatplotlib.pyplot.subplots()
.dpi (
int
, default:100
) – The number of dots per inch. This refers to thedpi
parameter inmatplotlib.figure.Figure
.suppress_display (
bool
, default:False
) – IfTrue
, the plot is only returned, and not displayed viamatplotlib.pyplot.show()
.ax (
Optional
[Axes
], default:None
) – If desired, you can provide an existingmatplotlib.axes.Axes
object onto which to plot. See the Examples of the different plotting functions to see how to do this (e.g.plot_sequence()
). If an existingmatplotlib.axes.Axes
object is supplied, this function returns the originalmatplotlib.figure.Figure
andmatplotlib.axes.Axes
objects.
- Return type:
- thebeat.helpers.plot_waveform(samples, fs, n_channels, style='seaborn-v0_8', title=None, figsize=None, dpi=100, suppress_display=False, ax=None)[source]#
This helper function plots a waveform of a sound using matplotlib.
- Parameters:
samples (
ndarray
) – The array containing the sound samples.fs (
int
) – Sampling frequency in hertz (e.g. 48000).n_channels (
int
) – Number of channels (1 for mono, 2 for stereo).style (
str
, default:'seaborn-v0_8'
) –Style used by matplotlib. See matplotlib style sheets reference.
title (
Optional
[str
], default:None
) – If desired, one can provide a title for the plot.figsize (
Optional
[tuple
], default:None
) – A tuple containing the desired output size of the plot in inches, e.g.(4, 1)
. This refers to thefigsize
parameter inmatplotlib.pyplot.figure()
.dpi (
int
, default:100
) – The resolution of the plot in dots per inch. This refers to thedpi
parameter inmatplotlib.pyplot.figure()
.suppress_display (
bool
, default:False
) – IfTrue
,matplotlib.pyplot.Figure.show()
is not run.ax (
Optional
[Axes
], default:None
) – If desired, you can provide an existingmatplotlib.axes.Axes
object onto which to plot. See the Examples of the different plotting functions to see how to do this (e.g.plot_sequence()
). If an existingmatplotlib.axes.Axes
object is supplied, this function returns the originalmatplotlib.figure.Figure
andmatplotlib.axes.Axes
objects.
- Return type:
- thebeat.helpers.read_wav(filepath, new_fs)[source]#
Internal function used to read a wave file. Returns the wave file’s samples and the sampling frequency. If dtype is different from np.float64, it converts the samples to that.
- thebeat.helpers.resample(samples, input_fs, output_fs)[source]#
Internal function used to resample sounds. Uses scipy.signal.resample
- thebeat.helpers.rhythm_to_binary(rhythm, smallest_note_value=16)[source]#
This helper function converts a rhythm to a binary representation.
- thebeat.helpers.sequence_to_binary(sequence, resolution)[source]#
Converts a sequence of millisecond onsets to a series of zeros and ones. Ones for the onsets.
- thebeat.helpers.synthesize_sound(duration_ms, fs, freq, n_channels, amplitude, oscillator)[source]#
- Return type:
- thebeat.helpers.write_wav(samples, fs, filepath, dtype=<class 'numpy.int16'>, metronome=False, metronome_ioi=None, metronome_amplitude=None)[source]#
This helper function writes the provided sound samples to disk as a wave file. See https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html for more info.
- Return type: