musicscore.score

class musicscore.score.Score(version='4.0', title=None, subtitle=None, get_quantized=False, new_system=False, *args, **kwargs)[source]

Bases: MusicTree, QuantizeMixin, FinalizeMixin, XMLWrapper

Parent type: None

Child 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_object is add to score’s xml_object as child

  • Part’s score_part.xml_object is added to score’s xml_part_list

Parameters:

childPart, required

Returns:

child

Return type:

Part

add_part(id: str) Part[source]

Creates and adds part

Parameters:

id – part’s id

Returns:

part

export_xml(path: pathlib.Path) None[source]

Creates a musicxml file

Parameters:

path – Output xml file

Returns:

None

finalize() None[source]

FinalizeMixin method

finalize can only be called once.

It calls finalize method of all children.

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[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_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_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_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_parent() Tree

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_possible_subdivisions(beat_quarter_duration: QuarterDuration | None = None) List[int]

QuantizeMixin method

This method is used by Beat’s quantize_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 or beat_quarter_duration as key does not exist, the parent’s possible subdivisions dictionary will be checked.

Score has a default POSSIBLE_SUBDIVISIONS dictionary which will be used if no other musicscore node on the path from self to root has its own possible subdivisions dictionary with beat_quarter_duration as a key. For setting possible subdivisions dictionary use always set_possible_subdivisions().

Parameters:

beat_quarter_duration – Used as key in possible subdivisions dictionary. If None and self is a Beat self.quarter_duration is used. If None and self is not a Beat it is set to 1.

Returns:

A list of possible subdivisions of a Beat. This is used by beat’s quantize_quarter_durations()

Return type:

List[int]

get_root() Tree

Tree method

Returns:

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

Return type:

Tree

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

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 XMLPartGroup

  • start_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 XMLGroupName

  • abbreviation – value of XMLGroupAbbreviation

  • kwargs – kwargs of XMLGroupSymbol

Returns:

None

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

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.

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

QuantizeMixin method

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

None

to_string(*args, **kwargs)

FinalizeMixin method

It triggers finalize() first before calling parent’s xml_object.to_string() method. If no xml_object exists (it means there is no direct MusicXML equavalent of this class) a ClassHasNoMusicXMLEquivalentError is thrown.

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

write(*args, **kwargs)[source]

Not implemented. Use Score.export_xml instead!

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

QuantizeMixin property

Type:

Optional[bool]

Return type:

bool

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

Set or get new_system property. It will be automatically set to True if a Measure sets its musicscore.measure.Measure.new_system to True. <encoding> in XMLScorePartwise’s <identification> will have a <support> child with attribute new-system if musicscore.score.Score.new_system is set to True.

property next: Tree | None

Tree property

Returns:

next sibling. None if 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: Tree | None

Tree property

Returns:

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

Return type:

Tree

property scaling: Scaling
Type:

Scaling

Returns:

Scaling

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 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 None and a subtitle object as credit already exists, this object will be removed.

  • If val is not None: - if a subtitle exists its value_ 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 None and a title object as credit already exists, this object will be removed.

  • If val is not None: - if a title exists its value_ will be replaced. - else a title as credit will be added.

Type:

Optional[str]

Returns:

Optional[str]

property up: Tree

Tree property

Returns:

get_parent()

Return type:

Tree

property version: str
Type:

Any

Returns:

xml_object’s version

Return type:

str

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.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'}