Algorithmic Music Composition With Python

This talk provides a general introduction into creating music algorithmically using Python. Little prior knowledge about music is assumed. It is helpful to know how sheet music looks and what the MIDI format is beforehand.

We will start by looking briefly into the basic building blocks of music (harmony, melody and rhythm) and what our goal is (creating sheet music and a playable MIDI file).

Then we will discuss the history of algorithmic composition in music and from that we will develop ideas how we can create music from algorithms and randomness.

For creating sheet music we will look into the packages Abjad und music21.

In the end will we will create a playable MIDI file for our music using MIDIUtil.

This session took place in track Python Language & Ecosystem and was classified suitable for novice domain / novice python by the speaker.

Transcript (auto)

Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.

Speaker 1 [00:08]

Hello everyone, today I want to talk about a hobby of mine, which is composing music, especially contemporary classical music. And I want to take a look at this from the perspective of Python today, so we'll be creating music algorithmically with Python. Maybe you remember yesterday's keynote, where we already heard about generated music, where we heard about Wagner-like music generated by an AI. and today I will also generate music but without an AI, just based on normal algorithms we already know. We will look at a Python package called Music 21 which we will use to generate scores, MIDI files and in the end actual music we will listen to here in the room. And this music is not handcrafted by us but rather a musical visualization of an algorithm or a sequence of numbers. So algorithmically created music is part of contemporary classical music or so-called Neue Musik, which is a modern style of music which emerged in the 20th century and its idea is to create new and experimental sound and ideas by moving away from traditional musical forms and harmonies. It may include unusual instruments like electronics, innovative ways of composing and a rich variety of auditory experiences. And instead of using dissonant sounds as a way of tension, which is released in the end by moving to a consonant sound, the dissonance is an emancipated element of music. And this you will also experience in the end when we will listen to our algorithmic compositions here. They will not sound like, for example, pop music or classical music from the 19th or 18th century, you know, but rather like art music from the 20th or 21st century. So, maybe you already know contemporary classical composers, for example, you might know John Cage, who was very famous for challenging anything traditional in music by incorporating self-made and experimental instruments, but also maybe his most famous piece is called Four Minutes, Thirty-Three Seconds, which consists of, for example, a piano player sitting in silence on stage for four minutes and thirty-three seconds. Another contemporary composer you might know is, if you're old enough to know them, to know the film The Truman Show, that's Philip Glass has made the score for that which consists of very repetitive pattern which only are subject to subtle changes. So now let's talk a little bit about what algorithmic composition is. And usually we mean by that that numerical output of an algorithm or a sequence of numbers is mapped to notes and thus generating music. And this has been well around before we already know computers. For example, in the 18th century, so-called musical games were very popular where you throw dice, then look up in a table, a bar of music, append that to your score of music, and in the end play it. So you have randomly generated music already in the time before we know computers. And the very famous of these schemas is attributed to Wolfgang Amadeus Mozart in 1793 and is called manual to compose as many waltzes with two dice as you like without being musical or understand anything about composition at all. How does something look like? You have a table where you can look up your result of throwing a dice and how many times you have already thrown a dice. From that you get a number and that number you use to look up a bar of music and this is then just appended to your music and then you have generated a waltz, for example, or a polonaise or other things which were popular at that time. These things fell out of fashion in the 19th century and were basically rediscovered in the 20th century in the emergence of modern music, where people after World War II looked into randomness and algorithmic music. This is a website where we could generate such a Wolfgang Amadeus Mozart waltz, but unfortunately my Wi-Fi won't work here, So you have to try that out for yourself, otherwise I would do that with you now, but it unfortunately doesn't want to work today. So we will have to skip that part and talk a little bit about what modern algorithmic composition is. So starting in the 1950s, people experimented with algorithms and randomness and neural networks in music. These are basically three famous pieces which you could look up if you are interested in what other people are doing with that. And actually the first one to create algorithmic music is Lea Ranhiller who used a computer in 1956 and used algorithms and randomness like Markov chains in there and random walks to create music. Which is called the Iliac Suite for String Quartet because he used the Iliac computer to create this music. So, now let's talk a little bit about what we can produce with Python in music. So one thing we can produce is a score, this is just a set of notes, which we then could give for example to a musician to play us. We could create a MIDI file, so a completely technical description of our music, which we can play or import in a digital audio workstation, or we can produce actual sound, like for example with synthesizers. And this talk focuses on the first two parts, the Music 21 package we will look at is able to create us a score, so musical notes, musical notation, which we can look up, print, store as much as we like, and it also can create MIDI files, which we then use to produce music. It itself cannot produce the actual music, we have to play the MIDI file on our PC or imported into music programs like digital audio workstations to actually generate sound. So MIDI, you might not know it, is a technical standard for storing and communicating music. It basically models every aspect of music, like pitch, timing, velocity, and many other things more. And if we can export, or if the export of our Python program is a MIDI file, we actually have a complete description of our music with all its aspects in there. So before we have a look at the Music23 package, I will just spend a few minutes to talk about some basic topics in music theory, so that you are able to follow what we are here doing. It's of course only a short introduction and I will have links if you want to study the subject more. What we will need for creative music is a description of the pitch or the note we want to play. And this is usually done internationally using scientific pitch notation which uses the letters from the alphabet from A to G with accidents like a sharp or a flat and a number indicating the octave. And here on this picture you can see how the scientific pitch notation, like for example C4 is then mapped to a usual musical notation or where on a keyboard you would find that. And this is usually the input we give, for example, to the Music 21 package later on. If you want to learn about music theory, I would recommend you this book. It's called Open Music Theory. It's like an open source book, so you can, it's on a Creative Commons license. You can download it, republish it, change it, cite it on a slide, like I do it now, and it creates basically every aspect of modern music theory, and I'm not able to tell you all about it in this book, so I have done only a little bit excerpt here, but if you want to know more and learn why music works and how it functions, this is a very good book to learn it for free, with very good exercises and questions in it. So I've taken some excerpts of this book, and one thing I find very important to know is what are node values. So we talked about the pitch, so the frequency of a node, now we have to talk about the duration. And this is usually done in a hierarchical order where we have one node, like for example a whole node, which is then represented, or which is the same time as two half nodes, and a half node is then two quarter nodes, and so on, so it's a system in order of two. And this we also give in the end to music21 as input to be able to describe music. There are other specialities like dots, for example, if you want to lengthen the value of a note by half, or we could tie two notes and then in the end have them play at the same time, just with an elongated length. Also, we have to talk a little bit about what actually sounds good together, and this is usually done by talking about chords. So for example, if you think of a C major scale, these are all the white keys on a keyboard. You can build up chords from that by stacking the notes like shown here. And if you use them, you have basically a guarantee or a high chance that your music sounds well together if you stay in the same scale and build up chords from that same scale and use it as accompaniment or musical material in general. And usually these chords are described either by their name, for example here we have a C chord or a D chord or an E chord, or by Roman numerals indicating the step on the diatonic scale which they are on. So C is the first note of the C major scale, so one is then the first chord of the C major scale, so the C chord. And usually if you look up in textbooks, chord progressions that are used as musical accompaniment are described by using these numerical values, like for example a 1-5-6-4 chord progression used in pop music or a 2-5-1 chord progression used in jazz music and with that we come to music 21 so this is the link to the package you can which will give you the documentation it's a normal Python package you can just pip install it like every price and package as well and it has very few dependencies and with itself it's a like complete solution for everything musical creating music creating chords transposing music and many more functionality. So how would it look like? We would start by importing everything from Music 21. And then we create the most basic object, a stream. A stream is like a Python list. And it's usually used as a container for musical objects. So here I can insert notes into my stream. For example, a C4 note with a length of a half note, or a D4 note which has the length of an eighth note, and so on. And in the end, I can render that stream with a show function and get the musical output in notation, or I can write it out as a MIDI file and then use it further on to process music. And now I can click here on Play, and this would be basically the output of that stream. The node object itself needs the pitch and scientific pitch notation, for example, as we had on the last slide, or here, C sharp 4. And then we need the length of the node, so we can do that with a type argument and write it out, like half node, quarter node, whatever. Or we can use the quarter length argument, where 1.0 represents a quarter note, and everything else is then calculated from that. So 0.5 is some half of a quarter note, so an eighth note. We can also render single notes with the show method, retrieve its name, and we can do mathematical or musical things with it, like transposing it and generating thus another note, either by giving the function the number of semitones we want to transpose the music or by giving it a musical interval with a name. So here M3 is a minor third. This is a musical name for five semitones. We can also work with scales. So we can, for example, create an A major scale and then retrieve the pitches which are notes belonging to the A major scale between two notes and use that to create music, which we will later do. The stream object is the basic container in Music 21, it works like a Python list, it can store several different types of music objects and in the end you will always use that to create music by either rendering it with the show method or by writing out MIDI. It differs a little bit from a normal Python list because you can store several things at the same index. So you don't need, for example, you can have four notes stored at position zero and that will then be sounded at the same time. There's also a hierarchy of stream objects, so you can create a score, a part, or a voice, or a measure. These are subclasses of stream, and you can use them in more complex musical work to represent a hierarchy of musical objects, score being your whole part, your whole music, part being like for example an instrument and voice being one voice in the instrument if you have like a polyphonic instrument. Here's an example how you could do that, you just create the objects, score objects, part objects, voice objects, measure objects and then And you can append things to that, like time signatures and other things. And in the end, you again create node objects and append them to voices or measures. And then, as I told you, these containers can store more than one thing at the same index. So you can, for example, in a measure, insert three voices at the same position. And then these notes will sound at the same time as you can see here in the rendering of the music. The next thing of interest is chords. So instead of creating three voices by hand, you can create a chord object with the pitches you want to have on that chord. Then you create automatically an object with three sounding notes at the same time. You can do that by specifying for example the pitches here or you can use this roman numeral notation which we've seen on the slide before. So here I can say I'm in the key of C and I want the chord which represents the fourth step of that scale. I can do that with a roman numeral object and get the correct pitches. I can also specify chords by using names. So there is a constant called Harmony.ChordTypes which gives you a dictionary which contains in first what kind of chord we can create, for example a major chord, then a description what this chord is, so a major chord is a base note, a major third and a fifth, and then in the end the textual description of how we can create the chord, for example by using M or MAJ or nothing in the description. And if we do that, for example, I can use the Harmony.ChordSymbol function, write down C major, then I get a C major chord. From this chord symbol object, I can create my chord and append it to a stream, show it or play it musically. And that is the general introduction to the package, and now let's make music with it. So one idea I had based on what I talked to you about the history of algorithmic composition is creating music from a stochastic process like a random walk. So random walk is a randomly generated path by a particle which either moves left or right or plus one or minus one. And if you look at realizations of such a stochastic process, it looks like this. Now is the question, how can we turn that into music? For example, we can say the output of a random walk, if we map that onto the seven nodes of a C major scale to create a melody, then we have a second random process, we draw the node lengths randomly as well. And maybe we, since we then have only a melody line, we write a simple accompaniment deterministically, which could be like a 1-5-6-4 chord progression. At first, let's define a random walk. So what I do here is I have a random walk which takes not only plus and minus one steps, but steps from minus three to plus three with different probabilities based on the weights given to random choices. And it's limited to the values minus seven to plus seven, so if it reaches plus seven it cannot go up, it can only go down. And this makes mapping the values onto seven nodes easier than, it could be done another way but it makes it easier numerically to do that. A random walk like this in the code would look like this on a graph and now we can turn it into music. We create a C major scale object, we create a part, we iterate over the values from the random walk, we draw a node length at random either a quarter node, an eighth eighth note, a half note, and so on. And then we map using the MyScaleNextPitch function. We map our numerical value to the next pitch on the corresponding scale. And then we render out the music. So, and if you want to create an accompaniment to that, we can use these Roman numeral objects as we've seen before. Here I create a cycle of one, five, six, and four, and use that to generate chords. I append the chord, I create the chords from that Roman numeral object and I append them to the corresponding stream. And in the end I can, again using the stream objects, merge the melody line, the accompaniment line and I get a full musical score with an accompaniment and a randomly generated melody line. here again due to time reasons and come to the second compositional idea I had and here I looked up the online encyclopedia of integer sequences which is like a mathematical archive of different sequences like for example prime numbers, Fibonacci numbers and such and the idea I had here was to choose like one sequence at random and turn it into deterministic music using a chord at random, a pitch at random, and chord duration at random, and just mix it up together so every sequence would generate a different kind of music, which is then like basically atonal music. So what I do here is I create a list of pitches, a list of available chord types from this Harmony.chordTypes constant, and available durations, and a stream object, and I iterate over such a sequence from the online integer sequence and use basically the sequence value as an index into this list to create then different chords and append them to a stream and thus in the end can render out again to create music and we listen to one of them now for example this is a zero zero zero zero one zero this is the euler totion function downloaded from the online encyclopedia of integer sequences and turned into music. Thank you. Since I have only one minute left, I will come directly here to the end. So I already recommended you the book Music Theory for the 21st Century Classroom to learn about basic music theory. I would also recommend you, if you're interested in creating music, look at Music 21 and its docs. And there's also a really nice paper called Procedural Music Composition with Python, which goes into the direction of creating minimal music using Music 21, which is also really, really cool. And yeah, if you have questions, we will now have like the question sessions. But you can also come to the Rosen booth in the end and ask me questions about that. And here you'll find some social channels to connect or maybe ask questions. Thank you for listening.

