Read & Write

Pypianoroll currently supports reading from and writing to MIDI files.

Functions

pypianoroll.read(path: Union[str, pathlib.Path], **kwargs) pypianoroll.multitrack.Multitrack[source]

Read a MIDI file into a Multitrack object.

Parameters

See also

pypianoroll.write()

Write a Multitrack object to a MIDI file.

pypianoroll.load()

Load a NPZ file into a Multitrack object.

pypianoroll.write(path: str, multitrack: Multitrack)[source]

Write a Multitrack object to a MIDI file.

Parameters

See also

pypianoroll.read()

Read a MIDI file into a Multitrack object.

pypianoroll.save()

Save a Multitrack object to a NPZ file.

pypianoroll.from_pretty_midi(midi: pretty_midi.pretty_midi.PrettyMIDI, resolution: int = 24, mode: str = 'max', algorithm: str = 'normal', collect_onsets_only: bool = False, first_beat_time: Optional[float] = None) pypianoroll.multitrack.Multitrack[source]

Return a Multitrack object converted from a PrettyMIDI object.

Parse a pretty_midi.PrettyMIDI object. The data type of the resulting piano rolls is automatically determined (int if ‘mode’ is ‘sum’ and np.uint8 if mode is ‘max’).

Parameters
  • midi (pretty_midi.PrettyMIDI) – PrettyMIDI object to parse.

  • mode ({'max', 'sum'}, default: 'max') – Merging strategy for duplicate notes.

  • algorithm ({'normal', 'strict', 'custom'}, default: 'normal') – Algorithm for finding the location of the first beat (see Notes).

  • collect_onsets_only (bool, default: False) – True to collect only the onset of the notes (i.e. note on events) in all tracks, where the note off and duration information are discarded. False to parse regular piano rolls.

  • first_beat_time (float, optional) – Location of the first beat, in sec. Required and only effective when using ‘custom’ algorithm.

Returns

Converted Multitrack object.

Return type

pypianoroll.Multitrack

Notes

There are three algorithms for finding the location of the first beat:

  • ‘normal’ : Estimate the location of the first beat using pretty_midi.PrettyMIDI.estimate_beat_start().

  • ‘strict’ : Set the location of the first beat to the time of the first time signature change. Raise a RuntimeError if no time signature change is found.

  • ‘custom’ : Set the location of the first beat to the value of argument first_beat_time. Raise a ValueError if first_beat_time is not given.

If an incomplete beat before the first beat is found, an additional beat will be added before the (estimated) beat starting time. However, notes before the (estimated) beat starting time for more than one beat are dropped.

pypianoroll.to_pretty_midi(multitrack: Multitrack, default_tempo: float = None, default_velocity: int = 64) pretty_midi.pretty_midi.PrettyMIDI[source]

Return a Multitrack object as a PrettyMIDI object.

Parameters
  • default_tempo (int, default: pypianoroll.DEFAULT_TEMPO (120)) – Default tempo to use. If attribute tempo is available, use its first element.

  • default_velocity (int, default: pypianoroll.DEFAULT_VELOCITY (64)) – Default velocity to assign to binarized tracks.

Returns

Converted PrettyMIDI object.

Return type

pretty_midi.PrettyMIDI

Notes

  • Tempo changes are not supported.

  • Time signature changes are not supported.

  • The velocities of the converted piano rolls will be clipped to [0, 127].

  • Adjacent nonzero values of the same pitch will be considered a single note with their mean as its velocity.

Note

Writing the tempo array and downbeat array to tempo change and time signature change events are not supported yet.