counterpoint generator

Table of Contents

counterpoint sheet music generator

using c++ and lilypond

What is counterpoint and why does it make a good candidate for using code to generate music?

Counterpoint is a specific style of music, where one melody is generated based on the note that came before, using specific rules. This means that if you can write a melody, the program can write a counterpoint to go along with it.

These images are from Burstein, L. Poundie., and Joseph Nathan. Straus. Concise Introduction to Tonal Harmony. W.W. Norton, 2016. The top image demonstrates a counterpoint melody, and the numbers represent the intervals between the notes. The bottom image shows some of the rules for what counterpoint melodies cannot do.

What were the requirements and challenges for this project?

Writing Music:

  • Translating music theory to code
  • Saving music notes
  • Using logic to check for errors
  • Deciding which part of the program does what

Exporting sheet music:

  • Understanding basic music rules
  • Integrating with LilyPond
  • Exporting different lengths
  • Exporting in different keys
  • Exporting with difrferent time signatures

Proejct details:

This program was written in C++, and was designed to export text that fit the .ly file format. Here is the class hierarchy diagram.

The goal for this project is to build a program that generates correct two-voice counterpoint.

This project has four distinct parts: a note class, a class/function that combines all the rules, classes of rules, and a class/function that outputs a .ly file.

Key Features

  • User chooses length, time signature, and species type
  • Follows all the rules for counterpoint
  • Outputs a LilyPond file with the generated music

Assumptions

  • The users are good actors
  • The user will not want to write counterpoint in a minor key
  • The user cannot choose what specific notes they want

Note: Caleb N. and I worked on this project together.

The code still runs, though there are now build errors. I ran it, and this is what the program looked like:

It created a file, which, once I found it, imports into LilyPond correctly and plays. You can copy the outputted text and put it into hacklily.org (also linked below) to see the sheet music. I generated the first two phrases in style 0, then in style 1, then style 2, then finally style 0 again.

Here is the first bit of the output:

I am very impressed that it still runs. It looks like there are some notes for which the logic may be wrong, given the large jumps up and down occasionally, and when I listened to it I heard at least one major second, which I don’t think should be there (the last note in measure five). Still, I haven’t looked at this code in three years, and I’m kinda impressed with my past self. What a cool project xD

I uploaded all the project files to github, as they were being hosted somewhere else, so at least I’ll have a copy of the code.

Here is a bonus image of what the program looked like when it ran: super impressive xD

music generated by the program:

outputted .txt file

some counterpoint I wrote:

sheetmusic pdf

the corresponding .ly file:

.ly file