How can you synthesize audio using the AI SDK in Laravel 13?

Audio synthesis in Laravel 13 is handled through the LaravelAiAudio class. The process is straightforward: provide your text, optionally customize the voice or style, and call the generate() method.

Basic Audio Synthesis

The simplest way to convert text to audio is:

use LaravelAiAudio;

$audio = Audio::of( I love coding with Laravel. )->generate();

// Access the raw audio content as a string
$rawContent = (string) $audio;

Customizing Voice and Style

You can control the generated audio using several helper methods:

Select Gender:

$audio = Audio::of( Hello from Laravel. )
   ->female() // Or ->male()
   ->generate();

Choose a Specific Voice:

$audio = Audio::of( Hello from Laravel. )
   ->voice( voice-id-or-name )
   ->generate();

Provide Instructions (for tone/style):

$audio = Audio::of( Hello from Laravel. )
   ->instructions( Said like a pirate )
   ->generate();

Storing the Audio

You can easily save the generated audio to your application is storage. The store() method saves the file to the default disk configured in config/filesystems.php:

$path = $audio->store();

Supported Providers

The AI SDK supports Text-to-Speech (TTS) across multiple providers. You can set your default provider in config/ai.php. Supported TTS providers include OpenAI, ElevenLabs, Gemini, Mistral, and OpenRouter . If you need to use a specific provider for a request, you can pass it to the generate() method.

Queue Long-Running Tasks

Audio generation can take time. It is recommended to queue the task so your HTTP request isn not blocked. You can dispatch the generation to a background job:

$audio = Audio::of( Long text to synthesize... )
   ->generateQueued();

If you need to perform actions after the audio is generated (like storing it or notifying a user), you can chain a callback:

$audio = Audio::of( Long text... )
   ->generateQueued()
   ->then(function ($audio) {
       // Handle the generated audio
   });

19 Views