Module livekit.plugins.playai

Classes

class TTS (*,
api_key: str | None = None,
user_id: str | None = None,
voice: str = 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',
language: str = 'english',
sample_rate: int = 24000,
model: TTSModel | str = 'Play3.0-mini',
word_tokenizer: tokenize.WordTokenizer = <livekit.agents.tokenize.basic.WordTokenizer object>,
**kwargs)
Expand source code
class TTS(tts.TTS):
    def __init__(
        self,
        *,
        api_key: str | None = None,
        user_id: str | None = None,
        voice: str = "s3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json",
        language: str = "english",
        sample_rate: int = 24000,
        model: TTSModel | str = "Play3.0-mini",
        word_tokenizer: tokenize.WordTokenizer = tokenize.basic.WordTokenizer(
            ignore_punctuation=False
        ),
        **kwargs,
    ) -> None:
        """
        Initialize the PlayAI TTS engine.

        Args:
            api_key (str): PlayAI API key.
            user_id (str): PlayAI user ID.
            voice (str): Voice manifest URL.
            model (TTSModel): TTS model, defaults to "Play3.0-mini".
            language (str): language, defaults to "english".
            sample_rate (int): sample rate (Hz), A number greater than or equal to 8000, and must be less than or equal to 48000
            word_tokenizer (tokenize.WordTokenizer): Tokenizer for processing text. Defaults to basic WordTokenizer.
            **kwargs: Additional options.
        """

        super().__init__(
            capabilities=tts.TTSCapabilities(
                streaming=True,
            ),
            sample_rate=sample_rate,
            num_channels=1,
        )

        api_key = api_key or os.environ.get("PLAYHT_API_KEY")
        user_id = user_id or os.environ.get("PLAYHT_USER_ID")

        if not api_key or not user_id:
            raise ValueError(
                "PlayHT API key and user ID are required. Set environment variables PLAYHT_API_KEY and PLAYHT_USER_ID or pass them explicitly."
            )
        _validate_kwargs(kwargs)
        self._config = TTSOptions(
            voice=voice,
            format=Format.FORMAT_OGG,  # Using OGG format for AudioDecoder
            sample_rate=sample_rate,
            language=Language(language),
            **kwargs,
        )

        self._opts = _Options(
            model=model,
            tts_options=self._config,
            word_tokenizer=word_tokenizer,
        )

        self._client = PlayHTAsyncClient(
            user_id=user_id,
            api_key=api_key,
        )

        self._streams = weakref.WeakSet[SynthesizeStream]()

    def update_options(
        self,
        *,
        voice: str | None = None,
        model: TTSModel | str | None = None,
        language: str | None = None,
        **kwargs,
    ) -> None:
        """
        Update the TTS options.
        """
        updates = {}
        if voice is not None:
            updates["voice"] = voice
        if language is not None:
            updates["language"] = Language(language)
        updates.update(kwargs)

        _validate_kwargs(updates)

        for key, value in updates.items():
            if value is not None:
                setattr(self._config, key, value)

        if model is not None:
            self._opts.model = model

    def synthesize(
        self,
        text: str,
        *,
        conn_options: Optional[APIConnectOptions] = None,
    ) -> "ChunkedStream":
        return ChunkedStream(
            tts=self,
            input_text=text,
            conn_options=conn_options,
            opts=self._opts,
        )

    def stream(
        self, *, conn_options: Optional[APIConnectOptions] = None
    ) -> "SynthesizeStream":
        stream = SynthesizeStream(
            tts=self,
            conn_options=conn_options,
            opts=self._opts,
        )
        self._streams.add(stream)
        return stream

Helper class that provides a standard way to create an ABC using inheritance.

Initialize the PlayAI TTS engine.

Args

api_key : str
PlayAI API key.
user_id : str
PlayAI user ID.
voice : str
Voice manifest URL.
model : TTSModel
TTS model, defaults to "Play3.0-mini".
language : str
language, defaults to "english".
sample_rate : int
sample rate (Hz), A number greater than or equal to 8000, and must be less than or equal to 48000
word_tokenizer : tokenize.WordTokenizer
Tokenizer for processing text. Defaults to basic WordTokenizer.
**kwargs
Additional options.

Ancestors

Methods

def stream(self, *, conn_options: Optional[APIConnectOptions] = None) ‑> livekit.plugins.playai.tts.SynthesizeStream
Expand source code
def stream(
    self, *, conn_options: Optional[APIConnectOptions] = None
) -> "SynthesizeStream":
    stream = SynthesizeStream(
        tts=self,
        conn_options=conn_options,
        opts=self._opts,
    )
    self._streams.add(stream)
    return stream
def synthesize(self, text: str, *, conn_options: Optional[APIConnectOptions] = None) ‑> livekit.plugins.playai.tts.ChunkedStream
Expand source code
def synthesize(
    self,
    text: str,
    *,
    conn_options: Optional[APIConnectOptions] = None,
) -> "ChunkedStream":
    return ChunkedStream(
        tts=self,
        input_text=text,
        conn_options=conn_options,
        opts=self._opts,
    )
def update_options(self,
*,
voice: str | None = None,
model: TTSModel | str | None = None,
language: str | None = None,
**kwargs) ‑> None
Expand source code
def update_options(
    self,
    *,
    voice: str | None = None,
    model: TTSModel | str | None = None,
    language: str | None = None,
    **kwargs,
) -> None:
    """
    Update the TTS options.
    """
    updates = {}
    if voice is not None:
        updates["voice"] = voice
    if language is not None:
        updates["language"] = Language(language)
    updates.update(kwargs)

    _validate_kwargs(updates)

    for key, value in updates.items():
        if value is not None:
            setattr(self._config, key, value)

    if model is not None:
        self._opts.model = model

Update the TTS options.

Inherited members