Skip to content

Hello, World

anstepp edited this page Dec 7, 2020 · 7 revisions

The below code will output a MusicXML file that contains just a Middle C.

from py2musicxml.notation import Note, Part, Score

duration = 4
octave = 4
pitch_class = 0

middle_c = Note(duration, octave, pitch_class)

time_signature = [(4,4)]

our_part = Part([middle_c], time_signature)

our_score = Score([our_part])

our_score.convert_to_xml("middle_c.musicxml")

Each line does something unique that allows us to create our importable MusicXML file. We can walk through each line together.

This line is an import statement that is used to give our script access to the Note, Part, and Score objects from the py2musicxml.notation namespace.

from py2musicxml.notation import Note, Part, Score

Without this line, we can't use Py2MusicXML! It is also important to note we're not importing all of Py2MusicXML, just the three objects we need for this score. If you wanted to, you could import other libraries here, but we don't need any.

On the next three lines:

duration = 4
octave = 4
pitch_class = 0

we declare three variables called duration, octave, and pitch_class. We assign them values that are integers. Each value represents something musically. In Py2MusicXML, a duration of 1 represents a quarter note. So, we assign a 4 to duration to get a whole note. Py2MusicXML handles all that under the hood - you don't have to worry about how, just that if you want a duration, relate it to a quarter note = 1. We assign a 4 to octave, and a pitch class of 0. Py2MusicXML uses scientific pitch notation for the octave, and musical set theory pitch class notation.

The next line is where things get good.

middle_c = Note(duration, octave, pitch_class)

This is where we create a Note using Py2MusicXML. Congratulations! In this case, we just create a Middle C, just one note. Notice that the Note object has three variables in the parenthesis where we call it called duration, octave, and pitch_class. These are the three variables we just declared in the previous bit of code. This is convenient, so we can remember what goes where. We could name them whatever we want (which could be very useful!), just as long as the arguments supplied to create a note are always in this order: duration, octave, and pitch class.

Next we make a time signature.

time_signature = [(4,4)]

The following code:

our_part = Part([middle_c], time_signature)

We create a Part from a list of notes. In this case, we just have one note, middle_c, that we enclose in brackets [], with is Python shorthand for a list. If we had more notes (or, better, a preexisting list of notes), we could feed them all in like this. The cool bit here is that Py2MusicXML will break everything into measures for you. It will cycle endlessly through the time signatures we feed it.

Just like creating a Part and using a list of Notes, we create a Score with a list of Parts.

our_score = Score([our_part])

Now, there's a Score object called our_score. To convert this score to MusicXML, there's a method we call on it with dot notation like so:

our_score.convert_to_xml("middle_c.musicxml")

The argument is the filename we want python to create and fill with the MusicXML.

If you are to open the middle_c.musicxml file in engraving software, you'll get the following:

Middle C

Continue on to Tutorial Two for instructions on how to complete a Major Scale.