/
/
1"""
2Audio buffer implementation for PCM audio streaming.
3
4AudioBuffer is the primary interface for all buffered audio streaming in Music Assistant.
5It stores raw decoded PCM audio (no filters applied) and provides methods to:
6- Fill the buffer from any async generator of audio chunks
7- Get raw or processed (filtered/resampled) audio streams
8- Seek within buffered audio
9"""
10
11from __future__ import annotations
12
13import asyncio
14import logging
15import time
16from collections import deque
17from collections.abc import AsyncGenerator, Callable
18from contextlib import aclosing, suppress
19from typing import TYPE_CHECKING, Any, Final
20
21from music_assistant_models.enums import (
22 ContentType,
23 MediaType,
24 VolumeNormalizationMode,
25)
26from music_assistant_models.errors import AudioError
27from music_assistant_models.media_items import AudioFormat
28
29from music_assistant.constants import MASS_LOGGER_NAME, VERBOSE_LOG_LEVEL
30from music_assistant.controllers.streams.constants import (
31 BUFFER_SIZE_MAP,
32 CONF_BUFFER_SIZE,
33 CONF_BUFFER_SIZE_DEFAULT,
34 RADIO_BUFFER_SIZE,
35 SEEK_WAIT_THRESHOLD,
36 STREAM_SLOT_WAIT_TIMEOUT,
37 BufferMode,
38 BufferSize,
39)
40from music_assistant.helpers.audio import arriving_audio_format, pcm_formats_match
41from music_assistant.helpers.ffmpeg import get_ffmpeg_stream
42from music_assistant.models.music_provider import MusicProvider
43
44if TYPE_CHECKING:
45 from music_assistant_models.streamdetails import StreamDetails
46
47 from music_assistant.mass import MusicAssistant
48
49LOGGER = logging.getLogger(f"{MASS_LOGGER_NAME}.audio_buffer")
50
51# Callback signature for cancel observers: invoked when the buffer is cancelled/cleared.
52CancelCallback = Callable[[], None]
53
54# Maximum seconds to wait for the first playable audio, on top of any time the producer
55# is allowed to spend waiting for a provider source-stream slot.
56BUFFER_READY_TIMEOUT: Final[int] = 15
57
58
59class AudioBufferEOF(Exception):
60 """Exception raised when the audio buffer reaches end-of-file."""
61
62
63class AudioBufferDiscarded(Exception):
64 """
65 Raised when a passive (analysis) reader requests a chunk evicted from the retained window.
66
67 Means the reader is a full window behind playback, so its session is dropped.
68 """
69
70
71class AudioBuffer:
72 """
73 Raw PCM audio buffer with seek support and optional filter processing.
74
75 Stores audio in the original sample rate and bit depth.
76 Use get_raw_stream() for unprocessed PCM and get_stream() for
77 filtered/resampled output.
78 """
79
80 def __init__(
81 self,
82 pcm_format: AudioFormat,
83 buffer_size: BufferSize = BufferSize.BALANCED,
84 mode: BufferMode = BufferMode.SEEKABLE,
85 ready_threshold: int = 1,
86 ) -> None:
87 """
88 Initialize AudioBuffer.
89
90 :param pcm_format: The PCM audio format specification.
91 :param buffer_size: Buffer size preset.
92 :param mode: Buffer mode (SEEKABLE for tracks, ROLLING for radio).
93 :param ready_threshold: Seconds of audio to buffer before signaling ready.
94 """
95 self.pcm_format = pcm_format
96 self.max_size_seconds = (
97 RADIO_BUFFER_SIZE if mode == BufferMode.ROLLING else BUFFER_SIZE_MAP[buffer_size]
98 )
99 self.mode = mode
100 self._ready_threshold = ready_threshold
101 self._ready_at_chunk = ready_threshold # updated by get_buffer to account for seek
102 self._chunks: deque[bytes] = deque()
103 self._discarded_chunks = 0
104 self._lock = asyncio.Lock()
105 self._data_available = asyncio.Condition(self._lock)
106 self._space_available = asyncio.Condition(self._lock)
107 self._eof_received = False
108 self._producer_task: asyncio.Task[None] | None = None
109 self._last_access_time: float = time.time()
110 self._inactivity_task: asyncio.Task[None] | None = None
111 self._cancelled = False
112 self._producer_error: Exception | None = None
113 self._background_tasks: set[asyncio.Task[None]] = set()
114 self.ready = asyncio.Event()
115 self._cancel_callbacks: list[CancelCallback] = []
116 self._ready_wait_lock = asyncio.Lock()
117
118 # -- Properties --
119
120 @property
121 def cancelled(self) -> bool:
122 """Return whether the buffer has been cancelled or cleared."""
123 if self._cancelled:
124 return True
125 return self._producer_task is not None and self._producer_task.cancelled()
126
127 @property
128 def has_error(self) -> bool:
129 """Return whether the producer encountered an error."""
130 return self._producer_error is not None
131
132 @property
133 def chunk_size_bytes(self) -> int:
134 """Return the size in bytes of one second of PCM audio."""
135 return self.pcm_format.pcm_sample_size
136
137 @property
138 def size_seconds(self) -> int:
139 """Return current size of the buffer in seconds."""
140 return len(self._chunks)
141
142 @property
143 def seconds_available(self) -> int:
144 """Return number of seconds of audio currently available."""
145 return len(self._chunks)
146
147 @property
148 def duration_available(self) -> float:
149 """Return the exact duration of resident PCM audio in seconds."""
150 return sum(len(chunk) for chunk in self._chunks) / self.pcm_format.pcm_sample_size
151
152 @property
153 def is_buffering(self) -> bool:
154 """Return whether the upstream source producer is still active."""
155 return self._producer_task is not None and not self._producer_task.done()
156
157 @property
158 def eof(self) -> bool:
159 """
160 Return whether the source stopped producing.
161
162 A source that failed after delivering audio also ends here, so pair this with
163 ``has_error`` when a clean finish is what matters.
164 """
165 return self._eof_received
166
167 @property
168 def first_buffered_chunk(self) -> int:
169 """Return the chunk number of the oldest chunk still retained in the buffer."""
170 return self._discarded_chunks
171
172 # -- Public methods --
173
174 def register_cancel_callback(self, callback: CancelCallback) -> None:
175 """
176 Register a callback to be invoked when the buffer is cancelled or cleared.
177
178 :param callback: Callable with no arguments, invoked on cancel.
179 """
180 self._cancel_callbacks.append(callback)
181
182 def is_valid(self, seek_position_ms: int = 0) -> bool:
183 """
184 Check if the buffer can serve the given seek position.
185
186 :param seek_position_ms: The position to seek to in milliseconds.
187 """
188 if self.cancelled:
189 return False
190
191 # reset inactivity timer â checking validity is activity
192 self._last_access_time = time.time()
193
194 seek_chunk = seek_position_ms // 1000
195
196 if seek_chunk < self._discarded_chunks:
197 return False
198
199 total_chunks = self._discarded_chunks + len(self._chunks)
200 if seek_chunk < total_chunks or self._eof_received:
201 return True
202
203 # chunk is ahead of what's buffered â check if close enough to wait
204 chunks_ahead = seek_chunk - total_chunks
205 return chunks_ahead <= SEEK_WAIT_THRESHOLD
206
207 async def get_raw_stream(
208 self, seek_position_ms: int = 0, exact_seek: bool = False
209 ) -> AsyncGenerator[bytes]:
210 """
211 Get raw (unprocessed) PCM audio from the buffer.
212
213 :param seek_position_ms: Starting position in milliseconds.
214 :param exact_seek: Preserve millisecond precision instead of quantizing to 100 ms.
215 """
216 if not exact_seek:
217 # align regular user seeks to 100ms steps to avoid rounding issues
218 seek_position_ms = (seek_position_ms // 100) * 100
219 chunk_number = seek_position_ms // 1000
220 # handle fractional seek: trim leading samples from the first chunk
221 fractional_ms = seek_position_ms % 1000
222 trim_bytes = 0
223 if fractional_ms > 0:
224 samples_to_trim = self.pcm_format.sample_rate * fractional_ms // 1000
225 bytes_per_sample = (self.pcm_format.bit_depth // 8) * self.pcm_format.channels
226 trim_bytes = samples_to_trim * bytes_per_sample
227
228 while True:
229 try:
230 self._last_access_time = time.time()
231 chunk = await self._get(chunk_number=chunk_number)
232 if trim_bytes > 0:
233 chunk = chunk[trim_bytes:]
234 trim_bytes = 0
235 yield chunk
236 chunk_number += 1
237 except AudioBufferEOF:
238 break
239
240 async def read_chunk_for_analysis(self, chunk_number: int) -> bytes:
241 """
242 Return one PCM chunk for a passive (analysis) reader, waiting until it is available.
243
244 A read-only accessor: it leaves the buffer untouched â no discard, no producer-space
245 signalling, no inactivity-timer reset â so an analysis reader never affects playback's
246 buffering.
247
248 :param chunk_number: Absolute chunk index to read.
249 :raises AudioBufferEOF: the stream ended before this chunk.
250 :raises AudioBufferDiscarded: the chunk has been evicted from the retained window (the
251 reader is a full window behind playback) or the buffer was torn down.
252 """
253 async with self._data_available:
254 while True:
255 if self.cancelled:
256 raise AudioBufferDiscarded
257 if chunk_number < self._discarded_chunks:
258 raise AudioBufferDiscarded
259 index = chunk_number - self._discarded_chunks
260 if index < len(self._chunks):
261 return self._chunks[index]
262 if self._producer_error:
263 raise self._producer_error
264 if self._eof_received:
265 raise AudioBufferEOF
266 await self._data_available.wait()
267
268 async def get_stream(
269 self,
270 output_format: AudioFormat,
271 seek_position_ms: int = 0,
272 filter_params: list[str] | None = None,
273 exact_seek: bool = False,
274 ) -> AsyncGenerator[bytes]:
275 """
276 Get processed audio from the buffer.
277
278 Returns audio in the requested output format with optional filters applied.
279 If no processing is needed, yields directly from the buffer.
280
281 :param output_format: The desired output PCM format.
282 :param seek_position_ms: Starting position in milliseconds.
283 :param filter_params: FFmpeg filter parameters to apply.
284 :param exact_seek: Preserve millisecond precision for the input buffer position.
285 """
286 # not `!=`: that compares equal for integer and float PCM of the same
287 # depth, and passing one through as the other reinterprets every sample
288 needs_ffmpeg = bool(filter_params) or not pcm_formats_match(self.pcm_format, output_format)
289
290 if not needs_ffmpeg:
291 async for chunk in self.get_raw_stream(
292 seek_position_ms=seek_position_ms, exact_seek=exact_seek
293 ):
294 yield chunk
295 return
296
297 async for chunk in get_ffmpeg_stream(
298 audio_input=self.get_raw_stream(
299 seek_position_ms=seek_position_ms, exact_seek=exact_seek
300 ),
301 input_format=self.pcm_format,
302 output_format=output_format,
303 filter_params=filter_params,
304 ):
305 yield chunk
306
307 def fill(self, audio_source: AsyncGenerator[bytes], source_name: str = "unknown") -> None:
308 """
309 Start filling the buffer from an async generator of PCM audio chunks.
310
311 :param audio_source: Async generator yielding 1-second PCM audio chunks.
312 :param source_name: Name for logging purposes.
313 """
314
315 async def _fill_task() -> None:
316 chunk_count = 0
317 status = "running"
318 try:
319 # aclosing guarantees the source generator (and any ffmpeg chain
320 # behind it) is finalized immediately when this task is cancelled,
321 # instead of lingering until garbage collection.
322 async with aclosing(audio_source):
323 async for chunk in audio_source:
324 chunk_count += 1
325 await self._put(chunk)
326 await asyncio.sleep(0)
327 await self._set_eof()
328 except asyncio.CancelledError:
329 status = "cancelled"
330 raise
331 except Exception as err:
332 status = "aborted with error"
333 # record the error before the EOF signal below, so readers that
334 # check for a producer error never observe the abort as a clean EOF
335 self._producer_error = err
336 raise
337 finally:
338 # signal EOF even on error if we produced valid chunks,
339 # so the consumer can read all buffered data before seeing the error
340 if status == "aborted with error" and chunk_count > 0:
341 await self._set_eof()
342 LOGGER.log(
343 VERBOSE_LOG_LEVEL,
344 "fill: %s (%s chunks) for %s",
345 status,
346 chunk_count,
347 source_name,
348 )
349
350 loop = asyncio.get_running_loop()
351 task = loop.create_task(_fill_task())
352 self._attach_producer_task(task)
353
354 async def clear(self, cancel_inactivity_task: bool = True) -> None:
355 """Reset the buffer, clearing all data and cancelling active tasks."""
356 chunk_count = len(self._chunks)
357 LOGGER.log(
358 VERBOSE_LOG_LEVEL,
359 "AudioBuffer.clear: Resetting buffer (had %s chunks, producer: %s)",
360 chunk_count,
361 self._producer_task is not None,
362 )
363 if self._producer_task and not self._producer_task.done():
364 self._producer_task.cancel()
365 with suppress(asyncio.CancelledError):
366 await self._producer_task
367
368 if cancel_inactivity_task and self._inactivity_task and not self._inactivity_task.done():
369 self._inactivity_task.cancel()
370 with suppress(asyncio.CancelledError):
371 await self._inactivity_task
372
373 # signal cancel callbacks only if the stream did not complete normally
374 if not self._eof_received:
375 for callback in list(self._cancel_callbacks):
376 try:
377 callback()
378 except Exception:
379 LOGGER.exception("Cancel callback failed during clear")
380
381 async with self._lock:
382 self._chunks = deque()
383 self._discarded_chunks = 0
384 self._eof_received = False
385 self._cancelled = True
386 self._producer_error = None
387 self.ready.clear()
388 self._cancel_callbacks.clear()
389 self._data_available.notify_all()
390 self._space_available.notify_all()
391
392 @staticmethod
393 async def get_buffer(
394 mass: MusicAssistant,
395 streamdetails: StreamDetails,
396 seek_position_ms: int = 0,
397 wait_ready: bool = False,
398 reason: str = "",
399 source_wait_timeout: float | None = STREAM_SLOT_WAIT_TIMEOUT,
400 ) -> AudioBuffer:
401 """
402 Get or create an AudioBuffer for the given streamdetails.
403
404 Reuses an existing valid buffer if available.
405 Buffer size is determined from the streams controller configuration.
406
407 :param mass: The MusicAssistant instance.
408 :param streamdetails: The stream details for the media.
409 :param seek_position_ms: Position in milliseconds to start from.
410 :param wait_ready: If True, wait for the first chunk before returning.
411 :param reason: Caller context for logging (e.g. 'prepare', 'streaming').
412 :param source_wait_timeout: Maximum seconds the producer may wait for a free
413 source-stream slot on the providing music provider, or None to wait
414 without a timeout.
415 :raises AudioError: If the buffer does not become ready, wrapping the typed
416 producer error (e.g. ProviderStreamLimitError) when there is one.
417 """
418 log_prefix = f"get_buffer[{reason}]" if reason else "get_buffer"
419 # the producer may spend its source wait before the first byte arrives,
420 # so the readiness budget covers that wait on top of the audio itself
421 ready_timeout = BUFFER_READY_TIMEOUT + (source_wait_timeout or 0)
422 # determine buffer size from config
423 buffer_size = BufferSize(
424 mass.config.get_raw_core_config_value(
425 "streams", CONF_BUFFER_SIZE, CONF_BUFFER_SIZE_DEFAULT
426 )
427 )
428 mode = (
429 BufferMode.ROLLING
430 if (not streamdetails.duration or not streamdetails.allow_seek)
431 else BufferMode.SEEKABLE
432 )
433
434 # reuse existing valid buffer
435 existing_buffer: AudioBuffer | None = streamdetails.buffer
436 if existing_buffer is not None:
437 if existing_buffer.has_error or not existing_buffer.is_valid(seek_position_ms):
438 LOGGER.debug(
439 "%s: Existing buffer invalid for %s (seek_ms: %s, discarded: %s)",
440 log_prefix,
441 streamdetails.uri,
442 seek_position_ms,
443 existing_buffer._discarded_chunks,
444 )
445 streamdetails.buffer = None
446 # a still-filling producer holds one of the provider's source-stream slots.
447 # The replacement needs a slot, so take this one back only when the provider
448 # has none free - otherwise a superseded consumer keeps draining its audio.
449 provider = mass.get_provider(streamdetails.provider, return_unavailable=True)
450 must_release_slot = (
451 existing_buffer.is_buffering
452 and isinstance(provider, MusicProvider)
453 and provider.max_concurrent_streams is not None
454 and not provider.has_available_stream_slot
455 )
456 if must_release_slot or time.time() - existing_buffer._last_access_time > 30:
457 await asyncio.shield(existing_buffer.clear())
458 # else: an active consumer is still reading via its local reference;
459 # the inactivity monitor will clean up after it finishes
460 else:
461 LOGGER.debug(
462 "%s: Reusing buffer for %s - available: %ss, seek_ms: %s, discarded: %s",
463 log_prefix,
464 streamdetails.uri,
465 existing_buffer.seconds_available,
466 seek_position_ms,
467 existing_buffer._discarded_chunks,
468 )
469 if wait_ready:
470 await existing_buffer._wait_until_ready(streamdetails, ready_timeout)
471 return existing_buffer
472
473 # convert ms to seconds for get_media_stream (FFmpeg works in seconds)
474 seek_seconds = seek_position_ms // 1000
475
476 # for large seeks without existing buffer, start at seek position.
477 # A realtime source can not produce the skipped audio any faster than playback,
478 # so it always seeks at the source instead of buffering up to the seek point.
479 buffer_seek_seconds = seek_seconds if streamdetails.is_realtime or seek_seconds > 60 else 0
480
481 pcm_format = _buffer_pcm_format(streamdetails)
482
483 # determine ready threshold: how many seconds of audio must be buffered
484 # before signaling ready for playback
485 queue = mass.player_queues.get(streamdetails.queue_id) if streamdetails.queue_id else None
486 crossfade_enabled = bool(
487 queue and queue.crossfade_enabled and streamdetails.media_type == MediaType.TRACK
488 )
489 dynamic_normalization = (
490 streamdetails.volume_normalization_mode == VolumeNormalizationMode.DYNAMIC
491 )
492 if streamdetails.is_realtime:
493 # A realtime source fills the buffer at playback pace, so every second of
494 # audio asked for here is a second of extra startup delay - on a seek or a
495 # track change as much as on a start. The queue's crossfade setting buys
496 # nothing for such a source, because MA's own crossfade is force-disabled
497 # for it (it has no audio to spare for an overlap), so only dynamic
498 # normalization, which genuinely needs lookahead, raises this.
499 ready_threshold = 2 if dynamic_normalization else 1
500 elif crossfade_enabled:
501 ready_threshold = 8
502 elif dynamic_normalization:
503 # radio streams are continuous so the normalization will converge quickly,
504 # use a lower threshold to reduce startup latency
505 ready_threshold = 3 if streamdetails.media_type == MediaType.RADIO else 5
506 else:
507 ready_threshold = 2
508
509 # cap threshold at buffer capacity to prevent deadlock
510 max_size = RADIO_BUFFER_SIZE if mode == BufferMode.ROLLING else BUFFER_SIZE_MAP[buffer_size]
511 ready_threshold = min(ready_threshold, max_size)
512
513 LOGGER.debug(
514 "%s: Creating new buffer for %s (mode: %s, size: %s, seek_ms: %s)",
515 log_prefix,
516 streamdetails.uri,
517 mode,
518 buffer_size,
519 seek_position_ms,
520 )
521 audio_buffer = AudioBuffer(pcm_format, buffer_size, mode, ready_threshold=ready_threshold)
522 # align chunk numbering with the actual stream start position so that
523 # get_raw_stream(seek_position_ms) requests the correct chunk number
524 audio_buffer._discarded_chunks = buffer_seek_seconds
525 # set the chunk number at which the buffer should signal ready,
526 # accounting for seek position so we have enough data past the seek point
527 seek_chunk = seek_position_ms // 1000
528 audio_buffer._ready_at_chunk = seek_chunk + ready_threshold
529 streamdetails.buffer = audio_buffer
530
531 # attach analyze jobs for ahead-of-time processing
532 # skip AudioSource and SoundEffect â they should not feed the long-running analyzer flow
533 # (radio still runs analysis; the analyzer caps it at 10 minutes)
534 if seek_position_ms == 0 and streamdetails.media_type not in (
535 MediaType.AUDIO_SOURCE,
536 MediaType.SOUND_EFFECT,
537 ):
538 # audio analysis providers (loudness, beat tracking, key detection, etc.).
539 # Fire-and-forget: analysis setup â including a possible model (re)load â must never
540 # delay the buffer fill. The analysis worker reads the retained chunks once ready.
541 mass.create_task(
542 mass.streams.audio_analysis.start_analysis(audio_buffer, streamdetails)
543 )
544
545 # start filling from the media stream (seek in seconds for FFmpeg)
546 audio_source = mass.streams.audio.get_media_stream(
547 streamdetails,
548 pcm_format,
549 seek_position=buffer_seek_seconds,
550 filter_params=None,
551 source_wait_timeout=source_wait_timeout,
552 )
553 audio_buffer.fill(audio_source, source_name=streamdetails.uri)
554
555 if wait_ready:
556 await audio_buffer._wait_until_ready(streamdetails, ready_timeout)
557
558 return audio_buffer
559
560 # -- Private methods --
561
562 async def _wait_until_ready(self, streamdetails: StreamDetails, ready_timeout: float) -> None:
563 """
564 Wait until this buffer can serve playback or raise its producer failure.
565
566 :param streamdetails: Stream details currently referencing this buffer.
567 :param ready_timeout: Maximum seconds to wait for enough buffered audio.
568 """
569 async with self._ready_wait_lock:
570 if not self.ready.is_set():
571 try:
572 await asyncio.wait_for(self.ready.wait(), timeout=ready_timeout)
573 except TimeoutError as err:
574 producer_error = await self._clear_failed_buffer(streamdetails)
575 if isinstance(producer_error, AudioError):
576 raise producer_error from err
577 raise AudioError("Timeout waiting for audio data") from (producer_error or err)
578 # ready was signaled but check if it was due to a producer error
579 # (ready is also set by _notify_on_producer_error)
580 if not self.has_error:
581 return
582 producer_error = await self._clear_failed_buffer(streamdetails)
583 # surface a typed producer failure (e.g. a source capacity limit) as-is,
584 # so callers can act on it instead of on a generic wrapper
585 if isinstance(producer_error, AudioError):
586 raise producer_error
587 raise AudioError("Failed to stream audio") from producer_error
588
589 async def _clear_failed_buffer(self, streamdetails: StreamDetails) -> Exception | None:
590 """
591 Detach and clear this buffer after preparation failed.
592
593 :param streamdetails: Stream details currently referencing this buffer.
594 :return: The producer error recorded before the buffer was cleared.
595 """
596 producer_error = self._producer_error
597 if streamdetails.buffer is self:
598 streamdetails.buffer = None
599 await asyncio.shield(self.clear())
600 return producer_error
601
602 async def _put(self, chunk: bytes) -> None:
603 """
604 Put a 1-second chunk of PCM audio into the buffer.
605
606 Waits for space when the buffer is full (backpressure).
607 """
608 async with self._lock:
609 if self._cancelled:
610 return
611
612 if self._eof_received:
613 LOGGER.log(
614 VERBOSE_LOG_LEVEL, "AudioBuffer._put: EOF already received, rejecting chunk"
615 )
616 return
617
618 # wait for the consumer to free space when buffer is full
619 await self._wait_for_space()
620
621 chunk_position = self._discarded_chunks + len(self._chunks)
622 self._chunks.append(chunk)
623 if LOGGER.isEnabledFor(VERBOSE_LOG_LEVEL):
624 LOGGER.log(
625 VERBOSE_LOG_LEVEL,
626 "AudioBuffer._put: Added chunk at position %s (size: %s bytes, buffer: %s)",
627 chunk_position,
628 len(chunk),
629 len(self._chunks),
630 )
631
632 if not self.ready.is_set() and (
633 self._discarded_chunks + len(self._chunks) >= self._ready_at_chunk
634 or len(self._chunks) >= self.max_size_seconds
635 ):
636 self.ready.set()
637
638 self._data_available.notify_all()
639
640 async def _set_eof(self) -> None:
641 """Signal that no more data will be added to the buffer."""
642 async with self._lock:
643 LOGGER.log(
644 VERBOSE_LOG_LEVEL,
645 "AudioBuffer._set_eof: Marking EOF (buffer has %s chunks)",
646 len(self._chunks),
647 )
648 self._eof_received = True
649 if not self.ready.is_set():
650 self.ready.set()
651 self._data_available.notify_all()
652 self._space_available.notify_all()
653
654 async def _get(self, chunk_number: int = 0) -> bytes:
655 """
656 Get one second of audio at the given chunk position.
657
658 Waits until the chunk is available. Discards old chunks when full.
659
660 :raises AudioBufferEOF: If EOF is reached or the buffer was cleared.
661 :raises AudioError: If the chunk has been discarded or the producer failed.
662 """
663 async with self._data_available:
664 if len(self._chunks) == 0:
665 # Producer errors also set EOF after buffered data; preserve the real failure.
666 if self._producer_error:
667 raise self._producer_error
668 if self._eof_received or self.cancelled:
669 raise AudioBufferEOF
670 if self.cancelled:
671 raise AudioBufferEOF
672
673 if self.mode == BufferMode.ROLLING:
674 return await self._get_rolling()
675
676 return await self._get_seekable(chunk_number)
677
678 async def _get_rolling(self) -> bytes:
679 """
680 Pop the next chunk from the buffer (FIFO).
681
682 Must be called while holding _data_available lock.
683 """
684 while len(self._chunks) == 0:
685 if self._producer_error:
686 raise self._producer_error
687 if self.cancelled or self._eof_received:
688 raise AudioBufferEOF
689 await self._data_available.wait()
690
691 result = self._chunks.popleft()
692 self._discarded_chunks += 1
693 self._space_available.notify_all()
694 return result
695
696 async def _get_seekable(self, chunk_number: int) -> bytes:
697 """
698 Get a specific chunk by number from the buffer.
699
700 Must be called while holding _data_available lock.
701 """
702 if chunk_number < self._discarded_chunks:
703 msg = (
704 f"Chunk {chunk_number} has been discarded "
705 f"(buffer starts at {self._discarded_chunks})"
706 )
707 raise AudioError(msg)
708
709 buffer_index = chunk_number - self._discarded_chunks
710 while buffer_index >= len(self._chunks):
711 # Producer errors also set EOF after buffered data; preserve the real failure.
712 if self._producer_error:
713 raise self._producer_error
714 if self.cancelled or self._eof_received:
715 raise AudioBufferEOF
716 # if the buffer is full and we need a chunk that hasn't arrived yet,
717 # the producer is blocked waiting for space â evict to unblock it
718 if len(self._chunks) >= self.max_size_seconds:
719 self._chunks.popleft()
720 self._discarded_chunks += 1
721 buffer_index = chunk_number - self._discarded_chunks
722 self._space_available.notify_all()
723 continue
724 await self._data_available.wait()
725 buffer_index = chunk_number - self._discarded_chunks
726
727 result = self._chunks[buffer_index]
728
729 # free space for the producer when buffer is at capacity,
730 # but only if the producer is still running and needs space
731 if (
732 len(self._chunks) >= self.max_size_seconds
733 and not self._eof_received
734 and self._producer_task
735 and not self._producer_task.done()
736 ):
737 self._chunks.popleft()
738 self._discarded_chunks += 1
739 self._space_available.notify_all()
740
741 return result
742
743 async def _wait_for_space(self) -> None:
744 """Wait until buffer has space. Must be called while holding _lock."""
745 while len(self._chunks) >= self.max_size_seconds:
746 if self._cancelled:
747 return
748 await self._space_available.wait()
749
750 def _attach_producer_task(self, task: asyncio.Task[Any]) -> None:
751 """Attach a background task that fills the buffer."""
752 self._producer_task = task
753
754 def _on_producer_done(t: asyncio.Task[Any]) -> None:
755 if t.cancelled():
756 return
757 exc = t.exception()
758 if exc is not None and isinstance(exc, Exception):
759 self._producer_error = exc
760 loop = asyncio.get_running_loop()
761 task = loop.create_task(self._notify_on_producer_error())
762 self._background_tasks.add(task)
763 task.add_done_callback(self._background_tasks.discard)
764
765 task.add_done_callback(_on_producer_done)
766
767 if self._inactivity_task is None or self._inactivity_task.done():
768 self._last_access_time = time.time()
769 loop = asyncio.get_running_loop()
770 self._inactivity_task = loop.create_task(self._monitor_inactivity())
771
772 async def _monitor_inactivity(
773 self, inactivity_timeout: float = 300, check_interval: float = 30
774 ) -> None:
775 """
776 Clear the buffer once it has been inactive for inactivity_timeout seconds.
777
778 :param inactivity_timeout: Seconds without access before the buffer is released.
779 :param check_interval: Seconds between inactivity checks.
780 """
781 while True:
782 await asyncio.sleep(check_interval)
783 time_since_access = time.time() - self._last_access_time
784 # break on inactivity regardless of how many chunks remain: a rolling buffer
785 # that has drained to empty (e.g. an abandoned radio stream) must still release
786 # its resources and stop this monitor, otherwise the task loops forever
787 if time_since_access > inactivity_timeout:
788 LOGGER.log(
789 VERBOSE_LOG_LEVEL,
790 "AudioBuffer: No activity for %.1fs, clearing (%s chunks)",
791 time_since_access,
792 len(self._chunks),
793 )
794 break
795 await self.clear(cancel_inactivity_task=False)
796
797 async def _notify_on_producer_error(self) -> None:
798 """Notify waiting consumers that the producer has failed."""
799 async with self._lock:
800 if not self.ready.is_set():
801 self.ready.set()
802 self._data_available.notify_all()
803
804
805def _buffer_pcm_format(streamdetails: StreamDetails) -> AudioFormat:
806 """
807 Return the PCM format a buffer for these streamdetails holds.
808
809 The buffer stores decoded PCM, so it follows the audio that actually
810 arrives: ``audio_format`` may describe a source the provider decoded on our
811 behalf and can differ in depth or rate, in which case deriving the buffer
812 from it would resample or truncate real audio.
813
814 :param streamdetails: The stream the buffer is for.
815 """
816 arriving = arriving_audio_format(streamdetails)
817 return AudioFormat(
818 content_type=ContentType.from_bit_depth(arriving.bit_depth),
819 sample_rate=arriving.sample_rate,
820 bit_depth=arriving.bit_depth,
821 # buffer the stereo fold of a surround source, so audio analysis measures
822 # the same audio that is played back rather than the untouched surround mix
823 channels=min(arriving.channels, 2),
824 )
825