PHP Text to Speech via Amazon Polly API
Firstly what is Amazon Polly?
Well Amazon Polly is a service that turns text to voice, simple as that. While a big part of their focus is IOT, Amazon Polly can be used in a wide variety of applications: from education to content or even business management. Just imagine a Chef that takes his online orders via Amazon Echo.
What is great for this new service is that already comes with support for 24 languages (atm):
- Danish
- Dutch
- English (Australian)
- English (British)
- English (Indian)
- English (US)
- English (Welsh)
- French
- French (Canadian)
- German
- Icelandic
- Italian
- Japanese
- Korean
- Norwegian
- Polish
- Portuguese (Brazilian)
- Portuguese (European)
- Romanian
- Russian
- Spanish
- Spanish (Latin American)
- Swedish
- Turkish
- Welsh
And such a great product comes with API and PHP SDK that you can download here
The rest is pretty simple once we include the autoloader
require '/path/to/aws-autoloader.php';
Keep in mind that we will need to configure AWS Access Keys.
Creating Amazon Polly Client
use Aws\Polly\PollyClient; $config = [ 'version' => 'latest', 'region' => 'us-east-1', //region 'credentials' => [ 'key' => 'your aws access key', 'secret' => 'your aws secret key', ] ]; $client = new PollyClient($config);
after we just need to configure the SynthesizeSpeech method with required parameters
$args = [ 'OutputFormat' => 'mp3', 'Text' => "<speak><prosody rate='medium'>your text goes here..</prosody></speak>", 'TextType' => 'ssml', 'VoiceId' => "Joanna", ]; $result = $client->synthesizeSpeech($args); $resultData = $result->get('AudioStream')->getContents();
Afterwards we just listen the output
$size = strlen($resultData); // File size $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte header('Content-Transfer-Encoding:chunked'); header("Content-Type: audio/mpeg"); header("Accept-Ranges: 0-$length"); header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); echo $resultData;
Or we can download the mp3
header('Content-length: ' . strlen($resultData)); header('Content-Disposition: attachment; filename="polly-text-to-speech.mp3"'); header('X-Pad: avoid browser bug'); header('Cache-Control: no-cache'); echo $resultData;