From a79bfa56638c77c3d6bc7bcc2e0e80ad365dc9db Mon Sep 17 00:00:00 2001
From: Roman Bazalevsky <rvb@rvb.name>
Date: Wed, 21 Sep 2016 14:55:26 +0300
Subject: [PATCH] =?utf8?q?=D0=92=D0=B5=D0=B1-=D0=BF=D1=80=D0=BE=D1=81?=
 =?utf8?q?=D0=BB=D0=BE=D0=B9=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=81?=
 =?utf8?q?=D0=B8=D0=BD=D1=82=D0=B5=D0=B7=D0=B0=20=D1=80=D0=B5=D1=87=D0=B8?=
 =?utf8?q?=20festival.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Работает с уже запущенным сервером, поддерживает передачу языка в параметре.
---
 festival-php/festival_class_inc.php | 58 +++++++++++++++++++++++++++++
 festival-php/index.php              | 31 +++++++++++++++
 2 files changed, 89 insertions(+)
 create mode 100644 festival-php/festival_class_inc.php
 create mode 100644 festival-php/index.php

diff --git a/festival-php/festival_class_inc.php b/festival-php/festival_class_inc.php
new file mode 100644
index 0000000..2e48b31
--- /dev/null
+++ b/festival-php/festival_class_inc.php
@@ -0,0 +1,58 @@
+<?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
+?>
\ No newline at end of file
diff --git a/festival-php/index.php b/festival-php/index.php
new file mode 100644
index 0000000..28e6dcf
--- /dev/null
+++ b/festival-php/index.php
@@ -0,0 +1,31 @@
+<?php
+
+$lang=$_GET['tl'];
+$text=$_GET['q'];
+
+if ( !$text ) {
+  exit;
+}
+
+include("festival_class_inc.php");
+$tts = new festival;
+$fullPath=$tts->text2Wav($text,$lang);
+
+if ($fd = fopen ($fullPath, "r")) {
+    $fsize = filesize($fullPath);
+    $path_parts = pathinfo($fullPath);
+    $ext = strtolower($path_parts["extension"]);
+    header("Content-type: application/octet-stream");
+    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
+    header("Content-length: $fsize");
+    header("Cache-control: private"); //use this to open files directly
+    while(!feof($fd)) {
+        $buffer = fread($fd, 2048);
+        echo $buffer;
+    }
+}
+fclose ($fd);
+
+// $tts->text2Speech('The authors email address is. p scott @ u w c dot a c dot zed ay');
+
+?>
-- 
2.34.1