-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
54 lines (40 loc) · 1.52 KB
/
app.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
import tkinter as tk
from queue import Queue
from rompler import Rompler
from translations import KEYBOARD_KEY_TO_MIDI_NOTE
from views.main_view import MainView
class Application(object):
def __init__(self, window, *a, **kw):
super(Application, self).__init__(*a, **kw)
self._window = window
# Create and start an audio thread.
# It's going to communicate with the main thread through a thread-safe queue.
self._notes_queue = Queue(maxsize=1)
self._rompler = Rompler(name="AudioThread", notes_queue=self._notes_queue)
self._rompler.start()
# Create the app's GUI
self._view = MainView(window, self._rompler)
# Stop the audio thread when the app is closing
window.protocol("WM_DELETE_WINDOW", self._on_closing)
window.bind("<Key>", self._handle_keypress)
window.bind("<KeyRelease>", self._handle_keyrelease)
def _on_closing(self):
self._rompler.stop.set()
self._window.destroy()
def _handle_keypress(self, event):
try:
key = event.char
midi_note = KEYBOARD_KEY_TO_MIDI_NOTE[key]
self._notes_queue.put(midi_note)
self._view.on_key_pressed(key)
except KeyError:
# note not supported
pass
def _handle_keyrelease(self, event):
key = event.char
self._view.on_key_released(key)
if __name__ == '__main__':
root = tk.Tk()
app = Application(root)
root.attributes("-topmost", True)
root.mainloop()