-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio.py
91 lines (63 loc) · 2.07 KB
/
audio.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
from talon.voice import Key, Context
from subprocess import Popen, PIPE
ctx = Context('audio')
def run_script(script):
p = Popen(['osascript'], stdin=PIPE,
stdout=PIPE, stderr=PIPE, universal_newlines=True)
p.communicate(script)
p.terminate()
def text_to_number(m, numwords={}):
tmp = [str(s).lower() for s in m.dgndictation[0]._words]
words = [parse_word(word) for word in tmp]
# Special case to set volume to max
if "hundred" in words:
return 100
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
]
tens = [
"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
]
for idx, word in enumerate(units):
numwords[word] = (1, idx)
for idx, word in enumerate(tens):
numwords[word] = (1, idx * 10)
result = 0
for word in words:
if word not in numwords:
return -1
scale, increment = numwords[word]
result = result * scale + increment
return result
def parse_word(word):
word = word.lstrip('\\').split('\\', 1)[0]
return word
def play_pause(m):
script = '''tell app "iTunes" to playpause'''
run_script(script)
def next_track(m):
script = '''tell app "iTunes" to play next track'''
run_script(script)
def previous_track(m):
script = '''tell app "iTunes" to play previous track'''
run_script(script)
def set_volume(m):
volume = text_to_number(m)
if volume == -1:
return
volume = str(volume)
script = '''set volume output volume ''' + volume
run_script(script)
keymap = {
# 'play pause': play_pause,
# 'track next': next_track,
# 'track last': previous_track,
'set volume <dgndictation>': set_volume,
# 'set system volume <dgndictation>': set_system_volume,
}
ctx.keymap(keymap)