pymusicxml.score_components.Measure

class pymusicxml.score_components.Measure(contents: Optional[Union[Sequence[DurationalObject], Sequence[Sequence[DurationalObject]]]] = None, time_signature: Optional[tuple] = None, key: Optional[Union[KeySignature, str, int]] = None, clef: Optional[Union[Clef, str, tuple]] = None, barline: Optional[str] = None, staves: Optional[str] = None, number: int = 1, directions_with_displacements: Sequence[tuple[Direction, float]] = (), transpose: Optional[Transpose] = None)[source]

Bases: MusicXMLComponent, MusicXMLContainer

Class representing a measure of music, perhaps with multiple voices.

Parameters:
  • contents – Either a list of Notes / Chords / Rests / Tuplets / BeamedGroups or a list of voices, each of which is a list of Notes / Chords / Rests / Tuplets / BeamedGroups.

  • time_signature – in tuple form, e.g. (3, 4) for “3/4”

  • key – either a TraditionalKeySignature or NonTraditionalKeySignature object, an integer representing the number of sharps (or flats if negative), or a string to be parsed into a key signature, such as “G major”, “F# lydian”, etc.

  • clef – either None (for no clef), a Clef object, a string (like “treble”), or a tuple like (“G”, 2) to represent the clef letter, the line it lands, and an optional octave transposition as the third parameter

  • barline – either None, which means there will be a regular barline, “double”, “end”, or any of the barline names used in the MusicXML standard.

  • staves – for multi-part music, like piano music

  • number – which number in the score. Will be set automatically by the containing Part

  • directions_with_displacements – this is a way of placing directions at arbitrary positions in the bar. It takes a list of tuples of (direction, position in the bar), where position in the bar is a floating point number of quarter notes.

Methods

directions()

Returns a tuple of all to the Diections in this measure, both those attached to notes and those passed to the directions_with_displacements constructor argument.

iter_directions()

Iterates through the Directions in this measure, both those attached to notes and those passed to the directions_with_displacements constructor argument.

iter_leaves([which_voices])

Iterates through the Notes/Chords/Rests in this measure, expanding out any tuplets or beam groups.

iter_notations()

Iterates through the Notations in this measure, in order.

leaves([which_voices])

Returns a tuple of all to the Notes/Chords/Rests in this measure, expanding out any tuplets or beam groups.

notations()

Returns a tuple of all to the Notation in this measure, returned in order within the measure.

render()

Renders this component to a tuple of ElementTree.Element.

wrap_as_score()

Wraps this component in a Score so that it can be exported and viewed

Inherited Methods

append(value)

S.append(value) -- append value to the end of the sequence

clear()

count(value)

export_to_file(file_path[, pretty_print])

Exports this musical object (wrapped as a score) to the given file path.

extend(values)

S.extend(iterable) -- extend sequence by appending elements from the iterable

index(value, [start, [stop]])

Raises ValueError if the value is not present.

insert(i, o)

Insert the given object before the given index.

pop([index])

Raise IndexError if list is empty or index is out of range.

remove(value)

S.remove(value) -- remove first occurrence of value.

reverse()

S.reverse() -- reverse IN PLACE

to_xml([pretty_print])

Renders this component to MusicXML, adding a version tag, but not wrapping it up as a full score.

view_in_software(command)

Uses the given terminal command to create a score out of this musical object, and open it in music notation software.

Attributes

voices

Returns a tuple of the voices in this Measure

property voices: tuple[Sequence[MusicXMLComponent]]

Returns a tuple of the voices in this Measure

iter_leaves(which_voices=None) Iterator[tuple[Note | Chord | Rest, float]][source]

Iterates through the Notes/Chords/Rests in this measure, expanding out any tuplets or beam groups. The notes/chords/rests draw from the specified voices.

Parameters:

which_voices – List of voices to return notes from (numbered 0, 1, 2, 3). The default value of None returns notes from all voices.

:return tuples of (Note/Chord/Rest, beat within measure)

leaves(which_voices=None) Sequence[Note | Chord | Rest][source]

Returns a tuple of all to the Notes/Chords/Rests in this measure, expanding out any tuplets or beam groups. The notes/chords/rests and draw from the specified voices, and are returned in order within the measure.

Parameters:

which_voices – List of voices to return notes from (numbered 0, 1, 2, 3). The default value of None returns notes from all voices.

iter_directions() Iterator[tuple[Direction, float]][source]

Iterates through the Directions in this measure, both those attached to notes and those passed to the directions_with_displacements constructor argument.

:return tuples of (Direction, beat within measure)

iter_notations() Iterator[tuple[Notation, float]][source]

Iterates through the Notations in this measure, in order.

:return tuples of (Notation, beat_within_measure)

directions() Sequence[Direction][source]

Returns a tuple of all to the Diections in this measure, both those attached to notes and those passed to the directions_with_displacements constructor argument. Returned in order within the measure.

notations() Sequence[Notation][source]

Returns a tuple of all to the Notation in this measure, returned in order within the measure.

render() Sequence[Element][source]

Renders this component to a tuple of ElementTree.Element. (The reason for making it a tuple is that musical objects like chords are represented by several notes side by side, with all but the first containing a </chord> tag.)

wrap_as_score() Score[source]

Wraps this component in a Score so that it can be exported and viewed

append(value)

S.append(value) – append value to the end of the sequence

clear() None -- remove all items from S
count(value) integer -- return number of occurrences of value
export_to_file(file_path: str, pretty_print: bool = True) None

Exports this musical object (wrapped as a score) to the given file path.

Parameters:
  • file_path – The path of the file we want to write to.

  • pretty_print – If True, breaks the MusicXML onto multiple lines, with indentation

extend(values)

S.extend(iterable) – extend sequence by appending elements from the iterable

index(value[, start[, stop]]) integer -- return first index of value.

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

insert(i, o) None

Insert the given object before the given index.

Parameters:
  • i – Index at which to insert

  • o – Object to insert

pop([index]) item -- remove and return item at index (default last).

Raise IndexError if list is empty or index is out of range.

remove(value)

S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.

reverse()

S.reverse() – reverse IN PLACE

to_xml(pretty_print: bool = False) str

Renders this component to MusicXML, adding a version tag, but not wrapping it up as a full score.

Parameters:

pretty_print – If True, breaks the MusicXML onto multiple lines, with indentation

view_in_software(command: str) None

Uses the given terminal command to create a score out of this musical object, and open it in music notation software.

Parameters:

command – The terminal command corresponding to the software with which we want to open the score.