Multitrack Class

Pypianoroll provides the pypianoroll.Multitrack class for handling multitrack piano rolls.

class pypianoroll.Multitrack(name: Optional[str] = None, resolution: Optional[int] = None, tempo: Optional[numpy.ndarray] = None, downbeat: Optional[numpy.ndarray] = None, tracks: Optional[Sequence[pypianoroll.track.Track]] = None)[source]

A container for multitrack piano rolls.

This is the core class of Pypianoroll.

name

Multitrack name.

Type

str, optional

resolution

Time steps per quarter note.

Type

int

tempo

Tempo (in qpm) at each time step. Length is the total number of time steps. Cast to float if not of data type float.

Type

ndarray, dtype=float, shape=(?, 1), optional

downbeat

Boolean array that indicates whether the time step contains a downbeat (i.e., the first time step of a bar). Length is the total number of time steps.

Type

ndarray, dtype=bool, shape=(?, 1), optional

tracks

Music tracks.

Type

sequence of pypianoroll.Track, optional

append(track: pypianoroll.track.Track) → _Multitrack[source]

Append a Track object to the track list.

Parameters

track (pypianoroll.Track) – Track to append.

Returns

Return type

Object itself.

binarize(threshold: float = 0) → _Multitrack[source]

Binarize the piano rolls.

Parameters

threshold (int or float) – Threshold to binarize the piano rolls. Defaults to zero.

Returns

Return type

Object itself.

blend(mode: Optional[str] = None)numpy.ndarray[source]

Return the blended pianoroll.

Parameters

mode ({'sum', 'max', 'any'}, optional) – Blending strategy to apply along the track axis. For ‘sum’ mode, integer summation is performed for binary piano rolls. Defaults to ‘sum’.

Returns

Blended piano roll.

Return type

ndarray, shape=(?, 128)

clip(lower: int = 0, upper: int = 127) → _Multitrack[source]

Clip (limit) the the piano roll into [lower, upper].

Parameters
  • lower (int) – Lower bound. Defaults to 0.

  • upper (int) – Upper bound. Defaults to 127.

Returns

Return type

Object itself.

Note

Only affect StandardTrack instances.

copy()[source]

Return a copy of the multitrack.

Returns

Return type

A copy of the object itself.

Notes

Arrays are copied using numpy.copy().

count_downbeat() → int[source]

Return the number of downbeats.

Returns

Number of downbeats.

Return type

int

Note

Return value is calculated based only on the attribute downbeat.

get_downbeat_steps()numpy.ndarray[source]

Return the indices of time steps that contain downbeats.

Returns

Indices of time steps that contain downbeats.

Return type

ndarray, dtype=int

get_length() → int[source]

Return the maximum active length of the piano rolls.

Returns

Maximum active length (in time steps) of the piano rolls, where active length is the length of the piano roll without trailing silence.

Return type

int

get_max_length() → int[source]

Return the maximum length of the piano rolls.

Returns

Maximum length (in time steps) of the piano rolls.

Return type

int

is_valid(attr: Optional[str] = None) → bool[source]

Return True if an attribute is valid.

Parameters

attr (str) – Attribute to validate. Defaults to validate all attributes.

Returns

Whether the attribute has a valid type and value.

Return type

bool

is_valid_type(attr: Optional[str] = None) → bool[source]

Return True if an attribute is of a valid type.

Parameters

attr (str) – Attribute to validate. Defaults to validate all attributes.

Returns

Whether the attribute is of a valid type.

Return type

bool

pad(pad_length) → _Multitrack[source]

Pad the piano rolls.

Notes

The lengths of the resulting piano rolls are not guaranteed to be the same.

Parameters

pad_length (int) – Length to pad along the time axis.

Returns

Return type

Object itself.

See also

pypianoroll.Multitrack.pad_to_multiple()

Pad the piano rolls so that their lengths are some multiples.

pypianoroll.Multitrack.pad_to_same()

Pad the piano rolls so that they have the same length.

pad_to_multiple(factor: int) → _Multitrack[source]

Pad the piano rolls so that their lengths are some multiples.

Pad the piano rolls at the end along the time axis of the minimum length that makes the lengths of the resulting piano rolls multiples of factor.

Parameters

factor (int) – The value which the length of the resulting piano rolls will be a multiple of.

Returns

Return type

Object itself.

Notes

Lengths of the resulting piano rolls are necessarily the same.

See also

pypianoroll.Multitrack.pad()

Pad the piano rolls.

pypianoroll.Multitrack.pad_to_same()

Pad the piano rolls so that they have the same length.

pad_to_same() → _Multitrack[source]

Pad the piano rolls so that they have the same length.

Pad shorter piano rolls at the end along the time axis so that the resulting piano rolls have the same length.

Returns

Return type

Object itself.

See also

pypianoroll.Multitrack.pad()

Pad the piano rolls.

pypianoroll.Multitrack.pad_to_multiple()

Pad the piano rolls so that their lengths are some multiples.

plot(axs: Optional[Sequence[matplotlib.axes._axes.Axes]] = None, **kwargs)numpy.ndarray[source]

Plot the multitrack piano roll.

Refer to pypianoroll.plot_multitrack() for full documentation.

remove_empty() → _Multitrack[source]

Remove tracks with empty pianorolls.

save(path: str, compressed: bool = True)[source]

Save to a NPZ file.

Refer to pypianoroll.save() for full documentation.

set_nonzeros(value: int) → _Multitrack[source]

Assign a constant value to all nonzero entries.

Parameters

value (int) – Value to assign.

Returns

Return type

Object itself.

set_resolution(resolution: int, rounding: Optional[str] = 'round') → _Multitrack[source]

Set the resolution.

Parameters
  • resolution (int) – Target resolution.

  • rounding ({'round', 'ceil', 'floor'}) – Rounding mode. Defaults to ‘round’.

Returns

Return type

Object itself.

stack()numpy.ndarray[source]

Return the piano rolls stacked as a 3D tensor.

Returns

Stacked piano roll, provided as (track, time, pitch).

Return type

ndarray, shape=(?, ?, 128)

to_pretty_midi(**kwargs)[source]

Return as a PrettyMIDI object.

Refer to pypianoroll.to_pretty_midi() for full documentation.

transpose(semitone: int) → _Multitrack[source]

Transpose the piano rolls by a number of semitones.

Parameters

semitone (int) – Number of semitones to transpose. A positive value raises the pitches, while a negative value lowers the pitches.

Returns

Return type

Object itself.

Notes

Drum tracks are skipped.

trim(start: Optional[int] = None, end: Optional[int] = None) → _Multitrack[source]

Trim the trailing silences of the piano rolls.

Parameters
  • start (int, optional) – Start time. Defaults to 0.

  • end (int, optional) – End time. Defaults to active length.

Returns

Return type

Object itself.

validate(attr=None) → _Multitrack[source]

Raise an error if an attribute has an invalid type or value.

Parameters

attr (str) – Attribute to validate. Defaults to validate all attributes.

Returns

Return type

Object itself.

validate_type(attr=None)[source]

Raise an error if an attribute has an invalid type.

Parameters

attr (str) – Attribute to validate. Defaults to validate all attributes.

Returns

Return type

Object itself.

write(path: str)[source]

Write to a MIDI file.

Refer to pypianoroll.write() for full documentation.