Skip to main content

TTS API refrence

API Reference

Now that you've seen a quick example, let's dive into the complete API documentation. This section provides detailed documentation of the TalkScriberTTSClient class and all its methods. Understanding these will help you build more complex applications.

Constructor Parameters

TalkScriberTTSClient(
host="localhost", # TTS server hostname
port=9099, # TTS server port
text="Hello, this is a test...", # Default text to speak
speaker_name="tara", # Speaker voice to use
api_key=None, # Required API key for authentication
enable_playback=True, # Enable real-time audio playback
save_audio_path=None # Optional path to save audio file
)

Core Methods

Now let's examine each method in detail. These are the primary methods you'll use in your applications: Runs a complete TTS session: connect, send text, receive audio, and play.

Returns: bool - True if successful, False otherwise

Example:

client = TalkScriberTTSClient(
api_key="your_key",
text="Hello world!",
speaker_name="tara"
)
success = client.run_simple_test()
connect()

Establishes WebSocket connection to the TTS server.

Returns: bool - True if connection successful

Example:

if client.connect():
print("Connected successfully")
else:
print("Connection failed")
disconnect()

Closes the WebSocket connection and stops audio playback.

Example:

client.disconnect()
send_speak_request(text, speaker_name="tara")

Sends a TTS request to the server.

Parameters:

  • text (str): Text to convert to speech
  • speaker_name (str): Voice to use (default: "tara")

Returns: bool - True if request sent successfully

Note: The speaker_name parameter is currently ignored - the client uses the speaker_name from the constructor.

Example:

client.send_speak_request("Hello, how are you?")  # speaker_name parameter ignored
init_audio()

Initializes audio playback system.

Returns: bool - True if audio initialized successfully

Example:

if client.init_audio():
print("Audio system ready")

Audio Configuration

The client uses several audio configuration constants that you can adjust based on your needs:

Usage Patterns

Now that you understand the API, let's explore different usage patterns. These examples show how to use the client in various scenarios, from simple one-shot operations to complex interactive applications.

from client.tts_client import TalkScriberTTSClient

# Basic usage with default settings
client = TalkScriberTTSClient(
api_key="your_api_key",
text="Hello, this is a simple test.",
speaker_name="tara"
)
success = client.run_simple_test()

2. Silent Mode (No Audio Playback)

# Useful for testing or when you only want to save audio
client = TalkScriberTTSClient(
api_key="your_api_key",
text="This will be saved but not played.",
enable_playback=False,
save_audio_path="./output/silent_audio.wav"
)
success = client.run_simple_test()

3. Audio File Saving

# Save audio to file with playback
client = TalkScriberTTSClient(
api_key="your_api_key",
text="This will be played and saved.",
enable_playback=True,
save_audio_path="./output/audio.wav"
)
success = client.run_simple_test()

Configuration Options

Understanding the configuration options is crucial for optimizing performance. This section covers all the settings you can adjust to fine-tune the client behavior.

SettingDefaultDescription
enable_playbackTrueEnable real-time audio playback
BUFFER_SIZE_CHUNKS5Number of audio chunks to buffer before playback
SAMPLE_RATE24000Audio sample rate (must match server)

Buffer Size Recommendations

Choosing the right buffer size is essential for balancing latency and audio quality. Here are our recommendations:

Error Handling

Even with the best setup, you might encounter issues. This section provides guidance on handling common errors and debugging connection problems.

try:
client = TalkScriberTTSClient(api_key="your_key")
success = client.run_simple_test()
if not success:
print("TTS operation failed")
except ValueError as e:
print(f"Configuration error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")