# using python midi https://github.com/vishnubob/python-midi
# install python libray with this command
# pip install git+https://github.com/vishnubob/python-midi

# library reads mid file and creates a python object
# need to transform this format

# midi.NoteOnEvent(tick=720, channel=0, data=[62, 127]),
#   midi.NoteOnEvent(tick=0, channel=0, data=[60, 127]),
#   midi.NoteOffEvent(tick=240, channel=0, data=[62, 127]),
#  midi.NoteOffEvent(tick=0, channel=0, data=[60, 127]),
#  midi.NoteOnEvent(tick=0, channel=0, data=[64, 127]),
#   midi.NoteOnEvent(tick=0, channel=0, data=[65, 127]),
#   midi.NoteOffEvent(tick=240, channel=0, data=[64, 127]),
#   midi.NoteOffEvent(tick=0, channel=0, data=[65, 127]),

# into this form

# song = [
# [],
# [],
# [],
# [D,C],
# [],
# [E,F],
# []
#]

import midi
pattern = midi.read_midifile("mario.mid")

midinotes = {
48: 'C3', 50: 'D3', 55: 'G3', 57: 'A3', 59: 'B3',
60: 'C4', 62: 'D4', 64: 'E4', 65: 'F4', 66: 'Gb4', 67: 'G4', 68: 'Ab4', 69: 'A4', 70: 'Bb4', 71: 'B4',
72: 'C5', 73: 'Db5', 74: 'D5', 75: 'Eb5', 76: 'E5', 77: 'F5', 78: 'Gb5', 79: 'G5', 80: 'Ab5', 81: 'A5', 82: 'Bb5', 83: 'B5',
84: 'C6', 86: 'D6', 88: 'E6'
}

# define two arrays the full song and each line
song = []
line = []

# pattern[1] is the part of the midi object that I need where notes are described
for note in pattern[1]:
    if isinstance(note, midi.NoteOnEvent):
        linejump = note.tick/240
        for i in range(0,linejump):
            song.append([])
        aux = note.data[0]
        line.append(midinotes[aux])
    if isinstance(note, midi.NoteOffEvent):
        if line:
            song.append(line)
            line = []

print song
