Skip to content

Instantly share code, notes, and snippets.

@joshcirre
Created May 4, 2026 17:35
Show Gist options
  • Select an option

  • Save joshcirre/ccc213893b4d367edf4ce8311e63cdcf to your computer and use it in GitHub Desktop.

Select an option

Save joshcirre/ccc213893b4d367edf4ce8311e63cdcf to your computer and use it in GitHub Desktop.
Playing a custom (or cloned) voice in the terminal on command.
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Laravel\Ai\Audio;
#[Signature('speak {text* : The text to speak} {--voice=qgmzByK5IxRmIg8UeLks : ElevenLabs voice ID}')]
#[Description('Generate audio with ElevenLabs and play it through the terminal')]
final class Speak extends Command
{
public function handle(): int
{
$text = implode(' ', $this->argument('text'));
$voice = (string) $this->option('voice');
$this->components->info("Generating audio: \"{$text}\"");
$response = Audio::of($text)->voice($voice)->generate();
$path = tempnam(sys_get_temp_dir(), 'speak_').'.mp3';
file_put_contents($path, $response->content());
$this->components->info('Playing...');
$player = match (PHP_OS_FAMILY) {
'Darwin' => 'afplay',
'Linux' => 'mpg123 -q',
'Windows' => 'powershell -c (New-Object Media.SoundPlayer',
default => null,
};
if ($player === null) {
$this->components->error('Unsupported OS for audio playback.');
return self::FAILURE;
}
passthru(escapeshellcmd($player).' '.escapeshellarg($path));
@unlink($path);
return self::SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment