Implementation:Elevenlabs Elevenlabs python Audio Output Utilities
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Audio_Processing, Media_Output |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tools for playing, saving, and streaming audio output provided by the elevenlabs-python SDK.
Description
The play.py module provides three standalone functions for consuming audio output from TTS generation:
- play(): Buffers audio and plays through ffplay (default), sounddevice, or IPython Audio
- save(): Writes audio bytes to a file
- stream(): Pipes audio chunks to mpv for progressive playback and returns the full audio
All three functions accept both bytes (complete audio) and Iterator[bytes] (streaming audio) as input.
Usage
Import these functions directly from the elevenlabs package. Use play for quick playback during development, save for production file generation, and stream when you need real-time playback of streaming TTS output.
Code Reference
Source Location
- Repository: elevenlabs-python
- File: src/elevenlabs/play.py
- Lines: L13-97
Signature
def play(
audio: Union[bytes, Iterator[bytes]],
notebook: bool = False,
use_ffmpeg: bool = True,
) -> None:
"""Play audio through system speakers.
Args:
audio: Audio data or streaming audio chunks.
notebook: Use IPython Audio display (for Jupyter).
use_ffmpeg: Use ffplay for playback (default True).
If False, uses sounddevice/soundfile.
"""
def save(
audio: Union[bytes, Iterator[bytes]],
filename: str,
) -> None:
"""Save audio to a file.
Args:
audio: Audio data or streaming audio chunks.
filename: Output file path.
"""
def stream(
audio_stream: Iterator[bytes],
) -> bytes:
"""Stream audio through mpv and return the full audio.
Args:
audio_stream: Streaming audio byte chunks.
Returns:
bytes: Complete concatenated audio data.
"""
Import
from elevenlabs import play, save, stream
I/O Contract
Inputs (play)
| Name | Type | Required | Description |
|---|---|---|---|
| audio | Union[bytes, Iterator[bytes]] | Yes | Audio data or streaming chunks |
| notebook | bool | No | Use IPython Audio display (default False) |
| use_ffmpeg | bool | No | Use ffplay for playback (default True) |
Inputs (save)
| Name | Type | Required | Description |
|---|---|---|---|
| audio | Union[bytes, Iterator[bytes]] | Yes | Audio data or streaming chunks |
| filename | str | Yes | Output file path |
Inputs (stream)
| Name | Type | Required | Description |
|---|---|---|---|
| audio_stream | Iterator[bytes] | Yes | Streaming audio byte chunks |
Outputs
| Name | Type | Description |
|---|---|---|
| play() returns | None | Plays audio through speakers |
| save() returns | None | Writes audio file to disk |
| stream() returns | bytes | Full concatenated audio after progressive playback via mpv |
Usage Examples
Play Audio
from elevenlabs import ElevenLabs, play
client = ElevenLabs()
audio = client.text_to_speech.convert(
voice_id="JBFqnCBsd6RMkjVDRZzb",
text="Hello world!",
model_id="eleven_multilingual_v2",
)
# Play using ffplay (default)
play(audio)
# Play in Jupyter notebook
play(audio, notebook=True)
# Play using sounddevice (requires pip install sounddevice soundfile)
play(audio, use_ffmpeg=False)
Save to File
from elevenlabs import ElevenLabs, save
client = ElevenLabs()
audio = client.text_to_speech.convert(
voice_id="JBFqnCBsd6RMkjVDRZzb",
text="Save this to a file.",
model_id="eleven_multilingual_v2",
output_format="mp3_44100_128",
)
save(audio, "output.mp3")
Stream with mpv
from elevenlabs import ElevenLabs, stream
client = ElevenLabs()
audio_iterator = client.text_to_speech.convert(
voice_id="JBFqnCBsd6RMkjVDRZzb",
text="This will play progressively as it generates.",
model_id="eleven_multilingual_v2",
)
# Plays in real-time via mpv and returns complete audio
full_audio = stream(audio_iterator)
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment