예제 #1
0
/**
 * Mathieu
 * Génère la synthèse vocale via google
 * Ou la récupère depuis notre cache si elle y est déjà
 * http://translate.google.com/translate_tts?ie=utf-8&tl=fr&q=...
 * @param string $text
 * @return string $mp3Path
 */
function getSpeechFromString($text)
{
    $text = html_entity_decode(strip_tags($text));
    $sansAccent = array("idee", "demarches", "ecouter", "ecrire");
    $avecAccent = array("idée", "démarche", "écouter", "écrire");
    $text = str_ireplace($sansAccent, $avecAccent, $text);
    $key = sha1($text);
    $mp3Path = "cacheTextToSpeech" . DIRECTORY_SEPARATOR . $key . ".mp3";
    // si une seule requete google
    if (strlen($text) <= 100) {
        if (!file_exists($mp3Path)) {
            $url = "http://translate.google.com/translate_tts?ie=utf-8&tl=fr&q=" . urlencode($text);
            $mp3data = file_get_contents($url);
            file_put_contents($mp3Path, $mp3data);
        }
    } else {
        // génération de la liste des mp3
        $mp3Parts = array();
        $i = 0;
        do {
            $i++;
            $textPart = smartSplitText($text, 100);
            $keyPart = sha1($textPart);
            $mp3Parts[$keyPart] = getSpeechFromString($textPart);
            $text = substr($text, strlen($textPart), strlen($text) - strlen($textPart));
        } while (trim($text) != "" && $i < 10);
        // assemblage de la liste des MP3
        $mp3 = new mp3();
        foreach ($mp3Parts as $keyPart => $mp3Part) {
            $mp3->mergeBehind(new mp3($mp3Part));
        }
        file_put_contents($mp3Path, $mp3->str);
    }
    return $mp3Path;
}
 function multiJoin($newpath, $array)
 {
     foreach ($array as $path) {
         $mp3 = new mp3($path);
         $mp3->striptags();
         $mp3_1 = new mp3($newpath);
         $mp3->mergeBehind($mp3_1);
         $mp3->save($newpath);
     }
 }