spoken

The expression of the words a TTS service reports speaking.

Part of expressive conversation (alpha). A TTS service that reports word timestamps tells which words it has spoken, and Pipecat’s word tracking gives each word the stretch of the LLM’s text it covers, tags included, as its raw_text. A word covers the text from the end of the word before it, so tags written between two words arrive with the second.

SpokenExpression keeps the text sent in one audio context, with the ExpressionSpan list format() returned for it. Looking up each spoken word’s text in it gives the delivery the word is spoken in and the sounds performed before it, which the TTS service reports to clients as the word is spoken.

Alpha. The tag syntax and these types are likely to change between releases.

class pipecat.expressive.spoken.SpokenExpression[source]

Bases: object

Tells the delivery and sounds of the words a TTS service reports speaking in one audio context.

Each TTSContext holds one, so it lasts as long as its audio context. Units are added as they are sent, and words are looked up as they are spoken, in order: each lookup searches from where the last one ended.

Example:

spoken = SpokenExpression()
spoken.add("<<warm>> It's [[laugh]] ^^back^^ up.", spans)

spoken.spoken("<<warm>> It's")
# (["warm"], [])
spoken.spoken("[[laugh]] ^^back")
# (["warm", "emphasis"], [(TextType.EXPRESSION_SOUND, "laugh")])
spoken.spoken("up.")
# (["warm"], [])
__init__()[source]

Initialize the spoken expression.

property delivery: list[str] | None

The delivery of the text last reported spoken, or None when no text was added.

add(text: str, spans: list[ExpressionSpan])[source]

Keep a unit sent to the service, for the words it reports later.

Units follow one another in the context’s text, and the spans are moved to where the unit falls in it.

Parameters:
  • text – The unit, as the LLM wrote it.

  • spans – What each stretch of the unit performs.

spoken(text: str | None) → tuple[list[str] | None, list[tuple[TextType, str]]][source]

Read the delivery of text the service reports speaking, and the sounds before it.

The text’s delivery is that of its first letter that falls in words, so a delivery tag’s own name, such as warm in "<<warm>> It's", isn’t taken for a word. A sound already reported by spoken_markup() isn’t reported again, and one written after the text’s last word, such as the laugh in "back [[laugh]]!", is left for the service’s markup or the next text to report, since it is performed after the word.

Parameters:

text – The LLM’s text it covers, tags included, such as a word’s raw_text, or a whole sentence’s from a service without word timestamps.

Returns:

Its delivery, or None when no text was added, and the sounds and pauses performed since the text before it, each with its text type and its text. Text that can’t be found keeps the delivery of the text before it.

unreported() → list[tuple[TextType, str]][source]

Read the sounds and pauses not yet reported, once the context’s audio has ended.

A sound after the last words, such as the laugh in "Bye [[laugh]]", has no later text to report it, and a service may not report its markup.

Returns:

Each such sound or pause, with its text type and its text.

spoken_markup() → tuple[TextType, str] | None[source]

Read markup the service reports speaking as a word, such as [laughs].

Some services include their markup in their word timestamps. It covers none of the LLM’s text, so it has no raw_text, and is matched by position instead: it stands for the next sound or pause still to be spoken, which is reported with it, at its time, as written in its tag. Markup reached before any sound, such as a delivery’s [warmly], stands for none, and isn’t spoken:

spoken.spoken("<<warm>> It's")
spoken.spoken_markup()       # "[laughs]" reported as a word
# (TextType.EXPRESSION_SOUND, "laugh")
spoken.spoken("[[laugh]] ^^back")
# (["warm", "emphasis"], []): already reported
Returns:

The sound or pause, with its text type, or None when the markup stands for none.