-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.py
executable file
·300 lines (258 loc) · 11 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# Based on code from https://github.com/osakared/midifile.py
# which appears to be based on
# https://github.com/gasman/jasmid/blob/master/midifile.js
# Original license:
"""
Copyright (c) 2014, Thomas J. Webb
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import pygame, struct, time, statistics, glob
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"
def read_byte(self, 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
try:
file = open(file_name, 'rb')
if file.read(4) != b'MThd': raise Exception('Not a MIDI file')
self.file_name = file_name
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
self.tracks = []
for i in range(0, self.track_count):
self.tracks.append([])
for nn, track in enumerate(self.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
print("Meta: " + 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]:
print(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]
print("tempo =", self.tempo, "bpm")
# 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
print("program change, channel", channel, "=", param1)
continue
size -= 1
param2 = self.read_byte(file)
# detect MIDI ons and MIDI offs
if type == 0x9:
track.append(Note(channel, param1, param2, abs_time))
elif type == 0x8:
for note in reversed(track):
if note.channel == channel and note.pitch == param1:
note.duration = abs_time - note.start
break
except Exception as e:
print("Cannot parse MIDI file: " + str(e))
finally:
file.close()
def __str__(self):
s = ""
for i, track in enumerate(self.tracks):
s += "Track " + str(i+1) + "\n"
for note in track:
s += str(note) + "\n"
return s
RES = 650, 440
BACKGROUND = 100, 100, 100
TARGET_FPS = 60 # FPS that PyGame is expcted to run at
SONG_DIR = "goldberg"
def ini(s, res):
"Load samples"
global audio
audio = {}
for n in range(2, 89):
# samples are numbered with middle C == 39 (i.e. off by one)
audio[n] = pygame.mixer.Sound("midisnd/midi%02u.wav" % (n - 1))
audio[n].set_volume(0.2)
def load_song(s, res, fn):
"Load song data"
global notes, t, inc, first, last
notes = []
first, last = 1e9, -1
m = MidiFile(fn)
for tn in range(len(m.tracks)):
for n in m.tracks[tn]:
if n.velocity > 0:
tt = int(1000 * n.start)
first = min(first, tt)
last = max(last, tt)
notes.append([n.pitch - 20, tt])
inc = 20 # advance by this many MIDI time units during each PyGame frame
t = 0
#print(notes)
def play(s, res, fpsfac, tfac):
"Play all notes that should trigger during this PyGame frame"
global t
s.scroll(dx = -2)
pygame.draw.rect(s, BACKGROUND, [RES[0]-2, 0, 2, RES[1]])
# apply user tempo selection and FPS adjustment
rinc = int(inc * fpsfac * tfac)
for x, y in notes:
if t <= y-first < t + rinc:
audio[x].stop()
audio[x].play()
pygame.draw.rect(s, (255,255,255), [RES[0]-2, RES[1]-5*x, 2, 2])
#print(y, x)
t += rinc
# end of song?
if t <= last + RES[0] * rinc:
return True
else:
return False
class MIDI:
def __init__(self):
pygame.init()
pygame.mixer.init()
self.res = RES
self.screen = pygame.display.set_mode(self.res)
pygame.display.set_caption('midi')
self.screen.fill(BACKGROUND)
self.clock = pygame.time.Clock()
ini(self.screen, self.res)
self.calib = 10
self.samp = []
self.last = 0
self.mlist = sorted(glob.glob(SONG_DIR + "/*.mid"))
self.mselect = 0
load_song(self.screen, self.res, self.mlist[self.mselect])
self.tempo = 1
self.paused = False
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
self.mselect += 1
self.mselect = self.mselect % len(self.mlist)
load_song(self.screen, self.res, self.mlist[self.mselect])
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
self.mselect -= 1
self.mselect = self.mselect % len(self.mlist)
load_song(self.screen, self.res, self.mlist[self.mselect])
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
self.tempo += 0.05
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
self.tempo -= 0.05
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
self.paused = not self.paused
def run(self):
self.running = True
while self.running:
self.clock.tick(60)
self.events()
self.update()
pygame.quit()
def update(self):
# measure FPS for automatic adjustment
if self.calib > 0:
f = 1 / (time.time() - self.last)
self.last = time.time()
self.calib -= 1
self.samp.append(f)
self.fps = statistics.median(self.samp)
print("FPS", self.fps)
return
self.tempo = max(0.1, min(3, self.tempo))
pygame.display.set_caption('midi (%s , tempo %.2f)' % (self.mlist[self.mselect], self.tempo))
if self.paused: return
# play notes
if not play(self.screen, self.res, TARGET_FPS / self.fps, self.tempo):
# advance to next song in playlist
self.mselect += 1
self.mselect = self.mselect % len(self.mlist)
load_song(self.screen, self.res, self.mlist[self.mselect])
pygame.display.flip()
c = MIDI()
c.run()