-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiano_player.py
executable file
·88 lines (69 loc) · 2.05 KB
/
piano_player.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
#!/usr/bin/env python3
import pygame as pg
import traceback
from midi_input import MidiInput
from keypad import Keypad
diatonic_color = (66,128,231)
chromatic_color = (59,101,151)
exit = False
clock = pg.time.Clock()
# inicjalizacja midi i ekranu
midi = MidiInput()
pg.display.init()
width = pg.display.Info().current_w
height = pg.display.Info().current_h
size = width, height
screen = pg.display.set_mode( size, pg.FULLSCREEN )
pg.mouse.set_visible( False )
background = (12,12,12)
screen.fill(background)
pg.display.update()
# inicjalizacja klawiatury
try:
keypad = Keypad( 'img/piano.png', 'img/keys/', width, 24 )
except:
traceback.print_exc()
exit = True
screen.blit( keypad, (keypad.offset,height - keypad.get_height()) )
pg.display.update()
# rozpoczęcie działania
midi.start()
while not exit:
for event in pg.event.get([pg.QUIT, pg.USEREVENT]):
if event.type == pg.QUIT:
exit = True
elif event.type == pg.USEREVENT:
try:
if event.NoteOn:
keypad.NoteOn(event.Pitch, event.Player)
elif event.NoteOff:
keypad.NoteOff(event.Pitch, event.Player)
except:
traceback.print_exc()
exit = True
try:
keypad.NextFrame()
except:
traceback.print_exc()
exit = True
screen.fill( background )
screen.blit( keypad, (keypad.offset, height - keypad.get_height()) )
for rect in keypad.rects:
if rect[1]:
my_rect = rect[0].copy()
my_rect.left += keypad.offset
my_rect.top += height - keypad.get_height()
pg.draw.rect( screen, rect[2], my_rect )
for rect in keypad.rects:
if not rect[1]:
my_rect = rect[0].copy()
my_rect.left += keypad.offset
my_rect.top += height - keypad.get_height()
pg.draw.rect( screen, rect[2], my_rect )
pg.display.update()
clock.tick(60)
# zakończenie działania
midi.end = True
midi.join()
midi.close()
pg.display.quit()