how does livekit work with deepgram to do tts? ho...
# ask-ai
j
how does livekit work with deepgram to do tts? how does it decide when to break out a piece of text into a a speech chunk. I'm seeing some challenges in how the chunking creates a bit of abrupt speed/tonality changes at the chunk junctures.
t
Thanks for reaching out, Albert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just wanted to share how LiveKit works with Deepgram for text-to-speech (TTS) synthesis through the LiveKit Agents framework. Basically, your text gets sent to Deepgram’s TTS API, and you receive the synthesized audio in real time via WebSocket streaming. For streaming synthesis, the text is broken down into chunks before being sent to the
SynthesizeStream
method. The
word_tokenizer
parameter helps split the text into individual words or chunks you define. Each chunk is sent as a JSON message to Deepgram, and a
Flush
message makes sure the audio is fully synthesized before moving on to the next piece. If you notice any sudden changes in speed or tone at the chunk boundaries, it might be related to how the text is chunked or tokenized. Tweaking the chunk size or adding prompts to guide speech features like filler words or natural pauses can really help smooth things out. Feel free to reach out if you want more details or a hand with the setup—I’m here to help! Sources: examples/other/text-to-speech/elevenlabs_tts.py | Deepgram TTS integration guide | LiveKit Docs | Deepgram TTS integration guide | LiveKit Docs | TTS | LiveKit Agents
j
how do i tweak the chunk size. is it through word_tokenizer?
and how do i do this? adding prompts to guide speech features like filler words or natural pauses can really help smooth things out.
t
If you want to add prompts that guide speech features like filler words or natural pauses in LiveKit's Deepgram TTS integration, you can simply include those right in your input text. For example:
Copy code
await session.say("One moment please... um, let me check that for you.")
Deepgram's TTS models are pretty great at picking up informal speech patterns naturally, so adding filler words like "um" or "uh," or punctuation like ellipses "..." or commas, helps make the speech sound smoother and more natural. If you're looking for more control, Deepgram also supports text-based prompts that let you influence things like vocal expressiveness, pauses, and intonation. You can check out their Text-to-Speech Prompting Guide for all the details on how to use these features. Feel free to reach out if you want a hand setting this up or have any other questions! Sources: Deepgram TTS integration guide | LiveKit Docs | Deepgram TTS integration guide | LiveKit Docs | Deepgram integration guide | LiveKit Docs
j
how do i tweak the chunk size. is it through word_tokenizer?
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!
j
To create larger chunks for Deepgram TTS, you have several options to modify how the
WordTokenizer
groups text before sending it to the TTS engine. Option 1: Use SentenceTokenizer Instead The most effective approach is to replace the default
WordTokenizer
with a
SentenceTokenizer
, which will create much larger chunks based on sentences rather than individual words: basic.py:34-48
Copy code
from livekit.agents import tokenize  
from livekit.plugins import deepgram  
  
# Use sentence-based chunking instead of word-based  
sentence_tokenizer = tokenize.basic.SentenceTokenizer(min_sentence_len=50)  
tts = deepgram.TTS(word_tokenizer=sentence_tokenizer)
Option 2: Modify WordTokenizer Settings You can adjust the
WordTokenizer
parameters to create larger word groups: basic.py:72-76
Copy code
# Create larger word chunks by ignoring punctuation  
custom_tokenizer = tokenize.basic.WordTokenizer(  
    ignore_punctuation=True,  # This prevents breaking on punctuation  
    split_character=False     # This prevents character-level splitting  
)  
tts = deepgram.TTS(word_tokenizer=custom_tokenizer)
Option 3: Custom Chunking Implementation For maximum control, you could implement a custom tokenizer that groups multiple sentences or paragraphs together, similar to how the RAG example handles text chunking: rag_db_builder.py:112-131 The key insight is that in the Deepgram TTS streaming implementation, each
WordStream
object becomes a separate speech segment: tts.py:231-244 By using a
SentenceTokenizer
or modifying the
WordTokenizer
settings, you'll reduce the number of separate segments sent to Deepgram, which should minimize the abrupt speed/tonality changes you're experiencing at chunk boundaries. Notes The default Deepgram TTS configuration uses
WordTokenizer(ignore_punctuation=False)
, which creates very small chunks (individual words). tts.py:81-82 Switching to sentence-based chunking or adjusting punctuation handling will create significantly larger, more natural-sounding speech segments.
Key Questions for LiveKit 1. Custom tts_node vs SentenceTokenizer Current: We override tts_node with sentence-based text processing Alternative: Use SentenceTokenizer in TTS configuration Question: Should we use both, either/or, or is one approach redundant? 2. Processing Pipeline Interaction Current setup: # Custom sentence chunking in tts_node async def process_with_sentence_chunking(): # Break at sentence boundaries only sentence_pattern = r'([.!?][""]?\s+)' # Default Deepgram TTS (uses WordTokenizer) tts=deepgram.TTS(model=voice_model) # No tokenizer specified Question: Does our sentence chunking get overridden by Deepgram's internal WordTokenizer? 3. Optimal Configuration Which approach provides smoother TTS: - Custom tts_node processing + SentenceTokenizer - Custom tts_node processing only - SentenceTokenizer only (remove custom processing) - Different combination? 4. Source Code Analysis Request Based on Deepgram TTS plugin implementation: - How does word_tokenizer interact with custom tts_node processing? - Does SentenceTokenizer eliminate need for custom chunking? - Performance and audio quality implications of each approach? Current Problem Context - Abrupt speed/tonality changes at chunk boundaries - Custom sentence-based text processing implemented - Need guidance on optimal chunking strategy for smooth TTS