Music Class¶
The muspy.Music
class is the core element of MusPy. It is a universal container for symbolic music.
Attributes | Description | Type | Default |
---|---|---|---|
metadata | Metadata | muspy.Metadata |
muspy.Metadata() |
resolution | Time steps per beat | int | muspy.DEFAULT_RESOLUTION |
tempos | Tempo changes | list of muspy.Tempo |
[] |
key_signatures | Key signature changes | list of muspy.KeySignature |
[] |
time_signatures | Time signature changes | list of muspy.TimeSignature |
[] |
downbeats | Downbeat positions | list of int | [] |
lyrics | Lyrics | list of muspy.Lyric |
[] |
annotations | Annotations | list of muspy.Annotation |
[] |
tracks | Music tracks | list of muspy.Track |
[] |
Hint
An example of a MusPy Music object as a YAML file is available here.
-
class
muspy.
Music
(metadata: muspy.classes.Metadata = None, resolution: int = None, tempos: List[muspy.classes.Tempo] = None, key_signatures: List[muspy.classes.KeySignature] = None, time_signatures: List[muspy.classes.TimeSignature] = None, beats: List[muspy.classes.Beat] = None, lyrics: List[muspy.classes.Lyric] = None, annotations: List[muspy.classes.Annotation] = None, tracks: List[muspy.classes.Track] = None)[source] A universal container for symbolic music.
This is the core class of MusPy. A Music object can be constructed in the following ways.
muspy.Music()
: Construct by setting values for attributes.muspy.Music.from_dict()
: Construct from a dictionary that stores the attributes and their values as key-value pairs.muspy.read()
: Read from a MIDI, a MusicXML or an ABC file.muspy.load()
: Load from a JSON or a YAML file saved bymuspy.save()
.muspy.from_object()
: Convert from a music21.Stream, amido.MidiFile
, apretty_midi.PrettyMIDI
or apypianoroll.Multitrack
object.
-
metadata
¶ Metadata.
Type: muspy.Metadata
, default: Metadata()
-
resolution
¶ Time steps per quarter note.
Type: int, default: muspy.DEFAULT_RESOLUTION (24)
-
tempos
¶ Tempo changes.
Type: list of muspy.Tempo
, default: []
-
key_signatures
¶ Key signatures changes.
Type: list of muspy.KeySignature
, default: []
-
time_signatures
¶ Time signature changes.
Type: list of muspy.TimeSignature
, default: []
-
beats
¶ Beats.
Type: list of muspy.Beat
, default: []
-
lyrics
¶ Lyrics.
Type: list of muspy.Lyric
, default: []
-
annotations
¶ Annotations.
Type: list of muspy.Annotation
, default: []
-
tracks
¶ Music tracks.
Type: list of muspy.Track
, default: []
Note
Indexing a Music object returns the track of a certain index. That is,
music[idx]
returnsmusic.tracks[idx]
. Length of a Music object is the number of tracks. That is,len(music)
returnslen(music.tracks)
.-
adjust_resolution
(target: int = None, factor: float = None, rounding: Union[str, Callable] = 'round') → muspy.music.Music[source] Adjust resolution and timing of all time-stamped objects.
Parameters: - target (int, optional) – Target resolution.
- factor (int or float, optional) – Factor used to adjust the resolution based on the formula: new_resolution = old_resolution * factor. For example, a factor of 2 double the resolution, and a factor of 0.5 halve the resolution.
- rounding ({'round', 'ceil', 'floor'} or callable, default:) –
- 'round' – Rounding mode.
Returns: Return type: Object itself.
-
adjust_time
(func: Callable[[int], int], attr: str = None, recursive: bool = True) → BaseType Adjust the timing of time-stamped objects.
Parameters: Returns: Return type: Object itself.
-
append
(obj) → ComplexBaseType Append an object to the corresponding list.
This will automatically determine the list attributes to append based on the type of the object.
Parameters: obj – Object to append.
-
clip
(lower: int = 0, upper: int = 127) → muspy.music.Music[source] Clip the velocity of each note for each track.
Parameters: Returns: Return type: Object itself.
-
copy
() → BaseType Return a shallow copy of the object.
This is equivalent to
copy.copy(self)()
.Returns: Return type: Shallow copy of the object.
-
deepcopy
() → BaseType Return a deep copy of the object.
This is equivalent to
copy.deepcopy(self)()
Returns: Return type: Deep copy of the object.
-
extend
(other: Union[ComplexBaseType, Iterable[T_co]], deepcopy: bool = False) → ComplexBaseType Extend the list(s) with another object or iterable.
Parameters: - other (
muspy.ComplexBase
or iterable) – If an object of the same type is given, extend the list attributes with the corresponding list attributes of the other object. If an iterable is given, callmuspy.ComplexBase.append()
for each item. - deepcopy (bool, default: False) – Whether to make deep copies of the appended objects.
Returns: Return type: Object itself.
- other (
-
fix_type
(attr: str = None, recursive: bool = True) → BaseType Fix the types of attributes.
Parameters: Returns: Return type: Object itself.
-
classmethod
from_dict
(dict_: Mapping[KT, VT_co], strict: bool = False, cast: bool = False) → BaseType Return an instance constructed from a dictionary.
Instantiate an object whose attributes and the corresponding values are given as a dictionary.
Parameters: Returns: Return type: Constructed object.
-
get_end_time
(is_sorted: bool = False) → int[source] Return the the time of the last event in all tracks.
This includes tempos, key signatures, time signatures, note offsets, lyrics and annotations.
Parameters: is_sorted (bool, default: False) – Whether all the list attributes are sorted.
-
get_real_end_time
(is_sorted: bool = False) → float[source] Return the end time in realtime.
This includes tempos, key signatures, time signatures, note offsets, lyrics and annotations. Assume 120 qpm (quarter notes per minute) if no tempo information is available.
Parameters: is_sorted (bool, default: False) – Whether all the list attributes are sorted.
-
infer_beats
() → List[muspy.classes.Beat][source] Infer beats from the time signature changes.
This assumes that there is a downbeat at each time signature change (this is not always true, e.g., for a pickup measure).
Returns: List of beats inferred from the time signature changes. Return an empty list if no time signature is found. Return type: list of muspy.Beat
-
is_valid
(attr: str = None, recursive: bool = True) → bool Return True if an attribute has a valid type and value.
This will recursively apply to an attribute’s attributes.
Parameters: Returns: Whether the attribute has a valid type and value.
Return type: See also
muspy.Base.validate()
- Raise an error if an attribute has an invalid type or value.
muspy.Base.is_valid_type()
- Return True if an attribute is of a valid type.
-
is_valid_type
(attr: str = None, recursive: bool = True) → bool Return True if an attribute is of a valid type.
This will apply recursively to an attribute’s attributes.
Parameters: Returns: Whether the attribute is of a valid type.
Return type: See also
muspy.Base.validate_type()
- Raise an error if a certain attribute is of an invalid type.
muspy.Base.is_valid()
- Return True if an attribute has a valid type and value.
-
pretty_str
(skip_missing: bool = True) → str Return the attributes as a string in a YAML-like format.
Parameters: skip_missing (bool, default: True) – Whether to skip attributes with value None or those that are empty lists. Returns: Stored data as a string in a YAML-like format. Return type: str See also
muspy.Base.print()
- Print the attributes in a YAML-like format.
-
print
(skip_missing: bool = True) Print the attributes in a YAML-like format.
Parameters: skip_missing (bool, default: True) – Whether to skip attributes with value None or those that are empty lists. See also
muspy.Base.pretty_str()
- Return the the attributes as a string in a YAML-like format.
-
remove_duplicate
(attr: str = None, recursive: bool = True) → ComplexBaseType Remove duplicate items from a list attribute.
Parameters: Returns: Return type: Object itself.
-
remove_invalid
(attr: str = None, recursive: bool = True) → ComplexBaseType Remove invalid items from a list attribute.
Parameters: Returns: Return type: Object itself.
-
save
(path: Union[str, pathlib.Path], kind: str = None, **kwargs)[source] Save loselessly to a JSON or a YAML file.
Refer to
muspy.save()
for full documentation.
-
save_json
(path: Union[str, pathlib.Path], **kwargs)[source] Save loselessly to a JSON file.
Refer to
muspy.save_json()
for full documentation.
-
save_yaml
(path: Union[str, pathlib.Path])[source] Save loselessly to a YAML file.
Refer to
muspy.save_yaml()
for full documentation.
-
show
(kind: str, **kwargs)[source] Show visualization.
Refer to
muspy.show()
for full documentation.
-
show_pianoroll
(**kwargs)[source] Show pianoroll visualization.
Refer to
muspy.show_pianoroll()
for full documentation.
-
show_score
(**kwargs)[source] Show score visualization.
Refer to
muspy.show_score()
for full documentation.
-
sort
(attr: str = None, recursive: bool = True) → ComplexBaseType Sort a list attribute.
Parameters: Returns: Return type: Object itself.
-
synthesize
(**kwargs) → numpy.ndarray[source] Synthesize a Music object to raw audio.
Refer to
muspy.synthesize()
for full documentation.
-
to_event_representation
(**kwargs) → numpy.ndarray[source] Return in event-based representation.
Refer to
muspy.to_event_representation()
for full documentation.
-
to_mido
(**kwargs) → mido.midifiles.midifiles.MidiFile[source] Return as a MidiFile object.
Refer to
muspy.to_mido()
for full documentation.
-
to_music21
(**kwargs) → music21.stream.base.Stream[source] Return as a Stream object.
Refer to
muspy.to_music21()
for full documentation.
-
to_note_representation
(**kwargs) → numpy.ndarray[source] Return in note-based representation.
Refer to
muspy.to_note_representation()
for full documentation.
-
to_object
(kind: str, **kwargs)[source] Return as an object in other libraries.
Refer to
muspy.to_object()
for full documentation.
-
to_ordered_dict
(skip_missing: bool = True, deepcopy: bool = True) → collections.OrderedDict Return the object as an OrderedDict.
Return an ordered dictionary that stores the attributes and their values as key-value pairs.
Parameters: Returns: A dictionary that stores the attributes and their values as key-value pairs, e.g., {“attr1”: value1, “attr2”: value2}.
Return type: OrderedDict
-
to_pianoroll_representation
(**kwargs) → numpy.ndarray[source] Return in piano-roll representation.
Refer to
muspy.to_pianoroll_representation()
for full documentation.
-
to_pitch_representation
(**kwargs) → numpy.ndarray[source] Return in pitch-based representation.
Refer to
muspy.to_pitch_representation()
for full documentation.
-
to_pretty_midi
(**kwargs) → pretty_midi.pretty_midi.PrettyMIDI[source] Return as a PrettyMIDI object.
Refer to
muspy.to_pretty_midi()
for full documentation.
-
to_pypianoroll
(**kwargs) → pypianoroll.multitrack.Multitrack[source] Return as a Multitrack object.
Refer to
muspy.to_pypianoroll()
for full documentation.
-
to_representation
(kind: str, **kwargs) → numpy.ndarray[source] Return in a specific representation.
Refer to
muspy.to_representation()
for full documentation.
-
transpose
(semitone: int) → muspy.music.Music[source] Transpose all the notes by a number of semitones.
Parameters: semitone (int) – Number of semitones to transpose the notes. A positive value raises the pitches, while a negative value lowers the pitches. Returns: Return type: Object itself. Notes
Drum tracks are skipped.
-
validate
(attr: str = None, recursive: bool = True) → BaseType Raise an error if an attribute has an invalid type or value.
This will apply recursively to an attribute’s attributes.
Parameters: Returns: Return type: Object itself.
See also
muspy.Base.is_valid()
- Return True if an attribute has a valid type and value.
muspy.Base.validate_type()
- Raise an error if an attribute is of an invalid type.
-
validate_type
(attr: str = None, recursive: bool = True) → BaseType Raise an error if an attribute is of an invalid type.
This will apply recursively to an attribute’s attributes.
Parameters: Returns: Return type: Object itself.
See also
muspy.Base.is_valid_type()
- Return True if an attribute is of a valid type.
muspy.Base.validate()
- Raise an error if an attribute has an invalid type or value.
-
write
(path: Union[str, pathlib.Path], kind: str = None, **kwargs)[source] Write to a MIDI, a MusicXML, an ABC or an audio file.
Refer to
muspy.write()
for full documentation.
-
write_abc
(path: Union[str, pathlib.Path], **kwargs)[source] Write to an ABC file.
Refer to
muspy.write_abc()
for full documentation.
-
write_audio
(path: Union[str, pathlib.Path], **kwargs)[source] Write to an audio file.
Refer to
muspy.write_audio()
for full documentation.
-
write_midi
(path: Union[str, pathlib.Path], **kwargs)[source] Write to a MIDI file.
Refer to
muspy.write_midi()
for full documentation.
-
write_musicxml
(path: Union[str, pathlib.Path], **kwargs)[source] Write to a MusicXML file.
Refer to
muspy.write_musicxml()
for full documentation.