-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadaptive_player.py
More file actions
351 lines (294 loc) · 12.6 KB
/
adaptive_player.py
File metadata and controls
351 lines (294 loc) · 12.6 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Adaptive audio player with EMA buffering and per-session metrics.
Adapted from mlx_audio's AudioPlayer but with full instrumentation:
- Underrun counting (empty buffer during active playback)
- Per-callback buffer depth tracking
- TTFA measurement (time from first queued audio to first audible output)
- Structured PlaybackMetrics returned on completion
"""
import sys
import time
from collections import deque
from dataclasses import dataclass, field
from threading import Event, Lock
import numpy as np
import sounddevice as sd
@dataclass
class PlaybackMetrics:
"""Frozen snapshot of a single playback session."""
# Audio
duration_sec: float = 0.0
total_samples: int = 0
sample_rate: int = 24_000
# Timing
ttfa_sec: float = 0.0 # first queue_audio → first audible output
total_wall_sec: float = 0.0
overall_rtf: float = 0.0 # duration_sec / total_wall_sec
# Chunks (from generator)
chunk_count: int = 0
per_chunk: list[dict] = field(default_factory=list)
# Buffer health
startup_delay_sec: float = 0.0
peak_buffer_samples: int = 0
min_buffer_samples: int = 0
underrun_count: int = 0
# Memory
peak_memory_gb: float = 0.0
# Mode
mode: str = "streaming"
def to_dict(self) -> dict:
return {
"status": "ok",
"mode": self.mode,
"audio": {
"duration_sec": round(self.duration_sec, 2),
"total_samples": self.total_samples,
},
"timing": {
"ttfa_sec": round(self.ttfa_sec, 3),
"total_wall_sec": round(self.total_wall_sec, 2),
"overall_rtf": round(self.overall_rtf, 2),
},
"chunks": {
"count": self.chunk_count,
"per_chunk": self.per_chunk,
},
"buffer": {
"startup_delay_sec": round(self.startup_delay_sec, 3),
"peak_samples": self.peak_buffer_samples,
"min_samples": self.min_buffer_samples,
"underruns": self.underrun_count,
},
"memory_peak_gb": round(self.peak_memory_gb, 2),
}
class AdaptivePlayer:
"""Callback-based audio player with EMA-adaptive startup buffering.
Usage:
player = AdaptivePlayer(sample_rate=24000)
# In a background thread:
for chunk in generate(...):
player.queue_audio(chunk_audio, chunk_meta={...})
player.mark_done()
# In the foreground:
metrics = player.wait()
"""
# EMA parameters (same as mlx_audio AudioPlayer)
EMA_ALPHA = 0.25
MEASURE_WINDOW = 0.25 # seconds between rate measurements
MIN_BUFFER_SECONDS = 1.5 # startup threshold = arrival_rate * this
def __init__(self, sample_rate: int = 24_000, buffer_size: int = 2048, device: int | str | None = None):
self.sample_rate = sample_rate
self.buffer_size = buffer_size
self.device = device # sounddevice output device index or name
# Buffer
self._buffer: deque[np.ndarray] = deque()
self._buffer_lock = Lock()
# Stream
self._stream: sd.OutputStream | None = None
self._playing = False
self._drain_event = Event()
self._stream_finished = Event() # set by sounddevice finished_callback
self._generation_done = False
# EMA arrival rate tracking
self._window_sample_count = 0
self._window_start = time.perf_counter()
self._arrival_rate = float(sample_rate) # assume realtime initially
# Metrics accumulators
self._first_queue_time: float | None = None
self._first_pull_time: float | None = None
self._total_queued_samples = 0
self._peak_buffer = 0
self._min_buffer = sys.maxsize
self._underruns = 0
self._chunk_metrics: list[dict] = []
self._startup_delay = 0.0
self._peak_memory_gb = 0.0
# Progress tracking (for PipelineState position updates)
self._samples_played = 0
# Synchronization: set when mark_done() is called
self._done_event = Event()
# ------------------------------------------------------------------
# Callback (runs in audio thread)
# ------------------------------------------------------------------
def _callback(self, outdata: np.ndarray, frames: int, time_info, status):
outdata.fill(0)
filled = 0
with self._buffer_lock:
while filled < frames and self._buffer:
buf = self._buffer[0]
to_copy = min(frames - filled, len(buf))
outdata[filled : filled + to_copy, 0] = buf[:to_copy]
filled += to_copy
if to_copy == len(buf):
self._buffer.popleft()
else:
self._buffer[0] = buf[to_copy:]
current_buffer = sum(map(len, self._buffer))
# Progress tracking (lock-free; only written here in audio thread)
self._samples_played += filled
# Metrics
if filled > 0 and self._first_pull_time is None:
self._first_pull_time = time.perf_counter()
if self._playing:
if current_buffer < self._min_buffer:
self._min_buffer = current_buffer
if current_buffer > self._peak_buffer:
self._peak_buffer = current_buffer
if filled == 0 and self._playing:
self._underruns += 1
# Stop only when buffer is empty AND generation is done
if current_buffer == 0 and filled < frames and self._generation_done:
self._drain_event.set()
raise sd.CallbackStop()
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
LEAD_SILENCE_SEC = 0.2 # silence before first audio to let device settle
def queue_audio(self, samples: np.ndarray, chunk_meta: dict | None = None):
"""Queue audio samples for playback. Called from generator thread."""
samples = np.asarray(samples, dtype=np.float32)
if len(samples) == 0:
return
now = time.perf_counter()
if self._first_queue_time is None:
self._first_queue_time = now
# Prepend silence so the audio device is settled before speech starts
silence = np.zeros(int(self.sample_rate * self.LEAD_SILENCE_SEC), dtype=np.float32)
with self._buffer_lock:
self._buffer.append(silence)
self._total_queued_samples += len(silence)
# EMA arrival rate
self._window_sample_count += len(samples)
if now - self._window_start >= self.MEASURE_WINDOW:
elapsed = now - self._window_start
inst_rate = self._window_sample_count / elapsed
self._arrival_rate = self.EMA_ALPHA * inst_rate + (1 - self.EMA_ALPHA) * self._arrival_rate
self._window_sample_count = 0
self._window_start = now
with self._buffer_lock:
self._buffer.append(samples)
self._total_queued_samples += len(samples)
current_buffer = sum(map(len, self._buffer))
if current_buffer > self._peak_buffer:
self._peak_buffer = current_buffer
# Record per-chunk metrics
if chunk_meta is not None:
chunk_meta["buffer_depth"] = current_buffer
self._chunk_metrics.append(chunk_meta)
mem = chunk_meta.get("peak_memory_gb", 0.0)
if mem > self._peak_memory_gb:
self._peak_memory_gb = mem
# Adaptive startup
needed = int(self._arrival_rate * self.MIN_BUFFER_SECONDS)
if not self._playing and current_buffer >= needed:
self._startup_delay = now - self._first_queue_time
self._start_stream()
def mark_done(self):
"""Signal that the generator has finished producing audio."""
self._generation_done = True
self._done_event.set()
# Nothing was generated — unblock wait() immediately
if self._total_queued_samples == 0:
self._drain_event.set()
return
# If we never hit the buffer threshold (very short text), start now
if not self._playing:
if self._first_queue_time is not None:
self._startup_delay = time.perf_counter() - self._first_queue_time
self._start_stream()
def get_progress(self) -> tuple[int, int]:
"""Return (samples_played, total_samples_queued) for position tracking.
Called by PipelineState to compute spoken_pct. The samples_played
counter is updated in the audio callback; total_samples_queued is
updated in queue_audio(). Both are monotonically increasing.
"""
return self._samples_played, self._total_queued_samples
def wait(self, timeout: float = 120.0) -> PlaybackMetrics:
"""Block until playback finishes. Returns metrics."""
# Wait for generation to at least finish before checking state
self._done_event.wait(timeout=timeout)
# Wait for buffer to drain (callback raises CallbackStop)
self._drain_event.wait(timeout=timeout)
# Wait for sounddevice to fully flush the device buffer
self._stream_finished.wait(timeout=5.0)
self._stop_stream()
return self._build_metrics()
# ------------------------------------------------------------------
# Internal
# ------------------------------------------------------------------
def _resolve_device(self):
"""Resolve the output device, falling back to system default if unavailable."""
if self.device is None:
return None # sounddevice uses system default
try:
devices = sd.query_devices()
if isinstance(self.device, int):
if self.device < len(devices):
info = devices[self.device]
if info["max_output_channels"] > 0:
return self.device
elif isinstance(self.device, str):
for i, d in enumerate(devices):
if self.device in d["name"] and d["max_output_channels"] > 0:
return i
except Exception:
pass
# Device unavailable — fall back to system default.
return None
def _start_stream(self):
self._stream_finished.clear()
resolved = self._resolve_device()
self._stream = sd.OutputStream(
samplerate=self.sample_rate,
channels=1,
dtype="float32",
device=resolved,
callback=self._callback,
finished_callback=self._on_stream_finished,
blocksize=self.buffer_size,
)
self._stream.start()
self._playing = True
self._drain_event.clear()
def _on_stream_finished(self):
"""Called by sounddevice after stream fully stops (all audio flushed)."""
self._stream_finished.set()
def _stop_stream(self):
try:
if self._stream:
self._stream.stop()
self._stream.close()
finally:
self._stream = None
self._playing = False
def flush(self):
"""Discard everything and stop playback immediately."""
with self._buffer_lock:
self._buffer.clear()
self._generation_done = True
self._stop_stream()
self._drain_event.set()
self._stream_finished.set()
self._done_event.set()
def _build_metrics(self) -> PlaybackMetrics:
duration = self._total_queued_samples / self.sample_rate
now = time.perf_counter()
wall = (now - self._first_queue_time) if self._first_queue_time else 0.0
ttfa = 0.0
if self._first_pull_time and self._first_queue_time:
ttfa = self._first_pull_time - self._first_queue_time
return PlaybackMetrics(
duration_sec=duration,
total_samples=self._total_queued_samples,
sample_rate=self.sample_rate,
ttfa_sec=ttfa,
total_wall_sec=wall,
overall_rtf=duration / wall if wall > 0 else 0.0,
chunk_count=len(self._chunk_metrics),
per_chunk=self._chunk_metrics,
startup_delay_sec=self._startup_delay,
peak_buffer_samples=self._peak_buffer,
min_buffer_samples=self._min_buffer if self._min_buffer != sys.maxsize else 0,
underrun_count=self._underruns,
peak_memory_gb=self._peak_memory_gb,
mode="streaming" if len(self._chunk_metrics) > 1 else "batch",
)