madmom.io

Input/output package.

madmom.io.open_file(*args, **kwds)[source]

Context manager which yields an open file or handle with the given mode and closes it if needed afterwards.

Parameters:
filename : str or file handle

File (handle) to open.

mode: {‘r’, ‘w’}

Specifies the mode in which the file is opened.

Yields:
Open file (handle).
madmom.io.load_events(*args, **kwargs)[source]

Load a events from a text file, one floating point number per line.

Parameters:
filename : str or file handle

File to load the events from.

Returns:
numpy array

Events.

Notes

Comments (lines starting with ‘#’) and additional columns are ignored, i.e. only the first column is returned.

madmom.io.write_events(events, filename, fmt='%.3f', delimiter='\t', header=None)[source]

Write the events to a file, one event per line.

Parameters:
events : numpy array

Events to be written to file.

filename : str or file handle

File to write the events to.

fmt : str or sequence of strs, optional

A single format (e.g. ‘%.3f’), a sequence of formats, or a multi-format string (e.g. ‘%.3f %.3f’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

madmom.io.load_onsets(*args, **kwargs)

Load a events from a text file, one floating point number per line.

Parameters:
filename : str or file handle

File to load the events from.

Returns:
numpy array

Events.

Notes

Comments (lines starting with ‘#’) and additional columns are ignored, i.e. only the first column is returned.

madmom.io.write_onsets(events, filename, fmt='%.3f', delimiter='\t', header=None)

Write the events to a file, one event per line.

Parameters:
events : numpy array

Events to be written to file.

filename : str or file handle

File to write the events to.

fmt : str or sequence of strs, optional

A single format (e.g. ‘%.3f’), a sequence of formats, or a multi-format string (e.g. ‘%.3f %.3f’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

madmom.io.load_beats(*args, **kwargs)[source]

Load the beats from the given file, one beat per line of format ‘beat_time’ [‘beat_number’].

Parameters:
filename : str or file handle

File to load the beats from.

downbeats : bool, optional

Load only downbeats instead of beats.

Returns:
numpy array

Beats.

madmom.io.write_beats(beats, filename, fmt=None, delimiter='\t', header=None)[source]

Write the beats to a file.

Parameters:
beats : numpy array

Beats to be written to file.

filename : str or file handle

File to write the beats to.

fmt : str or sequence of strs, optional

A single format (e.g. ‘%.3f’), a sequence of formats (e.g. [‘%.3f’, ‘%d’]), or a multi-format string (e.g. ‘%.3f %d’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

madmom.io.load_downbeats(filename)[source]

Load the downbeats from the given file.

Parameters:
filename : str or file handle

File to load the downbeats from.

Returns:
numpy array

Downbeats.

madmom.io.write_downbeats(beats, filename, fmt=None, delimiter='\t', header=None)[source]

Write the downbeats to a file.

Parameters:
beats : numpy array

Beats or downbeats to be written to file.

filename : str or file handle

File to write the beats to.

fmt : str or sequence of strs, optional

A single format (e.g. ‘%.3f’), a sequence of formats (e.g. [‘%.3f’, ‘%d’]), or a multi-format string (e.g. ‘%.3f %d’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

Notes

If beats contains both time and number of the beats, they are filtered to contain only the downbeats (i.e. only the times of those beats with a beat number of 1).

madmom.io.load_notes(*args, **kwargs)[source]

Load the notes from the given file, one note per line of format ‘onset_time’ ‘note_number’ [‘duration’ [‘velocity’]].

Parameters:
filename: str or file handle

File to load the notes from.

Returns:
numpy array

Notes.

madmom.io.write_notes(notes, filename, fmt=None, delimiter='\t', header=None)[source]

Write the notes to a file.

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

Notes, row format ‘onset_time’ ‘note_number’ [‘duration’ [‘velocity’]].

filename : str or file handle

File to write the notes to.

fmt : str or sequence of strs, optional

A sequence of formats (e.g. [‘%.3f’, ‘%d’, ‘%.3f’, ‘%d’]), or a multi-format string, e.g. ‘%.3f %d %.3f %d’, in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

Returns:
numpy array

Notes.

madmom.io.load_segments(filename)[source]

Load labelled segments from file, one segment per line. Each segment is of form <start> <end> <label>, where <start> and <end> are floating point numbers, and <label> is a string.

Parameters:
filename : str or file handle

File to read the labelled segments from.

Returns:
segments : numpy structured array

Structured array with columns ‘start’, ‘end’, and ‘label’, containing the beginning, end, and label of segments.

madmom.io.write_segments(segments, filename, fmt=None, delimiter='\t', header=None)[source]

Write labelled segments to a file.

Parameters:
segments : numpy structured array

Labelled segments, one per row (column definition see SEGMENT_DTYPE).

filename : str or file handle

Output filename or handle.

fmt : str or sequence of strs, optional

A sequence of formats (e.g. [‘%.3f’, ‘%.3f’, ‘%s’]), or a multi-format string (e.g. ‘%.3f %.3f %s’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

Returns:
numpy structured array

Labelled segments

Notes

Labelled segments are represented as numpy structured array with three named columns: ‘start’ contains the start position (e.g. seconds), ‘end’ the end position, and ‘label’ the segment label.

madmom.io.load_chords(filename)

Load labelled segments from file, one segment per line. Each segment is of form <start> <end> <label>, where <start> and <end> are floating point numbers, and <label> is a string.

Parameters:
filename : str or file handle

File to read the labelled segments from.

Returns:
segments : numpy structured array

Structured array with columns ‘start’, ‘end’, and ‘label’, containing the beginning, end, and label of segments.

madmom.io.write_chords(segments, filename, fmt=None, delimiter='\t', header=None)

Write labelled segments to a file.

Parameters:
segments : numpy structured array

Labelled segments, one per row (column definition see SEGMENT_DTYPE).

filename : str or file handle

Output filename or handle.

fmt : str or sequence of strs, optional

A sequence of formats (e.g. [‘%.3f’, ‘%.3f’, ‘%s’]), or a multi-format string (e.g. ‘%.3f %.3f %s’), in which case delimiter is ignored.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

Returns:
numpy structured array

Labelled segments

Notes

Labelled segments are represented as numpy structured array with three named columns: ‘start’ contains the start position (e.g. seconds), ‘end’ the end position, and ‘label’ the segment label.

madmom.io.load_key(filename)[source]

Load the key from the given file.

Parameters:
filename : str or file handle

File to read key information from.

Returns:
str

Key.

madmom.io.write_key(key, filename, header=None)[source]

Write key string to a file.

Parameters:
key : str

Key name.

filename : str or file handle

Output file.

header : str, optional

String that will be written at the beginning of the file as comment.

Returns:
key : str

Key name.

madmom.io.load_tempo(filename, split_value=1.0, sort=None, norm_strengths=None, max_len=None)[source]

Load tempo information from the given file.

Tempo information must have the following format: ‘main tempo’ [‘secondary tempo’ [‘relative_strength’]]

Parameters:
filename : str or file handle

File to load the tempo from.

split_value : float, optional

Value to distinguish between tempi and strengths. values > split_value are interpreted as tempi [bpm], values <= split_value are interpreted as strengths.

sort : bool, deprecated

Sort the tempi by their strength.

norm_strengths : bool, deprecated

Normalize the strengths to sum 1.

max_len : int, deprecated

Return at most max_len tempi.

Returns:
tempi : numpy array, shape (num_tempi[, 2])

Array with tempi. If no strength is parsed, a 1-dimensional array of length ‘num_tempi’ is returned. If strengths are given, a 2D array with tempi (first column) and their relative strengths (second column) is returned.

madmom.io.write_tempo(tempi, filename, delimiter='\t', header=None, mirex=None)[source]

Write the most dominant tempi and the relative strength to a file.

Parameters:
tempi : numpy array

Array with the detected tempi (first column) and their strengths (second column).

filename : str or file handle

Output file.

delimiter : str, optional

String or character separating columns.

header : str, optional

String that will be written at the beginning of the file as comment.

mirex : bool, deprecated

Report the lower tempo first (as required by MIREX).

Returns:
tempo_1 : float

The most dominant tempo.

tempo_2 : float

The second most dominant tempo.

strength : float

Their relative strength.