-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
288 lines (233 loc) · 8.67 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QPushButton
from PyQt5.QtCore import QThread, pyqtSignal
import os
import queue
import re
import sys
import time
from google.cloud import speech
import pyaudio
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"./credential.json"
STREAMING_LIMIT = 240000
SAMPLE_RATE = 16000
CHUNK_SIZE = int(SAMPLE_RATE / 10)
def get_current_time() -> int:
return int(round(time.time() * 1000))
class ResumableMicrophoneStream:
def __init__(
self: object,
rate: int,
chunk_size: int,
) -> None:
self._rate = rate
self.chunk_size = chunk_size
self._num_channels = 1
self._buff = queue.Queue()
self.closed = True
self.start_time = get_current_time()
self.restart_counter = 0
self.audio_input = []
self.last_audio_input = []
self.result_end_time = 0
self.is_final_end_time = 0
self.final_request_end_time = 0
self.bridging_offset = 0
self.last_transcript_was_final = False
self.new_stream = True
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=self._num_channels,
rate=self._rate,
input=True,
frames_per_buffer=self.chunk_size,
stream_callback=self._fill_buffer,
)
def __enter__(self: object) -> object:
self.closed = False
return self
def __exit__(
self: object,
type: object,
value: object,
traceback: object,
) -> object:
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(
self: object,
in_data: object,
*args: object,
**kwargs: object,
) -> object:
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self: object) -> object:
while not self.closed:
data = []
if self.new_stream and self.last_audio_input:
chunk_time = STREAMING_LIMIT / len(self.last_audio_input)
if chunk_time != 0:
if self.bridging_offset < 0:
self.bridging_offset = 0
if self.bridging_offset > self.final_request_end_time:
self.bridging_offset = self.final_request_end_time
chunks_from_ms = round(
(self.final_request_end_time - self.bridging_offset)
/ chunk_time
)
self.bridging_offset = round(
(len(self.last_audio_input) - chunks_from_ms) * chunk_time
)
for i in range(chunks_from_ms, len(self.last_audio_input)):
data.append(self.last_audio_input[i])
self.new_stream = False
chunk = self._buff.get()
self.audio_input.append(chunk)
if chunk is None:
return
data.append(chunk)
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
self.audio_input.append(chunk)
except queue.Empty:
break
yield b"".join(data)
def listen_print_loop(responses: object, stream: object) -> None:
for response in responses:
if get_current_time() - stream.start_time > STREAMING_LIMIT:
stream.start_time = get_current_time()
break
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
result_seconds = 0
result_micros = 0
if result.result_end_time.seconds:
result_seconds = result.result_end_time.seconds
if result.result_end_time.microseconds:
result_micros = result.result_end_time.microseconds
stream.result_end_time = int((result_seconds * 1000) + (result_micros / 1000))
corrected_time = (
stream.result_end_time
- stream.bridging_offset
+ (STREAMING_LIMIT * stream.restart_counter)
)
if result.is_final:
print(str(corrected_time) + ": " + transcript + "\n")
stream.is_final_end_time = stream.result_end_time
stream.last_transcript_was_final = True
if re.search(r"\b(exit|quit)\b", transcript, re.I):
print("Exiting...\n")
stream.closed = True
break
else:
print(str(corrected_time) + ": " + transcript + "\r")
stream.last_transcript_was_final = False
class WorkerThread(QThread):
finished = pyqtSignal()
progress = pyqtSignal(int)
def run(self):
try:
client = speech.SpeechClient()
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=SAMPLE_RATE,
language_code="ja-JP",
max_alternatives=1,
)
streaming_config = speech.StreamingRecognitionConfig(
config=config, interim_results=True
)
mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print('Listening, say "Quit" or "Exit" to stop.')
with mic_manager as stream:
while not stream.closed:
print("\n" + str(STREAMING_LIMIT * stream.restart_counter) + ": NEW REQUEST\n")
stream.audio_input = []
audio_generator = stream.generator()
requests = (
speech.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator
)
responses = client.streaming_recognize(streaming_config, requests)
listen_print_loop(responses, stream)
if stream.result_end_time > 0:
stream.final_request_end_time = stream.is_final_end_time
stream.result_end_time = 0
stream.last_audio_input = []
stream.last_audio_input = stream.audio_input
stream.audio_input = []
stream.restart_counter = stream.restart_counter + 1
if not stream.last_transcript_was_final:
print("\n")
stream.new_stream = True
except Exception as e:
print(f"An unknown error occurred: {type(e).__name__} - {str(e)}")
class QRecordButton(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self.setStyleSheet("""
QPushButton {
background-color: #e74c3c;
color: #fff;
padding: 10px 20px;
border-radius: 10px;
}
QPushButton:pressed {
background-color: #c0392b;
}
""")
self.setText("Record Audio")
self.setCheckable(True)
self.toggled.connect(self.start_stop_recording)
def start_stop_recording(self, checked):
if checked:
self.worker = WorkerThread()
self.worker.progress.connect(self.on_progress)
self.worker.start()
self.setText("Stop Recording")
else:
self.worker.terminate()
self.setText("Record Audio")
def on_progress(self, progress):
print(f"Progress: {progress}%")
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Speech recognition")
self.setGeometry(100, 100, 320, 120)
layout = QVBoxLayout()
label = QLabel("Speech recognition")
record_button = QRecordButton()
layout.addWidget(label)
layout.addWidget(record_button)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet("""
QMainWindow {
background-color: #333;
color: #fff;
}
QLabel {
color: #fff;
padding-left: 8px;
}
""")
window = MyWindow()
window.show()
sys.exit(app.exec_())