forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (37 loc) · 1.25 KB
/
main.py
File metadata and controls
48 lines (37 loc) · 1.25 KB
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
import os
if os.name == "posix":
from boombox import GstBoomBox as BoomBox
else:
from boombox import BoomBox
from pynput import keyboard
class KeyboardXylophone:
"""Listen for key events and play a tone based on its value."""
def __init__(
self, duration_ms: int = 250, volume: float = 0.1, freq_mul: int = 10
) -> None:
self._boombox = BoomBox("")
self.duration_ms = duration_ms
self.volume = volume
self.freq_mul = freq_mul
def play_sound(self, frequency: float):
"""Play sound at given frequency."""
self._boombox.play_tone(frequency, self.duration_ms, self.volume)
def on_press(self, key):
"""Play sound on key press."""
try:
self.play_sound(ord(key.char) * self.freq_mul)
except AttributeError:
pass
def on_release(self, key):
"""Return False if Escape is released."""
if key == keyboard.Key.esc:
return False
def run(self):
"""Start listening for key events."""
with keyboard.Listener(
on_press=self.on_press, on_release=self.on_release
) as listener:
listener.join()
if __name__ == "__main__":
kbdxylo = KeyboardXylophone()
kbdxylo.run()