pascalkleindienst/laravel-text-to-speech

This is my package laravel-text-to-speech

v0.3.0 2025-06-01 12:02 UTC

This package is auto-updated.

Last update: 2025-06-01 12:03:29 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A simple and elegant Laravel package that converts text to speech using various TTS engines. Easily integrate voice synthesis into your Laravel applications with support for multiple languages, voices, and audio formats.

Features

  • 🎤 Multiple TTS engine support
  • 🌍 Multi-language compatibility
  • 🔊 Customizable voice options
  • ⚡️ Simple, fluent API
  • 🚀 Easy Laravel integration

Perfect for creating accessible applications, voice notifications, audiobooks, or any project requiring speech synthesis.

📖️ Installation

You can install the package via composer:

composer require pascalkleindienst/laravel-text-to-speech

You can publish the config file with:

php artisan vendor:publish --tag="laravel-text-to-speech-config"

This is the contents of the published config file:

return [
    'driver' => env('TEXT_TO_SPEECH_DRIVER'),
    'language' => env('TEXT_TO_SPEECH_LANGUAGE'),

    'audio' => [
        'path' => env('TEXT_TO_SPEECH_AUDIO_PATH', 'audio'),
        'format' => env('TEXT_TO_SPEECH_AUDIO_FORMAT', 'mp3'),
        'disk' => env('TEXT_TO_SPEECH_AUDIO_DISK', 'local'),
    ],

    'google' => [
        'private_key' => env('TEXT_TO_SPEECH_GOOGLE_PRIVATE_KEY'),
        'type' => env('TEXT_TO_SPEECH_GOOGLE_TYPE', 'service_account'),
        'client_email' => env('TEXT_TO_SPEECH_GOOGLE_CLIENT_EMAIL'),
        'voice' => env('TEXT_TO_SPEECH_GOOGLE_VOICE', 'de-DE-Chirp3-HD-Achernar'),
    ],
    
    'polly' => [
        'key' => env('TEXT_TO_SPEECH_POLLY_KEY'),
        'secret' => env('TEXT_TO_SPEECH_POLLY_SECRET'),
        'token' => env('TEXT_TO_SPEECH_POLLY_TOKEN', false),
        'version' => env('TEXT_TO_SPEECH_POLLY_VERSION', 'latest'),
        'region' => env('TEXT_TO_SPEECH_POLLY_REGION', 'us-east-1'),
        'format' => env('TEXT_TO_SPEECH_POLLY_FORMAT', 'mp3'),
        'engine' => env('TEXT_TO_SPEECH_POLLY_ENGINE', 'standard'),
        'voice' => env('TEXT_TO_SPEECH_POLLY_VOICE', 'Joanna'),
        'text_type' => env('TEXT_TO_SPEECH_POLLY_TEXT_TYPE', 'text'),
    ],

    'system' => [
        'rate' => env('TEXT_TO_SPEECH_SYSTEM_RATE', 175),
        'pitch' => env('TEXT_TO_SPEECH_SYSTEM_PITCH', 50),
        'volume' => env('TEXT_TO_SPEECH_SYSTEM_VOLUME', 100),
        'voice' => env('TEXT_TO_SPEECH_SYSTEM_VOICE', 'german-mbrola-1'),
    ],
];

Driver Prequisites

System Driver

When using the system driver, you need to install the following packages:

Google Driver

When using the google driver, you need to install the following packages:

composer require google/cloud-text-to-speech

💻 Usage

use PascalKleindienst\LaravelTextToSpeech\Facades\TextToSpeech;
use PascalKleindienst\LaravelTextToSpeech\Source;

// The text is converted to speech and stored in a file (configured in the "audio" config).
TextToSpeech::convert('Hello World!'); 

// The text is converted to speech and additionallystored in the given file.
TextToSpeech::convert('Hello World!')->save('path/to/file.mp3'); 

// Store the converted text on a different disk
TextToSpeech::convert('Hello World!')->disk('s3')->save('path/to/file.txt');

// convert text from a file
TextToSpeech::from(Source::File)->convert('path/to/file.txt');

/**
 * Change the language and/or voice used for the speech. 
 * NOTE: 
 *   This will overwrite the language and voice settings in the config file.
 *   Also the format for the language might be different for each engine, e.g. de vs de-DE
 */
TextToSpeech::language('de-DE')->voice('male')->convert('Hallo Welt!');

// Use the system engine to convert text to speech, needs espeak-ng to be installed
TextToSpeech::engine('system')->convert('Hello System Engine!');

// Use the Google engine to convert text to speech, needs google cloud sdk to be installed
TextToSpeech::engine('google')->convert('Hello Google Engine!');

👨‍🔬 Testing

composer test

📝 Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.