Skip to content

Commit

Permalink
saving MIDI files
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaeleidus committed Mar 5, 2012
1 parent 5bf124d commit d08a82c
Show file tree
Hide file tree
Showing 5 changed files with 2,018 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pkl
*.pyc
*.pyc
*.mid
17 changes: 16 additions & 1 deletion geneticMusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import random
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
Expand Down Expand Up @@ -59,7 +60,7 @@ def mainOctave(self):
class Chord:
def __init__(self):
self.notes=[]
self.intensity = random.randint(1, 10)
self.intensity = random.randint(1, 10) * 10
def addRandomNotes(self, scale, count):
self.notes = random.sample(scale.scaleNotes, count)
def __repr__(self):
Expand Down Expand Up @@ -95,6 +96,19 @@ def __repr__(self):
def getKey(self):
return notes[self.key]

def createFile(self):
MIDI = MIDIFile(1)
MIDI.addTrackName(0,0,self.name)
beat = 0
for chord in self.chords:
for note in chord.notes:
MIDI.addNote(0,0,note,beat,1,chord.intensity)
beat = beat + 1
midiFile = open("Songs/%d.%d-%s.mid"%(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) + ']'
Expand Down Expand Up @@ -137,6 +151,7 @@ def main(argv=None):
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:
Expand Down
Loading

0 comments on commit d08a82c

Please sign in to comment.