In the first part of the article, we have learned about many musical and technical concepts. Now it is time to use them to build an automatic composer. Before doing so, however, we must make certain assumptions (or rather simplifications):
- the pieces will consist of 8 bars in periodic structure (antecedent 4 bars, consequent 4 bars)
- the metre will be 4/4 (i.e. 4 quarter notes to each bar, accent on the first and third measures of the bar)
- the length of each motif is 1 bar (although this requirement appears restrictive, many popular pieces are built precisely from motifs that last 1 bar).
- only C major key will be used (if necessary, we can always transpose the piece to any key after its generated)
- we will limit ourselves to about 25 most common varieties of harmonic degrees (there are 7 degrees, but some of them have several versions, with additional sounds which change the chord colour).
What is needed to create a musical piece?
In order to automatically create a simple musical piece, we need to:
- generate the harmony of a piece – chords and their rhythm
- create motifs – their sounds (pitches) and rhythm
- create variations of these motifs – as above
- combinate the motifs and variations into a melody, matching them with the harmony
Having mastered the basics, we can move on to the first part of automatic composing – generating a harmony. Let’s start by creating a rhythm of the harmony.
Although one might be tempted to create a statistical model of the harmonic rhythm, unfortunately, (at least at the time of writing this article) there is no available base which would make this possible. Given the above, we must handle this in a different way – let’s come up with such a model ourselves. For this purpose, let’s choose a few “sensible” harmonic rhythms and give them some “sensible” probability.
The rhythms in the table are presented in terms of chord duration, and the duration is shown in the number of quarter notes. Some rhythms last two bars (e.g. , [6, 2]), and others one bar (, [1, 1, 2] etc.).
Generating a rhythm of the harmony proceeds as follows. We draw new rhythms until we have as many bars as we needed (8 in our case). Sometimes certain complications may arise from the fact that the rhythms have different lengths. For example, there may be a situation where to complete the generation we need the last rhythm that lasts 4 quarter notes, but we draw one that lasts 8 quarter notes. In this case, in order to avoid unnecessary problems, we can force drawing from a subset of 4-quarter-note rhythms.
Then, in line with the above findings, let’s suppose that we drew the following rhythms:
- antecedent: [4, 4], [2, 2], [3, 1],
- consequent: [3, 1], , [2, 2]
In the next step, we will be using the concept of likelihood. It is a probability not normalised to one (so-called pseudo-probability), which helps to assess the relative probability level of different events. For example, if the likelihoods of events A and B are 10 and 20 respectively, this means that event B is twice as likely as event A. These likelihoods might as well be 1 and 2 or 0.005 and 0.01. From the likelihoods, probability can be calculated. If we assume that only events A and B can occur, then their probability will be respectively:
In order to generate probable harmonic flows, we will first prepare the N-gram models of harmonic degrees. To this end, we will use N-gram models available on github (https://github.com/DataStrategist/Musical-chord-progressions).
In our example, we will use 1-, 2-, 3-, 4- and 5-grams.
In the rhythm of the antecedent’s harmony, there are 6 rhythmic values, so we need to prepare the flow of 6 harmonic degrees. We generate the first chord using unigrams (1-grams). Now, we first prepare the likelihoods for each possible degree and then draw while taking these likelihoods into consideration. The formula for likelihood is quite simple in this case
- X means any harmonic degree
- p(X) is the probability of the 1-gram of X
In this case, we drew IV degree (in this key of F major).
We generate the second chord using bigrams and unigrams, with a greater weight for bigrams.
likelihoodX=weight2gramp(X v IV)+weight1gram*p(X)
- p(X v IV) is the probability of the flow (IV, X)
- weightNgram is the adopted N-gram weight (the greater the weight, the greater the impact of this N-gram model, and the smaller the impact of other models)
We can adopt N-gram weights as we wish. For this example, we chose the following:
The next chord we drew was: vi degree (a minor).
The generation of the third chord is similar, except that we can now use 3-grams:
likelihoodX=weight3gramp(X v IV, vi)+weight2gramp(X v IV)+weight1gram*p(X)
And so we continue until we have generated all the necessary chords. In our case, we drew:
IV, vi, I, iii, IV, vi (in the adopted key of C major these are, respectively, F major, a minor, C major, e minor, F major and a minor chords).
This is not a very common chord progression but, as it turns out, it occurs in 5 popular songs (https://www.hooktheory.com/trends#node=22.214.171.124.4.6&key=rel)
We were able to generate the rhythms and chords which are the components of the harmony of a piece. However, it should still be noted here that, for the sake of simplicity, we didn’t take into account two important factors:
- The harmonic flows of the antecedent and consequent are very often linked in some way. The harmony of the consequent may be identical with that of the antecedent or perhaps slightly altered to create the impression that these two sentences are somehow linked.
- The antecedent and consequent almost always end on specific harmonic degrees. This is not a strict rule, but some harmonic degrees are far more likely than others at the end of musical sentences.
For the purposes of the example, however, the task can be deemed completed. The harmony of the piece is ready, now we only need to create a melody to this harmony. In the next part of our article, you will find out how to compose such a melody.