version: 1.0 homepage: http://ardoino.com ************************************************/ class pvox { public $ftkey = "ft_StUfF_key"; // Defined in speech tools public $ftserver = array("ip" => "localhost", "port" => 1314); public $string = ""; public $wave = ""; function __construct() {} // set festival ip and port function set_ftserver($ftserver) { if($ftserver["ip"] != "") $this->ftserver["ip"] = $ftserver["ip"]; if($ftserver["port"] > 0) $this->ftserver["port"] = $ftserver["port"]; } // set string to be spoken function set_string($string) { if($string != "") $this->string = $string; } // connect to server, send commands and the string function vox() { $this->wave = ""; if($this->string != "") { $fp = fsockopen($this->ftserver["ip"], $this->ftserver["port"]); fwrite($fp, "(Parameter.set 'Wavefiletype 'riff)\n"); fwrite($fp, "(tts_textall \"".$this->string."\" 'file)\n"); $buf = ""; $FLAG_FT_WAVE = FALSE; $FLAG_FT_EOF = FALSE; while(!feof($fp) && !$FLAG_FT_EOF) { $buf_tmp = ""; $buf_tmp = fgets($fp, 1024); if($FLAG_FT_WAVE) { if($ftk_pos = strpos($buf_tmp, $this->ftkey)) { $buf_tmp = substr($buf_tmp, 0, $ftk_pos); $FLAG_FT_EOF = TRUE; } $buf .= $buf_tmp; } if($buf_tmp == "WV\n") $FLAG_FT_WAVE = TRUE; } fclose($fp); $this->wave = $buf; } } // save wave function wsave() { if($this->wave != "") { if($this->string) $file_name = md5($this->string).".wav"; else $file_name = rand(1000, 1000000).".wav"; $fp = fopen($file_name, "w+"); fwrite($fp, $this->wave); fclose($fp); } } // output wave function play() { if($this->wave != "") { echo $this->wave; } } // output wave (for browsers) function play_web() { if($this->wave != "") { header("Content-type: audio/mpeg"); echo $this->wave; } } } // TEST CODE FOR COMMAND LINE $pv = new pvox(); $pv->set_ftserver(array("ip" => "192.168.0.2", "port" => 31337)); $pv->set_string("Hello world!"); $pv->vox(); $pv->wsave(); // TEST CODE FOR USE WITHIN BROWSERS $pv = new pvox(); $pv->set_ftserver(array("ip" => "192.168.0.2", "port" => 31337)); $pv->set_string($_GET["string"]); $pv->vox(); $pv->play_web(); ?>