Speaker 2 [24:07]

Thank you so much for your talk Hendrik and please everybody be encouraged to ask your questions via Slido and we have quite a few questions One of them and the first one is can we translate the scale into a guitar tab and play guitar sounds instead of keyboard instrument sounds?

Speaker 1 [24:22]

So this would be easily be possible. So what music 21 produces here as a score is just like a music XML Document under the hood when we could use other software to create guitar tabs from that easily But I think music 21 itself cannot do it, but we would need like third-party software

Speaker 2 [24:42]

Okay, and then I guess the next one ties into that. Do you happen to know whether Music21 has also support for guitar tabs?

Speaker 1 [24:48]

I think not.

Speaker 2 [24:51]

Is the package Music21 capable to check whether a composition respects classical composition rules like avoiding quint or octave parallels?

Speaker 1 [25:00]

You could program that, so Music 21 was basically developed not with the intention to create music, but rather with the intention to analyze music, which has a lot of analysis functions, which I have not shown yet. But you could load a Music XML document into it and then do analysis and look at, for example, movements of notes and chords and stuff like that and do such an analysis with that.

Speaker 2 [25:29]

Thank you. And then another question, additional to the other question about in-between notes, is microtonal music possible?

Speaker 1 [25:37]

I have not checked that so I would say maybe I don't know

