Created
February 9, 2018 03:05
-
-
Save vdaluz/1baa55edcfbf080f757fc26671924a48 to your computer and use it in GitHub Desktop.
Music generated from the Fibonacci sequence and output to midi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import midi | |
# Music generated from the Fibonacci sequence and output to midi | |
# Inspired by https://dev.to/daniel40392/visualizing-fibonacci-for-the-music-lover-in-you-2609 | |
# Using https://github.com/vishnubob/python-midi for midi output | |
# Please excuse any python or music theory mistakes, I'm not an expert at either | |
print('Welcome to the Fibonacci Composer.') | |
x = int(input('Enter Song Length: ')) | |
k = input('Enter Song Key (C, G, D, A, E, B, F#, Db, Ab, Eb, Bb, F): ') | |
q = input('Enter Key Quality (maj, min): ') | |
keys = { | |
'C': ["C","D","E","F","G","A","B"], | |
'G': ["G","A","B","C","D","E","F#"], | |
'D': ["D","E","F#","G","A","B","C#"], | |
'A': ["A","B","C#","D","E","F#","G#"], | |
'E': ["E","F#","G#","A","B","C#","D#"], | |
'B': ["B","C#","D#","E","F#","G#","A#"], | |
'F#': ["F#","G#","A#","B","C#","D#","E#"], | |
'Db': ["Db","Eb","F","Gb","Ab","Bb","C"], | |
'Ab': ["Ab","Bb","C","Db","Eb","F","G"], | |
'Eb': ["Eb","F","G","Ab","Bb","C","D"], | |
'Bb': ["Bb","C","D","Eb","F","G","A"], | |
'F': ["F","G","A","Bb","C","D","E"] | |
} | |
relatives = { | |
'A': 'C', | |
'E': 'G', | |
'B': 'D', | |
'F#': 'A', | |
'Db': 'E', | |
'Ab': 'B', | |
'Eb': 'F#', | |
'Bb': 'Db', | |
'F': 'Ab', | |
'C': 'Eb', | |
'G': 'Bb', | |
'D': 'F' | |
} | |
pitches = { | |
'C': midi.C_3, | |
'G': midi.G_3, | |
'D': midi.D_3, | |
'A': midi.A_3, | |
'E': midi.E_3, | |
'B': midi.B_3, | |
'F#': midi.Fs_3, | |
'Gb': midi.Fs_3, | |
'C#': midi.Cs_3, | |
'Db': midi.Cs_3, | |
'G#': midi.Gs_3, | |
'Ab': midi.Gs_3, | |
'D#': midi.Ds_3, | |
'Eb': midi.Ds_3, | |
'A#': midi.As_3, | |
'Bb': midi.As_3, | |
'E#': midi.F_3, | |
'F': midi.F_3 | |
} | |
key = keys[k] if q=='maj' else keys[relatives[k]] | |
song = [] | |
def fib(n): | |
"""Function that generates notes based off the Fibonnachi Series""" | |
a=0 | |
b=1 | |
print('Fibonacci in ' + str(k) + str(q) + ': ', end=' ') | |
first = 0 if q=='maj' else 5 | |
song.append(key[first]) | |
for i in range (1,n): | |
fib=a+b | |
mod = fib%7 - 1 if fib%7 - 1 >=0 else 6 | |
mod = mod if q=='maj' else (mod+5)%7 | |
song.append(key[mod]) | |
a=b | |
b=fib | |
for i in song: | |
print (i + ', ', end='') | |
print('\n') | |
to_midi(song, 'fibonacci-' + str(k) + str(q) + '.mid') | |
def to_midi(song, filename): | |
pattern = midi.Pattern() | |
track = midi.Track() | |
pattern.append(track) | |
for i in song: | |
on = midi.NoteOnEvent(tick=0, velocity=60, pitch=pitches[i]) | |
track.append(on) | |
off = midi.NoteOffEvent(tick=100, pitch=pitches[i]) | |
track.append(off) | |
eot = midi.EndOfTrackEvent(tick=1) | |
track.append(eot) | |
midi.write_midifile(filename, pattern) | |
while x != '': | |
fib(x) | |
print() | |
quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment