Fibonacci music

I spent most of today messing around with music based on the Fibonacci sequence. The result I’m happiest with has four voices, each using the Fibonacci sequence mod 12 to determine the pitch (in semitones), and using the sequence with other moduli (4, 5, 3 and 2 respectively) to determine the timing.

Fibonacci MIDI (9.8KB)

Fibonacci MP3 (3.0MB)

It could generously be described as “atonal crap”, but it sort of has moments of not-completely-awful-ness. Sort of.

Python code below the fold.

This produces output that can be included in a Lilypond file. For the record: I’m not actually expecting anyone to actually do this (other than me), I’m just sort of documenting it for posterity.

SCALE = ['c','cis','d','dis','e','f','fis',
         'g','gis','a','ais','b']

class Fibonacci:
    def __init__(self, mod, offset=0, coeff=1, a=0, b=1):
        self.mod = mod
        self.offset = offset
        self.coeff = coeff
        self.a = a
        self.b = b

    def next(self):
        val = self.coeff * self.a + self.offset
        (self.a, self.b) = (self.b, (self.a + self.b) % self.mod)
        return val

def note(n):
    suffix = ''
    while n < 0:
        suffix += ','
        n += len(SCALE)
    while n >= len(SCALE):
        suffix += '\''
        n -= len(SCALE)
    return SCALE[n] + suffix

def make_music(n, voices):
    for (rests, note_seq, time_seq) in voices:
        print '{',

        count = rests * 16

        while rests > 0:
            if rests >= 4:
                print 'r1',
                rests -= 4
            elif rests >= 2:
                print 'r2',
                rests -= 2
            elif rests >= 1:
                print 'r4',
                rests -= 1

        while count < n:
            time = 2**time_seq.next()
            print '%s%d' % (note(note_seq.next()), time),
            count += 64 // time
        print '}'

if __name__ == '__main__':
    make_music(9312, [
            (0, Fibonacci(12, 12), Fibonacci(4, 0)),
            (23, Fibonacci(12, 5), Fibonacci(5, 0)),
            (46, Fibonacci(12, 0), Fibonacci(3, 0)),
            (69, Fibonacci(12, -7), Fibonacci(2, 0))
            ])

Leave a Comment