how can I generate audio for a given text message ...
# ask-ai
c
how can I generate audio for a given text message (but not play it) through the agentsession tts node
t
Thanks for reaching out, Nathan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to generate audio from a text message using the AgentSession TTS node but don’t want it to play automatically, you can totally use the standalone TTS feature. This lets you create audio from text and grab the audio frames without having them play in the room right away. Here’s a simple way to do it: 1. First, create a TTS instance with your favorite plugin (like Cartesia, ElevenLabs, or others). 2. Next, use the
stream()
method on that TTS instance to get a
SynthesizeStream
. This stream lets you send text in and get back the synthesized audio frames. 3. Then, push your text into the stream with
push_text()
, and when you’re done, call
end_input()
to finish up the synthesis. 4. Finally, you can grab the audio frames from the stream (they come as
SynthesizedAudio
) and choose to save them to a file or buffer instead of sending them straight to a participant. Here’s a quick example to show you what I mean:
Copy code
from livekit.agents.tts import SynthesizedAudio
from livekit.plugins import cartesia
from typing import AsyncIterable

tts = cartesia.TTS(model="sonic-english")
tts_stream = tts.stream()

# Collect audio frames without playing
audio_frames = []
async for text in ["Hello, this is a test message."]:
    tts_stream.push_text(text)
tts_stream.end_input()

async for audio_data in tts_stream:
    if isinstance(audio_data, SynthesizedAudio):
        audio_frames.append(audio_data.audio.frame)
This way, you have complete control over the audio you generate—you can process it, save it, or play it however you like, instead of having it automatically play in the room. Sources: plugins/cartesia/src/tts.ts
c
Are there other ways of doing this
I want to plug this into the session.say(...) function
t
Absolutely! There are a couple of ways you can generate audio from text without actually playing it right away: 1. Try using the
synthesize()
method. It gives you a
ChunkedStream
object with the audio data, which you can gather into a complete audio frame to use later—no playback needed. Here’s a quick example:
Copy code
audio_stream = tts.synthesize("Hello, world!")
audio_frame = await audio_stream.collect()
Once you have the
audio_frame
, you can save it or use it whenever you want, without triggering any sound. 1. You can also turn off audio output by setting
audio_enabled=False
in
RoomOutputOptions
. This way, the agent handles text-to-speech behind the scenes without playing it, but still keeps text-only output in sync. Both of these options give you the flexibility to work with the audio exactly how you need. If you want to chat more about it or need help setting it up, just let me know! Sources: examples/other/text-to-speech/README.md | Text-to-speech (TTS) integrations | LiveKit Docs | Text and transcriptions | LiveKit Docs | StreamAdapter | LiveKit Agents | TTS | LiveKit Agents
Hey there! So, the
session.say()
function in LiveKit is mainly built to create speech and play it right away for the user. It doesn’t really support just generating the audio without playing it, since it’s designed to do both at the same time. If you’re looking to generate audio without having it play immediately, no worries! You can use the standalone TTS feature instead. That means you’d create a TTS instance and use the
stream()
or
synthesize()
methods to generate the audio frames. From there, you can handle or save the audio however you like. This way, you get full control over the audio without it automatically playing. If you want, I’m here to help you set this up or answer any questions you have about it! Sources: Agent speech and audio | LiveKit Docs | Agent speech | LiveKit Docs | Text-to-speech (TTS) integrations | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
c
Give me a working example using synthesize piped into say
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!