Pitch-based Representation

muspy.to_pitch_representation(music: Music, use_hold_state: bool = False, dtype: Union[numpy.dtype, type, str] = <class 'int'>) → numpy.ndarray[source]

Encode a Music object into pitch-based representation.

The pitch-based represetantion represents music as a sequence of pitch, rest and (optional) hold tokens. Only monophonic melodies are compatible with this representation. The output shape is T x 1, where T is the number of time steps. The values indicate whether the current time step is a pitch (0-127), a rest (128) or, optionally, a hold (129).

Parameters:
  • music (muspy.Music) – Music object to encode.
  • use_hold_state (bool, default: False) – Whether to use a special state for holds.
  • dtype (np.dtype, type or str, default: int) – Data type of the return array.
Returns:

Encoded array in pitch-based representation.

Return type:

ndarray, shape=(?, 1)

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

Decode pitch-based representation into a Music object.

Parameters:
  • array (ndarray) – Array in pitch-based 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.
  • use_hold_state (bool, default: False) – Whether to use a special state for holds.
  • default_velocity (int, default: muspy.DEFAULT_VELOCITY (64)) – Default velocity value to use when decoding.
Returns:

Decoded Music object.

Return type:

muspy.Music

References

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

class muspy.PitchRepresentationProcessor(use_hold_state: bool = False, default_velocity: int = 64)[source]

Pitch-based representation processor.

The pitch-based represetantion represents music as a sequence of pitch, rest and (optional) hold tokens. Only monophonic melodies are compatible with this representation. The output shape is T x 1, where T is the number of time steps. The values indicate whether the current time step is a pitch (0-127), a rest (128) or, optionally, a hold (129).

use_hold_state

Whether to use a special state for holds.

Type:bool, default: False
default_velocity

Default velocity value to use when decoding.

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

Decode pitch-based representation into a Music object.

Parameters:array (ndarray) – Array in pitch-based representation to decode. Cast to integer if not of integer type.
Returns:Decoded Music object.
Return type:muspy.Music object

See also

muspy.from_pitch_representation()
Return a Music object converted from pitch-based representation.
encode(music: muspy.music.Music) → numpy.ndarray[source]

Encode a Music object into pitch-based representation.

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

See also

muspy.to_pitch_representation()
Convert a Music object into pitch-based representation.