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 ({"A", "A#m", "Ab", "Abm", "Am", "B", "Bb", "Bbm", "Bm", "C","C#", "C#m", "Cb", "Cm", "D", "D#m", "Db", "Dm", "E", "Eb","Ebm", "Em", "F", "F#", "F#m", "Fm", "G", "G#m", "Gb", "Gm"}) – Name of the key signature
Returns:Tuple containing the number of fifths and the mode
Return type:(int, str)


>>> 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.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.

  • fifths (int) – Number of fifths
  • mode ({'major', 'minor', None, -1, 1}) – Mode of the key signature

The name of the key signature, e.g. ‘Am’

Return type:



>>> fifths_mode_to_key_name(0, 'minor')
>>> fifths_mode_to_key_name(0, 'major')
>>> fifths_mode_to_key_name(3, 'major')
>>> fifths_mode_to_key_name(-1, 1)

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.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)[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).

  • 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.

  • 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, and time_div
  • 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.


>>> 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]])


The default values in this function assume that the input note_array represents a score.

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”.

  • 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.

Structured array with pitch, onset, duration and velocity fields.

Return type:



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).