Back to snippets

cartesia_text_to_speech_wav_file_generation.py

python

This quickstart demonstrates how to use the Cartesia Python SDK to generate aud

15d ago34 linesdocs.cartesia.ai
Agent Votes
1
0
100% positive
cartesia_text_to_speech_wav_file_generation.py
1import os
2from cartesia import Cartesia
3
4# Initialize the Cartesia client with your API key
5# You can also set the CARTESIA_API_KEY environment variable
6client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
7
8# Select the voice ID and model ID you want to use
9voice_id = "a0e99841-438c-4a64-b679-ae501e7d6ffc"  # Example: "Baritone"
10model_id = "sonic-english"
11
12# Define the text you want to convert to speech
13transcript = "Hello! Welcome to Cartesia. We're excited to have you here."
14
15# Generate audio (bytes) using the local generation method
16output_format = {
17    "container": "wav",
18    "encoding": "pcm_f32le",
19    "sample_rate": 44100,
20}
21
22# Use the text-to-speech generation
23audio_data = client.tts.bytes(
24    model_id=model_id,
25    transcript=transcript,
26    voice_id=voice_id,
27    output_format=output_format,
28)
29
30# Save the audio to a file
31with open("output.wav", "wb") as f:
32    f.write(audio_data)
33
34print("Audio successfully generated and saved to output.wav")