Переделано с временных файлов на proc_open + mp3 кодирование на лету.
authorRoman Bazalevsky <rvb@rvb.name>
Thu, 22 Sep 2016 14:19:56 +0000 (17:19 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Thu, 22 Sep 2016 14:19:56 +0000 (17:19 +0300)
festival-php/festival_class_inc.php
festival-php/index.php

index 2e48b3187c8e0110f3ed68bf73b512bc0c103d71..bd960a1d4d1cfc1123b9996e7e424fb6faea2197 100644 (file)
@@ -12,46 +12,43 @@ class festival
        /**
         * Method to wrap API calls to the festival tts engine
         * This function will create an utterance and output it to $outputfile. '.wav';
        /**
         * Method to wrap API calls to the festival tts engine
         * This function will create an utterance and output it to $outputfile. '.wav';
-        * @author Paul Scott (base design) + Roman Bazalevskiy (adaptation for multilingual+server environment)
+        * @author Paul Scott (base design) + Roman Bazalevskiy (adaptation for multilingual+server environment+no temp files!)
         * @param $string
         * @return waveform
         */
        function text2Wav($string,$lang = "ru")
        {
         * @param $string
         * @return waveform
         */
        function text2Wav($string,$lang = "ru")
        {
-               //create a random number filename so that simultaneous users don't overwrite each other
-               $tempfilename = tempnam('/tmp','fest-');
-               $filename = $tempfilename . '.txt';
-               $outputfile = $tempfilename . '.wav';
-               
-               if (!$handle = fopen($filename, "w")) 
-               {
-                       //we cannot open the file handle!
-                       return false;
-               }
-               // Write $string to our opened file.
-               if (fwrite($handle, $string) === FALSE) 
-               {
-                       //couldn't write the file...Check permissions
-                       return false;
-               }
-               //close the file handle - we are done with it
-               fclose($handle);
                
                $prolog="/etc/festival/$lang.scm";
                if (!file_exists($prolog)) {
                        $prolog="/etc/festival/default.scm";
                }
                
                
                $prolog="/etc/festival/$lang.scm";
                if (!file_exists($prolog)) {
                        $prolog="/etc/festival/default.scm";
                }
                
-               //initialise and execute the festival C engine
                //make sure that your environment is set to find the festival binaries!
                //make sure that your environment is set to find the festival binaries!
-               $cmd = "festival_client --prolog $prolog --ttw $filename --output $outputfile";
+               $cmd = "festival_client --prolog $prolog --ttw | lame -f - -";
                //execute the command
                //execute the command
-               exec($cmd);
-               
-               //delete the text file (Temp)
-               unlink($filename);
-               //finally return the uptput file path and filename
-               return $outputfile;
+               $descriptorspec = array(
+                   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
+                   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
+                   2 => array("pipe", "w")   // stderr is a file to write to
+                 );
+               $cwd = '/tmp';
+               $env = array();
+               $process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
+               if (is_resource($process)) {
+                       fwrite($pipes[0],$string);
+                       fclose($pipes[0]);
+                       $data = stream_get_contents($pipes[1]);
+                       fclose($pipes[1]);
+                       fclose($pipes[2]);
+                       $proc_result = proc_close($process);
+               }
+
+               if (!$proc_result) {
+                 return $data;
+               } else {
+                 return '';
+               }
        }
        
 }//end class
        }
        
 }//end class
index bc269960b64901914216fabb1094bd48aafbf421..769b8ce82490310b07ae4a03085a1c15a37a8229 100644 (file)
@@ -9,24 +9,14 @@ if ( !$text ) {
 
 include("festival_class_inc.php");
 $tts = new festival;
 
 include("festival_class_inc.php");
 $tts = new festival;
-$fullPath=$tts->text2Wav($text,$lang);
+$data=$tts->text2Wav($text,$lang);
 
 
-if ($fd = fopen ($fullPath, "r")) {
+if ($data) {
     $fsize = filesize($fullPath);
     $fsize = filesize($fullPath);
-    $path_parts = pathinfo($fullPath);
-    $ext = strtolower($path_parts["extension"]);
-    header("Content-type: application/octet-stream");
-    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
-    header("Content-length: $fsize");
-    header("Cache-control: private"); //use this to open files directly
-    while(!feof($fd)) {
-        $buffer = fread($fd, 2048);
-        echo $buffer;
-    }
+    $path_parts = pathinfo();
+    $ext = 'mp3';
+    header("Content-type: audio/mpeg");
+    echo $data;
 }
 }
-fclose ($fd);
-unlink($fullPath);
-
-// $tts->text2Speech('The authors email address is. p scott @ u w c dot a c dot zed ay');
 
 ?>
 
 ?>