Skip to content

FileSink

Base classes

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

⋔ Inheritance diagram

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

🛈 DocStrings

Bases: AudioSink

Audio sink that writes PCM audio to a WAV file.

Parameters:

Name Type Description Default
path str | PathLike[str]

Path to the output WAV file.

required
sample_rate int

Audio sample rate in Hz. Defaults to 24000.

24000
channels int

Number of audio channels. Defaults to 1 (mono).

1
Source code in src/anyvoice/sinks.py
class FileSink(AudioSink):
    """Audio sink that writes PCM audio to a WAV file.

    Args:
        path: Path to the output WAV file.
        sample_rate: Audio sample rate in Hz. Defaults to 24000.
        channels: Number of audio channels. Defaults to 1 (mono).
    """

    def __init__(
        self,
        path: str | os.PathLike[str],
        *,
        sample_rate: int = 24000,
        channels: int = 1,
    ) -> None:
        self._path = path
        self._sample_rate = sample_rate
        self._channels = channels
        self._chunks: list[bytes] = []

    async def __aenter__(self) -> Self:
        """Prepare for receiving audio."""
        self._chunks = []
        return self

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        """Write collected audio to WAV file."""
        import wave

        if not self._chunks:
            return

        audio_data = b"".join(self._chunks)

        with wave.open(str(self._path), "wb") as wf:
            wf.setnchannels(self._channels)
            wf.setsampwidth(2)  # 16-bit = 2 bytes
            wf.setframerate(self._sample_rate)
            wf.writeframes(audio_data)

        self._chunks = []

    async def write(self, chunk: bytes) -> None:
        """Buffer audio chunk for later writing.

        Args:
            chunk: PCM audio data to save.
        """
        if chunk:
            self._chunks.append(chunk)

__aenter__ async

__aenter__() -> Self

Prepare for receiving audio.

Source code in src/anyvoice/sinks.py
async def __aenter__(self) -> Self:
    """Prepare for receiving audio."""
    self._chunks = []
    return self

__aexit__ async

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

Write collected audio to WAV file.

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:
    """Write collected audio to WAV file."""
    import wave

    if not self._chunks:
        return

    audio_data = b"".join(self._chunks)

    with wave.open(str(self._path), "wb") as wf:
        wf.setnchannels(self._channels)
        wf.setsampwidth(2)  # 16-bit = 2 bytes
        wf.setframerate(self._sample_rate)
        wf.writeframes(audio_data)

    self._chunks = []

write async

write(chunk: bytes) -> None

Buffer audio chunk for later writing.

Parameters:

Name Type Description Default
chunk bytes

PCM audio data to save.

required
Source code in src/anyvoice/sinks.py
async def write(self, chunk: bytes) -> None:
    """Buffer audio chunk for later writing.

    Args:
        chunk: PCM audio data to save.
    """
    if chunk:
        self._chunks.append(chunk)

:fa-brands-github: Show source on GitHub