musicscore.note

class musicscore.note.Note(midi, quarter_duration=None, *args, **kwargs)[source]

Bases: MusicTree, XMLWrapper, QuarterDurationMixin

Parent type: Chord

Child type: Midi

XMLClass

alias of XMLNote

add_child(child: Tree) Tree

Tree method

Check and add child to list of children. Child’s parent is set to self.

Parameters:

child

Returns:

child

Return type:

Tree

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[Tree]

Tree method

Returns:

list of added children.

Return type:

List[Tree]

get_children_of_type(type) List[Tree]

Tree method

Returns:

list of added children of type.

Return type:

List[Tree]

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_coordinates_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

>>> class TestTree(Tree):
...   def _check_child_to_be_added(self, child):
...      return True
>>> root = TestTree()
>>> root.get_coordinates_in_tree()
'0'
>>> child1 = root.add_child(TestTree())
>>> child2 = root.add_child(TestTree())
>>> grandchild1 = child2.add_child(TestTree())
>>> grandchild2 = child2.add_child(TestTree())
>>> child1.get_coordinates_in_tree()
'1'
>>> child2.get_coordinates_in_tree()
'2'
>>> grandchild1.get_coordinates_in_tree()
'2.1'
>>> grandchild2.get_coordinates_in_tree()
'2.2'
get_indentation() str

Tree method

Returns:

indentation according to level (layer number). As default it is used for creating tabs in tree_representation

Return type:

str

get_layer(level: int, key: Callable | None = None) list

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 | None = None) list

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

Return type:

nested list of Tree

get_or_create_xml_notations() XMLNotations[source]

If note’s xml_object has no XMLNotations as child this child will be created.

Returns:

XMLNotations

get_parent() Tree

Tree method

Returns:

parent. None for root.

Return type:

Tree

get_parent_chord()[source]

returns parent_chord

get_parent_measure() Measure[source]
Returns:

parent Measure

get_root() Tree

Tree method

Returns:

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

Return type:

Tree

get_staff_number() int[source]
Returns:

number of parent Staff

get_voice_number() int[source]
Returns:

number of parent Voice

iterate_leaves() Iterator[Tree]

Tree method

Returns:

A generator iterating over all leaves.

remove(child: Tree) 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 = None) None[source]
Parameters:

type – ‘start’, ‘stop’, None: if None and note has XMLTie objects with both types ValueError is raised.

Returns:

None

replace_child(old, new, 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

reversed_path_to_root() Iterator[Tree]

Tree method

Returns:

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

start_tie() None[source]

Start a tie if not already started.

stop_tie() None[source]

Stop a tie if not already stopped.

to_string(*args, **kwargs) str

XMLWrapper method

Calls to_string method of self.xml_object

Returns:

musicxml snippet

traverse() Iterator[Tree]

Tree method

Traverse all tree nodes.

Returns:

generator

tree_representation(key: Callable | None = None, tab: Callable | None = None) str

Tree method

Parameters:
  • key – An optional callable if None compact_repr property of each node is called.

  • tab – An optional callable if None get_indentation() method of each node is called.

Returns:

a representation of all nodes as string in tree form.

Return type:

str

property compact_repr: str

Tree property

Returns:

compact representation of a node. Default is the string representation. This property is used as default in the tree_representation method and can be customized in subclasses to get the most appropriate representation.

Return type:

str

default_show_accidental_signs = 'modern'

Class attribute of MusicTree

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: bool
Returns:

True if an element XMLTie with type ‘start’ is under note’s xml_object children.

Return type:

bool

property is_tied_to_next: bool
Returns:

same as is_tied

property is_tied_to_previous: bool
Returns:

True if an element XMLTie with type ‘stop’ is under note’s xml_object children.

Return type:

bool

property level: int

Tree property

Returns:

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

Return type:

nonnegative int

>>> class TestTree(Tree):
...   def _check_child_to_be_added(self, child):
...      return True
>>> root = TestTree()
>>> root.level
0
>>> ch = root.add_child(TestTree()).add_child(TestTree()).add_child(TestTree())
>>> ch.level
3
property midi: Midi

Chord.midi property must be a Midi object with a parent Chord. Midi with value 0 means rest. Setting this property will set Midis parent_note to self.

Returns:

note’s Midi.

property next: Tree | None

Tree property

Returns:

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

Return type:

Tree

property parent_chord: Chord
Returns:

notes parent. Same as self.up

property previous: Tree | None

Tree property

Returns:

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

Return type:

Tree

property quarter_duration: QuarterDuration

Set and get the duration measured in quarters.

Setting value can be of types int, float, quicktions.Fraction, QuarterDuration

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: Tree

Tree property

Returns:

get_parent()

Return type:

Tree

property xml_object: XMLElement

XMLWrapper method

This musicscore class has an equivalent MusicXML class which can be reached via this property.

If an attribute is not found directly in the main class, it wll be passed on to __get_attribute__ or __set__attribute__ methods of self.xml_object. It can also use the short cut attributes of XMLElement to get or set the first child.

Returns:

wrapped MusicXML element of type XMLClass

musicscore.note.tie(*notes)[source]

Tie notes.

musicscore.note.untie(*notes)[source]

Untie notes.