audio_context_manager

Audio contexts a TTS service plays one after another.

class pipecat.utils.context.audio_context_manager.AudioContextFrame(context_id: str)[source]

Bases: Frame

Marks where an audio context plays among the queued frames.

Parameters:

context_id – The audio context to play.

class pipecat.utils.context.audio_context_manager.AudioContextManager[source]

Bases: object

Audio contexts and frames, played in the order they are queued.

Each audio context has its own queue of items (audio, other frames, word timestamps), so a context’s audio can arrive while an earlier one is still playing. next_for_playback() returns the next context or frame to play, and get() returns a context’s items until the context ends.

playing_context_id is the context next_for_playback() returned last. The caller sets it back to None when that context stops playing.

__init__()[source]

Initialize the audio context manager.

property context_ids: list[str]

The audio contexts that haven’t finished playing.

available(context_id: str) → bool[source]

Whether an audio context exists and hasn’t finished playing.

Parameters:

context_id – The audio context to check.

async create(context_id: str, *, interruptible: bool = True)[source]

Create an audio context and queue it to play after what’s already queued.

Parameters:
  • context_id – The audio context to create.

  • interruptible – Whether reset() drops the context while it waits to play.

async append(context_id: str, item: Any)[source]

Append an item to an audio context. None ends the context.

Audio is trimmed to whole samples, and a split sample is carried to the context’s next audio frame. Providers may cut their PCM stream at any byte, but consumers read audio frame by frame.

Parameters:
  • context_id – The audio context to append to. It must exist.

  • item – The item to append, or None to end the context.

keepalive(context_id: str)[source]

Restart the wait of get() for an audio context, if it exists.

Parameters:

context_id – The audio context still in use.

async put_frame(frame: Frame)[source]

Queue a frame to play after what’s already queued.

Parameters:

frame – The frame to queue.

async end_playback()[source]

End playback once everything already queued has played.

async next_for_playback() → Frame | None[source]

Wait for the next audio context or frame to play.

Contexts removed before they play are skipped.

Returns:

An AudioContextFrame, whose context becomes the playing one, another queued frame, or None when playback ends.

async get(context_id: str, timeout: float) → Any[source]

Return an audio context’s next item, waiting up to timeout for it.

A keepalive restarts the wait and is not returned.

Parameters:
  • context_id – The audio context to read.

  • timeout – Seconds to wait for an item.

Returns:

The next item, or None when the context ends.

Raises:

TimeoutError – If no item arrives in time.

remove(context_id: str)[source]

Remove an audio context and its queued items.

Parameters:

context_id – The audio context to remove.

reset()[source]

Drop the frames and contexts waiting to play, except uninterruptible ones.

The dropped contexts stay available until they are removed.