Piano-roll Representation

muspy.to_pianoroll_representation(music: Music, encode_velocity: bool = True, dtype: Union[numpy.dtype, type, str] = None) → numpy.ndarray[source]

Encode notes into piano-roll representation.

Parameters:
  • music (muspy.Music) – Music object to encode.
  • encode_velocity (bool, default: True) – Whether to encode velocities. If True, a binary-valued array will be return. Otherwise, an integer array will be return.
  • dtype (np.dtype, type or str, optional) – Data type of the return array. Defaults to uint8 if encode_velocity is True, otherwise bool.
Returns:

Encoded array in piano-roll representation.

Return type:

ndarray, shape=(?, 128)

muspy.from_pianoroll_representation(array: numpy.ndarray, resolution: int = 24, program: int = 0, is_drum: bool = False, encode_velocity: bool = True, default_velocity: int = 64) → muspy.music.Music[source]

Decode piano-roll representation into a Music object.

Parameters:
  • array (ndarray) – Array in piano-roll representation to decode.
  • resolution (int, default: muspy.DEFAULT_RESOLUTION (24)) – Time steps per quarter note.
  • program (int, default: 0 (Acoustic Grand Piano)) – Program number, according to General MIDI specification [1]. Valid values are 0 to 127.
  • is_drum (bool, default: False) – Whether it is a percussion track.
  • encode_velocity (bool, default: True) – Whether to encode velocities.
  • default_velocity (int, default: muspy.DEFAULT_VELOCITY (64)) – Default velocity value to use when decoding. Only used when encode_velocity is True.
Returns:

Decoded Music object.

Return type:

muspy.Music

References

[1] https://www.midi.org/specifications/item/gm-level-1-sound-set

class muspy.PianoRollRepresentationProcessor(encode_velocity: bool = True, default_velocity: int = 64)[source]

Piano-roll representation processor.

The piano-roll represetantion represents music as a time-pitch matrix, where the columns are the time steps and the rows are the pitches. The values indicate the presence of pitches at different time steps. The output shape is T x 128, where T is the number of time steps.

encode_velocity

Whether to encode velocities. If True, a binary-valued array will be return. Otherwise, an integer array will be return.

Type:bool, default: True
default_velocity

Default velocity value to use when decoding if encode_velocity is False.

Type:int, default: 64
decode(array: numpy.ndarray) → muspy.music.Music[source]

Decode piano-roll representation into a Music object.

Parameters:array (ndarray) – Array in piano-roll representation to decode. Cast to integer if not of integer type. If encode_velocity is True, casted to boolean if not of boolean type.
Returns:Decoded Music object.
Return type:muspy.Music object

See also

muspy.from_pianoroll_representation()
Return a Music object converted from piano-roll representation.
encode(music: muspy.music.Music) → numpy.ndarray[source]

Encode a Music object into piano-roll representation.

Parameters:music (muspy.Music object) – Music object to encode.
Returns:Encoded array in piano-roll representation.
Return type:ndarray (np.uint8)

See also

muspy.to_pianoroll_representation()
Convert a Music object into piano-roll representation.