Skip to content

Speaker

Runnable example

python/examples/speaker_record.py — streams for 5 s, prints peak amplitude, and saves raw float32 LE to speaker_output.raw.

resoio.speaker.SpeakerClient

SpeakerClient(socket_path: str | None = None)

Bases: _BaseClient[SpeakerStub]

Async client for the Resonite IO Speaker service over a UDS.

Use as an async context manager so the gRPC channel is closed deterministically. Socket resolution mirrors :class:resoio.ConnectionClient. The wire format is fixed at 48 kHz / Stereo / float32 LE; constants are exposed at module level (:data:SAMPLE_RATE, :data:CHANNELS, :data:DTYPE).

Source code in src/resoio/_client.py
def __init__(self, socket_path: str | None = None) -> None:
    # Defer resolution to __aenter__ so env vars patched between
    # construction and connection are honoured, and so resolution
    # errors surface at the connect site.
    self._explicit_path: str | None = socket_path
    self._channel: Channel | None = None
    self._stub: TStub | None = None
    self._resolved_path: str | None = None

stream async

stream() -> AsyncIterator[SpeakerChunk]

Stream the Resonite final audio mix from the server.

Yields one :class:SpeakerChunk per server-emitted AudioFrame. Raises :class:RuntimeError if called outside async with.

Source code in src/resoio/speaker.py
async def stream(self) -> AsyncIterator[SpeakerChunk]:
    """Stream the Resonite final audio mix from the server.

    Yields one :class:`SpeakerChunk` per server-emitted ``AudioFrame``.
    Raises :class:`RuntimeError` if called outside ``async with``.
    """
    stub = self._require_stub()
    request = SpeakerStreamRequest()
    async for raw in stub.stream_audio(request):
        samples = np.frombuffer(raw.samples, dtype=np.float32).reshape(-1, CHANNELS)
        yield SpeakerChunk(
            samples=samples,
            unix_nanos=raw.unix_nanos,
            frame_id=raw.frame_id,
        )

resoio.speaker.SpeakerChunk dataclass

SpeakerChunk(samples: NDArray[float32], unix_nanos: int, frame_id: int)

One decoded audio chunk from the speaker stream.

samples is an (N, 2) float32 view over the protobuf payload bytes (read-only; call .copy() for a writable array). Channels are interleaved L,R in the wire bytes and reshaped to columns [L, R]. unix_nanos is the bridge tap timestamp in UTC nanos since the Unix epoch. frame_id is a server-side monotonic counter that restarts at 0 per stream() call.

resoio.speaker.SAMPLE_RATE module-attribute

SAMPLE_RATE: Final[int] = 48000

resoio.speaker.CHANNELS module-attribute

CHANNELS: Final[int] = 2

resoio.speaker.DTYPE module-attribute

DTYPE: Final[dtype[float32]] = dtype(float32)