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.
Installation
composer require bestmomo/laravel-edge-tts
Publish the config file (if needed):
php artisan vendor:publish --provider="Bestmomo\EdgeTTS\EdgeTTSPackageServiceProvider" --tag="config"
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');
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"));
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"));
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',
]);
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"));
}
}
}
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',
]);
config/edge-tts.php:
'default' => [
'voice' => 'en-US-AriaNeural',
'rate' => 0.8, // 0.5 to 2.0
'pitch' => 0, // -24 to 24
],
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();
}
Voice Availability Not all voices are available for all languages. Check the Edge TTS documentation for supported voices.
File Permissions Ensure the storage directory is writable:
chmod -R 775 storage/app/audio
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
}
config/edge-tts.php:
'debug' => env('EDGE_TTS_DEBUG', false),
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';
}
}
Middleware for Authentication If the service requires authentication in the future, create middleware:
$edgeTTS->withHeaders([
'Authorization' => 'Bearer ' . config('edge-tts.token'),
]);
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);
}
How can I help you explore Laravel packages today?