-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidi.py
209 lines (176 loc) · 7.7 KB
/
midi.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import struct
import logging
logger = logging.getLogger(__name__)
class Note(object):
"""
Represents a single MIDI note
"""
note_names = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']
def __init__(self, channel, pitch, velocity, start, duration = 0):
self.channel = channel
self.pitch = pitch
self.velocity = velocity
self.start = start
self.duration = duration
def __str__(self):
s = Note.note_names[(self.pitch - 9) % 12]
s += str(self.pitch // 12 - 1)
s += " " + str(self.velocity)
s += " " + str(self.start) + " " + str(self.start + self.duration) + " "
return s
def get_end(self):
return self.start + self.duration
class MidiFile(object):
"""
Represents the notes in a MIDI file
"""
@staticmethod
def read_byte(file):
return struct.unpack('B', file.read(1))[0]
def read_variable_length(self, file, counter):
counter -= 1
num = self.read_byte(file)
if num & 0x80:
num = num & 0x7F
while True:
counter -= 1
c = self.read_byte(file)
num = (num << 7) + (c & 0x7F)
if not (c & 0x80):
break
return num, counter
def __init__(self, file_name):
self.tempo = 120
self.file_name = file_name
file = None
try:
file = open(self.file_name, 'rb')
if file.read(4) != b'MThd': raise Exception('Not a MIDI file')
size = struct.unpack('>i', file.read(4))[0]
if size != 6: raise Exception('Unusual MIDI file with non-6 sized header')
self.format = struct.unpack('>h', file.read(2))[0]
self.track_count = struct.unpack('>h', file.read(2))[0]
self.time_division = struct.unpack('>h', file.read(2))[0]
finally:
if file:
file.close()
def read_track(self, track_num=1):
file = None
try:
file = open(self.file_name, 'rb')
if file.read(4) != b'MThd': raise Exception('Not a MIDI file')
size = struct.unpack('>i', file.read(4))[0]
if size != 6: raise Exception('Unusual MIDI file with non-6 sized header')
self.format = struct.unpack('>h', file.read(2))[0]
self.track_count = struct.unpack('>h', file.read(2))[0]
self.time_division = struct.unpack('>h', file.read(2))[0]
# Now to fill out the arrays with the notes
tracks = []
for i in range(0, self.track_count):
tracks.append([])
for nn, track in enumerate(tracks):
abs_time = 0.
if file.read(4) != b'MTrk': raise Exception('Not a valid track')
size = struct.unpack('>i', file.read(4))[0]
# To keep track of running status
last_flag = None
while size > 0:
delta, size = self.read_variable_length(file, size)
delta /= float(self.time_division)
abs_time += delta
size -= 1
flag = self.read_byte(file)
# Sysex messages
if flag == 0xF0 or flag == 0xF7:
# print "Sysex"
while True:
size -= 1
if self.read_byte(file) == 0xF7: break
# Meta messages
elif flag == 0xFF:
size -= 1
type = self.read_byte(file)
if type == 0x2F: # end of track event
self.read_byte(file)
size -= 1
break
logger.debug("Meta: %s", str(type))
length, size = self.read_variable_length(file, size)
message = file.read(length)
# if type not in [0x0, 0x7, 0x20, 0x2F, 0x51, 0x54, 0x58, 0x59, 0x7F]:
logger.debug("%s %s", length, message)
if type == 0x51: # qpm/bpm
# http://www.recordingblogs.com/sa/Wiki?topic=MIDI+Set+Tempo+meta+message
self.tempo = 6e7 / struct.unpack('>i', b'\x00' + message)[0]
logger.debug("tempo = %sbpm", self.tempo)
# MIDI messages
else:
if flag & 0x80:
type_and_channel = flag
size -= 1
param1 = self.read_byte(file)
last_flag = flag
else:
type_and_channel = last_flag
param1 = flag
type = ((type_and_channel & 0xF0) >> 4)
channel = type_and_channel & 0xF
if type == 0xC: # detect MIDI program change
logger.debug("program change, channel %s = %s", channel, param1)
continue
size -= 1
param2 = self.read_byte(file)
# detect MIDI ons and MIDI offs
if type == 0x9:
note = Note(channel, param1, param2, abs_time)
if nn == track_num:
logger.debug("%s", note)
track.append(note)
elif type == 0x8:
for note in reversed(track):
if note.channel == channel and note.pitch == param1:
note.duration = abs_time - note.start
break
finally:
if file:
file.close()
return self.parse_into_song(tracks[track_num])
def parse_into_song(self, track):
notes = {}
song = []
last2 = 0
def getnote(q):
for x in q.keys():
if q[x] > 0:
return x
return None
for nn in track:
nn = str(nn).split()
start, stop = float(nn[2]), float(nn[3])
if start != stop: # note ends because of NOTE OFF event
if last2 > -1 and start - last2 > 0:
song.append(('r', getdur(last2, start)))
song.append((nn[0].lower(), getdur(start, stop)))
last2 = stop
elif float(nn[1]) == 0 and notes.get(nn[0].lower(), -1) >= 0: # note ends because of NOTE ON with velocity = 0
if last2 > -1 and notes[nn[0].lower()] - last2 > 0:
song.append(('r', getdur(last2, notes[nn[0].lower()])))
song.append((nn[0].lower(), getdur(notes[nn[0].lower()], start)))
notes[nn[0].lower()] = -1
last2 = start
elif float(nn[1]) > 0 and notes.get(nn[0].lower(), -1) == -1: # note ends because of new note
old = getnote(notes)
if old != None:
if notes[old] != start:
song.append((old, getdur(notes[old], start)))
notes[old] = -1
elif start - last2 > 0:
song.append(('r', getdur(last2, start)))
notes[nn[0].lower()] = start
last2 = start
return song
def getdur(a, b):
"""
Calculate note length for PySynth"
"""
return 4 / (b - a)