-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonlinepianoplayer.py
252 lines (208 loc) · 5.46 KB
/
onlinepianoplayer.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
import time
import ctypes
import sys
import pyHook
from win32gui import PumpMessages, PostQuitMessage
import threading
SendInput = ctypes.windll.user32.SendInput
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time",ctypes.c_ulong),
("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
def PressKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
user32 = ctypes.WinDLL('user32', use_last_error=True)
#dict. of key scancodes
keydict = {"Q":0x10,
"W":0x11,
"E":0x12,
"R":0x13,
"T":0x14,
"Y":0x15,
"U":0x16,
"I":0x17,
"O":0x18,
"P":0x19,
"[":0x1A,
"]":0x1B,
"RETURN":0x1C,
"LCONTROL":0x1D,
"A":0x1E,
"S":0x1F,
"D":0x20,
"F":0x21,
"G":0x22,
"H":0x23,
"J":0x24,
"K":0x25,
"L":0x26,
";":0x27,
"'":0x28,
"`":0x29,
"LSHIFT":0x2A,
"\\":0x2B,
"Z":0x2C,
"X":0x2D,
"C":0x2E,
"V":0x2F,
"B":0x30,
"N":0x31,
"M":0x32,
",":0x33,
".":0x34,
"/":0x35,
"RSHIFT":0x36,
"*":0x37,
"LMENU":0x38,
" ":0x39,
"CAPITAL":0x3A,
"1":0x02,
"2":0x03,
"3":0x04,
"4":0x05,
"5":0x06,
"6":0x07,
"7":0x08,
"8":0x09,
"9":0x0A,
"0":0x0B,
"!":0x02,
"@":0x03,
"#":0x04,
"$":0x05,
"%":0x06,
"^":0x07,
"&":0x08,
"*":0x09,
"(":0x0A,
")":0x0B
}
MUSIC = '''p [wP] y o P [qs] i p [(P]
o P d [8D] o d s [wP] y
o P [qs] i p [(P] o P d
[8D] o d s [wP] y o P [qs] i
p [(P] o P d [8D] o d s
[wP] y o P [qp] t i [(o] t
i [(o] i o p [5wP] y o P
[4qs] i p [@(P] o P d [18D] o
d s [5wP] y o P [4qs] i p [@(P]
o P d [18D] o d s [5wP] y
o P [4qs] i p [@(P] o P d
[18D] o d s [5wP] y o P [4qp] t
i [@(o] t i [@(o] i o p
[5wP] y [5wo] P [4qs] i [4qp] P [@(] o
[@(P] d [18D] o [18d] s [5wP] y [5wo] P
[4qs] i [4qp] P [@(] o [@(P] d [18D] o
[18d] s [5wP] y [5wo] P [4qs] i [4qp] P
[@(] o [@(P] d [18D] o [18d] s [5wP] y
[5wo] P [4qp] t [4qi] o [5w] t i o
[5w] [5w] y y Y Y y'''
MUSIC = MUSIC.replace("\n", " ").replace("{", "[").replace("}", "]").replace("|", " ").replace(" - ", " ")
TEMPO = 120
cont = True
killSelf = False
def play(v=False, contf=None):
'''
Plays out the string listed in music by sending out keystrokes.
Chords can be played with placing keys in brackets - i.e. [keys].
| are converted to " " for compatibility with some sheet music.
Curly braces are interpreted as brackets: remove those if not supposed to be chords.
Args:
v (optional): default false, sets the verbose flag to true
contf (optional): default None, python function to verify if playing should continue
Returns:
N/A
'''
global killSelf
delay = 1 / TEMPO * 60
if v:
print(f"Time between each note: {delay}")
if contf:
contf()
building = False
mult = 1/4
pDelay = delay
pMult = mult
specials = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")"]
for b in MUSIC:
if cont:
if b == "]":
delay = pDelay
mult = pMult
elif b == "[":
pDelay = delay
delay = 0.005
pMult = mult
mult = 1
elif b != " ":
bd = keydict.get(b.upper(), 0x2C)
use_shift = b.isupper() or b in specials
if use_shift:
PressKey(keydict.get("LSHIFT"))
PressKey(bd)
ReleaseKey(bd)
if use_shift:
ReleaseKey(keydict.get("LSHIFT"))
time.sleep(delay * mult)
elif cont == False:
while not cont:
time.sleep(0.01)
else:
break
killSelf = True
def _hhook(event):
global cont
if event.ScanCode == 0x3F:
cont = not cont
print(f"Swapped: {event.Key} pressed")
elif event.ScanCode == 0x3E or killSelf:
cont = None
print(f"Stopped: {event.Key} pressed")
PostQuitMessage(0)
hm.UnhookKeyboard()
return True
def _hook():
global hm
hm = pyHook.HookManager()
hm.KeyDown = _hhook
hm.HookKeyboard()
PumpMessages()
def hook():
t = threading.Thread(target=_hook)
t.start()
def main():
play(v=True, contf=hook)
if __name__ == "__main__":
time.sleep(5)
main()