-
Notifications
You must be signed in to change notification settings - Fork 0
/
piano.py
executable file
·47 lines (42 loc) · 2.03 KB
/
piano.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
#!/usr/bin/env python3
import midi
import curses
from curses import ascii
import argparse
import threading
keyboard = '''\
┌───┬───┬─┬───┬───┬───┬───┬─┬───┬─┬───┬───┬───┬───┬─┬───┬───┬────┐
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ │ W │ │ E │ │ │ T │ │ Y │ │ U │ │ │ O │ │ P │ │ │
│ └─┬─┘ └─┬─┘ │ └─┬─┘ └─┬─┘ └─┬─┘ │ └─┬─┘ └─┬─┘ │ │
│ A │ S │ D │ F │ G │ H │ J │ K │ L │ ; │ ' │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴────┘
'''
keyboard_w = len(keyboard.splitlines()[0])
keyboard_h = len(keyboard.splitlines())
keys = "awsedftgyhujkolp;'"
# C4 - F5
notes = range(60, 60+len(keys))
key2note = dict(zip(map(ord, keys), notes))
def draw_piano(win: 'curses.window'):
for i, row in enumerate(keyboard.splitlines()):
win.addstr(i, 0, row)
def main(scr: 'curses.window', midi_port=None, key_dur=.25):
mp = midi.MidiPlayer(midi_port)
curses.curs_set(0)
scrh, scrw = scr.getmaxyx()
win = scr.subwin((scrh - keyboard_h)//2, (scrw - keyboard_w)//2)
draw_piano(win)
while True:
key = win.getch()
if key in {ord('q'), ascii.ctrl(ord('C')), ascii.ESC}:
break
note = key2note.get(key)
if note:
threading.Thread(target=mp.play_note, args=(note, key_dur)).start()
if __name__ == '__main__':
p = argparse.ArgumentParser()
p.add_argument('-p', '--port', type=int, help='Midi port')
p.add_argument('-d', '--key-dur', type=float, default=.25, help='Key press duration')
args = p.parse_args()
curses.wrapper(main, args.port, args.key_dur)