Skip to content

Compositions

charlies-world edited this page Oct 31, 2021 · 4 revisions

Adding Sections to a Composition

We're pros at this now:

# make notes...
# make measures...
# make voices...

section_one = Section()
section_two = Section()
section_three = Section()

# add voices to sections...

composition = Composition()
composition.add_section(section_one)
composition.add_section(section_two)
composition.add_section(section_three)

The Sections will be written in the order they are added.

Other Functions

print(composition.get_number_of_sections()) # 3
retrieved_section = composition.get_section_at_index(0) # retrieved_section is section_one

We can also get the total length of the composition in seconds as a decimal using composition.get_length().

Writing the data to a MIDI file

We will use the constructor for the MIDIDataFileWriter class to write the data:

MIDIDataFileWriter(my_composition, put_voices_in_same_track=True, fileName="my_composition")

The first parameter, a Composition, is the only required parameter. fileName refers to what the file should be named when it's written. put_voices_in_same_track refers to how the Voices in differing Sections are written - when False, each voice will be given its own MIDI track, and when True, each MIDI voice will be combined into one shared track between Sections (so each first Voice in Section one and two will share a track, and so will each second Voice in Section one and two, etc.) (meaning the number of Voices determines determine the number of tracks in the MIDI file. When setting to True, make sure each of your sections has the same number of Voices). This value defaults to True.

The MIDI file will be written in the same directory as your program.

Wrapping Up

Now we know how to take our piece from a Note to a Composition, and now we can start using these structures in the context of a generative piece of music, or whatever else you might want to use these for.

If you keep reading, you'll be able to see some more specific and complex code examples utilizing comPoYse. If you check out my other repositories, you'll be able to find entire projects utilizing comPoYse. Be sure to check out the MIDIUtility class which includes some common functions for using comPoYse.