Skip to content

API Reference parrot.integrations.liveavatar.avatar_ws

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.integrations.liveavatar.avatar_ws

View source

Avatar audio bridge — WebSocket PCM push (FEAT-242 Phase A — Module 2).

Ports the LiveAvatar starter avatar_ws.py (websockets library) to aiohttp per project standards.

Responsibilities:

  • Wait for session.state_updated == "connected" before sending any frames.
  • Emit agent.speak / agent.speak_end / agent.interrupt protocol frames.
  • Send PCM as base64 INSIDE the agent.speak JSON message (the LITE-mode protocol carries audio in-band, not as raw binary WS frames): {"type": "agent.speak", "audio": "<base64 PCM 16-bit 24 kHz>"} PCM (already 24 kHz mono 16-bit from Supertonic) is sliced into chunks:
    • First chunk: ≈ 400 ms (~19 200 bytes)
    • Subsequent: ≈ 1 s (~48 000 bytes)
    • Hard cap: 1 MB per packet
  • Reconnect on WS disconnect. There is NO in-band auth handshake — the ws_url returned by /v1/sessions/start is already authenticated.
  • Input PCM is already 24 kHz mono 16-bit — no resampling is done.

Protocol reference: https://docs.liveavatar.com/docs/lite-mode/events

PCM size constants (from supertonic_backend.py, verified): _SAMPLE_RATE = 24000 _CHANNELS = 1 (mono) _SAMPLE_WIDTH = 2 (16-bit / 2 bytes per sample) => 1 s = 24000 * 1 * 2 = 48 000 bytes => 400 ms ≈ 9 600 samples * 2 bytes = 19 200 bytes

Class AvatarSendTimeout

A vendor send exceeded its per-send deadline.

Distinct from a generic :class:RuntimeError so the broadcast session can treat "the avatar sink is stalling" as a fallback trigger (spec §2 runtime fallback triggers: "send timeout") rather than as an unknown error.

Alias AvatarSessionHandle

Re-exported from parrot.integrations.liveavatar.models.AvatarSessionHandle.

Class AvatarWebSocket

WebSocket bridge that pushes PCM audio frames to the LiveAvatar media server.

Emits the agent.speak, agent.speak_end, and agent.interrupt protocol frames required by the LiveAvatar LITE mode.

No resampling is applied: input PCM is assumed to be 24 kHz mono 16-bit, which is exactly what Supertonic produces.

Usage::

async with AvatarWebSocket(handle) as ws:
    await ws.start_speaking()
    await ws.send_audio_frame(pcm_bytes)
    await ws.finish_speaking()
Parameter Type Default Description
handle AvatarSessionHandle The active :class:AvatarSessionHandle providing the WS URL and session token.
session Optional[aiohttp.ClientSession] None Optional external aiohttp.ClientSession. When None the class creates and owns one.
assume_connected bool False When True the connected gate is opened as soon as the WS handshake completes, instead of waiting for a fresh session.state_updated == "connected" event. Use this when attaching to an already-started, already-connected session (the per-turn :class:AvatarTurnSpeaker reuse path): the server only emits the connected state once, at the moment the session first becomes connected — a WS that attaches later never sees a re-emitted event, so waiting for it would always time out. The one-shot orchestrator, which opens its WS at the exact connect transition, leaves this False and genuinely waits for the event.
on_event Optional[EventCallback] None Called with every parsed server message. This is the only observation point for LITE lifecycle events until the live gate (TASK-2950) confirms their names, so messages are forwarded whole and unfiltered rather than being mapped to invented constants.
on_close Optional[CloseCallback] None Called once with a short reason string when the transport closes permanently. Only meaningful with auto_reconnect=False.
auto_reconnect bool True Keep the legacy silent-reconnect behaviour (default). A broadcast sets this False: an avatar control socket that drops is a fallback trigger, and silently reconnecting would hide it and strand the audience in a half-dead avatar mode.
send_timeout_s Optional[float] None Per-send deadline. Exceeding it raises :class:AvatarSendTimeout (spec §2 default for broadcast: 2 s).
aggregate bool False Buffer PCM across send_audio_frame calls so small Nova chunks become ~1 s vendor frames instead of a flood of tiny agent.speak messages.

Attributes:

Attribute Type Description
speaking_state Optional[str]
closed asyncio.Event
pending_bytes int Bytes buffered but not yet submitted to the vendor. Only these are safe to re-route on a fallback cutover: anything already submitted may or may not have been played, and replaying it would duplicate speech (spec §2).

Method AvatarWebSocket.start_speaking

Block until the avatar media server is ready to receive audio.

The LITE-mode protocol has no separate "begin speaking" frame — audio is carried per-chunk inside agent.speak (see :meth:send_audio_frame). This method only awaits the session.state_updated == "connected" gate so callers can fail fast if the server never becomes ready.

start_speaking() -> None

Raises:

  • RuntimeError — If the connected gate is not opened within :data:_CONNECT_TIMEOUT.

Method AvatarWebSocket.send_audio_frame

Push PCM audio to the avatar as base64 agent.speak messages.

Slices pcm into appropriately-sized chunks and sends each as a {"type": "agent.speak", "audio": "<base64>"} JSON frame (LITE-mode carries audio in-band, not as raw binary WS frames):

  • First slice: ≈ 400 ms (~19 200 bytes).
  • Subsequent slices: ≈ 1 s (~48 000 bytes).
  • Hard cap: 1 MB per packet.

No resampling — input must be 24 kHz mono 16-bit.

send_audio_frame(pcm: bytes) -> None

Parameter Type Default Description
pcm bytes Raw PCM bytes (int16 LE mono 24 kHz).

Raises:

  • RuntimeError — If the WebSocket is not open.

Method AvatarWebSocket.finish_speaking

Send the agent.speak_end frame to flush the playback buffer.

Carries a fresh event_id which the server echoes back in its agent.speak_ended response.

finish_speaking() -> None

Raises:

  • RuntimeError — If the WebSocket connection is not open.

Method AvatarWebSocket.interrupt

Send the agent.interrupt frame to clear scheduled audio.

interrupt() -> None

Raises:

  • RuntimeError — If the WebSocket connection is not open.

Attribute CloseCallback

Attribute EventCallback

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally