<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L411-L666) |

On-device speech synthesis via MediaPipe CalculatorGraph.

```python
mp.tasks.audio.TextToSpeech(
    handle: ctypes.c_void_p
)
```

<br />

<br />

<br />

| ## Args ||
|---|---|
| `handle` | Opaque C pointer created by `MpTextToSpeechCreate`. |

<br />

## Methods

### `cancel`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L651-L654)

```python
cancel() -> None
```

Cancels any in-flight synthesis or streaming session.

### `close`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L656-L660)

```python
close() -> None
```

Closes the synthesizer and releases native C++ resources.

### `create_from_model_path`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L457-L473)

```python
@classmethod
create_from_model_path(
    model_path: str,
    delegate: `mp.tasks.BaseOptions.Delegate` = _BaseOptions.Delegate.CPU,
    default_voice_id: str = '',
    speaking_rate: float = 1.0
) -> 'TextToSpeech'
```

Creates a TextToSpeech instance from a `.litertlm` model path.

### `create_from_options`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L423-L455)

```python
@classmethod
create_from_options(
    options: `mp.tasks.audio.TextToSpeechOptions`
) -> 'TextToSpeech'
```

Creates a TextToSpeech instance from `TextToSpeechOptions`.

### `list_voices`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L475-L510)

```python
list_voices() -> list[VoiceInfo]
```

Lists all voices available in the loaded model bundle.

### `open_stream`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L546-L581)

```python
open_stream(
    initial_text: str = '',
    voice_id: Optional[str] = None,
    locale: Optional[str] = None,
    finish_input: bool = False
) -> 'TtsStream'
```

Opens an incremental or single-utterance streaming synthesis session.

<br />

<br />

<br />

| Args ||
|---|---|
| `initial_text` | Optional initial text fragment to enqueue immediately. |
| `voice_id` | Optional speaker voice ID override. |
| `locale` | Optional BCP-47 locale override. |
| `finish_input` | If `False`, additional tokens/words can be appended via [`TtsStream.push_text()`](https://developers.google.com/edge/api/mediapipe/python/mp/tasks/audio/TtsStream.md.txt#push_text) followed by [`TtsStream.finish_input()`](https://developers.google.com/edge/api/mediapipe/python/mp/tasks/audio/TtsStream.md.txt#finish_input). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An active `TtsStream` handle supporting `push_text()`, `finish_input()`, and iteration over `TtsAudioResult` chunks. ||

<br />

### `start_stream`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L583-L596)

```python
start_stream(
    initial_text: str = '',
    voice_id: Optional[str] = None,
    locale: Optional[str] = None,
    finish_input: bool = False
) -> 'TtsStream'
```

Deprecated alias for `open_stream`.

### `synthesize`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L512-L544)

```python
synthesize(
    text: str, voice_id: Optional[str] = None, locale: Optional[str] = None
) -> `mp.tasks.audio.TtsAudioResult`
```

Synthesizes `text` synchronously and returns mono float32 PCM audio.

<br />

<br />

<br />

| Args ||
|---|---|
| `text` | Input text to synthesize. |
| `voice_id` | Optional speaker voice ID override; if `None`, falls back to `default_voice_id` configured in `TextToSpeechOptions`. |
| `locale` | Optional BCP-47 locale override; if `None`, falls back to `default_locale` configured in `TextToSpeechOptions`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `TtsAudioResult` containing the full synthesized audio waveform. ||

<br />

### `synthesize_stream`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L598-L623)

```python
synthesize_stream(
    text: str, voice_id: Optional[str] = None, locale: Optional[str] = None
) -> Iterator[`mp.tasks.audio.TtsAudioResult`]
```

Streams synthesized audio chunk-by-chunk.

<br />

<br />

<br />

| Args ||
|---|---|
| `text` | Input text to synthesize into clause-level audio chunks. |
| `voice_id` | Optional speaker voice ID override; if `None`, falls back to `default_voice_id` configured in `TextToSpeechOptions`. |
| `locale` | Optional BCP-47 locale override; if `None`, falls back to `default_locale` configured in `TextToSpeechOptions`. |

<br />

<br />

<br />

<br />

| Yields ||
|---|---|
| `TtsAudioResult` chunks as each clause finishes acoustic + vocoder synthesis. ||

<br />

### `__enter__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L662-L663)

```python
__enter__() -> 'TextToSpeech'
```

### `__exit__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/audio/text_to_speech.py#L665-L666)

```python
__exit__(
    exc_type, exc_val, exc_tb
) -> None
```