Кеширование сгенерированных фраз в Redis
[php.git] / festival-php / voice_class_inc.php
1 <?php
2 class voice
3 {
4         
5         function __construct() {
6                 $this->mode = 'rhvoice';
7                 $this->pitch = -0.1;
8                 $this->speed = -0.3;
9                 $this->display = ":99";
10         }
11
12         function text2Wav($string,$lang = "ru")
13         {
14                 
15                 $redis = new Redis();
16                 $hash ='';
17                 if ($redis->pconnect('127.0.0.1', 6379, 2.5)) {
18                         $hash="voice-".hash("sha256","$lang-$string");
19                         $result=$redis->get($hash);
20                         if ($result) {
21                                 return $result;
22                         }
23                 }
24                 
25                 if ($this->mode == 'festival') {
26                 
27                         $prolog="/etc/festival/$lang.scm";
28                         if (!file_exists($prolog)) {
29                                 $prolog="/etc/festival/default.scm";
30                         }
31                 
32                         //make sure that your environment is set to find the festival binaries!
33                         $cmd = "festival_client --prolog $prolog --ttw | lame -f - -";
34                 } elseif ($this->mode == 'rhvoice') {
35                         $cmd = "DISPLAY=".$this->display." RHVoice-client -s Anna+CLB -r ".$this->speed." -p ".$this->pitch." | lame -f - -";
36                 }
37                 
38                 print $cmd;
39                 
40                 //execute the command
41                 $descriptorspec = array(
42                     0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
43                     1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
44                     2 => array("pipe", "w")   // stderr is a file to write to
45                   );
46                 $cwd = '/tmp';
47                 $env = array();
48                 $process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
49                 if (is_resource($process)) {
50                         fwrite($pipes[0],$string);
51                         fclose($pipes[0]);
52                         $data = stream_get_contents($pipes[1]);
53                         fclose($pipes[1]);
54                         fclose($pipes[2]);
55                         $proc_result = proc_close($process);
56                 }
57
58                 if (!$proc_result) {
59                   if ($hash) {
60                     $redis->set($hash,$data);
61                   }
62                   return $data;
63                 } else {
64                   return '';
65                 }
66         }
67         
68 }//end class
69 ?>