-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaudio_worker_client.py
More file actions
316 lines (275 loc) · 11.4 KB
/
audio_worker_client.py
File metadata and controls
316 lines (275 loc) · 11.4 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from __future__ import annotations
import atexit
import json
import os
import queue
import select
import subprocess
import sys
import threading
import time
from typing import Any, Callable, Optional
class AudioWorkerClient:
"""Client for the audio recording worker subprocess (audio_worker.py)."""
_WORKER_STARTUP_TIMEOUT_S = 10
_START_TIMEOUT_S = 10
_STOP_TIMEOUT_S = 10
_CANCEL_TIMEOUT_S = 5
_WRITE_TIMEOUT_S = 2.0
_WRITE_LOCK_TIMEOUT_S = 2.0
def __init__(self):
self._proc: subprocess.Popen[str] | None = None
self._messages: "queue.Queue[dict[str, Any]]" = queue.Queue()
self._reader_thread: threading.Thread | None = None
self._lock = threading.Lock()
self._write_lock = threading.Lock()
self._next_id = 1
self._cleanup_registered = False
self._waveform_callback: Optional[Callable[[list[float], float], None]] = None
def set_waveform_callback(self, callback: Optional[Callable[[list[float], float], None]]):
"""Set callback for waveform updates (values, raw_peak)."""
self._waveform_callback = callback
def is_running(self) -> bool:
return self._proc is not None and self._proc.poll() is None
def ensure_running(self) -> None:
with self._lock:
self._ensure_running_locked()
def stop(self, force: bool = False) -> None:
with self._lock:
self._stop_locked(force=force)
def start_recording(self, *, device_name: str | None, sample_rate: int, channels: int) -> None:
with self._lock:
last_error: Exception | None = None
for attempt in range(2):
try:
self._ensure_running_locked()
req_id = self._next_id
self._next_id += 1
payload = json.dumps(
{
"type": "start",
"id": req_id,
"device_name": device_name,
"sample_rate": sample_rate,
"channels": channels,
}
)
if not self._write_lock.acquire(timeout=self._WRITE_LOCK_TIMEOUT_S):
raise TimeoutError("Timed out waiting for audio worker write lock")
try:
self._write_line(payload + "\n", timeout_s=self._WRITE_TIMEOUT_S)
finally:
self._write_lock.release()
message = self._wait_for_locked(
lambda m: m.get("type") in {"started", "error"} and m.get("id") == req_id,
timeout_s=self._START_TIMEOUT_S,
)
if not message:
raise TimeoutError("Timed out starting audio recording")
if message.get("type") == "error":
raise RuntimeError(message.get("error") or "Failed to start recording")
return
except Exception as e:
last_error = e
self._stop_locked(force=True)
if attempt == 0:
continue
raise
if last_error:
raise last_error
def stop_recording(self, *, wav_path: str) -> tuple[int, float]:
with self._lock:
self._ensure_running_locked()
req_id = self._next_id
self._next_id += 1
payload = json.dumps({"type": "stop", "id": req_id, "wav_path": wav_path})
if not self._write_lock.acquire(timeout=self._WRITE_LOCK_TIMEOUT_S):
raise TimeoutError("Timed out waiting for audio worker write lock")
try:
self._write_line(payload + "\n", timeout_s=self._WRITE_TIMEOUT_S)
finally:
self._write_lock.release()
message = self._wait_for_locked(
lambda m: m.get("type") in {"stopped", "error"} and m.get("id") == req_id,
timeout_s=self._STOP_TIMEOUT_S,
)
if not message:
raise TimeoutError("Timed out stopping audio recording")
if message.get("type") == "error":
raise RuntimeError(message.get("error") or "Failed to stop recording")
frames = message.get("frames")
peak = message.get("peak")
try:
return int(frames or 0), float(peak or 0.0)
except (TypeError, ValueError):
return 0, 0.0
def cancel_recording(self) -> None:
with self._lock:
if not self.is_running():
return
req_id = self._next_id
self._next_id += 1
payload = json.dumps({"type": "cancel", "id": req_id})
if not self._write_lock.acquire(timeout=self._WRITE_LOCK_TIMEOUT_S):
raise TimeoutError("Timed out waiting for audio worker write lock")
try:
self._write_line(payload + "\n", timeout_s=self._WRITE_TIMEOUT_S)
finally:
self._write_lock.release()
message = self._wait_for_locked(
lambda m: m.get("type") in {"canceled", "error"} and m.get("id") == req_id,
timeout_s=self._CANCEL_TIMEOUT_S,
)
if not message:
raise TimeoutError("Timed out cancelling audio recording")
if message.get("type") == "error":
raise RuntimeError(message.get("error") or "Failed to cancel recording")
def _read_stdout(self, proc: subprocess.Popen[str], messages: "queue.Queue[dict[str, Any]]") -> None:
assert proc.stdout is not None
for line in proc.stdout:
line = line.strip()
if not line:
continue
try:
msg = json.loads(line)
if msg.get("type") == "waveform":
cb = self._waveform_callback
if cb:
try:
cb(msg.get("values", []), float(msg.get("raw_peak", 0.0) or 0.0))
except Exception:
pass
else:
messages.put(msg)
except json.JSONDecodeError:
messages.put({"type": "stdout", "line": line})
messages.put({"type": "eof"})
def _write_line(self, line: str, timeout_s: float) -> None:
assert self._proc is not None
assert self._proc.stdin is not None
fd = self._proc.stdin.fileno()
data = line.encode("utf-8")
total = 0
deadline = time.time() + timeout_s
while total < len(data):
remaining = deadline - time.time()
if remaining <= 0:
raise TimeoutError("Timed out writing to audio worker stdin")
_, writable, _ = select.select([], [fd], [], remaining)
if not writable:
raise TimeoutError("Timed out writing to audio worker stdin")
written = os.write(fd, data[total:])
if written <= 0:
raise RuntimeError("Failed to write to audio worker stdin")
total += written
def _wait_for_locked(self, predicate, timeout_s: int) -> dict[str, Any] | None:
deadline = time.time() + timeout_s if timeout_s > 0 else None
while True:
if deadline is not None:
remaining = deadline - time.time()
if remaining <= 0:
return None
else:
remaining = None
try:
message = self._messages.get(timeout=remaining)
except queue.Empty:
return None
if message.get("type") == "eof":
return {"type": "error", "error": "Audio worker exited unexpectedly"}
if predicate(message):
return message
def _ensure_running_locked(self) -> None:
if self.is_running():
return
self._stop_locked(force=True)
worker_path = os.path.join(os.path.dirname(__file__), "audio_worker.py")
if not os.path.exists(worker_path):
raise FileNotFoundError(f"Missing audio worker at {worker_path}")
env = os.environ.copy()
env.setdefault("PYTHONUNBUFFERED", "1")
env["STT_PARENT_PID"] = str(os.getpid())
last_error: Exception | None = None
for attempt in range(2):
proc = subprocess.Popen(
[sys.executable, "-u", worker_path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=None, # inherit stderr to avoid pipe deadlocks
text=True,
bufsize=1,
env=env,
)
messages: "queue.Queue[dict[str, Any]]" = queue.Queue()
thread = threading.Thread(target=self._read_stdout, args=(proc, messages), daemon=True)
thread.start()
self._proc = proc
self._messages = messages
self._reader_thread = thread
ready = self._wait_for_locked(
lambda m: m.get("type") in {"ready", "error"},
timeout_s=self._WORKER_STARTUP_TIMEOUT_S,
)
if ready and ready.get("type") == "ready":
if not self._cleanup_registered:
atexit.register(self.stop)
self._cleanup_registered = True
return
if not ready:
last_error = TimeoutError("Audio worker did not become ready in time")
else:
last_error = RuntimeError(ready.get("error") or "Audio worker failed to start")
self._stop_locked(force=True)
if attempt == 0:
time.sleep(0.1)
continue
if last_error:
raise last_error
raise RuntimeError("Audio worker failed to start")
def _stop_locked(self, force: bool = False) -> None:
proc = self._proc
self._proc = None
if proc is None:
return
# Close stdin first to signal worker to stop and unblock any writes.
try:
if proc.stdin:
proc.stdin.close()
except Exception:
pass
try:
if not force and proc.poll() is None:
try:
proc.wait(timeout=1)
except subprocess.TimeoutExpired:
pass
if proc.poll() is None:
proc.terminate()
# Close stdout to unblock reader thread before waiting.
try:
if proc.stdout:
proc.stdout.close()
except Exception:
pass
try:
proc.wait(timeout=2)
except subprocess.TimeoutExpired:
pass
if proc.poll() is None:
proc.kill()
try:
proc.wait(timeout=1)
except subprocess.TimeoutExpired:
pass
except Exception:
try:
if proc.poll() is None:
proc.kill()
except Exception:
pass
finally:
try:
if proc.stdout:
proc.stdout.close()
except Exception:
pass