It’s really amazing how Google is providing cool APIs for every this and that! And here comes another one that impressed me 😀 I am talking about the Text To Speech API by Google. It’s fantastic! I wrote a php wrapper class that would help you create mp3 files from texts 🙂 Of course, using Google as the medium!
Here’s the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php // FileName: tts.php /* * A PHP Class that converts Text into Speech using Google's Text to Speech API * * Author: * Abu Ashraf Masnun * https://masnun.com * */ class TextToSpeech { public $mp3data; function __construct($text="") { $text = trim($text); if(!empty($text)) { $text = urlencode($text); $this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}"); } } function setText($text) { $text = trim($text); if(!empty($text)) { $text = urlencode($text); $this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}"); return $mp3data; } else { return false; } } function saveToFile($filename) { $filename = trim($filename); if(!empty($filename)) { return file_put_contents($filename,$this->mp3data); } else { return false; } } } ?> |
And here’s demo :
1 2 3 4 5 6 |
<?php require "tts.php"; $tts = new TextToSpeech(); $tts->setText("Hello World!"); $tts->saveToFile("masnun.mp3"); ?> |
You can alternatively pass the text to the constructor of the object like this:
1 2 3 4 5 |
<?php require "tts.php"; $tts = new TextToSpeech("Hello World!"); $tts->saveToFile("masnun.mp3"); ?> |
That is simple, isn’t that? Hope you like it!
31 replies on “Google’s Text to Speech API : A PHP Wrapper Class”
[…] This post was Twitted by masnun […]
[…] This post was mentioned on Twitter by Abu Ashraf Masnun, jimmayes. jimmayes said: RT @masnun: Using Google's Text To Speech API with PHP: http://bit.ly/8FSuO1 #Google #PHP […]
[…] See the original post: Google's Text to Speech API : A PHP Wrapper Class | maSnun.com […]
Nifty nice class… carry on digging google 🙂
Hello, if i would speech a text without limit of 100 char, how can i split the text and speech all one next another?
Let me know plz and sorry for my english.
Split the texts in small blocks and get the sound files for each.
Yes, my problem is how execute one after another the link that i have created.
how do I know when the execution of the first link is completed to start the second?
Thanks
When the saveToFile() method returns true, it means the portion is done.
i have modified this so it can support French, but when i tried to add some unique character in French like “Et vous êtes?” it won’t working correctly.. it sounds weird..
It’s a problem on Google’s side I think.
utf8_encode($text);
Thanks buddy 🙂
facing error in your code
Notice: Undefined variable: mp3data in E:\wamp\www\voice\tts.php on line 27
hey neha, are you on php4?
no ,i am using php 5.3.0
There’s something going wrong with the OO model.
Is there any way i can convert speech to text
I use Google 🙂
http://ab-log.ru/smart-house/speech/speech-recognition
[…] […]
Hi, I furkan,
http://www.masnun.me/2009/12/14/googles-text-to-speech-api-a-php-wrapper-class.html
I have done a google translation to audio file at this address
http://www.oddcast.com/home/demos/tts/tts_example.php
Would you like the script I want to help you make this address??
Fantastic! Right what i was looking for! Thank you very much
I’ve tested the script on my server but it won’t work. Can you tell me why? It replies the following error: ” Warning: file_get_contents(http://translate.google.com/translate_tts?q=Hello+World%21) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/specialr/public_html/bot/tts.php on line 17″
This don’t work anymore. Google block this method
[…] http://www.masnun.com/2009/12/14/googles-text-to-speech-api-a-php-wrapper-class.html […]
They haven’t blocked it, you just need to include the variable “tl=en” within the address.
So instead of it being:
file_get_contents("http://translate.google.com/translate_tts?q={$text}");
You will have to use:
file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");
That should work. 🙂
In the wettest method you need to return ‘$this->mp3data’ not ‘$mp3data’. That’s why it’s undefined.
Darn typo. It should have been “setText” not “wettest”
assalamualekum ,
can you please tell me how we convert our text to voice carrying more than 100 strings can you please help me with a proper procedure on it .
thankyou
i dont want to save audio file i just only need to read the text what to do
( ! ) Parse error: syntax error, unexpected ‘World’ (T_STRING) in C:\wamp\www\t2s\index.php on line 5
This gives an error. Please find some solution as soon as possible.