<?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)
	 * @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";
		}
		
		//initialise and execute the festival C engine
		//make sure that your environment is set to find the festival binaries!
		$cmd = "festival_client --prolog $prolog --ttw $filename --output $outputfile";
		//execute the command
		exec($cmd);
		
		//delete the text file (Temp)
		unlink($filename);
		//finally return the uptput file path and filename
		return $outputfile;
	}
	
}//end class
?>