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 |
||
resolution |
Time steps per beat |
int |
|
tempos |
Tempo changes |
list of |
[] |
key_signatures |
Key signature changes |
list of |
[] |
time_signatures |
Time signature changes |
list of |
[] |
barlines |
Barlines |
list of |
[] |
beats |
Beats |
list of |
[] |
lyrics |
Lyrics |
list of |
[] |
annotations |
Annotations |
list of |
[] |
tracks |
Music tracks |
list of |
[] |
real_time |
Whether times are in real time (seconds) |
bool |
False |
Hint
An example of a MusPy Music object as a YAML file is available here.
- class muspy.Music(metadata=None, resolution=None, tempos=None, key_signatures=None, time_signatures=None, barlines=None, beats=None, lyrics=None, annotations=None, tracks=None, real_time=False)[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 attributesmuspy.Music.from_dict(): Construct from a dictionary that stores the attributes and their values as key-value pairsmuspy.read(): Read from a MIDI, a MusicXML or an ABC filemuspy.load(): Load from a JSON or a YAML file saved bymuspy.save()muspy.from_object(): Convert from a music21.Stream,mido.MidiFile,pretty_midi.PrettyMIDIorpypianoroll.Multitrackobject
- 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: []
- barlines
Barlines.
- Type:
list of
muspy.Barline, 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: []
- real_time
Are the times of different objects in real time (seconds)?
- Type:
bool, default: False
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).- get_real_time(time)[source]
Return the given time in real time (seconds).
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:
time (int) – The time (in metrical time steps) to be converted into real time (seconds).
- get_end_time(is_sorted=False)[source]
Return the time of the last event across all the tracks.
This includes tempos, key signatures, time signatures, barlines, beats, lyrics, annotations, note offsets and chord offsets.
- Parameters:
is_sorted (bool, default: False) – Whether all the list attributes are sorted.
- get_real_end_time(is_sorted=False)[source]
Return the end time in real time (seconds).
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_barlines(overwrite=False)[source]
Infer barlines from the time signatures.
This assumes that there is a barline at each time signature change.
- Parameters:
overwrite (bool, default: False) – Whether to overwrite existing barlines.
- Return type:
Object itself.
- Raises:
ValueError – If no time signature is found.
- infer_barlines_and_beats(overwrite=False)[source]
Infer barlines and 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). Return an empty list if no time signature is found.
- Parameters:
overwrite (bool, default: False) – Whether to overwrite existing barlines or beats.
- Return type:
Object itself.
- Raises:
ValueError – If no time signature is found.
- adjust_resolution(target=None, factor=None, rounding='round')[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.
- Return type:
Object itself.
- clip(lower=0, upper=127)[source]
Clip the velocity of each note for each track.
- convert_to_real_time()[source]
Convert all times and durations in this object into real time (seconds). Returns a new
muspy.Musicobject.- Return type:
New
muspy.Musicobject with altered times and durations.
- realize_annotations()[source]
Realize all annotations through note velocities and durations. Returns a new
muspy.Musicobject.- Return type:
New
muspy.Musicobject with altered notes and chords (via their velocities and durations).
- transpose(semitone)[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.
- Return type:
Object itself.
Notes
Drum tracks are skipped.
- trim(end)[source]
Trim the track.
- Parameters:
end (int) – End time, excluding (i.e, the max time will be end - 1).
- Return type:
Object itself.
- save(path, kind=None, **kwargs)[source]
Save loselessly to a JSON or a YAML file.
Refer to
muspy.save()for full documentation.
- save_json(path, **kwargs)[source]
Save loselessly to a JSON file.
Refer to
muspy.save_json()for full documentation.
- save_yaml(path)[source]
Save loselessly to a YAML file.
Refer to
muspy.save_yaml()for full documentation.
- write(path, kind=None, **kwargs)[source]
Write to a MIDI, a MusicXML, an ABC or an audio file.
Refer to
muspy.write()for full documentation.
- write_midi(path, **kwargs)[source]
Write to a MIDI file.
Refer to
muspy.write_midi()for full documentation.
- write_musicxml(path, **kwargs)[source]
Write to a MusicXML file.
Refer to
muspy.write_musicxml()for full documentation.
- write_abc(path, **kwargs)[source]
Write to an ABC file.
Refer to
muspy.write_abc()for full documentation.
- write_audio(path, **kwargs)[source]
Write to an audio file.
Refer to
muspy.write_audio()for full documentation.
- to_object(kind, **kwargs)[source]
Return as an object in other libraries.
Refer to
muspy.to_object()for full documentation.
- to_music21(**kwargs)[source]
Return as a Stream object.
Refer to
muspy.to_music21()for full documentation.
- to_mido(**kwargs)[source]
Return as a MidiFile object.
Refer to
muspy.to_mido()for full documentation.
- to_pretty_midi(**kwargs)[source]
Return as a PrettyMIDI object.
Refer to
muspy.to_pretty_midi()for full documentation.
- to_pypianoroll(**kwargs)[source]
Return as a Multitrack object.
Refer to
muspy.to_pypianoroll()for full documentation.
- to_representation(kind, **kwargs)[source]
Return in a specific representation.
Refer to
muspy.to_representation()for full documentation.
- to_pitch_representation(**kwargs)[source]
Return in pitch-based representation.
Refer to
muspy.to_pitch_representation()for full documentation.
- to_pianoroll_representation(**kwargs)[source]
Return in piano-roll representation.
Refer to
muspy.to_pianoroll_representation()for full documentation.
- to_event_representation(**kwargs)[source]
Return in event-based representation.
Refer to
muspy.to_event_representation()for full documentation.
- to_note_representation(**kwargs)[source]
Return in note-based representation.
Refer to
muspy.to_note_representation()for full documentation.
- show(kind, **kwargs)[source]
Show visualization.
Refer to
muspy.show()for full documentation.
- show_score(**kwargs)[source]
Show score visualization.
Refer to
muspy.show_score()for full documentation.
- show_pianoroll(**kwargs)[source]
Show pianoroll visualization.
Refer to
muspy.show_pianoroll()for full documentation.
- synthesize(**kwargs)[source]
Synthesize a Music object to raw audio.
Refer to
muspy.synthesize()for full documentation.
- adjust_time(func, attr=None, recursive=True)
Adjust the timing of time-stamped objects.
- append(obj)
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.
- copy()
Return a shallow copy of the object.
This is equivalent to
copy.copy(self)().- Return type:
Shallow copy of the object.
- deepcopy()
Return a deep copy of the object.
This is equivalent to
copy.deepcopy(self)()- Return type:
Deep copy of the object.
- extend(other, deepcopy=False)
Extend the list(s) with another object or iterable.
- Parameters:
other (
muspy.ComplexBaseor 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.
- Return type:
Object itself.
- fix_type(attr=None, recursive=True)
Fix the types of attributes.
- classmethod from_dict(dict_, strict=False, cast=False)
Return an instance constructed from a dictionary.
Instantiate an object whose attributes and the corresponding values are given as a dictionary.
- Parameters:
- Return type:
Constructed object.
- is_valid(attr=None, recursive=True)
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=None, recursive=True)
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=True)
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:
See also
muspy.Base.print()Print the attributes in a YAML-like format.
- print(skip_missing=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=None, recursive=True)
Remove duplicate items from a list attribute.
- remove_invalid(attr=None, recursive=True)
Remove invalid items from a list attribute.
- sort(attr=None, recursive=True)
Sort a list attribute.
- to_ordered_dict(skip_missing=True, deepcopy=True)
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
- validate(attr=None, recursive=True)
Raise an error if an attribute has an invalid type or value.
This will apply recursively to an attribute’s attributes.
- Parameters:
- 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=None, recursive=True)
Raise an error if an attribute is of an invalid type.
This will apply recursively to an attribute’s attributes.
- Parameters:
- 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.