-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneticMusic.py
176 lines (159 loc) · 6.62 KB
/
geneticMusic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import copy
from pprint import pprint
import random
import string
import pickle
import sys
import random
import os
from urllib.request import urlopen
from progress_bar import ProgressBar
from midiutil.MidiFile3 import MIDIFile
middleC = 60
base = middleC-3 #note value of middle A
octave = 12 #there are 12 potential notes in each octave
generationSize = 20 #how many songs should be generated in each generation
filename = "GeneticMusic" #.pkl will be added
songFolder = "Songs"
noteList = ['A-1','A-1#','B-1','C-1','C-1#','D-1','D-1#','E-1','F-1','F-1#','G-1','G-1#',
'A','A#','B','C','C#','D','D#','E','F','F#','G','G#',
'A2','A2#','B2','C2','C2#','D2','D2#','E2','F2','F2#','G2','G2#',
'A3','A3#','B3','C3','C3#','D3','D3#','E3','F3','F3#','G3','G3#']
keys = range(base, base+octave)
notes = {}
for i in range(len(noteList)): #build dictionary of notes to lookup the name of the note later
notes[i+base-octave] = noteList[i]
class Mode:
def __init__(self, name, notes):
self.name = name
self.notes = notes
modes = [
Mode("Ionian", [0,2,4,5,7,9,11]),
Mode("Dorian", [0,2,3,5,7,9,10]),
Mode("Phrygian", [0,1,3,5,7,8,10]),
Mode("Freygish", [0,1,4,5,7,9,11]),
Mode("Lydian", [0,2,4,6,7,9,11]),
Mode("Mixolydian", [0,2,4,5,7,9,10]),
Mode("Aeolian", [0,2,3,5,7,8,10]),
Mode("Locrian", [0,1,3,5,6,8,10])
]
class Scale:
def __init__(self, key, mode):
self.start = key
self.key = notes[key]
self.mode = mode
self.scaleNotes = []
for note in self.mode.notes:
self.scaleNotes.append(note+self.start-octave)
for note in self.mode.notes:
self.scaleNotes.append(note+self.start)
for note in self.mode.notes:
self.scaleNotes.append(note+self.start+octave)
self.scaleNotes.append(self.start+(2*octave)) #repeat the first scale degree 2 octaves up
def mainOctave(self):
return self.scaleNotes[8-1:(2*8)]
def __repr__(self):
return repr("%s %s" % (self.key, self.mode.name))
class Chord:
types = {"triad":[0,2,4], "seventh":[0,2,4,6], "dominant9":[0,2,4,8], "dominant11":[0,2,4,8,10], "dominant13":[0,2,3,8,12]}
def __init__(self):
self.degrees=[] #scale degrees
self.intensity = random.randint(1, 10) * 10
def addRandomNotes(self, count):
#if count != 0:
#count = 1
self.degrees = random.sample(range(22), count)
# self.chordType = random.choice(list(Chord.types.keys()))
# startDegree = random.randint(0,9)
# self.degrees = [degree+startDegree for degree in Chord.types[self.chordType]]
def __repr__(self):
return ( "%s" % (repr(self.degrees)))
def randomWord(wordType):
urls = ["http://api.wordnik.com/v4/words.json/randomWord?hasDictionaryDef=true&includePartOfSpeech=adverb,adjective&api_key=1a726d79d51b76f7664090f16750cc75292f05d3df6083875",
"http://api.wordnik.com/v4/words.json/randomWord?hasDictionaryDef=true&includePartOfSpeech=noun,adjective,verb&api_key=1a726d79d51b76f7664090f16750cc75292f05d3df6083875"]
words = str(urlopen(urls[wordType]).read()).replace('"', '')
word = dict(item.split(":") for item in words[3:-2].split(','))['word']
return ''.join(e for e in word if e.isalnum())
class Song:
def __init__(self):
self.mutationPoints = 10
self.generation = 0
self.songnum = 0
self.name = randomWord(0) + " " + randomWord(1)
self.bpm = random.randint(100, 300)
self.key = random.randint(base,base+octave-1)
self.mode = random.choice(modes)
self.scale = Scale(self.key, self.mode)
self.chords = []
self.score = -1
self.parents = None
for _ in range(20):
chord = Chord()
chord.addRandomNotes(int(random.triangular(0,7,3)))
self.chords.append(chord)
def __repr__(self):
return ( "Song(%s.%s: %s, 'tempo': %s, 'scale' : %s, 'chords' : %s )" %(repr(self.generation), repr(self.songnum), repr(self.name), repr(self.bpm), repr(self.scale), repr(self.chords)))
def getKey(self):
return notes[self.key]
def createFile(self):
MIDI = MIDIFile(1)
MIDI.addTrackName(0,0,self.name)
MIDI.addTempo(0,0, self.bpm)
beat = 0
for chord in self.chords:
for degree in chord.degrees:
MIDI.addNote(0,0,self.scale.scaleNotes[degree],beat,1,chord.intensity)
beat = beat + 1
if not os.path.exists(songFolder):
os.makedirs(songFolder)
midiFile = open("%s/%d.%d-%s.mid"%(songFolder, self.generation, self.songnum, self.name), 'wb')
MIDI.writeFile(midiFile)
midiFile.close()
def prompt(prompt, options = [], required = False):
if len(options) > 0:
prompt = prompt + '[' + '|'.join(options) + ']'
prompt = prompt + ': '
resp = input(prompt)
while (required and resp == "") or (len(options)> 0 and resp not in options and not (not required and resp == "")):
print ("Please enter a valid option")
resp = input(prompt)
return resp
def main(argv=None):
if argv is None:
argv = sys.argv
data = {0:[]}
songs = data[max(list(data.keys()))]
print ("Would you like to:\n\t[1]Load a previous saved generation\n\t[2]Generate a new random generation\n\t[3]Print Previous Generation Data")
choice = prompt("Please select an option", ["1", "2", "3"], True)
if choice == "1":
print ("Loading last generation")
data = pickle.load(open(filename+".pkl", "rb"))
songs = data[max(list(data.keys()))]
for song in songs:
print ("%d.%d: %s" % (song.generation, song.songnum, song.name))
if song.score == -1:
while True:
try:
score = float(prompt("Song score"))
song.score = score
pickle.dump(data, open(filename+".pkl", "wb"))
break
except ValueError:
print("Please enter a valid number")
else:
print ("Score: %s" % (song.score))
print ()
elif choice == "2":
print ("Generating Songs")
for i in ProgressBar.progressbar(range(generationSize)):
songs.append(Song())
s = songs[-1]
s.songnum = i
s.createFile()
pickle.dump(data, open(filename+".pkl", "wb"))
print("Generation 0 generated and saved to file")
else:
data = pickle.load(open(filename+".pkl", "rb"))
pprint(data[max(list(data.keys()))])
if __name__ == "__main__":
main()