PHP code example of lucianotonet / cartesia-php

1. Go to this page and download the library: Download lucianotonet/cartesia-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lucianotonet / cartesia-php example snippets




use LucianoTonet\CartesiaPHP\CartesiaClient;

// Initialize the Cartesia client with your API key
$client = new CartesiaClient('your_api_key');

// Define the speech synthesis parameters
$modelId = 'sonic-english'; // Voice model ID
$transcript = 'Hello, world!'; // Text to be converted to speech
$voiceId = 'a0e99841-438c-4a64-b679-ae501e7d6091'; // Cloned voice ID

// Make a speech synthesis request
try {
    $response = $client->streamSpeechBytes([
        'context_id' => 'happy-monkeys-fly',
        'model_id' => $modelId,
        'transcript' => $transcript,
        'voice' => [
            'mode' => 'id',
            'id' => $voiceId
        ],
        'output_format' => [
            'container' => 'raw',
            'encoding' => 'pcm_f32le',
            'sample_rate' => 44100
        ]
    ]);

    // Save the generated audio to a file
    file_put_contents('hello_world.wav', $response->getBody());

    echo "Audio successfully generated at hello_world.wav\n";
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}
bash
composer 
bash
php -S 127.0.0.1:80