partitura.utils¶
- partitura.utils.compute_pianoroll(note_info, time_unit='auto', time_div='auto', onset_only=False, note_separation=False, pitch_margin=-1, time_margin=0, return_idxs=False, piano_range=False, remove_drums=True, remove_silence=True, end_time=None)[source]¶
Computes a piano roll from a structured note array (as generated by the note_array methods in partitura.score.Part and partitura.performance.PerformedPart instances).
- Parameters
note_info (structured array, Part, PartGroup, PerformedPart) – Note information
time_unit (('auto', 'beat', 'quarter', 'div', 'second')) –
time_div (int, optional) – How many sub-divisions for each time unit (beats for a score or seconds for a performance. See is_performance below).
onset_only (bool, optional) – If True, code only the onsets of the notes, otherwise code onset and duration.
pitch_margin (int, optional) – If pitch_margin > -1, the resulting array will have pitch_margin empty rows above and below the highest and lowest pitches, respectively; if pitch_margin == -1, the resulting pianoroll will have span the fixed pitch range between (and including) 1 and 127.
time_margin (int, optional) – The resulting array will have time_margin * time_div empty columns before and after the piano roll
return_idxs (bool, optional) – If True, return the indices (i.e., the coordinates) of each note in the piano roll.
piano_range (bool, optional) – If True, the pitch axis of the piano roll is in piano keys instead of MIDI note numbers (and there are only 88 pitches). This is equivalent as slicing piano_range_pianoroll = pianoroll[21:109, :].
remove_drums (bool, optional) – If True, removes the drum track (i.e., channel 9) from the notes to be considered in the piano roll. This option is only relevant for piano rolls generated from a PerformedPart. Default is True.
remove_silence (bool, optional) – If True, the first frame of the pianoroll starts at the onset of the first note, not at time 0 of the timeline.
end_time (int, optional) – The time corresponding to the ending of the last pianoroll frame (in time_unit). If None this is set to the last note offset.
- Returns
pianoroll (scipy.sparse.csr_matrix) – A sparse int matrix of size representing the pianoroll; The first dimension is pitch, the second is time; The sizes of the dimensions vary with the parameters pitch_margin, time_margin, time_div, remove silence, and end_time.
pr_idx (ndarray) – Indices of the onsets and offsets of the notes in the piano roll (in the same order as the input note_array). This is only returned if return_idxs is True.
Examples
>>> import numpy as np >>> from partitura.utils import compute_pianoroll >>> note_array = np.array([(60, 0, 1)], dtype=[('pitch', 'i4'), ('onset_beat', 'f4'), ('duration_beat', 'f4')]) >>> pr = compute_pianoroll(note_array, pitch_margin=2, time_div=2) >>> pr.toarray() array([[0, 0], [0, 0], [1, 1], [0, 0], [0, 0]])
Notes
The default values in this function assume that the input note_array represents a score.
- partitura.utils.ensure_notearray(notearray_or_part, *args, **kwargs)[source]¶
Ensures to get a structured note array from the input.
- Parameters
notearray_or_part (structured ndarray, Score, Part, PerformedPart) – Input score information
kwargs (dict) – Additional arguments to be passed to partitura.utils.note_array_from_part().
- Returns
Structured array containing score information.
- Return type
structured ndarray
- partitura.utils.ensure_rest_array(restarray_or_part, *args, **kwargs)[source]¶
Ensures to get a structured note array from the input.
- Parameters
restarray_or_part (structured ndarray, Part or PerformedPart) – Input score information
- Returns
Structured array containing score information.
- Return type
structured ndarray
- partitura.utils.fifths_mode_to_key_name(fifths, mode=None)[source]¶
Return the key signature name corresponding to a number of sharps or flats and a mode. A negative value for fifths denotes the number of flats (i.e. -3 means three flats), and a positive number the number of sharps. The mode is specified as ‘major’ or ‘minor’. If mode is None, the key is assumed to be major.
- Parameters
fifths (int) – Number of fifths
mode ({'major', 'minor', None, -1, 1}) – Mode of the key signature
- Returns
The name of the key signature, e.g. ‘Am’
- Return type
str
Examples
>>> fifths_mode_to_key_name(0, 'minor') 'Am' >>> fifths_mode_to_key_name(0, 'major') 'C' >>> fifths_mode_to_key_name(3, 'major') 'A' >>> fifths_mode_to_key_name(-1, 1) 'F'
- partitura.utils.key_mode_to_int(mode)[source]¶
Return the mode of a key as an integer (1 for major and -1 for minor).
- Parameters
mode ({'major', 'minor', None, 1, -1}) – Mode of the key
- Returns
Integer representation of the mode.
- Return type
int
- partitura.utils.key_name_to_fifths_mode(key_name)[source]¶
Return the number of sharps or flats and the mode of a key signature name. A negative number denotes the number of flats (i.e. -3 means three flats), and a positive number the number of sharps. The mode is specified as ‘major’ or ‘minor’.
- Parameters
name (str) – Name of the key signature, i.e. Am, E#, etc
- Returns
Tuple containing the number of fifths and the mode
- Return type
(int, str)
Examples
>>> key_name_to_fifths_mode('Am') (0, 'minor') >>> key_name_to_fifths_mode('C') (0, 'major') >>> key_name_to_fifths_mode('A') (3, 'major')
- partitura.utils.pianoroll_to_notearray(pianoroll, time_div=8, time_unit='sec')[source]¶
Extract a structured note array from a piano roll.
For now, the structured note array is considered a “performance”.
- Parameters
pianoroll (array-like) – 2D array containing a piano roll. The first dimension is pitch, and the second is time. The value of each “pixel” in the piano roll is considered to be the MIDI velocity, and it is supposed to be between 0 and 127.
time_div (int) – How many sub-divisions for each time unit (see notearray_to_pianoroll).
time_unit ({'beat', 'quarter', 'div', 'sec'}) – time unit of the output note array.
- Returns
Structured array with pitch, onset, duration, velocity and note id fields.
- Return type
np.ndarray
Notes
Please note that all non-zero pixels will contribute to a note. For the case of piano rolls with continuous values between 0 and 1 (as might be the case of those piano rolls produced using probabilistic/generative models), we recomend to either 1) hard- threshold the piano roll to have only 0s (note-off) or 1s (note- on) or, 2) soft-threshold the notes (values below a certain threshold are considered as not active and scale the active notes to lie between 1 and 127).
- partitura.utils.show_diff(a, b)[source]¶
Show the difference between two strings, using the difflib package. The difference is printed to stdout.
- Parameters
a (str) – First string
b (str) – Second string
- partitura.utils.slice_notearray_by_time(note_array, start_time, end_time, time_unit='auto', clip_onset_duration=True)[source]¶
Get a slice of a structured note array by time
- Parameters
note_array (structured array) – Structured array with score information.
start_time (float) – Starting time
end_time (float) – End time
time_unit ({'auto', 'beat', 'quarter', 'second', 'div'} optional) – Time unit. If ‘auto’, the default time unit will be inferred from the note_array.
clip_onset_duration (bool optional) – Clip duration of the notes in the array to fit within the specified window
- Returns
note_array_slice – Structured array with only the score information between start_time and end_time.
- Return type
stuctured array