Examples
All example files can be found on GitHub under examples
See also
LilyPondUnofficialXMLTestSuite, MyXMLTestSuite and unit/integrity tests
Hello World (1)
In this example one note is added to score step by step. At first you need to install musicscore (s. Installation). After that follow the steps to create a simple musicxml file:
import
pathlib.Path andmusicscore.score.Score,musicscore.part.Part,musicscore.measure.Measure,musicscore.staff.Staff,musicscore.voice.Voice,musicscore.beat.Beatandmusicscore.chord.Chord.from pathlib import Path from musicscore.beat import Beat from musicscore.chord import Chord from musicscore.measure import Measure from musicscore.part import Part from musicscore.score import Score from musicscore.staff import Staff from musicscore.voice import Voice
- Alternatively you can import all classes via
from musicscore import * from pathlib import Path from musicscore import *Create a
Scores = Score(title="Hello World 1")Create and add a
Partto score. For creating aPartobject you have to pass a uniqueid(seeXSDSimpleTypeIDREFfor further information) during initialization. If nonameis set, part usesidas itsname.p = s.add_child(Part('hw1', name='HW1'))Create and add a
Measureto part.numberis required during initializing a measure object. Alternatively you can use part’s method obj:~musicscore.part.Part.add_measure() which takes care of number attribute and creates measure with the sameKeyandTime. It is also possible to add chord directly to part viaadd_chord()method. (s. next example).m = p.add_child(Measure(number=1))Create and add a
Staffto measure. Alternatively you could useadd_staff(). Measure has also other useful shortcut methods likeadd_voice().st = m.add_child(Staff(number=1))Create and add a
Voiceto staff. Alternatively you could use staff’sadd_voice().v = st.add_child(Voice(number=1))Create and add four
Beats with quarter_duration 1 to voice. (As alternative we can callupdate_beats()to add beets according to measure’s time signature. Default value is 4/4.)for _ in range(4): v.add_child(Beat(quarter_duration=1))Select the first beat, create and add a
Chordwith midi value 60 (C4) and quarter duration 4 to this beat.beat = v.get_children()[0] beat.add_child(Chord(60, 4))Use score’s
export_xml()to generate a xml file. An absolute path for the file (withxmlextension) must must be passed as a parameter to this method. In this example we use the pathlib library to get the path of the python file in which the code lives and change its extension from .py to .xml. You could use alsoos.pathor even a hardcoded path as string (not really recommended).xml_path = Path(__file__).with_suffix('.xml') s.export_xml(xml_path)Congrats! You have created your first xml file with musicscore. Now you can open it with a notation software and enjoy the sight ;-)
Hello World (2)
In this example one note is added to score using part’s
add_chord()method. This method takes care of creating and adding all needed objects. The result is exactly the same as inHello World (1), except for the updatedhw2-related IDs and no<staff>1</staff>element associated with the note. The latter is not implicitly added by default viaadd_chord(), and is unnecessary for single-part XMLs in general (refer to the MusicXML documentation for staff elements).
Create a
Scores = Score(title="Hello World 2")Create and add a
Partto score.p = s.add_child(Part('hw2', name='HW2'))Create and add a
Chordwith midi value 60 (C4) and quarter duration 4 as chord to the part (add_chord()).p.add_chord(Chord(60, 4))Use
export_xml()to generate a xml file. An absolute path for the file (withxmlextension) must be passed as a parameter to this method.xml_path = Path(__file__).with_suffix('.xml') s.export_xml(xml_path)