-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfalling_notes.py
More file actions
executable file
·303 lines (255 loc) · 8.54 KB
/
Copy pathfalling_notes.py
File metadata and controls
executable file
·303 lines (255 loc) · 8.54 KB
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
301
302
303
#!/usr/bin/python
# Piano trainer with falling MIDI notes and optional transposition
# Can also play the file via MIDI when the "play" commandline parameter is used.
# Press Space to pause
# Press F or S to go faster/slower
# Usage:
# python falling_notes.py midi_file [transposition] ["play"]
import os, sys
# let focus mouse clicks through to application
os.environ["SDL_MOUSE_FOCUS_CLICKTHROUGH"] = "1"
import pygame
import mido
try:
fn = sys.argv[1]
except:
fn = "midi/wtk1-prelude1.mid"
# piano size
SIZE = 1800, 300
# play notes via MIDI? third commandline argument
if len(sys.argv) > 3 and sys.argv[3] == "play":
PLAY_FILE = True
print("*** playing file via MIDI ***")
else:
PLAY_FILE = False
# transposition (halftones); second commandline argument
try:
TRANSPOSE = int(sys.argv[2])
print("*** transposing by %i halftones ***" % TRANSPOSE)
except:
TRANSPOSE = 0
# MIDI note velocity
VELOCITY = 80
# frame rate
FPS = 100
# falling notes area height
F_HEIGHT = 600
# normal falling notes speed
SPEED = 1 / FPS
# leave a little gap at the end of notes
END_GAP = 0.05
SIZE_TOTAL = SIZE[0], SIZE[1] + F_HEIGHT
inport = mido.open_input()
outport = mido.open_output()
# keyboard layout:
# 4 octaves centered on middle C = C4 (from C2 to C6)
MIN_NOTE, MAX_NOTE, WKEYS, LEFT_OCT = 36, 85, 29, 3
# 5 octaves (from C2 to C7)
# MIN_NOTE, MAX_NOTE, WKEYS, LEFT_OCT = 36, 97, 36, 3
# full 88-key piano keyboard
# MIN_NOTE, MAX_NOTE, WKEYS, LEFT_OCT = 21, 109, 52, 1 + 5/7
WHITE = 255, 255, 255
BLACK = 0, 0, 0
GRAY = 150, 150, 140
DARK = 20, 20, 20
RED = 255, 0, 0
# key horizontal position and type (0 = white, 1 = black)
kp = (
(0, 0),
(0.45, 1),
(1, 0),
(1.55, 1),
(2, 0),
(3, 0),
(3.4, 1),
(4, 0),
(4.5, 1),
(5, 0),
(5.6, 1),
(6, 0),
)
# read MIDI file
start = 200 * [0]
cur_time = 0
allow = "note_on", "note_off"
notelist = [[] for i in range(200)]
playlist = []
for msg in mido.MidiFile(fn):
cur_time += msg.time
if msg.type in allow:
if msg.type == "note_on" and msg.velocity > 0:
start[msg.note + TRANSPOSE] = cur_time
playlist.append((cur_time, msg))
if msg.type == "note_off" or (msg.type == "note_on" and msg.velocity == 0):
notelist[msg.note + TRANSPOSE].append((start[msg.note + TRANSPOSE], cur_time))
playlist.append((cur_time, msg))
# create table of piano key positions
kw = SIZE[0] // WKEYS
ww = kw - 4
wb = int(0.6 * kw) - 4
YP = SIZE[1] // 7
pianopos = 200 * [0]
# white keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1] == 0:
pianopos[i] = x - ww // 2, ww
# black keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1]:
pianopos[i] = x - wb // 2, wb
class ShowKeys:
def __init__(s):
pygame.init()
s.screen = pygame.display.set_mode(SIZE_TOTAL)
pygame.display.set_caption("Falling Notes")
s.screen.fill(WHITE)
s.clock = pygame.time.Clock()
# lights for internal MIDI notes
s.on = 200 * [0]
# lights for external MIDI notes
s.ext = 200 * [0]
s.mouse = False
s.back = pygame.Surface(SIZE)
s.background()
s.fall = pygame.Surface((SIZE[0], F_HEIGHT))
s.cur_time = 0
s.paused = False
s.speed = SPEED
def events(s):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
s.paused = not s.paused
if event.type == pygame.KEYDOWN and event.key == pygame.K_f:
s.speed *= 1.05
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
s.speed /= 1.05
if event.type == pygame.MOUSEBUTTONDOWN:
s.mouse = True
if event.type == pygame.MOUSEBUTTONUP:
s.mouse = False
if event.type == pygame.QUIT:
s.running = False
def run(s):
s.running = True
while s.running:
s.clock.tick(FPS)
s.events()
s.update()
pygame.quit()
def test_key(s, mx, my, rect):
x, y, xd, yd = rect
if not s.mouse:
return False
if (x <= mx <= x + xd) and (y <= my <= y + yd):
return True
else:
return False
def background(s):
s.back.fill(BLACK)
# draw white keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1] == 0:
pygame.draw.rect(s.back, GRAY, [x - ww // 2, 0, ww, SIZE[1]])
# draw black keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1]:
pygame.draw.rect(s.back, BLACK, [x - wb // 2, 0, wb, 0.63 * SIZE[1]])
def draw_notes(s):
s.fall.scroll(dy = 1)
pygame.draw.line(s.fall, DARK, (0, 0), (SIZE[0], 0))
for i in range(MIN_NOTE, MAX_NOTE):
if len(notelist[i]) == 0:
continue
for start, end in notelist[i]:
if start <= s.cur_time <= end - END_GAP:
pos, w = pianopos[i]
pygame.draw.line(s.fall, RED, (pos, 0), (pos + w, 0))
def update(s):
# process MIDI events from other sources
for msg in inport.iter_pending():
# ignore percussion channel
if msg.type == "note_on" and msg.channel != 9:
s.ext[msg.note] = 1
if msg.type == "note_off" or (msg.type == "note_on" and msg.velocity == 0):
s.ext[msg.note] = 0
s.screen.blit(s.back, (0, F_HEIGHT))
# draw white keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1] == 0:
if s.on[i] or s.ext[i]:
pygame.draw.rect(
s.screen, RED, [x - ww // 2, SIZE[1] - YP + F_HEIGHT, ww, YP]
)
# draw black keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1]:
if s.on[i] or s.ext[i]:
pygame.draw.rect(
s.screen, RED, [x - wb // 2, 0.63 * SIZE[1] - YP + F_HEIGHT, wb, YP]
)
mx, my = pygame.mouse.get_pos()
# test black keys
on_black = False
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1]:
if s.test_key(mx, my, [x - wb // 2, F_HEIGHT, wb, 0.63 * SIZE[1]]):
if s.on[i] == 0:
msg = mido.Message("note_on", note=i, velocity=VELOCITY)
outport.send(msg)
s.on[i] = 1
on_black = True
else:
if s.on[i] == 1:
msg = mido.Message("note_off", note=i)
outport.send(msg)
s.on[i] = 0
# test white keys
for i in range(MIN_NOTE, MAX_NOTE):
p = kp[i % 12]
oc = i // 12 - LEFT_OCT
x = kw * (p[0] + 7 * oc + 0.5)
if p[1] == 0:
if s.test_key(mx, my, [x - ww // 2, F_HEIGHT, ww, SIZE[1]]) and not on_black:
if s.on[i] == 0:
msg = mido.Message("note_on", note=i, velocity=VELOCITY)
outport.send(msg)
s.on[i] = 1
else:
if s.on[i] == 1:
msg = mido.Message("note_off", note=i)
outport.send(msg)
s.on[i] = 0
# draw falling notes
if not s.paused:
s.draw_notes()
s.screen.blit(s.fall, (0, 0))
s.cur_time += s.speed
if PLAY_FILE and len(playlist) > 0 and s.cur_time >= playlist[0][0] + F_HEIGHT / FPS:
t, msg = playlist.pop(0)
msg.note += TRANSPOSE
outport.send(msg)
pygame.display.set_caption("Falling Notes (speed: %.3fx)" % (s.speed / SPEED))
pygame.display.flip()
c = ShowKeys()
c.run()