PHP Text to Speech using AWS Polly

Karthikeyan K
Nov 5 · 3 min read

Amazon Polly is a Web Service used to convert any Text to Speech in real time. Also there is an option to download into MP3 format.

This text to speech service that uses advanced machine learning technologies to convert speech that sounds like a original human voice.

checkout my recent article on how to identify celebrities in an image using PHP and Amazon Rekognition API?

Why Amazon Polly?

It supports multiple languages across countries and variety of human voices. You can able to choose voices both male and female based on the country/region. It is awesome right? Yeah.

Benefits of Amazon Polly — PHP Text to Speech

  • Simple to Use
  • Low Cost — Pay as you go Pricing (On Demand)
  • Natural Human Voices
  • Speedy Response with Best user Experience
  • MP3 & OGG Support
  • Cloud Platform
  • FREE 5 Million Characters per Month (ie — You can read an average of 2000 emails/month)

Since there are so many APIs are available, I prefer Amazon polly because it is easy to add speech to your video, presentation, or online training course. Amazon Polly can generate speech in 24 languages, making it easy to add voice to applications with a global audience. With Amazon Polly you can read any content like RSS feed, news, or email, and store the speech in to MP3 audio formats — read more

To get started, you must download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:

require '/path/to/aws-autoloader.php';

Configuring the AWS Access Keys

In order to use amazon polly service, we should register an account with AWS and get the AWS Access key/secret within our AWS account. Please go through this link to get the credentails

Creating Amazon Polly Client

After getting credentails from amazon account, we need to create an polly client as below:

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);

Converting Text to Speech via Polly API

We just need to call the SynthesizeSpeech method, provide the text you wish to synthesize, select one of the available Text-to-Speech (TTS) voices, and specify an audio output format. Amazon Polly then synthesizes the provided text into a high-quality speech audio stream.

VoiceId — check this Polly Voice documentation

OutputFormat — This will be mp3, ogg_vorbis, or pcm.

TextType — This will be plain text or SSML. The default value is plain text. For more information, see Using SSML.

$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();

PHP Text to Speech — Download MP3 or Listen the Text

Now you need to convert your text to speech by listening the text or by downloading the text into MP3 format

Listening the text

$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;

Download the Text to Speech in MP3 Format

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;

take a quick demo now

I hope you like this article very much!. Please feel free to download the code and use it in your projects as well.

Karthikeyan K

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade