musicscore.midi

class musicscore.midi.Midi(value: float | int, accidental: Accidental | None = None, *args, **kwargs)[source]

Bases: MusicTree

Parent type: Note

Child type: Accidental

Midi is the representation of a Pitch with its midi value, and accidental sign. This object is used to create a Chord consisting of one or more pitches. The midi representation of a rest is a Midi object with value 0.

add_child(child: [<class 'musicscore.accidental.Accidental'>]) Accidental[source]

child’s _update method will be called after adding.

Parameters:

child

Returns:

child

Return type:

Accidental

add_tie(type: str) None[source]

A XMLTie child of type start or stop will be added to parent_note object

Parameters:

typestart, stop

Exception:

ValueError

classmethod create_tree_from_list(tree_list_representation: list[list[Any]], represented_attribute_names: list[str]) T
filter_nodes(key: Callable[[T], Any], return_value: Any) list[T]

Tree method

>>> root.filter_nodes(lambda node: node.get_level(), 2)
[grandchild1, grandchild2, grandchild3]
get_beat(*args, **kwargs) Beat

MusicTree method

This method can be used for Score and Part, Measure and Staff and Voice

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number, measure_number, staff_number, voice_number, beat_number depending on musicscore’s class. A Staff for example needs voice_number and beat_number while a Score needs all keyword arguments.

Return type:

Beat

get_beats() List[Beat]

MusicTree method

This method can be used for Score and Part, Measure, Staff and Voice.

Returns:

a flat list of all beats.

Return type:

List[Beat]

get_children() list[T]

Tree method

Returns:

list of added children.

get_children_of_type(type_: type) list[T]

Tree method

Returns:

list of added children of type.d

Return type:

list[Tree]

get_chord(*args, **kwargs) Chord

MusicTree method

This method can be used for Score and Part, Measure, Staff, Voice and Beat

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number, measure_number, staff_number, voice_number, beat_number, chord_number depending on musicscore’s class. A Staff for example needs voice_number, beat_number and chord_number while a Score needs all keyword arguments.

Return type:

Chord

get_chords() List[Chord]

MusicTree method

This method can be used for Score and Part, Measure and Staff, Voice and Beat

Returns:

a flat list of all chords.

Return type:

List[Chord]

get_distance(reference: T | None = None) int
>>> root.get_distance()
0
>>> greatgrandchild1.get_distance()
3
>>> greatgrandchild1.get_distance(child2)
2
get_farthest_leaf() T
>>> root.get_farthest_leaf()
greatgrandchild1
get_layer(level: int, key: Callable[[T], Any] | None = None) Any

Tree method

Parameters:
  • level – layer number where 0 is the root.

  • key – An optional callable for each node in the layer.

Returns:

All nodes on this level. The leaves of branches which are shorter than the given level will be repeated on this and all following layers.

Return type:

list

get_leaves(key: Callable[[T], Any] | None = None) list[Union[Any, list[Any]]]

Tree method

Parameters:

key – An optional callable to be called on each leaf.

Returns:

nested list of leaves or values of key(leaf) for each leaf

>>> root.get_leaves()
[child1, [[greatgrandchild1, greatgrandchild2], grandchild2], child3, [grandchild3]]
get_level() int

Tree

Returns:

0 for root, 1, 2 etc. for each layer of children

Return type:

nonnegative int

>>> root.get_level()
0
>>> child1.get_level()
1
>>> grandchild1.get_level()
2
>>> greatgrandchild1.get_level()
3
get_list_representation(key: Callable[[Tree[T]], Any]) list[list[Any]]
get_measure(*args, **kwargs) Measure

MusicTree method

This method can be used for Score and Part

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number, measure_number depending on musicscore’s class.

Return type:

Measure

get_number_of_layers() int
>>> root.get_number_of_layers()
3
get_parent() T | None

Tree method

Returns:

parent. None for root.

Return type:

Tree

get_part(*args, **kwargs) Part

MusicTree method

This method can be used for Score

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number.

Return type:

Part

get_pitch_or_rest() XMLPitch | XMLRest[source]
Returns:

XMLPitch or XMLRest object associated with this Midi.

get_position_in_tree() str

Tree method

Returns:

0 for root. 1, 2, … for layer 1. Other layers: x.y.z…. Example: 3.2.2 => third child of secod child of second child of the root.

Return type:

str

>>> print(root.get_tree_representation(key=lambda node: node.get_position_in_tree()))
└── 0
    ├── 1
    ├── 2
    │   ├── 2.1
    │   │   ├── 2.1.1
    │   │   └── 2.1.2
    │   └── 2.2
    ├── 3
    └── 4
        └── 4.1
get_reversed_path_to_root() list[T]

Tree method

Returns:

path from self upwards through all ancestors up to the root.

>>> greatgrandchild1.get_reversed_path_to_root()
[greatgrandchild1, grandchild1, child2, root]
get_root() T

Tree method

Returns:

root (upmost node of a tree which has no parent)

Return type:

Tree

>>> greatgrandchild1.get_root() == root
True
>>> child4.get_root() == root
True
>>> root.get_root() == root
True
get_self_with_key(key: Callable[[T], Any] | None = None) Any
get_staff(*args, **kwargs) Staff

MusicTree method

This method can be used for Score, Part and Measure

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number, measure_number, staff_number depending on musicscore’s class.

Return type:

Staff

get_staff_number()[source]
Returns:

get manually set staff number (necessary for cross-staff notation)

get_tree_representation(key: Callable[[T], Any] | None = None, space: int = 3) str

Tree method

Parameters:

key – An optional callable if None string(node) is called.

Returns:

a representation of all nodes as string in tree form.

>>> print(root.get_tree_representation())
└── root
    ├── child1
    ├── child2
    │   ├── grandchild1
    │   │   ├── greatgrandchild1
    │   │   └── greatgrandchild2
    │   └── grandchild2
    ├── child3
    └── child4
        └── grandchild3
get_voice(*args, **kwargs) Voice

MusicTree method

This method can be used for Score and Part, Measure and Staff

Parameters:
  • args – can be used instead of kwargs. A mixture of args and kwargs is not allowed.

  • kwargspart_number, measure_number, staff_number, voice_number depending on musicscore’s class.

Return type:

Voice

iterate_leaves() Iterator[T]

Tree method

Returns:

A generator iterating over all leaves.

remove(child: T) None

Tree method

Child’s parent will be set to None and child will be removed from list of children.

Parameters:

child

Returns:

None

remove_children() None

Tree method

Calls remove() on all children.

Returns:

None

remove_tie(type: str) None[source]
Parameters:

typestart, stop

Exception:

ValueError

replace_child(old: T, new: T, index: int = 0) None

Tree method

Parameters:
  • old – child or function

  • new – child

  • index – index of old child in the list of its appearances

Returns:

None

set_staff_number(val)[source]

Set staff number manually (necessary for cross-staff notation).

transpose(val: float) Midi[source]

Adds val to value and returns self

traverse() Iterator[T]

Tree method

Traverse all tree nodes.

Returns:

generator

property accidental: Accidental

Set or get Accidental object associated with this midi. If it is set to None an Accidental object will be created.

property content: Any
default_show_accidental_signs = 'modern'
property is_first_child: bool
property is_last_child: bool
>>> t = TestTree('root')
>>> for node in t.traverse():
...    if node.name in ['root', 'child4', 'grandchild2', 'grandchild3', 'greatgrandchild1']:
...        assert node.is_last_child
...    else:
...        assert not node.is_last_child
property is_leaf: bool

Tree property

Returns:

True if self has no children. False if self has one or more children.

Return type:

bool

property is_root: bool

Tree property

Returns:

True if self has no parent, else False.

Return type:

bool

property is_tied_to_next: bool
Returns:

True if this midi adds a XMLTie object of type start Note.

property is_tied_to_previous
Returns:

True if this midi adds a XMLTie object of type stop Note.

property name: str
Returns:

a string like C#3 consisting of step, accidental sign (or value) and octave. Midi with value 0 returns rest as its name.

property next: T | None

Tree property

Returns:

next sibling. None if this is the last current child of the parent.

Return type:

Tree

property notehead: XMLNotehead | None

Set or get notedhead property. This can be None or an XMLNotehead object. It is possible to set this property with a valid notehead value of type str. For permitted values see XMLNotehead.

property octave: int
Returns:

octave number of this midi.

property parent_chord: Chord

Set or get parent Chord object.

property parent_note: Note

Set or get parent Note object.

property previous: T | None

Tree property

Returns:

previous sibling. None if this is the first child of the parent.

Return type:

Tree

property show_accidental_signs: str

MusicTree property

  • If show_accidental_signs is set to None the first get_quantized of ancestors which is False or True will be returned.

  • If show_accidental_signs is set to None it will be converted to default_show_accidental_signs

  • Possible show_accidental_signs are: None, ‘modern’, ‘traditional’

Type:

Optional[str]

Return type:

str

property up: T | None

Tree property

Returns:

get_parent()

Return type:

Tree

property value: float | int

Set and get value of midi. A valid value must be of type float or int and can be between 12 and 127.

class musicscore.midi.MidiNote(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: Midi

Parent class of shorthand midi names: C, D, E, F, G, A, B. Example C(4, ‘#’) = C(4, ‘sharp’) = C(4, sharp)

class musicscore.midi.A(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.B(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.C(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.D(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.E(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.F(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

class musicscore.midi.G(octave: object, accidental_sign: object = None, *args: object, **kwargs: object)[source]

Bases: MidiNote

musicscore.midi.frequency_to_midi(frequency, a4=440)[source]
musicscore.midi.get_accidental_mode(midi_value: float | int, accidental_sign: str | None = None) str[source]
Parameters:
  • midi_value – a valid midi value in half steps (int or float)

  • accidental_signdouble-flat, flat-flat, bb, ffthree-quarters-flatflat, b, fquarter-flatNone, naturalquarter-sharpsharp, #, sthree-quarters-sharpdouble-sharp, sharp-sharp, x, ##, ss

Returns:

accidental_mode: standard, enharmonic, flat, sharp, force-flat, force-sharp

musicscore.midi.midi_to_frequency(midi, a4=440)[source]