<?php
class voice
{
	
	function __construct() {
		$this->mode = 'rhvoice';
		$this->pitch = -0.1;
		$this->speed = -0.3;
		$this->display = ":99";
	}

	function text2Wav($string,$lang = "ru")
	{
		
		print_r($this);
		
		if ($this->mode == 'festival') {
		
			$prolog="/etc/festival/$lang.scm";
			if (!file_exists($prolog)) {
				$prolog="/etc/festival/default.scm";
			}
		
			//make sure that your environment is set to find the festival binaries!
			$cmd = "festival_client --prolog $prolog --ttw | lame -f - -";
		} elseif ($this->mode == 'rhvoice') {
			$cmd = "DISPLAY=".$this->display." RHVoice-client -s Anna+CLB -r ".$this->speed." -p ".$this->pitch." | lame -f - -";
		}
		
		print $cmd;
		
		//execute the command
		$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
?>