Showing posts with label canon. Show all posts
Showing posts with label canon. Show all posts

Saturday, August 24, 2013

Algorithmic composition: generating tonal canons with Python and music21

1x spiced up chord progression (intermediate step in canon generation)

 

  What?

According to wikipedia:
Algorithmic composition is the technique of using algorithms to create music.

Some algorithms that have no immediate musical relevance are used by composers as creative inspiration for their music. Algorithms such as fractals, L-systems, statistical models, and even arbitrary data (e.g. census figures, GIS coordinates, or magnetic field measurements) have been used as source materials.
And:
In music, a canon is a contrapuntal compositional technique that employs a melody with one or more imitations of the melody played after a given duration (e.g., quarter rest, one measure, etc.).

How?

Last year I wrote a series of articles on an easy method for writing some types of canons:
In at least one of those articles I claimed that the methods described there should be easy to use as basis for automation. Here I automate the method for writing a simple canon as explained in the first of these articles.

Code?

The code discussed below is available under GPLv3 license on github: https://github.com/shimpe/canon-generator. Similar to the gcc compiler, the music you generate with this program is yours. The GPLv3 license only applies to the code itself.

It depends on free software only: python 2.7, music21 and MuseScore.

Explanation? 

This program generates canons from a given chord progression in a fixed key (no modulations for now, sorry!).

It does NOT generate atonal or experimental music, that is, if you're willing to accept the limitations of the program. It can occasionally generate "grave" errors against common practice rules (e.g. parallel fifths/octaves) -> see further explanation.

It closely follows the method as explained in the  Tutorial on my technique for writing a Canon article referenced above. If you want to understand in detail how it all works, please read that article first, and then come back here. If you just want to experiment with the different program settings, continue :)


One thing is worth explaining in more detail: the article mentions in one of the first steps of the recipe that the composer can start from a chord progression, and "spice it up" to get a choral.  So: how to we get a computer to spice up a chord progression, without building in thousands of composition rules?

I introduced some note transformations that:
  • introduce small steps between notes so as to generate something that could be interpreted as a melody
  • do not fundamentally alter the harmonic function in the musical context
To accomplish this I replace with a sequence of notes without altering the total duration of the fragment, e.g.
  • original note (half note) -> original note (quarter note), neighbouring note (8th note), original note (8th note)
Other transformations  look at the current note and the next note, and interpolate a note in between (again, without changing total duration)
  • original note (half note), next note -> original note (quarter note), note between original note and next note (quarter note), next note
A property of generating melodies by spicing up lists of notes using this method is that after spicing up a list of notes, you can spice the spiced up list again to get an even spicier list (more complex melody, both in pitch and in rhythm).

Finally a warning for the sensitive ears: for a composer to write a choral that obeys all the rules of the "common practice" takes years of study and lots of practice. Given the extreme simplicity of the program, the computer doesn't have any of this knowledge and it will happily generate errors against the common practice rules (e.g. parallel fifths and octaves). Not always, but sometimes, as dicated by randomness. Note that this is an area in which the program could be improved, by checking for errors while spicing up and skipping proposed spicings that introduce errors against the common practice rules.

Yet, despite the extreme simplicity of the method, the results can be surprisingly complex and in some cases sound interesting.

How can I use it?

In its current form, the program is not really easy to install and use, at least if you have no computer experience:
  • You need to install the free python programming language from http://www.python.org/download/releases. I recommend using version 2.7. Version 3 and later of python won't work! 
  • You also need to install the free music21toolkit for computer-aided musicology. Follow the instructions on their website. Music21 provides for vast amounts of music knowledge which would take a long time to write ourselves. I'm using only a fraction of its possibilities in the canon generator.
  • Then you need something to visualize and audition the MusicXml that is generated by the program. For our purposes, the free MuseScore program works perfectly.
  • Finally you need to get the free canon-gen.py program from the github repository https://github.com/shimpe/canon-generator.
The main function defined near the bottom of the canon-gen.py file contains some parameters you can edit to experiment with the generator:
  • chords = "C F Am Dm G C"
    #You can insert a new chord progression here. 
  • scale = music21.scale.MajorScale("C")
    #You can define a new scale in which the notes of the chords should be interpreted here 
  • voices = 5
    #Define the number of voices in your canon here 
  • quarterLength = 2
    #Define the length of the notes used to realize the chord progression
    #(don't choose them too short, since the automatic spicing-up will make them shorter) 
  • spice_depth = 1
    # Define how many times a stream (recursively) should be spiced up
    # e.g. setting 2 will first spice up the chords, then again spice up the already spiced chords.
    # scores very quickly become rhytmically very complex for settings > 2
  • stacking = 1
    # the code can generate multiple versions of the spiced up chord progression, and use those
    # versions to create extra voices
    # e.g. setting stacking = 2 will turn a 3-voice canon into a 3*2 = 6-voice canon 
  • voice_transpositions = { VOICE1 : +12, VOICE2 : 0, VOICE3 : -12, VOICE4: -24, VOICE5: 0 }
    # allow extra octave jumps between voices

What does it sound like?

This is a simple example generated with the program with settings
  • chords = "C F Am Dm G C"
  • scale = music21.scale.MajorScale("C")
  • voices = 5
  • quarterLength = 2 
  • spice_depth = 1
  • stacking = 1 
  • voice_transpositions = { VOICE1 : 0, VOICE2 : 0, VOICE3 : -12, VOICE4: -24, VOICE5: 0 }  

Ideas for future improvements

I see many possible improvements, most of which are low-hanging fruits. Feel free to jump in and improve the code :D
  • fix a known bug related to octaviations in keys other than C
  • support modulations, i.e. keep musical key per measure/beat instead of over the complete chord progression
  • extend the code to cover the things explained in the later articles: crab and table canons
  • see if the method/code can be extended to generate canons at the third, fifth, ...
  • smarter spicing up of chord progressions to avoid parallel fifths/octaves (e.g. rejecting a proposed spice if it introduces an error in the overall stream); or use Dmitry Tymoczko's voice leading spaces to ensure better voice leading by construction.
  • protect the end chord from getting spiced up
  • implement more note transformations, e.g. appogiatura
  • experiment with more rhythms
  • how can we better spice up the chord progressions without messing up too much of the original harmonies?
  • ... 

Wednesday, June 19, 2013

Seaside canon, for Douglas Hofstadter

What?

Here's a musical crab canon inspired by a blog post from Julia Galef, who wrote quite an impressive crab canon poem. My musical interpretation of her poem is a crab canon too. It differs from my previous crab canon in the following aspects:

  1.  This canon was conceived after serendipitously finding a poem written by Julia Galef, who kindly granted me permission to use it.
  2. Compared to the previous crab canon, this one uses more and longer pedal notes. What can I say - I just love pedal notes! 
  3. In contrast to my previous crab canon, this one modulates to different keys. Have you discovered my free modulation book yet?
  4. As a nice variation on a typical crab canon (where you can reverse the score), here you can also reverse the raw audio samples. That's right! When you reverse the raw audio samples and listen to the result, you get the same piece again. Mind you: I'm not just talking the notes, but also the timbres! Nothing should sound more "backward" than in the forward version of the piece. You can try it out! Download the raw audio and import it into your favourite audio editor. Then listen to it, and relisten to it in reverse.
  5. The spectrogram (slowly appearing in black on white) looks like the sea, and the score layed on top of it in colored rectangles looks like a frontal view of a crab. Very appropriate for a crab canon about seaside :) Ok, that's a joke (or not? Try squinting your eyes... :D ) 
 With respect to the images that pass by: in black on white you can see a spectrogram of the piece. In colored rectangles I've overlayed half of the notes that are being played in the canon (four voices). The other half of the notes you hear come from those same voices simultaneously playing backwards.

 I've developed a systematic method for constructing music canons, crab & palindrome canons, and table canons using Lilypond, fully documented in a series of articles that you can freely download and use. It's advised to read them in the order listed below:

Tutorial on my technique for writing a Canon
How to write a crab Canon
6-part Invention
How to write a table Canon

I'm aware that the method I devised in its current form still has limitations and does not necessarily yield music that can compete with the Big Composers, but who knows... I  still have some ideas for further experimentation. And besides, it's just too much fun :)

Saturday, July 28, 2012

Explanation about technique used in writing my 6-part invention

Download the article by clicking this link. You may have trouble downloading it with some versions of internet explorer. In that case use chrome or firefox instead.

You can hear the result of the tutorial in my previous post.

Friday, July 27, 2012

6-part Invention

What? Not another one??

In a previous post I explained what technique I used to write a Canon in 5 parts without much sweating. I've been experimenting a bit more with the technique and here's the result of one of those newer experiments: a 6-part invention.

Structure

As it goes with such pieces it's interesting to highlight the structure of the piece. The piece starts with a theme. The theme is repeated at different starting pitches (basic fugal treatment), and counter-subjects are introduced which are really just the theme in canon to itself. The canon is left to play in 3 voices at a few different starting pitches, and then gradually morphs into a different canon with a different theme. The new canon is also given some basic fugal treatment, and finally both canons sound simultaneously forming a 6-part super-canon with two themes playing simultaneously in 3 different time-delayed versions each. The piece rests in peace after a soothing final chord.

How did you make this?

The piece is generated using the techniques explained in my tutorial on writing canons, with some cool twists. I intend to fully explain the creation of this piece including the full score and algorithms in a new tutorial that will appear in a separate post. Stay tuned :)

Enough already... let me hear what this is all about

Ok.. ok... Here it is:

And here it is as well:

Tuesday, July 24, 2012

Tutorial on my technique for writing a canon

I promised to write a tutorial on how to write a canon like the one I posted in my previous post Canon in 5, and I did.

The tutorial is 12 pages long, but most of that space is taken up by white-space and examples. You will probably get the most out of it if you already know how to read music, although strictly speaking it is not a requirement to already benefit from the material in the tutorial.

You can get the tutorial in .pdf format by clicking this link. You may have trouble downloading it with some versions of internet explorer. In that case use chrome or firefox instead.

You can listen to the result of the tutorial here:

Drop me a note if you've been able to use the techniques explained in there.

Monday, July 23, 2012

Canon in 5

Canon in 5

So I've been experimenting today with an idea I had on how to write a canon without needing to know a lot of music theory. I want to explain the method I used in a later post, but here's the result of a test drive with a short theme repeated 5 times.

To conceive this 5 part fugue using the method I invented (or more likely: rediscovered :-) ) was a matter of a few minutes. The creation of the score and the rendering to audio and video took a bit longer.

You can hear the synthesizer version here:

You can download the score by clicking this link. And here's a preview of the score:

Enjoy!