Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Edge Tts Laravel Package

bestmomo/laravel-edge-tts

Laravel package integrating Microsoft Edge Text-to-Speech with streaming output, optional MP3 caching via Laravel storage, configurable default voice, and route security via middleware (auth/throttle). Provides a contract and facade, built on edge-tts-php.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bestmomo/laravel-edge-tts
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Bestmomo\EdgeTTS\EdgeTTSPackageServiceProvider" --tag="config"
    
  2. Basic Usage Convert text to speech and save as an MP3 file:

    use Bestmomo\EdgeTTS\EdgeTTS;
    
    $edgeTTS = new EdgeTTS();
    $edgeTTS->text('Hello, this is a test.')
            ->voice('en-US-AriaNeural') // Optional: Specify voice
            ->save('output.mp3');
    
  3. First Use Case Generate a welcome message for a user:

    $edgeTTS = new EdgeTTS();
    $edgeTTS->text("Welcome, {$user->name}! Your account is ready.")
            ->save(storage_path("app/audio/welcome_{$user->id}.mp3"));
    

Implementation Patterns

Common Workflows

  1. Dynamic Audio Generation Use in real-time for notifications or personalized messages:

    $edgeTTS = new EdgeTTS();
    $edgeTTS->text("Your order #{$order->id} is shipping today.")
            ->voice('en-US-JennyNeural')
            ->save(storage_path("app/audio/orders/{$order->id}.mp3"));
    
  2. Streaming Audio Stream audio directly to a user (e.g., in a Laravel Livewire component):

    return response()->stream(function () use ($edgeTTS) {
        $edgeTTS->text('Streaming audio example.')
                ->stream();
    }, 200, [
        'Content-Type' => 'audio/mpeg',
    ]);
    
  3. Batch Processing Generate multiple audio files in a queue job:

    class GenerateAudioJob implements ShouldQueue
    {
        public function handle()
        {
            $users = User::where('needs_audio', true)->get();
            foreach ($users as $user) {
                $edgeTTS = new EdgeTTS();
                $edgeTTS->text("Hello, {$user->name}!")
                        ->save(storage_path("app/audio/{$user->id}.mp3"));
            }
        }
    }
    
  4. Integration with Laravel Notifications Attach audio to email notifications:

    use Illuminate\Notifications\Messages\MailMessage;
    
    $message = new MailMessage();
    $message->line('Check out this audio message!');
    $message->attach(storage_path('app/audio/welcome.mp3'), [
        'mime' => 'audio/mpeg',
    ]);
    

Configuration Tips

  • Customize voices, rate, and pitch in config/edge-tts.php:
    'default' => [
        'voice' => 'en-US-AriaNeural',
        'rate' => 0.8, // 0.5 to 2.0
        'pitch' => 0,  // -24 to 24
    ],
    

Gotchas and Tips

Pitfalls

  1. Rate Limits The service may throttle requests if too many are made in a short time. Implement retries with exponential backoff:

    try {
        $edgeTTS->text('Test')->save('output.mp3');
    } catch (\Bestmomo\EdgeTTS\Exceptions\RateLimitException $e) {
        sleep(5); // Wait before retrying
        retry();
    }
    
  2. Voice Availability Not all voices are available for all languages. Check the Edge TTS documentation for supported voices.

  3. File Permissions Ensure the storage directory is writable:

    chmod -R 775 storage/app/audio
    
  4. Network Dependencies The package relies on an external service. Handle failures gracefully:

    try {
        $edgeTTS->text('Fallback text')->save('output.mp3');
    } catch (\Exception $e) {
        Log::error("Edge TTS failed: " . $e->getMessage());
        // Fallback to a pre-recorded audio file
    }
    

Debugging Tips

  • Enable verbose logging in config/edge-tts.php:
    'debug' => env('EDGE_TTS_DEBUG', false),
    
  • Check logs for HTTP errors or timeouts.

Extension Points

  1. Custom Voices Extend the Voice class to support additional voices:

    namespace App\Extensions;
    
    use Bestmomo\EdgeTTS\Voice;
    
    class CustomVoice extends Voice
    {
        public function __construct()
        {
            $this->voices['custom-voice'] = 'Custom Voice Name';
        }
    }
    
  2. Middleware for Authentication If the service requires authentication in the future, create middleware:

    $edgeTTS->withHeaders([
        'Authorization' => 'Bearer ' . config('edge-tts.token'),
    ]);
    
  3. Caching Responses Cache generated audio files to avoid reprocessing:

    $path = storage_path("app/audio/{$user->id}.mp3");
    if (!file_exists($path)) {
        $edgeTTS->text("Welcome, {$user->name}!")->save($path);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours