/melody.py

Brute-force generation of ten-note cantus firmus melodies and first species counterpoint

Primary LanguagePython

melody.py

melody.py generates random 10-note first species counterpoint melodies by first finding a random cantus firmus melody that matches a set of heuristics and then finding an appropriate counterpoint melody. A total of 16 heuristics are used for each individual melody and 4 heuristics are used to match two melodies in counterpoint. Results are outputted as MIDI files using Pyknon and then played using Timidity.

Table of Contents

1. Try It Now!
2. Video Demonstration
3. Usage
3.1. Installation
3.1.1. Ubuntu / Debian
3.1.2. Other
3.2. Running melody.py
3.2.1. Command-line interface
3.2.2. Web interface
4. Heuristics Used
4.1. For each individual melody
4.2. To match cantus firmus melody to first species melody
5. Wish List

1. Try It Now!

I've spun up an Amazon EC2 instance.

2. Video Demonstration

Link to Youtube video

3. Usage

You can use melody.py through the command-line or by running a small web server. The advantage of the latter is that you'll also be able to see generated sheet music.

3.1. Installation

3.1.1. Ubuntu / Debian
sudo apt-get install python-pip timidity
pip install pyknon

If you wish to use the web interface, also run

pip install flask

3.1.2. Other

Install Pyknon and Timidity.

If you wish to use the web interface, also install Flask. If you're using Internet Explorer (which supports MP3 but not WAV audio sources), make sure FFmpeg is installed.

3.2. Running melody.py

3.2.1. Command-line interface
python melody.py

will loop forever, generating new melodies continuously. Mash Ctrl-C a few times to stop it.

3.2.2. Web interface
python app.py

Then go to localhost:5000 (or whatever port is chosen).

4. Heuristics Used

4.1. For each individual melody

Both the cantus firmus and the first species must satisfy the following rules:

  1. No repeated notes in cantus firmus (one repetition allowed in first species).
  2. No leaps that are an octave or larger.
  3. No dissonant leaps.
  4. Between two and four leaps in total.
  5. Has a climax (highest note) that is not on the tonic or leading tone.
  6. Changes direction at least two times.
  7. No note repeated more than 3 times.
  8. Final note approached by a step (not a leap).
  9. Leaps larger than M3 must be followed by a change of direction.
  10. The leading tone must always be followed by the tonic.
  11. No more than two consecutive leaps in the same direction.
  12. The same interval cannot occur twice in a row.
  13. No noodling (that is, patterns such as N1 N2 N1 N2, for some notes N1 and N2).
  14. No runs of more than four consecutive notes.
  15. No unresolved melodic tension (that is, the start and end note of each run must be consonant together).
  16. No repeated three-note patterns.

4.2. To match cantus firmus melody to first species melody

Together, the cantus firmus and the first species must satisfy the following:

  1. No dissonant vertical intervals.
  2. No vertical intervals larger than a 12th (P8 + P5).
  3. No parallel fifths or octaves.
  4. No parallel three-note chains.

5. Wish List

  • minor key support
  • support for other kinds of counterpoint
  • support for melodies of other lengths (with rules tuned appropriately)
  • ability to import existing cantus firmus (as MIDI or note array)
  • minor: treating repeated notes as a single note of double duration rather than two separate notes