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.
- 1 Why Amazon Polly?
- 2 Benefits of Amazon Polly – Text to Speech
- 3 Download and Install Amazon Polly PHP SDK Client
- 4 Configuring the AWS Access Keys
- 5 Creating Amazon Polly Client
- 6 Converting Text to Speech via Polly API
- 7 Download MP3 or Listen the Text
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 – 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
Download and Install Amazon Polly PHP SDK Client
To get started, you must download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:
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
$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 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();
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-Range: bytes $start-$end/$size");
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');
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.
I will update this polly text to speech with multi language support using google translator in my next post.