Examples

All example files can be found on GitHub under examples

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:

  1. import pathlib.Path and musicscore.score.Score, musicscore.part.Part, musicscore.measure.Measure, musicscore.staff.Staff, musicscore.voice.Voice, musicscore.beat.Beat and musicscore.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 *
    
  2. Create a Score

    s = Score(title="Hello World 1")
    
  3. Create and add a Part to score. For creating a Part object you have to pass a unique id (see XSDSimpleTypeIDREF for further information) during initialization. If no name is set, part uses id as its name.

    p = s.add_child(Part('hw1', name='HW1'))
    
  4. Create and add a Measure to part. number is 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 same Key and Time. It is also possible to add chord directly to part via add_chord() method. (s. next example).

    m = p.add_child(Measure(number=1))
    
  5. Create and add a Staff to measure. Alternatively you could use add_staff(). Measure has also other useful shortcut methods like add_voice().

    st = m.add_child(Staff(number=1))
    
  6. Create and add a Voice to staff. Alternatively you could use staff’s add_voice().

    v = st.add_child(Voice(number=1))
    
  7. Create and add four Beat s with quarter_duration 1 to voice. (As alternative we can call update_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))
    
  8. Select the first beat, create and add a Chord with midi value 60 (C4) and quarter duration 4 to this beat.

    beat = v.get_children()[0]
    beat.add_child(Chord(60, 4))
    
  9. Use score’s export_xml() to generate a xml file. An absolute path for the file (with xml extension) 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 also os.path or even a hardcoded path as string (not really recommended).

    xml_path = Path(__file__).with_suffix('.xml')
    s.export_xml(xml_path)
    
  10. 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 in Hello World (1), except for the updated hw2-related IDs and no <staff>1</staff> element associated with the note. The latter is not implicitly added by default via add_chord(), and is unnecessary for single-part XMLs in general (refer to the MusicXML documentation for staff elements).

  1. Create a Score

    s = Score(title="Hello World 2")
    
  2. Create and add a Part to score.

    p = s.add_child(Part('hw2', name='HW2'))
    
  3. Create and add a Chord with midi value 60 (C4) and quarter duration 4 as chord to the part (add_chord()).

    p.add_chord(Chord(60, 4))
    
  4. Use export_xml() to generate a xml file. An absolute path for the file (with xml extension) must be passed as a parameter to this method.

    xml_path = Path(__file__).with_suffix('.xml')
    s.export_xml(xml_path)