-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_app.py
101 lines (84 loc) · 2.76 KB
/
audio_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
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
#!/usr/bin/env python
import curses
import time
import fluidsynth
import pyttsx
class AudioApp(object):
'''Base class for auditory UI.'''
def __init__(self, args):
# initialize text-to-speech
self.narrator = pyttsx.init()
#self.narrator.setProperty('voice', 'english-us') # espeak
self.narrator.setProperty('rate', 500)
# initialize audio synthesizer
self.synth = fluidsynth.Synth()
self.synth.start()
example = self.synth.sfload('example.sf2')
self.synth.program_select(0, example, 0, 0)
def speak(self, phrase):
if phrase:
self.narrator.say(phrase)
self.narrator.runAndWait()
def speak_char(self, char):
# TTS engine might not know how to pronounce these characters
mapping = {
'y': 'why',
'.': 'dot',
' ': 'space',
',': 'comma',
';': 'semicolon',
'-': 'dash',
':': 'colon',
'/': 'slash',
'\\': 'backslash',
'?': 'question mark',
'!': 'bang',
'@': 'at',
'#': 'pound',
'$': 'dollar',
'%': 'percent',
'*': 'star',
'^': 'caret',
'~': 'squiggle'
}
speakable = char.lower()
if speakable in mapping:
speakable = mapping[speakable]
elif char.isalpha():
speakable = char
else:
speakable = 'splork' # say something better
return self.speak(speakable)
def play_interval(self, size, duration, root=80, delay=0, intensity=30, channel=0):
self.synth.noteon(channel, root, intensity)
time.sleep(delay)
self.synth.noteon(channel, root + size, intensity)
time.sleep(duration)
self.synth.noteoff(channel, root)
self.synth.noteoff(channel, root + size)
def handle_key(self, key):
'''Individual apps (subclasses) must override this method.'''
raise NotImplementedError
def run(self):
'''Loop indefinitely, passing keystrokes to handle_key until that
method returns false.'''
try:
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
while True:
c = stdscr.getch()
if not self.handle_key(c): break
finally:
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
# for debugging
def simulate_keystroke(self, key):
#time.sleep(random.uniform(0.01, 0.05))
self.handle_key(key)
def simulate_typing(self, string):
for char in string:
self.simulate_keystroke(ord(char))