When Eric Morecambe mangled Grieg’s Piano Concerto on a TV special in 1971, he insisted he was “playing all the right notes, but not necessarily in the right order”. That’s a valid point, because there aren’t that many different notes on a piano and the only thing that distinguishes one tune from another is the order in which you play them.

To a mathematician or computer programmer the situation is crying out for quantitative analysis. The diagram above shows the “transition matrix” for one specific Beatles tune (using the MIDI standard where middle C is C5). It’s clear there’s a lot of order here. One thing that jumps out is that there’s only one “black” note, G#5, and it’s always followed by A5. In fact A5 is a very popular note, cropping up after no fewer than 8 different pitches. On the other hand, G#5 itself is very rare, only ever coming after D6, and then only 6% of the time.

As well as analysing the original tune, this allows us to write a new tune of our own using the same transition matrix. The result (as the aforementioned mathematicians and computer programmers will recognize) is a first-order Markov chain. Producing an algorithm of this type from scratch would be rather tedious (as indeed the initial analysis would be), but fortunately there’s some free software called OpenMusic which includes built-in Markov functions that make the process much simpler.

Of course, there’s more to a tune than the pitch of the notes – there’s the duration of a note too. But that can be analysed and reproduced by exactly the same method. I experimented with an algorithmic composition of my own, based on the Beatles song analysed above. As a first step, I used the OpenMusic Markov functions to generate a series of tune-fragments for both the “right hand” and “left hand” of the piano. Then, to give the composition some structure, I arranged the fragments in a rough approximation to classical sonata form.

I won’t say what the original song was, because I want to see if anyone can guess it. As a hint, I’ve inserted a brief quotation from the original at the mid-point of the piece. Here it is on YouTube: