Skip to content

Commit

Permalink
Reintroduce Markov Chain
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmhm committed Mar 12, 2017
1 parent cff9763 commit 5a966e4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
Binary file added midi/bach_simple.mid
Binary file not shown.
Binary file modified rebuilt.mid
Binary file not shown.
15 changes: 9 additions & 6 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ def __init__(self):
self.args = args

def run(self):
fileload, resolution, format = loadMidi.load('midi/test.mid')
#print fileload
fileload, resolution, format = loadMidi.load('midi/bach_simple.mid')
# print fileload
for note in fileload:
print note.val, note.len, note.pos

writeMidi.writeList(fileload, resolution, format)
stringNotes = convert.listToString(fileload)

# print loadMidi.load('midi/bach.mid')
mc = MarkovChain(1)
mc.add_string(stringNotes)
markovNotes = ' '.join(mc.generate_text(50))
print stringNotes
print mc.generate_text()
writeMidi.writeList(convert.stringToList(markovNotes), resolution, format)

# mc = MarkovChain(10)
# mc.add_file('book.txt')
# while (True):
# str = raw_input('\npress enter to generate new text')
# print str
Expand Down
7 changes: 4 additions & 3 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ def listToString(customList):
output = ''

for note in customList:
output += str(note.pos) + ',' + str(note.len) + ',' + str(note.val) + ',' + str(note.vel) + ', '
output += str(note.pos) + ',' + str(note.len) + ',' + str(note.val) + ',' + str(note.vel) + ' '

return output

def stringToList(stringList):
output = []
splitOne = stringList.split(' ')
for note in splitOne:
splitString = stringList.split(',')
output.append(CustomNote(pos=int(splitString[0]), len=int(splitString[1]), val=int(splitString[2]), vel=int(splitString[3])))
splitString = note.split(',')
if len(splitString) > 1 :
output.append(CustomNote(pos=int(splitString[0]), len=int(splitString[1]), val=int(splitString[2]), vel=int(splitString[3])))

return output
2 changes: 1 addition & 1 deletion src/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MarkovChain:
def __init__(self, key_words=2):
self.key_words = key_words
self.lookup_dict = defaultdict(list)
self._punctuation_regex = re.compile('[,.!;\?\:\-\[\]\n]+')
self._punctuation_regex = re.compile('[.!;\?\:\-\[\]\n]+')
self._seeded = False
self.__seed_me()

Expand Down

0 comments on commit 5a966e4

Please sign in to comment.