Google's Text to Speech API : A PHP Wrapper Class

It’s really amazing how Google is providing cool APIs for every this and that! And here comes another one that impressed me :D 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:

<?php
// FileName: tts.php
/*
 *  A PHP Class that converts Text into Speech using Google's Text to Speech API
 *
 * Author:
 * Abu Ashraf Masnun
 * http://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 :

<?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:

<?php
require "tts.php";
$tts = new TextToSpeech("Hello World!");
$tts->saveToFile("masnun.mp3");
?>

That is simple, isn’t that? Hope you like it!

This entry was posted in Blog Post and tagged . Bookmark the permalink.

15 Responses to Google's Text to Speech API : A PHP Wrapper Class

  1. Pingback: Twitted by masnun

  2. Pingback: Tweets that mention Google’s Text to Speech API : A PHP Wrapper Class | maSnun.com -- Topsy.com

  3. Pingback: Google's Text to Speech API : A PHP Wrapper Class | maSnun.com | Coder Online

  4. Lenin says:

    Nifty nice class… carry on digging google :)

  5. Fabio says:

    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.

  6. masnun says:

    Split the texts in small blocks and get the sound files for each.

  7. Fabio says:

    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

  8. masnun says:

    When the saveToFile() method returns true, it means the portion is done.

  9. bhawika says:

    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..

  10. masnun says:

    It’s a problem on Google’s side I think.

  11. Rajinesh says:

    Thanks buddy :)

  12. neha says:

    facing error in your code
    Notice: Undefined variable: mp3data in E:\wamp\www\voice\tts.php on line 27

  13. masnun says:

    hey neha, are you on php4?

  14. neha says:

    no ,i am using php 5.3.0

  15. masnun says:

    There’s something going wrong with the OO model.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">