-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi4fpga.py
97 lines (79 loc) · 2.26 KB
/
midi4fpga.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
import pretty_midi
import re
import sys
import os
DEBUG = False
COMMENT = ";"
def main():
argvs = sys.argv
argc = len(argvs)
if argc < 2:
print "specify input files"
quit()
filename = os.path.basename(argvs[1]).split(".")
if os.path.isdir("%s"%(filename[0])) == False:
os.makedirs("%s"%(filename[0]))
filepath = "%s/"%filename[0]
midi_data = pretty_midi.PrettyMIDI(argvs[1])
track_number = 0
for instrument in midi_data.instruments:
prev_end = 0
total_length = 0
if len(instrument.notes) == 0:
continue
fo = open(filepath+"%d_"%track_number
+pretty_midi.constants.INSTRUMENT_MAP[instrument.program]+".txt","w")
fo.write(COMMENT+pretty_midi.constants.INSTRUMENT_MAP[instrument.program]+"\n")
for note in instrument.notes:
start = midi_data.time_to_tick(note.start)
end = midi_data.time_to_tick(note.end)
length = end - start
pitch = note.pitch
velocity = note.velocity / 8
octave = int(pitch / 12 - 2)
note_num = pitch % 12
if note_num == 0:
note_code = "C"
elif note_num == 1:
note_code = "Db"
elif note_num == 2:
note_code = "D"
elif note_num == 3:
note_code = "Eb"
elif note_num == 4:
note_code = "E"
elif note_num == 5:
note_code = "F"
elif note_num == 6:
note_code = "Gb"
elif note_num == 7:
note_code = "G"
elif note_num == 8:
note_code = "Ab"
elif note_num == 9:
note_code = "A"
elif note_num == 10:
note_code = "Bb"
elif note_num == 11:
note_code = "B"
if DEBUG == True:
fo.write("%s%s %s %s %s\n"%(COMMENT,str(note_code)+str(octave) , start, end, length))
if prev_end > start:
continue
# print str(note_code)+str(octave) , start, end, length
rest_length = start - prev_end
if rest_length > 0:
while rest_length > 4000:
fo.write("R 0 %s\n"%4000)
rest_length -= 4000
total_length += 4000
fo.write("R 0 %s\n"%rest_length)
total_length += rest_length
fo.write("%s %s %s\n"%(str(note_code)+str(octave), velocity ,length))
prev_end = end
total_length += length
fo.write("CMD 0 0\n")
fo.write("%sTotal length %s\n\n"%(COMMENT,total_length))
track_number += 1
if __name__ == '__main__':
main()