Wednesday, 27 November 2013

Voice to text & text to mp3 using google's voice speech api Webkit


followwing is the demo :

<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<script type="text/javascript">
function searchthis() {
//$('#myform').submit(); //jQuery
document.getElementById("voiceform").submit(); //normal javascript
}
</script>

</head>


<body>
<form action="" method="get" id="voiceform" >
<input type="text" name="texttoaudio"  x-webkit-speech onwebkitspeechchange="searchthis()" />
</form>
<?php
if(isset($_GET['texttoaudio']))
{

    $text = $_GET['texttoaudio'];
    // Yes French is a beautiful language.
    $lang = "en";

    // Name of the MP3 file generated using the MD5 hash
    // Added things to prevent bug if you want the same sentence in two different languages
    $file  = md5($lang."?".urlencode($text));

    // Save the MP3 file in this folder with the .mp3 extension
    $file = $file .".mp3";


    // Verify if folder exists, if it doesn't, create it, if exists, verify CHMOD
    if(!is_dir("/"))
        mkdir("/");
    else
        if(substr(sprintf('%o', fileperms('/')), -4)!="0777")
            chmod("/", 0777);


    // If the MP3 file exists, do not create a new request
    if (!file_exists($file))
    {
        // Download the content
        $mp3 = file_get_contents(
        'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
        file_put_contents($file, $mp3);
    }
}
?>


</body>
</html>




for more demo :
https://www.google.com/intl/en/chrome/demos/speech.html