-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketManager.py
More file actions
99 lines (68 loc) · 1.91 KB
/
Copy pathSocketManager.py
File metadata and controls
99 lines (68 loc) · 1.91 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
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
import pyaudio
from tornado import websocket
import threading
import SpeechManager as Speech
import wave
import struct
import random
import time
clients = []
stream = None
p = pyaudio.PyAudio()
import speech_recognition as sr
class BufferContainer(sr.AudioSource):
def __init__(self):
self.event = threading.Event()
self.event.clear()
self.lines = []
def write(self, text):
self.lines.append(text)
self.event.set()
def writelines(self, *args):
for item in args: self.lines.append(item)
def open(self):
self.lines = []
def read(self, extra):
self.event.wait()
elem = None
if (len(self.lines) > 0):
elem = self.lines.pop()
# print("")
if (len(self.lines) < 1):
self.event.clear()
return elem
def close (self):
pass
buffer = BufferContainer()
class Handler(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
global stream
if len(clients) < 1:
print("opening streams")
stream = p.open(format=p.get_format_from_width(2),
# input=True,
channels=1,
rate=16000,
output=True)
speech_listener = threading.Thread(target=Speech.listen_for_speech, args=(buffer,))
speech_listener.daemon = True
speech_listener.start()
if self not in clients:
clients.append(self)
def on_close(self):
global stream
if self in clients:
clients.remove(self)
if len(clients) < 1:
print("closing streams")
stream.stream()
stream.close()
stream = None
def on_message(self, message):
global stream, buffer
if buffer is not None:
buffer.write(message)
if stream is not None:
stream.write(message)