سؤال

I am trying to make a device with Arduino that will read a lot short sentences out loud. I normally would use a voice shield for this but the text is in Dutch. And I can't find a way to have it speak understandable in dutch. (Can't find a good vocabulary file) and I don't have the time to create one myself.

Now I am trying to work around this problem by trying to download the google translate speech. I for example want to download this file automatically http://translate.google.com/translate_tts?tl=nl&q=%22Marrit%20is%20gek And transfer this file to the arduino to let him play it with an mp3 shield.

I found some php code on the web that I think should help me download the file.

$text = "Hello this is a test for voice api of google";
$text = urlencode($text);
$lang = urldecode("en");
$file  = "audio/" . md5($text) .".mp3";
   if (!file_exists($file) || filesize($file) == 0) {
     $mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input');
     if(file_put_contents($file, $mp3)){
        echo "Saved<br>";
     }else{
        echo "Wasn't able to save it !<br>";
     }
   } else {
     echo "Already exist<br>";
   }

When I run the code it says "saved". On my server I see a file but the file is only 2kb and is not playable. When I open the file in text edit I get this message.

403. That’s an error. Your client does not have permission to get URL /translate_tts?ie=UTF8&q=Hello+this+is+a+test+for+voice+api+of+google&tl=en&total=2&idx=0&textlen=44&prev=input from this server. (Client IP address: ......)

That’s all we know.

Now I am probably doing something stupid and I hope you can help me. If there is a better way for me to go about achieving what I am trying to achieve please tell me. Thanks in advance!

Update I see this is working to display the audio file.

$text = urlencode('my text');
$url = "http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=".$text;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$return = curl_exec($ch);
curl_close($ch);

echo $return;

But I do not know how to combine the codes so it will download it also. Can someone help me with that?

هل كانت مفيدة؟

المحلول

<?php
    $text = "Hello this is a test for voice api of google";
    $text = urlencode($text);
    $lang = urldecode("en");
    $file  = "audio/" . md5($text) .".mp3";
    if (!file_exists($file) || filesize($file) == 0) {
        $url = 'http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $mp3 = curl_exec($ch);
        curl_close($ch);

        if(file_put_contents($file, $mp3)){
            echo "Saved<br>";
        } else {
            echo "Wasn't able to save it !<br>";
        }
    } else {
        echo "Already exist<br>";
    }  
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top