Speaker 2 [25:43]

Okay, and then Sandra wants to know, please play the piece with the accompaniment.

Speaker 1 [25:48]

Oh, I can do that, we have to scroll back really a bit, it should be here.

Speaker 2 [26:18]

Awesome. Okay. Thank you. And then the next question, is non-temperament tuning also available?

Speaker 1 [26:30]

So basically Music 21 works in the normal temperament music system and I don't know if someone has like developed add-ons for that to support that but I think everything which is supported in this Music XML format which is used under the hood should be supported by or should be supportable by Music 21 in the end. Maybe you have to implement it yourself.

Speaker 2 [26:54]

Thank you. And the next question, does the MUSIC21 package allow to list inverted chords or triads in the ordered dictionaries too?

Speaker 1 [27:03]

I think that is possible as well, I showed only a small excerpt of this dictionary, it's really, really huge.

Speaker 2 [27:12]

Okay, thanks, and then next question. How does the package deal with non-european musical expressions like Arabic scales? Called Macam. That's the best Arabic that I have but or quarter steps between notes like half-flat

Speaker 1 [27:26]

So, built-in in Music 21 are common European scales like minor, major, pentatonic, church modes and stuff like that. But it has an interface for creating your own scales. So, if you have a scale you want to implement, it's really easy. You can give it like intervals on notes and stuff like that and can work with that in the end.

