Music is a fascinating blend of art and math. The creation of a song is absolutely an artistic, creative act — but one which generally follows quite a few explicit and implied rules. The music that we enjoy has patterns — if you play the major scale CDEFGAB and then stop, Western ears “want” to hear that final tonic C. We generally find major and minor triads to be harmonious, and generally find other combinations of tones to be dissonant.
A while ago, I came across a fascinating YouTube video on Shostakovich’s Fugue in A Major. Rather, it starts out in A and then goes on a harmonious tour of the Circle of Fifths, modulating only into keys that can be reached by a direct harmonious transition. C major, for example, could modulate into A minor, since C and E form part of the A minor triad. It’s beautiful, and seems to be able to just dance around the Circle of Fifths at will without creating any dissonances. It sounds like the sort of music gemstones would listen to.
That fugue (and video), along with having recently discovered FreeBasic’s MIDI capabilities gave me the idea to try writing a program to play random notes, choosing only from those which made musical sense. First, I had to find out what that meant — and an easy first answer turned out to be “those notes that form major or minor triads.”
From there, I made an Excel spreadsheet to figure out which transitions were possible. Neglecting octave duplicates, it turns out that for each pairing of two notes (that have to start out as either a minor third, major third, or fifth apart for this to work), exactly two new notes are possible — one major and one minor — since any two notes that are a minor third, major third, or fifth apart are musically ambiguous and can form one unique major and one unique minor chord. C and G, for instance, could form C major or C minor. C and E could make C major or A minor.
Seeding the system with C and E, I turned it loose and recorded the output with Audacity. The result is an aimless random walk around the piano keyboard, with few limitations — except that it never plays any dissonant notes. It’s a monkey at a piano keyboard, but either a minimally musically-inclined monkey or a charmed keyboard, enchanted to never play a sour note. (There’s also provision to bring the melody up or down an octave if it gets too low or too high — the farther from Middle C it gets, the more likely it is to be shifted.)
The next step was to add some variation. Tempo was easiest to change, so I added some code to vary the tempo between two limits. (In retrospect, the high limit may be a little too frenetic.)
With the tempo variations, this sounds a bit more human, but still lacks any real sense of where it’s going. At this point, I started wondering what would happen if it were to show bias in which transition it chose. For each possible new note, it can pick major or minor — so I decided to try biasing it one way or the other.
First, I tried making it 95% likely to take the major branch and 5% likely to take the minor one. Even though I knew this would result in a lot of major-key scales, it was still fascinating to see what a change in the music’s mood could be made, just from tweaking a few numbers.
At some point (maybe here), I also added a 1/8 probability of simply repeating the second-to-last note played, allowing for the possibility for the melody to pause between keys and perhaps return to an original key before committing to a new one.
Biasing for major turned out well, so of course the natural thing to do next was to bias for minor. This didn’t result (at least to my ears) in a melancholy sound, but instead a more introspective one.
Next, I wanted to try producing music which switched from major to minor. So instead of choosing the major or minor transition based on constant probabilities, I set it to choose major with 95% probability, but to switch this to 95% minor a small fraction of the time. This results in a piece which commits to major or minor scales, but periodically has mood changes. The music now understands both major and minor, and goes through phases of each.
There’s still a lot to explore here — even with these rules, the melodies that result are still closer to the random, unscripted sound of windchimes than they are to Vivaldi or Mozart. Adding some dynamical systems to influence the mood would probably add quite a bit of interest. Teaching it about motifs, themes, counterpoint, and all the rest … will be trickier. For now, there’s enough musical knowledge baked into the rules that the sounds produced at least sound “syntactically” correct, though — and every so often, it stumbles across beautiful-sounding motifs and chord progressions.
Its output is plausible enough at this point that if it were playing quietly in the background of a posh store, most customers would think it’s fancy classical music (even if the music majors would be shaking their heads at the lack of any real musical plan.) It’s acceptable-to-decent meditation music as it is, and could provide some interesting raw materials for the creation of handcrafted music.
And if you want a constant stream of happy, overcaffeinated, upbeat Musak, basically guaranteed to create endless original-if-soulless major triads 24/7 with no repeats, now there’s an app for that.
(All songs CC-BY-NC-SA 2020 M. Eric Carr / Northlight Cyberacoustics.)