madmom.utils

Utility package.

madmom.utils.suppress_warnings(function)[source]

Decorate the given function to suppress any warnings.

Parameters:
function : function

Function to be decorated.

Returns:
decorated function

Decorated function.

madmom.utils.filter_files(files, suffix)[source]

Filter the list to contain only files matching the given suffix.

Parameters:
files : list

List of files to be filtered.

suffix : str

Return only files matching this suffix.

Returns:
list

List of files.

madmom.utils.search_path(path, recursion_depth=0)[source]

Returns a list of files in a directory (recursively).

Parameters:
path : str or list

Directory to be searched.

recursion_depth : int, optional

Recursively search sub-directories up to this depth.

Returns:
list

List of files.

madmom.utils.search_files(files, suffix=None, recursion_depth=0)[source]

Returns the files matching the given suffix.

Parameters:
files : str or list

File, path or a list thereof to be searched / filtered.

suffix : str, optional

Return only files matching this suffix.

recursion_depth : int, optional

Recursively search sub-directories up to this depth.

Returns:
list

List of files.

Notes

The list of returned files is sorted.

madmom.utils.strip_suffix(filename, suffix=None)[source]

Strip off the suffix of the given filename or string.

Parameters:
filename : str

Filename or string to strip.

suffix : str, optional

Suffix to be stripped off (e.g. ‘.txt’ including the dot).

Returns:
str

Filename or string without suffix.

madmom.utils.match_file(filename, match_list, suffix=None, match_suffix=None, match_exactly=True)[source]

Match a filename or string against a list of other filenames or strings.

Parameters:
filename : str

Filename or string to match.

match_list : list

Match to this list of filenames or strings.

suffix : str, optional

Suffix of filename to be ignored.

match_suffix : str, optional

Match only files from match_list with this suffix.

match_exactly : bool, optional

Matches must be exact, i.e. have the same base name.

Returns:
list

List of matched files.

Notes

Asterisks “*” can be used to match any string or suffix.

madmom.utils.combine_events(events, delta, combine='mean')[source]

Combine all events within a certain range.

Parameters:
events : list or numpy array

Events to be combined.

delta : float

Combination delta. All events within this delta are combined.

combine : {‘mean’, ‘left’, ‘right’}

How to combine two adjacent events:

  • ‘mean’: replace by the mean of the two events
  • ‘left’: replace by the left of the two events
  • ‘right’: replace by the right of the two events
Returns:
numpy array

Combined events.

madmom.utils.quantize_events(events, fps, length=None, shift=None)[source]

Quantize the events with the given resolution.

Parameters:
events : list or numpy array

Events to be quantized.

fps : float

Quantize with fps frames per second.

length : int, optional

Length of the returned array. If ‘None’, the length will be set according to the latest event.

shift : float, optional

Shift the events by shift seconds before quantization.

Returns:
numpy array

Quantized events.

madmom.utils.quantize_notes(notes, fps, length=None, num_pitches=None, velocity=None)[source]

Quantize the notes with the given resolution.

Create a sparse 2D array with rows corresponding to points in time (according to fps and length), and columns to note pitches (according to num_pitches). The values of the array correspond to the velocity of a sounding note at a given point in time (based on the note pitch, onset, duration and velocity). If no values for length and num_pitches are given, they are inferred from notes.

Parameters:
notes : 2D numpy array

Notes to be quantized. Expected columns: ‘note_time’ ‘note_number’ [‘duration’ [‘velocity’]] If notes contains no ‘duration’ column, only the frame of the onset will be set. If notes has no velocity column, a velocity of 1 is assumed.

fps : float

Quantize with fps frames per second.

length : int, optional

Length of the returned array. If ‘None’, the length will be set according to the latest sounding note.

num_pitches : int, optional

Number of pitches of the returned array. If ‘None’, the number of pitches will be based on the highest pitch in the notes array.

velocity : float, optional

Use this velocity for all quantized notes. If set, the last column of notes (if present) will be ignored.

Returns:
numpy array

Quantized notes.

madmom.utils.expand_notes(notes, duration=0.6, velocity=100)[source]

Expand notes to include duration and velocity.

The given duration and velocity is only used if they are not set already.

Parameters:
notes : numpy array, shape (num_notes, 2)

Notes, one per row. Expected columns: ‘note_time’ ‘note_number’ [‘duration’ [‘velocity’]]

duration : float, optional

Note duration if not defined by notes.

velocity : int, optional

Note velocity if not defined by notes.

Returns:
notes : numpy array, shape (num_notes, 2)

Notes (including note duration and velocity).

class madmom.utils.OverrideDefaultListAction(sep=None, *args, **kwargs)[source]

An argparse action that works similarly to the regular ‘append’ action. The default value is deleted when a new value is specified. The ‘append’ action would append the new value to the default.

Parameters:
sep : str, optional

Separator to be used if multiple values should be parsed from a list.

madmom.utils.segment_axis(signal, frame_size, hop_size, axis=None, end='cut', end_value=0)[source]

Generate a new array that chops the given array along the given axis into (overlapping) frames.

Parameters:
signal : numpy array

Signal.

frame_size : int

Size of each frame [samples].

hop_size : int

Hop size between adjacent frames [samples].

axis : int, optional

Axis to operate on; if ‘None’, operate on the flattened array.

end : {‘cut’, ‘wrap’, ‘pad’}, optional

What to do with the last frame, if the array is not evenly divisible into pieces; possible values:

  • ‘cut’ simply discard the extra values,
  • ‘wrap’ copy values from the beginning of the array,
  • ‘pad’ pad with a constant value.
end_value : float, optional

Value used to pad if end is ‘pad’.

Returns:
numpy array, shape (num_frames, frame_size)

Array with overlapping frames

Notes

The array is not copied unless necessary (either because it is unevenly strided and being flattened or because end is set to ‘pad’ or ‘wrap’).

The returned array is always of type np.ndarray.

Examples

>>> segment_axis(np.arange(10), 4, 2)
array([[0, 1, 2, 3],
       [2, 3, 4, 5],
       [4, 5, 6, 7],
       [6, 7, 8, 9]])

Submodules