Track Class

The muspy.Track class is a container for music tracks. In MusPy, each track contains only one instrument.

Attributes Description Type Default
program MIDI program number int (0-127) 0
is_drum If it is a drum track bool False
name Track name str  
notes Musical notes list of muspy.Note []
chords Chords list of muspy.Chord []
lyrics Lyrics list of muspy.Lyric []
annotations Annotations list of muspy.Annotation []

(MIDI program number is based on General MIDI specification; see here.)

class muspy.Track(program: int = 0, is_drum: bool = False, name: str = None, notes: List[muspy.classes.Note] = None, chords: List[muspy.classes.Chord] = None, lyrics: List[muspy.classes.Lyric] = None, annotations: List[muspy.classes.Annotation] = None)[source]

A container for music track.

program

Program number, according to General MIDI specification [1]. Valid values are 0 to 127.

Type:int, default: 0 (Acoustic Grand Piano)
is_drum

Whether it is a percussion track.

Type:bool, default: False
name

Track name.

Type:str, optional
notes

Musical notes.

Type:list of muspy.Note, default: []
chords

Chords.

Type:list of muspy.Chord, default: []
annotations

Annotations.

Type:list of muspy.Annotation, default: []
lyrics

Lyrics.

Type:list of muspy.Lyric, default: []

Note

Indexing a Track object returns the note at a certain index. That is, track[idx] returns track.notes[idx]. Length of a Track object is the number of notes. That is, len(track) returns len(track.notes).

References

[1]https://www.midi.org/specifications/item/gm-level-1-sound-set
adjust_time(func: Callable[[int], int], attr: str = None, recursive: bool = True) → BaseType

Adjust the timing of time-stamped objects.

Parameters:
  • func (callable) – The function used to compute the new timing from the old timing, i.e., new_time = func(old_time).
  • attr (str, optional) – Attribute to adjust. Defaults to adjust all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
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.classes.Track[source]

Clip the velocity of each note.

Parameters:
  • lower (int, default: 0) – Lower bound.
  • upper (int, default: 127) – Upper bound.
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, call muspy.ComplexBase.append() for each item.
  • deepcopy (bool, default: False) – Whether to make deep copies of the appended objects.
Returns:

Return type:

Object itself.

fix_type(attr: str = None, recursive: bool = True) → BaseType

Fix the types of attributes.

Parameters:
  • attr (str, optional) – Attribute to adjust. Defaults to adjust all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
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:
  • dict (dict or mapping) – A dictionary that stores the attributes and their values as key-value pairs, e.g., {“attr1”: value1, “attr2”: value2}.
  • strict (bool, default: False) – Whether to raise errors for invalid input types.
  • cast (bool, default: False) – Whether to cast types.
Returns:

Return type:

Constructed object.

get_end_time(is_sorted: bool = False) → int[source]

Return the time of the last event.

This includes notes, chords, lyrics and annotations.

Parameters:is_sorted (bool, default: False) – Whether all the list attributes are sorted.
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:
  • attr (str, optional) – Attribute to validate. Defaults to validate all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
Returns:

Whether the attribute has a valid type and value.

Return type:

bool

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:
  • attr (str, optional) – Attribute to validate. Defaults to validate all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
Returns:

Whether the attribute is of a valid type.

Return type:

bool

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:
  • attr (str, optional) – Attribute to check. Defaults to check all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
Returns:

Return type:

Object itself.

remove_invalid(attr: str = None, recursive: bool = True) → ComplexBaseType

Remove invalid items from a list attribute.

Parameters:
  • attr (str, optional) – Attribute to validate. Defaults to validate all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
Returns:

Return type:

Object itself.

sort(attr: str = None, recursive: bool = True) → ComplexBaseType

Sort a list attribute.

Parameters:
  • attr (str, optional) – Attribute to sort. Defaults to sort all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
Returns:

Return type:

Object itself.

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:
  • skip_missing (bool, default: True) – Whether to skip attributes with value None or those that are empty lists.
  • deepcopy (bool, default: True) – Whether to make deep copies of the attributes.
Returns:

A dictionary that stores the attributes and their values as key-value pairs, e.g., {“attr1”: value1, “attr2”: value2}.

Return type:

OrderedDict

transpose(semitone: int) → muspy.classes.Track[source]

Transpose 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.
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:
  • attr (str, optional) – Attribute to validate. Defaults to validate all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
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:
  • attr (str, optional) – Attribute to validate. Defaults to validate all attributes.
  • recursive (bool, default: True) – Whether to apply recursively.
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.