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?
Contents
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
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:
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.
I will update this polly text to speech with multi language support using google translator in my next post.
That was a great tutorial!. Suppose my text contains 500 characters and I listen to the audio file. Lets say, I made some changes with my text and now its 550 characters. I listen to the audio file again. Will I be billed for (500 +550=1050) 1050 characters(I know its 4 $ /1 million characters) or will I be charged only if I download the file?
Thanks in advance!
Hi Divya,
You will be billed for 1050 characters.
Thanks
karthikeyan K
The audio doesnt work can you please help me with that?
with the header method it seems you cant put any other html content on the page??
Hello Ganesh,
Thank you for this great tutorial. Your code is working like a charm. Now I have a small problem I am developing an e-book app with javascript. I want one small feature that words should be highlighted on my html page when they are spoken. I read that it is possible through speechmarks. I have downloaded speech-marks but I don’t know how to use them to highlight text.
Please help.
Thank You
get error that there is no version latest?
Thank you for this post. Best tutorial I have read all year!
Hw to use multiple voices, Brian and Aditi ? in args
Hi Nikhil,
To my knowledge AWS Polly does not currently support independent voice-ids in a single rendering.
If there is an update, I will keep you posted.
Thanks
Hi there, great tutorial!!!!! have you got any sample code that works using startSpeechSynthesisTask?
Thanks in advance!
How to save mp3 file in particular path?
file_put_contents(“your.mp3”, $resultData);
Hi Karthikeyan K
Thanks for ur blog. I read and follow ur blog. but now i am unable to get the different language mp3 file in my panel. but in amazon aws panel it allows all language api. Please Give me suggetion how can change parameter or change any type of code in panel.
HI,
is it possible for you to share your code? so that I can able to help you
my email : karthi@w3lessons.info
Thanks
@disqus_0EUy39ZYLC:disqus is it working now ?
Hi Mohamed,
Seems you are using windows server..
To resolve the error, you need to define your CURL certificate authority information path
To do that,
Download the latest curl recognized certificates here: https://curl.haxx.se/ca/cacert.pem
Save the cacert.pem file in a reachable destination.
Then, in your php.ini file, scroll down to where you find [curl].
You should see the CURLOPT_CAINFO option commented out. Uncomment and point it to the cacert.pem file. You should have a line like this:
curl.cainfo = “certificate pathcacert.pem”
Save and close your php.ini. Restart your webserver and try your request again.
Thanks
Thanks!
I will try it.
Sure Mohamad 🙂