Text to Speech - Streaming

Stream real-time audio generation with our SDKs. This guide covers how to implement streaming text-to-speech synthesis for immediate audio playback and low-latency applications.

Prerequisites

Before you begin, make sure you have:

  • An aiOla API key (get one here)
  • Python 3.10+ (for Python SDK) or Node.js 18+ (for TypeScript SDK)

Step 1: Set up authentication

First, generate an access token and create your client:

For comprehensive authentication details, security considerations, and token management strategies, see our Authentication Guide.

from aiola import AiolaClient
# Generate access token
result = AiolaClient.grant_token(api_key='your-api-key')
access_token = result.access_token
# Create client
client = AiolaClient(access_token=access_token)

Step 2: Basic streaming synthesis

Here’s how to stream audio generation for immediate processing:

# Stream audio generation
text = "This is streaming text to speech synthesis."
stream = client.tts.stream(
text=text,
voice='tara',
language='en'
)
# Collect audio chunks as they arrive
audio_chunks = []
for chunk in stream:
audio_chunks.append(chunk)
# Process chunk in real-time if needed
print(f"Received chunk of {len(chunk)} bytes")
print("Streaming synthesis completed!")

Step 3: Async streaming (Python)

For asynchronous streaming operations:

Python
from aiola import AsyncAiolaClient
import asyncio
async def async_streaming_example():
# Generate access token
result = await AsyncAiolaClient.grant_token(api_key='your-api-key')
access_token = result.access_token
# Create async client
async_client = AsyncAiolaClient(access_token=access_token)
text = "This demonstrates async streaming synthesis."
stream = async_client.tts.stream(
text=text,
voice='tara',
language='en'
)
# Process chunks asynchronously
audio_chunks = []
async for chunk in stream:
audio_chunks.append(chunk)
print(f"Received chunk: {len(chunk)} bytes")
print("Async streaming completed!")
if __name__ == "__main__":
# Run the async function
asyncio.run(async_streaming_example())

Best practices

  1. Chunk Processing: Process chunks immediately for lower latency
  2. Buffer Management: Implement proper audio buffering for smooth playback
  3. Error Recovery: Handle network issues and retry failed streams
  4. Memory Usage: Process chunks incrementally to avoid memory buildup
  5. Audio Quality: Use appropriate sample rates and formats for your use case

Next steps

Now that you’ve implemented streaming text-to-speech synthesis, you can: