# How to algorithmically find a chord progression for an infinite arbitrary melody?

The number Pi is played with the right hand by mapping each digit to a note in the A minor scale:

```0  1  2  3  4  5  6  7  8  9
G# A  B  C  D  E  F  G# A  B
```

In the mean time the left hand arpeggiates certain chords. One chord generally lasts for two notes of the Pi melody. Here are the first few notes and the accompanying chords:

```Digits: 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 ...
Melody: C A D A E B B F E C E A B G#B C B C A D F B F D C C A C B G#B E G#B A A D A B G#A ...
Chords: Am  Dm  C   G7  C   C   E   E   Am  Am  Dm  Dm  C   Am  E   E   Am  Am  Dm  E     ...
```

I have not transcribed the whole song (over 100 digits) but so far I could not spot a repeating chord progression. I could neither find any pattern by which the chords could be deduced from the melody.

It may well be possible that the author used the chords that sounded good to him in the context of the song.

I was wondering however, if one could algorithmically determine the chords to go with the melody seeing that this song is potentially infinite.

Any help on this would be great! And if anyone can spot a pattern in the way chords are used that would be awesome.

This is mostly an experiment but I would also like to use it as an example in my upcoming talk on computer music.

• Most obvious patterns: Only using chords Am, Dm, C, G7, E, and F. (Usually) the chord is chosen to contain at least one note from the pair. Actually I bet these rules alone would produce a very similar result. – nonpop Sep 10 '13 at 8:55
• A basic music theory class should help with this. Learning how the chords are built, based on scales, quickly gives you the knowledge to figure out which chords work well with a particular note, and when to use a particular one. – the Tin Man Sep 11 '13 at 1:06
• Pi doesn't have a repeating pattern; chords that match the melody thus couldn't possibly have such a pattern. – delete me Sep 11 '13 at 15:52

To answer your main question, yes you can do this by applying counterpoint to the melody tones. If you pick up a small book called "The Study of Counterpoint" by Johann Fux, it basically provides you with an algorithm for harmonizing any melody tone.

The guy in the video isn't doing anything special;his left hand is just cycling through a standard and pretty basic chord progression.

• The left hand can't be cycling - that's the point of using pi. – reinierpost Sep 14 '13 at 14:10
• @reinierpost what i mean to say is that his left hand isn't doing any particularly interesting or unusual. – Michael Martinez Sep 16 '13 at 16:01
• @Michael Martinez: Possibly not - still it's interesting to ask what rules these chords abide by in the example, and what sorts of rules should be followed in general. Fux's rules are an example: they are already nontrivial in that the chords that can be played with a given note do not just depend on the note being played, but also on a small window of preceding chords. – reinierpost Sep 16 '13 at 17:08

The infinity of the series isn't much of a problem: any Turing-complete language can deal with infinities. In procedural languages this tends to require rather ugly loop constructs. It's much nicer in lazy functional languages, the most prominent being Haskell.

As for the algorithm itself – you can do something usable (if somewhat boring; the solution you quoted allows a bit more jazzy stuff) quite easily, based on resolving dominants.

```module PiMelody where

import Data.List

data MelodyNote = Gs | A | B | C' | D' | E' | F' | Gs' | A' | B'
deriving (Eq, Show, Enum)

type Melody = [MelodyNote] -- Assume simple all-quavers rythm.

piMelody :: Melody
piMelody = map toEnum piDigits

data Chord = Am | Dm | C | G7 | E
deriving (Eq, Show, Enum)

type Composition = [(Melody, Chord)]
-- (Infinite) list of pairs: a melody chunk, and what chord to go with it.

chordMNotes :: Chord -> [MelodyNote]       -- Without suspensions.
chordMNotes Am = [A , C', E', A']
chordMNotes Dm = [A , D', F', A']
chordMNotes E  = [Gs, B , D', E', Gs', B', F']  -- Minor dominant may also be diminished-7th.
chordMNotes C  = [C', E']
chordMNotes G7 = [B , D', F', B']

resolves :: Chord -> [Chord]
resolves E  = [Am, E]        -- Dominants should resolve to their tonic, if at all.
resolves G7 = [C, G7, Am, E] -- For major dominant, allow also resolving to minor parallels.
resolves _ = [Am .. E]       -- Non-dominant chord can resolve to anything.

accompany :: Melody -> Composition -- Choose suitable chords for a melody.

accompany melody = acc Am melody
where acc :: Chord -> Melody -> Composition

acc lastChord (n1:n2:ml)    -- Try to find a chord that fits over two melody notes
| (Just nextChord)         --  and works with the previous (possibly dominant) chord.
all(`elem` chordMNotes ch) [n1,n2]) \$ resolves lastChord
= ([n1,n2], nextChord) : acc nextChord ml
-- If two melody notes don't fit in one chord, use two.
| (Just c1) ```

(Code as a GitHub Gist)

It can be used thus:

\$ ghci PiMelody.hs
GHCi, version 7.6.2: http://www.haskell.org/ghc/  :? for help