musicscore.note
- class musicscore.note.Note(midi, quarter_duration=None, *args, **kwargs)[source]
Bases:
MusicTree,XMLWrapper,QuarterDurationMixinParent type:
ChordChild type:
Midi- add_child(child: T) T
TreemethodCheck and add child to list of children. Child’s parent is set to self.
- Parameters:
child –
- Returns:
child
- Return type:
Tree
- 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]
Treemethod>>> root.filter_nodes(lambda node: node.get_level(), 2) [grandchild1, grandchild2, grandchild3]
- get_beats() List[Beat]
MusicTreemethodThis method can be used for
ScoreandPart,Measure,StaffandVoice.- Returns:
a flat list of all beats.
- Return type:
List[
Beat]
- get_children() list[T]
Treemethod- Returns:
list of added children.
- get_children_of_type(type_: type) list[T]
Treemethod- Returns:
list of added children of type.d
- Return type:
list[
Tree]
- get_chords() List[Chord]
MusicTreemethodThis method can be used for
ScoreandPart,MeasureandStaff,VoiceandBeat- 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
Treemethod- 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:
0forroot,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_number_of_layers() int
>>> root.get_number_of_layers() 3
- get_or_create_xml_notations() XMLNotations[source]
If note’s
xml_objecthas noXMLNotationsas child this child will be created.- Returns:
- get_parent() T | None
Treemethod- Returns:
parent.
Noneforroot.- Return type:
Tree
- get_parent_chord()[source]
returns
parent_chord
- get_position_in_tree() str
Treemethod- 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]
Treemethod- 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
Treemethod- 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_tree_representation(key: Callable[[T], Any] | None = None, space: int = 3) str
Treemethod- Parameters:
key – An optional callable if
Nonestring(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
- iterate_leaves() Iterator[T]
Treemethod- Returns:
A generator iterating over all leaves.
- remove(child: T) None
TreemethodChild’s parent will be set to
Noneand child will be removed from list of children.- Parameters:
child –
- Returns:
None
- remove_tie(type: str | None = None) None[source]
- Parameters:
type – ‘start’, ‘stop’, None: if None and note has
XMLTieobjects with both types ValueError is raised.- Returns:
None
- replace_child(old: T, new: T, index: int = 0) None
Treemethod- Parameters:
old – child or function
new – child
index – index of old child in the list of its appearances
- Returns:
None
- to_string(*args, **kwargs) str
XMLWrappermethodCalls
to_stringmethod of self.xml_object- Returns:
musicxml snippet
- traverse() Iterator[T]
TreemethodTraverse all tree nodes.
- Returns:
generator
- 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
Treeproperty- Returns:
Trueif self has no children.Falseif self has one or more children.- Return type:
bool
- property is_root: bool
Treeproperty- Returns:
Trueif self has no parent, elseFalse.- Return type:
bool
- property is_tied: bool
- Returns:
True if an element
XMLTiewith 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
XMLTiewith type ‘stop’ is under note’s xml_object children.- Return type:
bool
- property midi: Midi
Chord.midi property must be aMidiobject with a parentChord.Midiwith value 0 means rest. Setting this property will setMidisparent_noteto self.- Returns:
note’s
Midi.
- property next: T | None
Treeproperty- Returns:
next sibling.
Noneif this is the last current child of the parent.- Return type:
Tree
- property previous: T | None
Treeproperty- Returns:
previous sibling.
Noneif 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,fractions.Fraction,QuarterDuration
- property show_accidental_signs: str
MusicTreepropertyIf show_accidental_signs is set to None the first get_quantized of ancestors which is
FalseorTruewill be returned.If
show_accidental_signsis set to None it will be converted todefault_show_accidental_signsPossible show_accidental_signs are: None, ‘modern’, ‘traditional’
- Type:
Optional[str]
- Return type:
str
- property up: T | None
Treeproperty- Returns:
- Return type:
Tree
- property xml_object: XMLElement
XMLWrappermethodThis musicscore class has an equivalent
MusicXMLclass 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 ofself.xml_object. It can also use the short cut attributes ofXMLElementto get or set the first child.- Returns:
wrapped MusicXML element of type
XMLClass