<?php
/**
 * Festival Wrapper class for the Festival Text To Speech Synthesis System
 * @link http://www.ctsr.ed.ac.uk/projects/festival/
 * @author Paul Scott (base design) + Roman Bazalevskiy (adaptation for multilingual+server environment)
 * @package Festival
 * @version 0.9
 */
class festival
{
	
	/**
	 * 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+no temp files!)
	 * @param $string
	 * @return waveform
	 */
	function text2Wav($string,$lang = "ru")
	{
		
		$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 - -";
		//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
?>