-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
92 lines (71 loc) · 2.75 KB
/
client.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
# -*- coding: utf-8 -*-
"""
Author: Fernando Lopez
Description: This script contains the definition of the websocket client.
"""
import pyaudio
import asyncio
import webrtcvad
import websockets
CHANNELS = 1
RATE = 16000
FRAME_DURATION = 30
FORMAT = pyaudio.paInt16
CHUNK = int(RATE * FRAME_DURATION / 1000)
INPUT_DEVICE = "UMC204HD 192k"
SERVER = "ws://localhost:8001/"
vad = webrtcvad.Vad()
vad.set_mode(1)
class SpeechRecognitionClient:
def __init__(self):
self.transcript = ""
self.audio = pyaudio.PyAudio()
self.getAudioInterface()
self.setStreamSettings()
asyncio.run(self.startStream())
self.stream.close()
self.audio.terminate()
async def startStream(self):
async with websockets.connect(SERVER) as websocket:
frames = b''
try:
while True:
frame = self.stream.read(CHUNK, exception_on_overflow=False)
if vad.is_speech(frame, RATE):
frames += frame
elif len(frames) > 1:
await websocket.send(frames)
frames = b''
text = await websocket.recv()
self.transcript = f"{self.transcript} {text}" if len(text) > 1 else self.transcript
print(f"> {self.transcript}")
except KeyboardInterrupt:
await websocket.close()
print(f"\nWebsocket closed.")
except websockets.exceptions.ConnectionClosedError:
print(f"\nWebsocket closed.")
except Exception:
print(f"\nWebsocket closed.")
finally:
print('*'*50)
print(f"\nTranscript: \n\n{self.transcript}\n")
def setStreamSettings(self):
self.stream = self.audio.open(
input_device_index=self.inputDevice,
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
def getAudioInterface(self):
self.inputDevice = None
numDevices = self.audio.get_host_api_info_by_index(0)["deviceCount"]
for index in range(numDevices):
name = self.audio.get_device_info_by_host_api_device_index(0, index).get("name")
if name == INPUT_DEVICE:
self.inputDevice = index
break
if not self.inputDevice:
raise ValueError(f"Audio device was not found under name: {INPUT_DEVICE}")
if __name__ == '__main__':
s = SpeechRecognitionClient()