-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.py
147 lines (121 loc) · 5.21 KB
/
keypad.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
import pygame as pg
from pygame import Surface
from os import path
from ast import literal_eval
FADE_PERIOD = 5
STEP = 2
p1_diatonic_color = (66,128,231)
p1_chromatic_color = (59,101,151)
p2_diatonic_color = (244,163,30)
p2_chromatic_color = (174,116,25)
def blit_alpha(target, source, location, opacity):
x,y = location
temp = Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
class Key():
def __init__(self, pitch, alpha, fadein: bool, fadeout: bool, rect):
self.pitch = pitch
self.alpha = alpha
self.fadein = fadein
self.fadeout = fadeout
self.rect = rect
class Keypad(Surface):
# offset, min_pitch, max_pitch, octave_width
# keypad_surface, key_surfaces, key_pressed
# key_info, rects
def __init__(self, piano_path, keys_path, screen_width, min_pitch: int):
min_pitch //= 12
min_pitch *= 12
self.min_pitch = min_pitch
keypad_octave = pg.image.load(piano_path)
octave_width, octave_heigh = keypad_octave.get_width(), keypad_octave.get_height()
self.octave_width = octave_width
octaves = int(screen_width / octave_width)
self.offset = int( (screen_width - (octave_width*octaves))/2 )
if self.offset > 0:
octaves += 2
self.offset -= octave_width
self.min_pitch -= 12
self.max_pitch = self.min_pitch + octaves*12 - 1
self.keypad_surface = Surface( (octaves*octave_width,octave_heigh) )
Surface.__init__(self, (octaves*octave_width,octave_heigh) )
for i in range(octaves):
self.keypad_surface.blit( keypad_octave, (i*octave_width,0) )
self.blit( self.keypad_surface, (0,0) )
self.key_surfaces = [[],[]]
self.key_info = []
inf = open(path.join(keys_path, 'key_info'), 'r')
for i in range(12):
self.key_surfaces[0].append( pg.image.load( path.join(keys_path, '0_'+ str(i)+'.png')) )
self.key_surfaces[1].append( pg.image.load( path.join(keys_path, '1_'+ str(i)+'.png')) )
line = inf.readline()
self.key_info.append( literal_eval(line) )
self.rects = []
self.key_pressed = []
self.key_pressed.append({})
self.key_pressed.append({})
def NewRect(self, pitch, player):
octave_offset = ((pitch - self.min_pitch) // 12) * self.octave_width
key_nr = pitch % 12
new_rect = pg.Rect( octave_offset + self.key_info[key_nr][0], 0, self.key_info[key_nr][1], 0 )
diatonic = key_nr in [0,2,4,5,7,9,11]
if diatonic:
if player == 0:
color = p1_diatonic_color
else:
color = p2_diatonic_color
else:
if player == 0:
color = p1_chromatic_color
else:
color = p2_chromatic_color
self.rects.append( (new_rect, diatonic, color) )
return new_rect
def NoteOn(self, pitch, player):
if self.min_pitch <= pitch <= self.max_pitch:
if pitch in self.key_pressed[player]:
if self.key_pressed[player][pitch].fadeout:
new_rect = self.NewRect(pitch, player)
self.key_pressed[player][pitch].rect = new_rect
self.key_pressed[player][pitch].fadein = True
self.key_pressed[player][pitch].fadeout = False
else:
new_rect = self.NewRect(pitch, player)
new_key = Key( pitch, 0, True, False, new_rect )
self.key_pressed[player][pitch] = new_key
def NoteOff(self, pitch, player):
if self.min_pitch <= pitch <= self.max_pitch:
if pitch in self.key_pressed[player]:
self.key_pressed[player][pitch].fadein = False
self.key_pressed[player][pitch].fadeout = True
def NextFrame(self):
self.blit( self.keypad_surface, (0,0) )
for player, keys in enumerate(self.key_pressed):
to_delete = []
for pitch, key in keys.items():
if key.fadeout:
key.alpha -= 1
if key.alpha <= 0:
key.fadeout = False
to_delete.append(pitch)
continue
else:
key.rect.height += STEP
if key.fadein:
if key.alpha < FADE_PERIOD:
key.alpha += 1
if key.alpha == FADE_PERIOD:
key.fadein = False
octave_offset = ((pitch - self.min_pitch) // 12) * self.octave_width
key_nr = pitch % 12
alpha = int((255/FADE_PERIOD)*key.alpha)
blit_alpha( self, self.key_surfaces[player][key_nr], (octave_offset,0), alpha )
for p in to_delete:
keys.pop(p)
for i in range( len(self.rects) - 1, -1, -1 ):
self.rects[i][0].top -= STEP
if self.rects[i][0].top + self.rects[i][0].height < -1000:
del self.rects[i]