Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import subprocess
- from threading import Thread
- ## Console
- class Console(object):
- @staticmethod
- def init():
- try:
- import termios
- Console.is_unix = True
- # Disable buffering
- Console.fd = sys.stdin.fileno()
- Console.oldattr = termios.tcgetattr(Console.fd)
- newattr = termios.tcgetattr(Console.fd)
- newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
- termios.tcsetattr(Console.fd, termios.TCSANOW, newattr)
- # Get getch
- Console.getch = lambda : sys.stdin.read(1)
- except ImportError:
- Console.is_unix = False
- # Get getch
- import msvcrt
- if sys.version_info.major == 2:
- Console.getch = msvcrt.getch
- else:
- Console.getch = lambda : msvcrt.getch().decode('utf-8')
- @staticmethod
- def reset():
- if Console.is_unix:
- import termios
- termios.tcsetattr(Console.fd, termios.TCSANOW, Console.oldattr)
- KEY_TIME = 0.3
- KEY_TO_FREQ1 = {
- "a": 261.63,
- "w": 277.18,
- "s": 293.66,
- "e": 311.13,
- "d": 329.63,
- "f": 349.23,
- "t": 369.99,
- "g": 392.00,
- "y": 415.30,
- "h": 440.00,
- "u": 466.16,
- "j": 493.88,
- "k": 523.25,
- "o": 554.37,
- "l": 587.33,
- "p": 622.25,
- ";": 659.25,
- ":": 659.25,
- "'": 698.46,
- '"': 698.46,
- "]": 739.99,
- "}": 739.99,
- }
- KEY_TO_FREQ = FREQ_TO_FREQ1
- def play_sound(freq, time):
- proc = subprocess.Popen(['speaker-test', '--frequency', str(freq), '--test', 'sine'],
- stdout=subprocess.DEVNULL)
- try:
- proc.wait(timeout=time)
- except subprocess.TimeoutExpired:
- proc.kill()
- class PlaySoundThread(Thread):
- def __init__(self, freq, time):
- Thread.__init__(self, daemon=True)
- self.freq = freq
- self.time = time
- def run(self):
- play_sound(self.freq, self.time)
- def play_char(ch):
- ch = ch.lower()
- if ch in KEY_TO_FREQ:
- PlaySoundThread(KEY_TO_FREQ[ch], KEY_TIME).start()
- def print_char(ch):
- if ch == '\x7f':
- ch = '\b \b'
- elif ch == '\x04':
- raise EOFError
- print(ch, end='', flush=True)
- def main():
- Console.init()
- try:
- while(True):
- ch = Console.getch()
- print_char(ch)
- play_char(ch)
- except (KeyboardInterrupt, EOFError):
- print()
- finally:
- Console.reset()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement