PHP class, pvox : Festival text-to-speech client
University of Edinburgh’s Festival Speech Synthesis Systems is a free software multi-lingual speech synthesis workbench that runs on multiple-platforms offering black box text to speech, as well as an open architecture for research in speech synthesis. It designed as a component of large speech technology systems.
Since I had problems with clients included in Speech Tools ( part of Festival software ),
I wrote Pvox .
Pvox is a PHP 5 client for the Festival Speech Synthesis Systems. It connects to a festival server, sends commands and the string to process and receives a wave file. It could be used as a command line script or to play spoken audio on a browser.
Version: 1.0
Changes: none
Usage :
- Run festival in server mode: festival –server
- Run pvox : read the examples below
// 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->save();
// 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();
<?php
/************************************************
Pvox, a Festival text-to-speech PHP5 client
(c) 2008 Paolo Ardoino < paolo.ardoino@gmail.com >
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();
?>
Download this code: pvox.txt


Dear Paolo, (oppure Caro Paulo :-)))
Do you think I can use Pvox integrated in Moodle code, to provide assistance to blind children.
Tnak you!!!
Man, I did not try this yet but if it does exactly what you said, AHA, you’re the dopest !
Thanks !