forked from wiresong/whistler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhistler.py
63 lines (57 loc) · 1.96 KB
/
whistler.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
import globalPluginHandler
import socket
import speech
import threading
import tones
import time
class GlobalPlugin(globalPluginHandler.GlobalPlugin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.q = []
self.t = threading.Thread(target=self.run, daemon=True)
self.t.start()
def extract(self, message):
return message[message.find("{") + 1 : message.find("}")]
def process_queue(self):
for item in self.q:
if item.startswith('q'):
speech.speakMessage(self.extract(item))
elif item.startswith('t'):
words = item.split()
tones.beep(int(words[1]), int(words[2]))
elif item.startswith('sh'):
words = item.split()
time.sleep(int(words[1])/1000)
self.q.clear()
def run(self):
buffer = ""
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind(("0.0.0.0", 5000))
while True:
try:
data = s.recv(1024).decode("utf-8")
buffer += data
while "\n" in buffer:
message, buffer = buffer.split("\n", 1)
self.process(message)
except:
pass
def process(self, message):
words = message.split()
match words[0]:
case "version":
speech.speakText("nvda Emacs server")
case "tts_reset":
speech.cancelSpeech()
self.q.clear()
case "tts_say":
speech.speakMessage(self.extract(message))
case "l":
speech.speakSpelling(self.extract(message))
case "q" | "t":
self.q.append(message)
case "s":
self.q.clear()
speech.cancelSpeech()
case "d":
self.process_queue()