Skip to content

Instantly share code, notes, and snippets.

@aravindpai
Created March 26, 2020 21:37
Show Gist options
  • Save aravindpai/a493614f1cbc64989b538b01cd616271 to your computer and use it in GitHub Desktop.
Save aravindpai/a493614f1cbc64989b538b01cd616271 to your computer and use it in GitHub Desktop.
def convert_to_midi(prediction_output):
offset = 0
output_notes = []
# create note and chord objects based on the values generated by the model
for pattern in prediction_output:
# pattern is a chord
if ('.' in pattern) or pattern.isdigit():
notes_in_chord = pattern.split('.')
notes = []
for current_note in notes_in_chord:
cn=int(current_note)
new_note = note.Note(cn)
new_note.storedInstrument = instrument.Piano()
notes.append(new_note)
new_chord = chord.Chord(notes)
new_chord.offset = offset
output_notes.append(new_chord)
# pattern is a note
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
# increase offset each iteration so that notes do not stack
offset += 1
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='music.mid')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment