musicscore.score
- class musicscore.score.Score(version='4.0', title=None, subtitle=None, get_quantized=False, simplified_sextuplets=None, new_system=False, *args, **kwargs)[source]
Bases:
MusicTree,QuantizeMixin,SimplifiedSextuplets,FinalizeMixin,XMLWrapperParent type:
NoneChild type:
Part- XMLClass
alias of
XMLScorePartwise
- add_child(child: Part) Part[source]
Check and add child to list of children. Child’s parent is set to self.
Part’s
xml_objectis add to score’sxml_objectas childPart’s
score_part.xml_objectis added to score’sxml_part_list
- classmethod create_tree_from_list(tree_list_representation: list[list[Any]], represented_attribute_names: list[str]) T
- export_xml(path: pathlib.Path) None[source]
Creates a musicxml file
- Parameters:
path – Output xml file
- Returns:
None
- filter_nodes(key: Callable[[T], Any], return_value: Any) list[T]
Treemethod>>> root.filter_nodes(lambda node: node.get_level(), 2) [grandchild1, grandchild2, grandchild3]
- finalize() None[source]
FinalizeMixinmethodfinalize can only be called once.
It calls finalize method of all children.
- get_beat(*args, **kwargs) Beat
MusicTreemethodThis method can be used for
ScoreandPart,MeasureandStaffandVoice- Parameters:
- Return type:
- 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_chord(*args, **kwargs) Chord
MusicTreemethodThis method can be used for
ScoreandPart,Measure,Staff,VoiceandBeat- Parameters:
args – can be used instead of
kwargs. A mixture of args and kwargs is not allowed.kwargs –
part_number,measure_number,staff_number,voice_number,beat_number,chord_numberdepending on musicscore’s class. AStafffor example needsvoice_number,beat_numberandchord_numberwhile aScoreneeds all keyword arguments.
- Return type:
- 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_measure(*args, **kwargs) Measure
MusicTreemethodThis method can be used for
ScoreandPart- Parameters:
args – can be used instead of
kwargs. A mixture of args and kwargs is not allowed.kwargs –
part_number,measure_numberdepending on musicscore’s class.
- Return type:
- get_number_of_layers() int
>>> root.get_number_of_layers() 3
- get_parent() T | None
Treemethod- Returns:
parent.
Noneforroot.- Return type:
Tree
- get_part(*args, **kwargs) Part
MusicTreemethodThis method can be used for
Score- Parameters:
args – can be used instead of
kwargs. A mixture of args and kwargs is not allowed.kwargs –
part_number.
- Return type:
- 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_possible_subdivisions(beat_quarter_duration: QuarterDuration | None = None) List[int]
QuantizeMixinmethodThis method is used by
Beat’squantize_quarter_durations().Possible subdivisions dictionary can be set with
set_possible_subdivisions(). Keys in the subdivisions dictionary correspond to beat quarter durations (e.g. 1, 1/2 etc.) For example if get_possible_subdivisions()[1] == [3, 5, 8] a beat with a quarter duration of 1 can after quantization consists only of eighth triplets, quintuplets and 32ths. If this dictionary is not set orbeat_quarter_durationas key does not exist, the parent’s possible subdivisions dictionary will be checked.Scorehas a defaultPOSSIBLE_SUBDIVISIONSdictionary which will be used if no other musicscore node on the path from self to root has its own possible subdivisions dictionary withbeat_quarter_durationas a key. For setting possible subdivisions dictionary use alwaysset_possible_subdivisions().- Parameters:
beat_quarter_duration – Used as key in possible subdivisions dictionary. If
Noneand self is aBeatself.quarter_durationis used. IfNoneand self is not aBeatit is set to 1.- Returns:
A list of possible subdivisions of a
Beat. This is used by beat’squantize_quarter_durations()- Return type:
List[int]
- 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_staff(*args, **kwargs) Staff
MusicTreemethodThis method can be used for
Score,PartandMeasure- Parameters:
args – can be used instead of
kwargs. A mixture of args and kwargs is not allowed.kwargs –
part_number,measure_number,staff_numberdepending on musicscore’s class.
- Return type:
- 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
- get_voice(*args, **kwargs) Voice
MusicTreemethodThis method can be used for
ScoreandPart,MeasureandStaff- Parameters:
args – can be used instead of
kwargs. A mixture of args and kwargs is not allowed.kwargs –
part_number,measure_number,staff_number,voice_numberdepending on musicscore’s class.
- Return type:
- group_parts(number: int | str, start_part_number: int, end_part_number: int, symbol: str = 'square', name: str | None = None, abbreviation: str | None = None, **kwargs) None[source]
Adds a XMLPartList child
- Parameters:
number – sets number property of
XMLPartGroupstart_part_number – number of part to start the group
end_part_number – number of part to end the group
symbol – symbol property of
XMLGroupSymbol:’none’, ‘brace’, ‘line’, ‘bracket’, ‘square’; default: ‘square’name – value of
XMLGroupNameabbreviation – value of
XMLGroupAbbreviationkwargs – kwargs of
XMLGroupSymbol
- Returns:
None
- 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
- 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
- set_multi_measure_rest(first_measure_number: int, last_measure_number: int) None[source]
Creates a multi measure rest
- Parameters:
first_measure_number – number of measure to start the multi measure rest
last_measure_number – number of measure to end the multi measure rest
- Returns:
None
- set_possible_subdivisions(subdivisions: list[int], beat_quarter_duration: QuarterDuration | None = None) None
QuantizeMixinmethodThis method is used to set or change possible subdivisions dictionary of a beat or its ascendants. For example if get_possible_subdivisions()[1] == [3, 5, 8] a beat with a quarter duration of 1 can after quantization consists only of eighth triplets, quintuplets and 32ths.
- Parameters:
subdivisions – list of possible subdivisions to be used in
musicscore.beat.Beat.quantize_quarter_durations()beat_quarter_duration – If
Noneand self is aBeatself.quarter_durationis used. IfNoneand self is not aBeatit is set to 1.
- Returns:
None
- to_string(*args, **kwargs)
FinalizeMixinmethodIt triggers
finalize()first before calling parent’sxml_object.to_string()method. If no xml_object exists (it means there is no direct MusicXML equavalent of this class) aClassHasNoMusicXMLEquivalentErroris thrown.
- traverse() Iterator[T]
TreemethodTraverse all tree nodes.
- Returns:
generator
- property content: Any
- default_show_accidental_signs = 'modern'
- property get_quantized: bool
QuantizeMixinpropertyIf
get_quantizedis set to None the firstget_quantizedof ancestors which isFalseorTruewill be returned.If
musicscore.score.Score.get_quantizedis set to None it will be converted toFalsemusicscore.measure.Measure.finalize()loops over all beats. Ifmusicscore.beat.Beat.get_quantizedreturnsTruemusicscore.beat.Beat.quantize_quarter_durations()is called.
- Type:
Optional[bool]
- Return type:
bool
- 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 new_system: bool
Set or get new_system property. It will be automatically set to
Trueif aMeasuresets itsmusicscore.measure.Measure.new_systemtoTrue.<encoding>inXMLScorePartwise’s<identification>will have a<support>child with attributenew-systemifmusicscore.score.Score.new_systemis set toTrue.
- property next: T | None
Treeproperty- Returns:
next sibling.
Noneif this is the last current child of the parent.- Return type:
Tree
- property page_layout: PageLayout
Set and get page layout. After setting value, page layout’s parent is set to self.
- Type:
PageLayout- Returns:
PageLayout
- property previous: T | None
Treeproperty- Returns:
previous sibling.
Noneif this is the first child of the parent.- Return type:
Tree
- 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 simplified_sextuplets: bool
- property staff_layout
Set and get staff layout. After setting value, staff layout’s parent is set to self.
- Type:
StaffLayout- Returns:
StaffLayout
- property subtitle
If val is
Noneand a subtitle object as credit already exists, this object will be removed.If val is not
None: - if a subtitle exists itsvalue_will be replaced. - else a subtitle as credit will be added.
- Type:
Optional[str]
- Returns:
Optional[str]
- property system_layout
Set and get system layout. After setting value, system layout’s parent is set to self.
- Type:
SystemLayout- Returns:
SystemLayout
- property title
If val is
Noneand a title object as credit already exists, this object will be removed.If val is not
None: - if a title exists itsvalue_will be replaced. - else a title as credit will be added.
- Type:
Optional[str]
- Returns:
Optional[str]
- property up: T | None
Treeproperty- Returns:
- Return type:
Tree
- property version: str
- Type:
Any
- Returns:
xml_object’s version
- Return type:
str
- 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
- musicscore.score.POSSIBLE_SUBDIVISIONS = {1/4: [2, 3], 1/2: [2, 3, 4, 5], 1/1: [2, 3, 4, 5, 6, 7, 8]}
- musicscore.score.SUBTITLE = {'default_x': {'A4': {'portrait': 616}}, 'default_y': {'A4': {'portrait': 1508}}, 'font_size': 18, 'halign': 'center', 'valign': 'top'}
- musicscore.score.TITLE = {'default_x': {'A4': {'portrait': 616}}, 'default_y': {'A4': {'portrait': 1573}}, 'font_size': 24, 'justify': 'center', 'valign': 'top'}