/
/
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
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 needs_ffmpeg = bool(filter_params) or self.pcm_format != output_format
287
288 if not needs_ffmpeg:
289 async for chunk in self.get_raw_stream(
290 seek_position_ms=seek_position_ms, exact_seek=exact_seek
291 ):
292 yield chunk
293 return
294
295 async for chunk in get_ffmpeg_stream(
296 audio_input=self.get_raw_stream(
297 seek_position_ms=seek_position_ms, exact_seek=exact_seek
298 ),
299 input_format=self.pcm_format,
300 output_format=output_format,
301 filter_params=filter_params,
302 ):
303 yield chunk
304
305 def fill(self, audio_source: AsyncGenerator[bytes], source_name: str = "unknown") -> None:
306 """
307 Start filling the buffer from an async generator of PCM audio chunks.
308
309 :param audio_source: Async generator yielding 1-second PCM audio chunks.
310 :param source_name: Name for logging purposes.
311 """
312
313 async def _fill_task() -> None:
314 chunk_count = 0
315 status = "running"
316 try:
317 # aclosing guarantees the source generator (and any ffmpeg chain
318 # behind it) is finalized immediately when this task is cancelled,
319 # instead of lingering until garbage collection.
320 async with aclosing(audio_source):
321 async for chunk in audio_source:
322 chunk_count += 1
323 await self._put(chunk)
324 await asyncio.sleep(0)
325 await self._set_eof()
326 except asyncio.CancelledError:
327 status = "cancelled"
328 raise
329 except Exception as err:
330 status = "aborted with error"
331 # record the error before the EOF signal below, so readers that
332 # check for a producer error never observe the abort as a clean EOF
333 self._producer_error = err
334 raise
335 finally:
336 # signal EOF even on error if we produced valid chunks,
337 # so the consumer can read all buffered data before seeing the error
338 if status == "aborted with error" and chunk_count > 0:
339 await self._set_eof()
340 LOGGER.log(
341 VERBOSE_LOG_LEVEL,
342 "fill: %s (%s chunks) for %s",
343 status,
344 chunk_count,
345 source_name,
346 )
347
348 loop = asyncio.get_running_loop()
349 task = loop.create_task(_fill_task())
350 self._attach_producer_task(task)
351
352 async def clear(self, cancel_inactivity_task: bool = True) -> None:
353 """Reset the buffer, clearing all data and cancelling active tasks."""
354 chunk_count = len(self._chunks)
355 LOGGER.log(
356 VERBOSE_LOG_LEVEL,
357 "AudioBuffer.clear: Resetting buffer (had %s chunks, producer: %s)",
358 chunk_count,
359 self._producer_task is not None,
360 )
361 if self._producer_task and not self._producer_task.done():
362 self._producer_task.cancel()
363 with suppress(asyncio.CancelledError):
364 await self._producer_task
365
366 if cancel_inactivity_task and self._inactivity_task and not self._inactivity_task.done():
367 self._inactivity_task.cancel()
368 with suppress(asyncio.CancelledError):
369 await self._inactivity_task
370
371 # signal cancel callbacks only if the stream did not complete normally
372 if not self._eof_received:
373 for callback in list(self._cancel_callbacks):
374 try:
375 callback()
376 except Exception:
377 LOGGER.exception("Cancel callback failed during clear")
378
379 async with self._lock:
380 self._chunks = deque()
381 self._discarded_chunks = 0
382 self._eof_received = False
383 self._cancelled = True
384 self._producer_error = None
385 self.ready.clear()
386 self._cancel_callbacks.clear()
387 self._data_available.notify_all()
388 self._space_available.notify_all()
389
390 @staticmethod
391 async def get_buffer(
392 mass: MusicAssistant,
393 streamdetails: StreamDetails,
394 seek_position_ms: int = 0,
395 wait_ready: bool = False,
396 reason: str = "",
397 source_wait_timeout: float | None = STREAM_SLOT_WAIT_TIMEOUT,
398 ) -> AudioBuffer:
399 """
400 Get or create an AudioBuffer for the given streamdetails.
401
402 Reuses an existing valid buffer if available.
403 Buffer size is determined from the streams controller configuration.
404
405 :param mass: The MusicAssistant instance.
406 :param streamdetails: The stream details for the media.
407 :param seek_position_ms: Position in milliseconds to start from.
408 :param wait_ready: If True, wait for the first chunk before returning.
409 :param reason: Caller context for logging (e.g. 'prepare', 'streaming').
410 :param source_wait_timeout: Maximum seconds the producer may wait for a free
411 source-stream slot on the providing music provider, or None to wait
412 without a timeout.
413 :raises AudioError: If the buffer does not become ready, wrapping the typed
414 producer error (e.g. ProviderStreamLimitError) when there is one.
415 """
416 log_prefix = f"get_buffer[{reason}]" if reason else "get_buffer"
417 # the producer may spend its source wait before the first byte arrives,
418 # so the readiness budget covers that wait on top of the audio itself
419 ready_timeout = BUFFER_READY_TIMEOUT + (source_wait_timeout or 0)
420 # determine buffer size from config
421 buffer_size = BufferSize(
422 mass.config.get_raw_core_config_value(
423 "streams", CONF_BUFFER_SIZE, CONF_BUFFER_SIZE_DEFAULT
424 )
425 )
426 mode = (
427 BufferMode.ROLLING
428 if (not streamdetails.duration or not streamdetails.allow_seek)
429 else BufferMode.SEEKABLE
430 )
431
432 # reuse existing valid buffer
433 existing_buffer: AudioBuffer | None = streamdetails.buffer
434 if existing_buffer is not None:
435 if existing_buffer.has_error or not existing_buffer.is_valid(seek_position_ms):
436 LOGGER.debug(
437 "%s: Existing buffer invalid for %s (seek_ms: %s, discarded: %s)",
438 log_prefix,
439 streamdetails.uri,
440 seek_position_ms,
441 existing_buffer._discarded_chunks,
442 )
443 streamdetails.buffer = None
444 # a still-filling producer holds one of the provider's source-stream slots.
445 # The replacement needs a slot, so take this one back only when the provider
446 # has none free - otherwise a superseded consumer keeps draining its audio.
447 provider = mass.get_provider(streamdetails.provider, return_unavailable=True)
448 must_release_slot = (
449 existing_buffer.is_buffering
450 and isinstance(provider, MusicProvider)
451 and provider.max_concurrent_streams is not None
452 and not provider.has_available_stream_slot
453 )
454 if must_release_slot or time.time() - existing_buffer._last_access_time > 30:
455 await asyncio.shield(existing_buffer.clear())
456 # else: an active consumer is still reading via its local reference;
457 # the inactivity monitor will clean up after it finishes
458 else:
459 LOGGER.debug(
460 "%s: Reusing buffer for %s - available: %ss, seek_ms: %s, discarded: %s",
461 log_prefix,
462 streamdetails.uri,
463 existing_buffer.seconds_available,
464 seek_position_ms,
465 existing_buffer._discarded_chunks,
466 )
467 if wait_ready:
468 await existing_buffer._wait_until_ready(streamdetails, ready_timeout)
469 return existing_buffer
470
471 # convert ms to seconds for get_media_stream (FFmpeg works in seconds)
472 seek_seconds = seek_position_ms // 1000
473
474 # for large seeks without existing buffer, start at seek position.
475 # A realtime source can not produce the skipped audio any faster than playback,
476 # so it always seeks at the source instead of buffering up to the seek point.
477 buffer_seek_seconds = seek_seconds if streamdetails.is_realtime or seek_seconds > 60 else 0
478
479 pcm_format = _buffer_pcm_format(streamdetails)
480
481 # determine ready threshold: how many seconds of audio must be buffered
482 # before signaling ready for playback
483 queue = mass.player_queues.get(streamdetails.queue_id) if streamdetails.queue_id else None
484 crossfade_enabled = bool(
485 queue and queue.crossfade_enabled and streamdetails.media_type == MediaType.TRACK
486 )
487 dynamic_normalization = (
488 streamdetails.volume_normalization_mode == VolumeNormalizationMode.DYNAMIC
489 )
490 if streamdetails.is_realtime:
491 # A realtime source fills the buffer at playback pace, so every second of
492 # audio asked for here is a second of extra startup delay - on a seek or a
493 # track change as much as on a start. The queue's crossfade setting buys
494 # nothing for such a source, because MA's own crossfade is force-disabled
495 # for it (it has no audio to spare for an overlap), so only dynamic
496 # normalization, which genuinely needs lookahead, raises this.
497 ready_threshold = 2 if dynamic_normalization else 1
498 elif crossfade_enabled:
499 ready_threshold = 8
500 elif dynamic_normalization:
501 # radio streams are continuous so the normalization will converge quickly,
502 # use a lower threshold to reduce startup latency
503 ready_threshold = 3 if streamdetails.media_type == MediaType.RADIO else 5
504 else:
505 ready_threshold = 2
506
507 # cap threshold at buffer capacity to prevent deadlock
508 max_size = RADIO_BUFFER_SIZE if mode == BufferMode.ROLLING else BUFFER_SIZE_MAP[buffer_size]
509 ready_threshold = min(ready_threshold, max_size)
510
511 LOGGER.debug(
512 "%s: Creating new buffer for %s (mode: %s, size: %s, seek_ms: %s)",
513 log_prefix,
514 streamdetails.uri,
515 mode,
516 buffer_size,
517 seek_position_ms,
518 )
519 audio_buffer = AudioBuffer(pcm_format, buffer_size, mode, ready_threshold=ready_threshold)
520 # align chunk numbering with the actual stream start position so that
521 # get_raw_stream(seek_position_ms) requests the correct chunk number
522 audio_buffer._discarded_chunks = buffer_seek_seconds
523 # set the chunk number at which the buffer should signal ready,
524 # accounting for seek position so we have enough data past the seek point
525 seek_chunk = seek_position_ms // 1000
526 audio_buffer._ready_at_chunk = seek_chunk + ready_threshold
527 streamdetails.buffer = audio_buffer
528
529 # attach analyze jobs for ahead-of-time processing
530 # skip AudioSource and SoundEffect â they should not feed the long-running analyzer flow
531 # (radio still runs analysis; the analyzer caps it at 10 minutes)
532 if seek_position_ms == 0 and streamdetails.media_type not in (
533 MediaType.AUDIO_SOURCE,
534 MediaType.SOUND_EFFECT,
535 ):
536 # audio analysis providers (loudness, beat tracking, key detection, etc.).
537 # Fire-and-forget: analysis setup â including a possible model (re)load â must never
538 # delay the buffer fill. The analysis worker reads the retained chunks once ready.
539 mass.create_task(
540 mass.streams.audio_analysis.start_analysis(audio_buffer, streamdetails)
541 )
542
543 # start filling from the media stream (seek in seconds for FFmpeg)
544 audio_source = mass.streams.audio.get_media_stream(
545 streamdetails,
546 pcm_format,
547 seek_position=buffer_seek_seconds,
548 filter_params=None,
549 source_wait_timeout=source_wait_timeout,
550 )
551 audio_buffer.fill(audio_source, source_name=streamdetails.uri)
552
553 if wait_ready:
554 await audio_buffer._wait_until_ready(streamdetails, ready_timeout)
555
556 return audio_buffer
557
558 # -- Private methods --
559
560 async def _wait_until_ready(self, streamdetails: StreamDetails, ready_timeout: float) -> None:
561 """
562 Wait until this buffer can serve playback or raise its producer failure.
563
564 :param streamdetails: Stream details currently referencing this buffer.
565 :param ready_timeout: Maximum seconds to wait for enough buffered audio.
566 """
567 async with self._ready_wait_lock:
568 if not self.ready.is_set():
569 try:
570 await asyncio.wait_for(self.ready.wait(), timeout=ready_timeout)
571 except TimeoutError as err:
572 producer_error = await self._clear_failed_buffer(streamdetails)
573 if isinstance(producer_error, AudioError):
574 raise producer_error from err
575 raise AudioError("Timeout waiting for audio data") from (producer_error or err)
576 # ready was signaled but check if it was due to a producer error
577 # (ready is also set by _notify_on_producer_error)
578 if not self.has_error:
579 return
580 producer_error = await self._clear_failed_buffer(streamdetails)
581 # surface a typed producer failure (e.g. a source capacity limit) as-is,
582 # so callers can act on it instead of on a generic wrapper
583 if isinstance(producer_error, AudioError):
584 raise producer_error
585 raise AudioError("Failed to stream audio") from producer_error
586
587 async def _clear_failed_buffer(self, streamdetails: StreamDetails) -> Exception | None:
588 """
589 Detach and clear this buffer after preparation failed.
590
591 :param streamdetails: Stream details currently referencing this buffer.
592 :return: The producer error recorded before the buffer was cleared.
593 """
594 producer_error = self._producer_error
595 if streamdetails.buffer is self:
596 streamdetails.buffer = None
597 await asyncio.shield(self.clear())
598 return producer_error
599
600 async def _put(self, chunk: bytes) -> None:
601 """
602 Put a 1-second chunk of PCM audio into the buffer.
603
604 Waits for space when the buffer is full (backpressure).
605 """
606 async with self._lock:
607 if self._cancelled:
608 return
609
610 if self._eof_received:
611 LOGGER.log(
612 VERBOSE_LOG_LEVEL, "AudioBuffer._put: EOF already received, rejecting chunk"
613 )
614 return
615
616 # wait for the consumer to free space when buffer is full
617 await self._wait_for_space()
618
619 chunk_position = self._discarded_chunks + len(self._chunks)
620 self._chunks.append(chunk)
621 if LOGGER.isEnabledFor(VERBOSE_LOG_LEVEL):
622 LOGGER.log(
623 VERBOSE_LOG_LEVEL,
624 "AudioBuffer._put: Added chunk at position %s (size: %s bytes, buffer: %s)",
625 chunk_position,
626 len(chunk),
627 len(self._chunks),
628 )
629
630 if not self.ready.is_set() and (
631 self._discarded_chunks + len(self._chunks) >= self._ready_at_chunk
632 or len(self._chunks) >= self.max_size_seconds
633 ):
634 self.ready.set()
635
636 self._data_available.notify_all()
637
638 async def _set_eof(self) -> None:
639 """Signal that no more data will be added to the buffer."""
640 async with self._lock:
641 LOGGER.log(
642 VERBOSE_LOG_LEVEL,
643 "AudioBuffer._set_eof: Marking EOF (buffer has %s chunks)",
644 len(self._chunks),
645 )
646 self._eof_received = True
647 if not self.ready.is_set():
648 self.ready.set()
649 self._data_available.notify_all()
650 self._space_available.notify_all()
651
652 async def _get(self, chunk_number: int = 0) -> bytes:
653 """
654 Get one second of audio at the given chunk position.
655
656 Waits until the chunk is available. Discards old chunks when full.
657
658 :raises AudioBufferEOF: If EOF is reached or the buffer was cleared.
659 :raises AudioError: If the chunk has been discarded or the producer failed.
660 """
661 async with self._data_available:
662 if len(self._chunks) == 0:
663 # Producer errors also set EOF after buffered data; preserve the real failure.
664 if self._producer_error:
665 raise self._producer_error
666 if self._eof_received or self.cancelled:
667 raise AudioBufferEOF
668 if self.cancelled:
669 raise AudioBufferEOF
670
671 if self.mode == BufferMode.ROLLING:
672 return await self._get_rolling()
673
674 return await self._get_seekable(chunk_number)
675
676 async def _get_rolling(self) -> bytes:
677 """
678 Pop the next chunk from the buffer (FIFO).
679
680 Must be called while holding _data_available lock.
681 """
682 while len(self._chunks) == 0:
683 if self._producer_error:
684 raise self._producer_error
685 if self.cancelled or self._eof_received:
686 raise AudioBufferEOF
687 await self._data_available.wait()
688
689 result = self._chunks.popleft()
690 self._discarded_chunks += 1
691 self._space_available.notify_all()
692 return result
693
694 async def _get_seekable(self, chunk_number: int) -> bytes:
695 """
696 Get a specific chunk by number from the buffer.
697
698 Must be called while holding _data_available lock.
699 """
700 if chunk_number < self._discarded_chunks:
701 msg = (
702 f"Chunk {chunk_number} has been discarded "
703 f"(buffer starts at {self._discarded_chunks})"
704 )
705 raise AudioError(msg)
706
707 buffer_index = chunk_number - self._discarded_chunks
708 while buffer_index >= len(self._chunks):
709 # Producer errors also set EOF after buffered data; preserve the real failure.
710 if self._producer_error:
711 raise self._producer_error
712 if self.cancelled or self._eof_received:
713 raise AudioBufferEOF
714 # if the buffer is full and we need a chunk that hasn't arrived yet,
715 # the producer is blocked waiting for space â evict to unblock it
716 if len(self._chunks) >= self.max_size_seconds:
717 self._chunks.popleft()
718 self._discarded_chunks += 1
719 buffer_index = chunk_number - self._discarded_chunks
720 self._space_available.notify_all()
721 continue
722 await self._data_available.wait()
723 buffer_index = chunk_number - self._discarded_chunks
724
725 result = self._chunks[buffer_index]
726
727 # free space for the producer when buffer is at capacity,
728 # but only if the producer is still running and needs space
729 if (
730 len(self._chunks) >= self.max_size_seconds
731 and not self._eof_received
732 and self._producer_task
733 and not self._producer_task.done()
734 ):
735 self._chunks.popleft()
736 self._discarded_chunks += 1
737 self._space_available.notify_all()
738
739 return result
740
741 async def _wait_for_space(self) -> None:
742 """Wait until buffer has space. Must be called while holding _lock."""
743 while len(self._chunks) >= self.max_size_seconds:
744 if self._cancelled:
745 return
746 await self._space_available.wait()
747
748 def _attach_producer_task(self, task: asyncio.Task[Any]) -> None:
749 """Attach a background task that fills the buffer."""
750 self._producer_task = task
751
752 def _on_producer_done(t: asyncio.Task[Any]) -> None:
753 if t.cancelled():
754 return
755 exc = t.exception()
756 if exc is not None and isinstance(exc, Exception):
757 self._producer_error = exc
758 loop = asyncio.get_running_loop()
759 task = loop.create_task(self._notify_on_producer_error())
760 self._background_tasks.add(task)
761 task.add_done_callback(self._background_tasks.discard)
762
763 task.add_done_callback(_on_producer_done)
764
765 if self._inactivity_task is None or self._inactivity_task.done():
766 self._last_access_time = time.time()
767 loop = asyncio.get_running_loop()
768 self._inactivity_task = loop.create_task(self._monitor_inactivity())
769
770 async def _monitor_inactivity(
771 self, inactivity_timeout: float = 300, check_interval: float = 30
772 ) -> None:
773 """
774 Clear the buffer once it has been inactive for inactivity_timeout seconds.
775
776 :param inactivity_timeout: Seconds without access before the buffer is released.
777 :param check_interval: Seconds between inactivity checks.
778 """
779 while True:
780 await asyncio.sleep(check_interval)
781 time_since_access = time.time() - self._last_access_time
782 # break on inactivity regardless of how many chunks remain: a rolling buffer
783 # that has drained to empty (e.g. an abandoned radio stream) must still release
784 # its resources and stop this monitor, otherwise the task loops forever
785 if time_since_access > inactivity_timeout:
786 LOGGER.log(
787 VERBOSE_LOG_LEVEL,
788 "AudioBuffer: No activity for %.1fs, clearing (%s chunks)",
789 time_since_access,
790 len(self._chunks),
791 )
792 break
793 await self.clear(cancel_inactivity_task=False)
794
795 async def _notify_on_producer_error(self) -> None:
796 """Notify waiting consumers that the producer has failed."""
797 async with self._lock:
798 if not self.ready.is_set():
799 self.ready.set()
800 self._data_available.notify_all()
801
802
803def _buffer_pcm_format(streamdetails: StreamDetails) -> AudioFormat:
804 """
805 Return the PCM format a buffer for these streamdetails holds.
806
807 The buffer stores decoded PCM, so it follows the audio that actually
808 arrives: ``audio_format`` may describe a source the provider decoded on our
809 behalf and can differ in depth or rate, in which case deriving the buffer
810 from it would resample or truncate real audio.
811
812 :param streamdetails: The stream the buffer is for.
813 """
814 arriving = arriving_audio_format(streamdetails)
815 return AudioFormat(
816 content_type=ContentType.from_bit_depth(arriving.bit_depth),
817 sample_rate=arriving.sample_rate,
818 bit_depth=arriving.bit_depth,
819 # buffer the stereo fold of a surround source, so audio analysis measures
820 # the same audio that is played back rather than the untouched surround mix
821 channels=min(arriving.channels, 2),
822 )
823