| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Sample script demonstrating how to use the Resemble AI create_direct method. |
| 4 | |
| 5 | The create_direct method allows you to synthesize speech directly - this is useful for quick synthesis tasks. |
| 6 | |
| 7 | Requirements: |
| 8 | - Resemble AI API key |
| 9 | - Project UUID |
| 10 | - Voice UUID |
| 11 | """ |
| 12 | |
| 13 | from resemble import Resemble |
| 14 | |
| 15 | def main(): |
| 16 | # Set your API key - get this from https://app.resemble.ai/account/api |
| 17 | API_KEY = "YOUR_API_KEY" |
| 18 | |
| 19 | # Set your project and voice UUIDs |
| 20 | PROJECT_UUID = "YOUR_PROJECT_UUID" |
| 21 | VOICE_UUID = "YOUR_VOICE_UUID" |
| 22 | |
| 23 | # Initialize the Resemble client with your API key |
| 24 | Resemble.api_key(API_KEY) |
| 25 | |
| 26 | # Text to synthesize |
| 27 | data = "Hello, this is a sample text being synthesized using the create_direct method." |
| 28 | |
| 29 | #Alternatively, you can pass in SSML to the data parameter. |
| 30 | #data = "<speak prompt=\"Speak in a British accent like you are from the heart of London, England.\">Hello, this is a sample text being synthesized using the create_direct endpoint.</speak>" |
| 31 | |
| 32 | try: |
| 33 | # Call the create_direct endpoint |
| 34 | response = Resemble.v2.clips.create_direct( |
| 35 | project_uuid=PROJECT_UUID, # Required: project UUID |
| 36 | voice_uuid=VOICE_UUID, # Required: voice UUID |
| 37 | data=data, # Required: text to synthesize |
| 38 | title="Direct Syn test", # Optional: title for the synthesis |
| 39 | precision="PCM_16", # Optional: audio precision |
| 40 | output_format="wav", # Optional: output format |
| 41 | sample_rate=48000 # Optional: sample rate in Hz |
| 42 | ) |
| 43 | |
| 44 | # - success: boolean indicating if the request was successful |
| 45 | if response.get('success'): |
| 46 | print("Synthesis successful!") |
| 47 | print(f"Response: {response}") |
| 48 | audio_content = response.get('audio_content') # Base64 encoded audio content. You can decode it to get the audio data. |
| 49 | else: |
| 50 | print("Synthesis failed") |
| 51 | print(f"Error: {response}") |
| 52 | except Exception as e: |
| 53 | print(f"An error occurred: {e}") |
| 54 | |
| 55 | if __name__ == "__main__": |
| 56 | main() |