Skip to content

CallbackSink

Base classes

Name Children Inherits
AudioSink
anyvoice.base
Abstract base class for audio output backends.

⋔ Inheritance diagram

graph TD
  427671724["sinks.CallbackSink"]
  3407701203["base.AudioSink"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3407701203 --> 427671724
  925645236 --> 3407701203
  2751680898 --> 925645236

🛈 DocStrings

Bases: AudioSink

Audio sink that passes audio chunks to a user-provided callback.

Useful for custom audio processing, streaming, or integration with other systems.

Parameters:

Name Type Description Default
callback Callable[[bytes], Awaitable[None]] | Callable[[bytes], None]

Async or sync callable that receives audio chunks.

required
on_close Callable[[], Awaitable[None]] | Callable[[], None] | None

Optional callback invoked when sink closes.

None
Source code in src/anyvoice/sinks.py
class CallbackSink(AudioSink):
    """Audio sink that passes audio chunks to a user-provided callback.

    Useful for custom audio processing, streaming, or integration
    with other systems.

    Args:
        callback: Async or sync callable that receives audio chunks.
        on_close: Optional callback invoked when sink closes.
    """

    def __init__(
        self,
        callback: Callable[[bytes], Awaitable[None]] | Callable[[bytes], None],
        *,
        on_close: Callable[[], Awaitable[None]] | Callable[[], None] | None = None,
    ) -> None:
        self._callback = callback
        self._on_close = on_close

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        """Invoke close callback if provided."""
        if self._on_close is not None:
            result = self._on_close()
            if asyncio.iscoroutine(result):
                await result

    async def write(self, chunk: bytes) -> None:
        """Pass audio chunk to the callback.

        Args:
            chunk: PCM audio data.
        """
        if chunk:
            result = self._callback(chunk)
            if asyncio.iscoroutine(result):
                await result

__aexit__ async

__aexit__(
    exc_type: type[BaseException] | None,
    exc_val: BaseException | None,
    exc_tb: TracebackType | None,
) -> None

Invoke close callback if provided.

Source code in src/anyvoice/sinks.py
async def __aexit__(
    self,
    exc_type: type[BaseException] | None,
    exc_val: BaseException | None,
    exc_tb: TracebackType | None,
) -> None:
    """Invoke close callback if provided."""
    if self._on_close is not None:
        result = self._on_close()
        if asyncio.iscoroutine(result):
            await result

write async

write(chunk: bytes) -> None

Pass audio chunk to the callback.

Parameters:

Name Type Description Default
chunk bytes

PCM audio data.

required
Source code in src/anyvoice/sinks.py
async def write(self, chunk: bytes) -> None:
    """Pass audio chunk to the callback.

    Args:
        chunk: PCM audio data.
    """
    if chunk:
        result = self._callback(chunk)
        if asyncio.iscoroutine(result):
            await result

:fa-brands-github: Show source on GitHub