-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_buffer.py
More file actions
49 lines (40 loc) · 1.65 KB
/
Copy pathaudio_buffer.py
File metadata and controls
49 lines (40 loc) · 1.65 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
import collections
import threading
import time
from typing import List
class AudioBuffer:
"""
Unified ring buffer for all captured PCM audio.
Every chunk is stored alongside the tts_active flag at capture time.
Egress sets/clears tts_active around playback; ingress gates STT on it
to prevent speaker echo from being fed into the STT backend.
replay() returns only the chunks captured after the most recent TTS-active
period, giving the STT backend echo-free look-back audio on state transitions.
"""
def __init__(self, maxlen: int, tts_tail_seconds: float = 0.0) -> None:
self._buf: collections.deque = collections.deque(maxlen=maxlen)
self._tts_active = False
self._tts_tail = tts_tail_seconds
self._quiet_until: float = 0.0
self._lock = threading.Lock()
def append(self, chunk: bytes) -> None:
with self._lock:
self._buf.append((chunk, self._tts_active))
def set_tts_active(self, active: bool) -> None:
with self._lock:
self._tts_active = active
if not active:
self._quiet_until = time.monotonic() + self._tts_tail
@property
def tts_active(self) -> bool:
with self._lock:
return self._tts_active or time.monotonic() < self._quiet_until
def replay(self) -> List[bytes]:
"""Chunks captured after the most recent TTS-active period (echo-free)."""
with self._lock:
items = list(self._buf)
last_tts = -1
for i, (_, active) in enumerate(items):
if active:
last_tts = i
return [chunk for chunk, _ in items[last_tts + 1:]]