word_timestamp_tracker

Timestamps for the words a TTS service speaks.

class pipecat.utils.context.word_timestamp_tracker.WordTimestamp(word: str, timestamp: float, context_id: str | None, includes_inter_frame_spaces: bool = False)[source]

Bases: object

A word a TTS provider reported, and when it is spoken.

Parameters:
  • word – The word.

  • timestamp – Seconds from the start of the word’s audio.

  • context_id – The audio context the word belongs to.

  • includes_inter_frame_spaces – Whether the word carries its own spacing.

includes_inter_frame_spaces: bool = False
class pipecat.utils.context.word_timestamp_tracker.WordTimestampTracker[source]

Bases: object

Turns word times reported from the start of their audio into PTS.

Timing starts when a context’s audio starts playing, or after the last word if that is later, so words keep their order across contexts. Words reported before then are cached and handed back by start().

last_pts is the PTS of the last word frame, and uninterruptible_last_pts that of the last uninterruptible one. reply_last_pts is that of the last word since start_reply().

__init__()[source]

Initialize the word timestamp tracker.

last_pts: int
uninterruptible_last_pts: int
property started: bool

Whether words are being timed.

property reply_last_pts: int | None

The PTS of the reply’s last word, or None if the reply timed no words.

start_reply()[source]

Note where a reply’s words start, for reply_last_pts.

start(now: int) → list[WordTimestamp][source]

Start timing words, if not started yet.

Parameters:

now – The current clock time, in nanoseconds.

Returns:

The words cached until now, to time in order.

cache(word: WordTimestamp)[source]

Hold a word until timing starts.

Parameters:

word – The word reported before timing started.

pts(timestamp: float) → int[source]

Get the PTS of a word.

Parameters:

timestamp – Seconds from the start of the word’s audio.

Returns:

The word’s PTS, in nanoseconds.

reset()[source]

Stop timing words and drop the cached ones.