Speaker 2 [27:47]

Thank you. And then two people actually have questions about MuseScore. Is it possible to convert MuseScore files into Music 21 Python statements? And can Music 21 export to MuseScore?

Speaker 1 [27:59]

So Music 21 can export to Music XML which can be imported into MuseScore and if you want to get from MuseScore into Music 21 I think you need to save in Music XML then import it and then you have like this hierarchy with score, stream, objects and such and can work with that.

Speaker 2 [28:20]

Thank you, and we are so good on time because we have one last question here and just one minute left Usually music is made up of repeating patterns of 4 times 8 equals 32 count phrases It's a way to make these compositions more repetitive and predictable for example for dancing

Speaker 1 [28:38]

It can be made, for example, I mentioned minimal music in the beginning with the composer Philip Glass and I mentioned in the end this article here, which is called Procedural Music Composition with Python. I cannot show it right now because my Wi-Fi won't work, but this is a paper which deals with exactly this type of question, generating minimal music with Music 21 in the style of, for example, Philip Glass, Steve Reich or Alvo Pärt.

Speaker 2 [29:06]

Thank you for your interest in Hendrik's talk, and thank you, Hendrik, for your talk.

Hendrik Niemeyer

Hendrik is a C++ developer and works on software for analysis of pipeline inspection data. This includes topics like machine learning, numerical mathematics and distributed computing. Before this he completed his PhD in physics at the University of Osnabrück with a thesis about quantum mechanics and numerical simulations where he got to know and and love programming and complex, mathematical tasks. His favorite programming languages, in which he also has the most experience, are C++, Python and Rust. He describes himself as a "learning enthusiast" who always gets absorbed in trying out new things. Therefore, he values being up to date with programming languages and using the latest features of them in a meaningful way.

Social card for talk: Algorithmic Music Composition With Python