Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * this is a PHP class which converts text to spoken language
  5. * Version: 1.4.2
  6. *
  7. * Usage:
  8. * $tts = new tts();
  9. * $d = $tts->say("text to convert", "language_code");
  10. *
  11. * Language Codes can be found at http://portalas.org/scripts/tts/docs/
  12. *
  13. * Author: Portalas - http://portalas.org/scripts/tts/
  14. */
  15.  
  16. $protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://';
  17. DEFINE('BASE_URL', $protocol . $_SERVER['HTTP_HOST'] . substr(__DIR__, strlen($_SERVER[ 'DOCUMENT_ROOT' ])) . DIRECTORY_SEPARATOR);
  18. DEFINE('ROOT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR );
  19.  
  20. $read = str_replace("'", "", $_POST['read']);
  21. $read = str_replace("&", "and", $read);
  22. $read = str_replace('"', "", $read);
  23. $read = filter_var($read,FILTER_SANITIZE_STRING);
  24. $lang = filter_var(strtolower($_POST['lang']),FILTER_SANITIZE_STRING);
  25.  
  26.  
  27. if ($read != "") {
  28.  
  29. $tts = new tts();
  30. $d = $tts->say($read, $lang);
  31.  
  32. echo $d;
  33. }
  34.  
  35.  
  36. class tts {
  37.  
  38. /**
  39. * @const audio folder location
  40. *
  41. */
  42. const audioDir = "audio/";
  43.  
  44.  
  45. /**
  46. * @public $text
  47. *
  48. */
  49. public $text;
  50.  
  51.  
  52. /**
  53. * @public $fetcher
  54. *
  55. */
  56. private $fetcher;
  57.  
  58. public function __construct($fetcher = null, $text='') {
  59.  
  60. if (!isset($this->fetcher)) {
  61. $this->fetcher = (function_exists('curl_version')) ? curl : fgc;
  62.  
  63. }
  64. }
  65.  
  66. /**
  67. * returns mp3 file from text
  68. *
  69. * @param - $text - holds text which will be converted to voice.
  70. *
  71. */
  72.  
  73. public function say($text,$lang) {
  74.  
  75. $this->text = $this->sanitize($text);
  76. $lang = $this->sanitize($lang);
  77. $this->lang_code = $lang;
  78.  
  79. $this->lang = $this->setLang($lang);
  80.  
  81. if ($this->lang_code == "en_us_deprecated") {
  82.  
  83. $file_url = $this->fetch("http://tts-api.com/tts.mp3?return_url=1&q=" . str_replace(" ","+",$this->text));
  84.  
  85. }
  86.  
  87. else {
  88.  
  89. $this->fileName_hashed = $this->hash($this->text);
  90. $isCached = $this->isCached($this->fileName_hashed);
  91.  
  92. if ($isCached === true) {
  93. $file_url = $this->makeURL($this->fileName_hashed);
  94. }
  95.  
  96. else {
  97. $array_splitText = $this->split($this->text);
  98. $size_of = sizeof($array_splitText);
  99.  
  100. for ($i = 0; $i < $size_of; $i++) {
  101. $encoded = urlencode($array_splitText[$i]);
  102. $_encoded = explode("UUUiL",$encoded);
  103. $writeToFile = $this->create($_encoded[0]);
  104. }
  105.  
  106. $file_url = $this->makeURL($this->fileName_hashed);
  107. }
  108. }
  109. return $file_url;
  110. }
  111.  
  112.  
  113. public function setLang($lang) {
  114.  
  115. switch ($lang) {
  116. case "en-us":
  117. $this->lang = "usenglishfemale";
  118. $this->exclude_chars = "-9139";
  119. break;
  120.  
  121. case "en_gb":
  122. $this->lang = "ukenglishfemale";
  123. $this->exclude_chars = "-9139";
  124. break;
  125.  
  126. case "en_au":
  127. $this->lang = "auenglishfemale";
  128. $this->exclude_chars = "-11939";
  129. break;
  130.  
  131. case "mx":
  132. $this->lang = "usspanishmale";
  133. $this->exclude_chars = "-15139";
  134. break;
  135.  
  136. case "zh_cn":
  137. case "zh-cn":
  138. case "zh":
  139. $this->lang = "chchinesefemale";
  140. $this->exclude_chars = "-28939";
  141. break;
  142.  
  143. case "jp":
  144. case "ja":
  145. $this->lang = "jpjapanesefemale";
  146. $this->exclude_chars = "-14939";
  147. break;
  148.  
  149. case "kr":
  150. case "ko":
  151. $this->lang = "krkoreanfemale";
  152. $this->exclude_chars = "-14939";
  153. break;
  154.  
  155. case "ar":
  156. $this->lang = "arabicmale";
  157. $this->exclude_chars = "-40939";
  158. break;
  159.  
  160.  
  161. case "hu":
  162. $this->lang = "huhungarianfemale";
  163. $this->exclude_chars = "-14939";
  164. break;
  165.  
  166.  
  167. case "pt-br":
  168. case "br":
  169. $this->lang = "brportuguesefemale";
  170. $this->exclude_chars = "-14939";
  171. break;
  172.  
  173.  
  174. case "pt":
  175. $this->lang = "eurportuguesefemale";
  176. $this->exclude_chars = "-14939";
  177. break;
  178.  
  179.  
  180. case "es":
  181. $this->lang = "eurspanishfemale";
  182. $this->exclude_chars = "-14939";
  183. break;
  184.  
  185.  
  186. case "ca":
  187. $this->lang = "eurcatalanfemale";
  188. $this->exclude_chars = "-15000";
  189. break;
  190.  
  191. case "cs":
  192. case "cz":
  193. $this->lang = "eurczechfemale";
  194. $this->exclude_chars = "-15000";
  195. break;
  196.  
  197.  
  198. case "da":
  199. case "dk":
  200. $this->lang = "eurdanishfemale";
  201. $this->exclude_chars = "-14000";
  202. break;
  203.  
  204. case "fi":
  205. $this->lang = "eurfinnishfemale";
  206. $this->exclude_chars = "-15000";
  207. break;
  208.  
  209. case "fr":
  210. $this->lang = "eurfrenchfemale";
  211. $this->exclude_chars = "-13000";
  212. break;
  213.  
  214. case "no":
  215. $this->lang = "eurnorwegianfemale";
  216. $this->exclude_chars = "-15000";
  217. break;
  218.  
  219.  
  220. case "pl":
  221. $this->lang = "eurpolishfemale";
  222. $this->exclude_chars = "-15000";
  223. break;
  224.  
  225. case "it":
  226. $this->lang = "euritalianfemale";
  227. $this->exclude_chars = "-15000";
  228. break;
  229.  
  230.  
  231. case "tr":
  232. $this->lang = "eurturkishfemale";
  233. $this->exclude_chars = "-14000";
  234. break;
  235.  
  236. case "gr":
  237. case "el":
  238. $this->lang = "eurgreekfemale";
  239. $this->exclude_chars = "-14000";
  240. break;
  241.  
  242.  
  243. case "de":
  244. $this->lang = "eurgermanfemale";
  245. $this->exclude_chars = "-14000";
  246. break;
  247.  
  248.  
  249. case "ru":
  250. $this->lang = "rurussianfemale";
  251. $this->exclude_chars = "-14000";
  252. break;
  253.  
  254.  
  255. case "se":
  256. case "sv":
  257. $this->lang = "swswedishfemale";
  258. $this->exclude_chars = "-14000";
  259. break;
  260.  
  261.  
  262. default:
  263. $this->lang = "usenglishfemale";
  264. $this->exclude_chars = "-9139";
  265. }
  266. return $this->lang;
  267. }
  268.  
  269. /**
  270. * filters and sanitizes text
  271. *
  272. * @param - $text - text which to be filtered
  273. *
  274. */
  275.  
  276. function sanitize($text) {
  277.  
  278. if ($text == null) {
  279. $text = "hey! the input is empty. type something in there!";
  280. }
  281.  
  282. $var = filter_var($text, FILTER_SANITIZE_STRING);
  283. $var = str_replace(PHP_EOL, '', $var);
  284. $var = urldecode($var);
  285.  
  286. return $var;
  287. }
  288.  
  289.  
  290. /**
  291. * If a number of chars is bigger than 100, the variable is split into peaces, as the API allows to convert text
  292. * which is up to 100 chars
  293. * @param - $text - text to split
  294. *
  295. */
  296. private function split($text, $i=0) {
  297.  
  298. $text_split = array();
  299. $text_words = explode(' ', $text);
  300. $text_split[$i] = NULL;
  301.  
  302. foreach ($text_words as $w) {
  303. $w = trim($w);
  304. $w = stripslashes($w);
  305. if (strlen($text_split[$i] . ' ' . strip_tags($w)) < 100) {
  306. $text_split[$i] = $text_split[$i] . ' ' . $w;
  307. if (preg_match('/[,.!?-]$/', $w)) { $i++; }
  308. } else {
  309. $text_split[++$i] = $w;
  310.  
  311. }
  312. }
  313. return $text_split;
  314. }
  315.  
  316.  
  317. /**
  318. * checking IF the same text was converted before, IF yes returning cached file
  319. *
  320. * @param - $fileName_hashed - hashed (md5) text
  321. *
  322. */
  323.  
  324. private function isCached($fileName_hashed) {
  325.  
  326. if (!isset($fileName_hashed) && $this->text != NULL) {
  327.  
  328. $fileName_hashed = $this->hash($this->text);
  329. }
  330.  
  331.  
  332. if (file_exists(ROOT_DIR.self::audioDir.$fileName_hashed. '.mp3')) {
  333. return true;
  334. }
  335. else { return false; }
  336.  
  337. }
  338.  
  339.  
  340. /**
  341. * converting split text strings into a mp3 file.
  342. *
  343. * @param - $line - split text string
  344. *
  345. */
  346. public function create($line) {
  347. if (!file_exists($this->makePathURL($this->fileName_hashed))) {
  348. $fetched = $this->fetch("http://www.ispeech.org/p/generic/getaudio?speed=0&action=convert&voice=".$this->lang."&text=".$line);
  349. $fetched = ( !empty($this->exclude_chars) || isset($this->exclude_chars) ) ? substr($fetched, 0, $this->exclude_chars) : $fetched;
  350. file_put_contents($this->makePathURL($this->fileName_hashed), $fetched) or die("cannot create mp3 file. Check folder permissions. - $this->fileName_hashed - $fetched");
  351. }
  352.  
  353. else {
  354. $fetched = $this->fetch("http://www.ispeech.org/p/generic/getaudio?speed=0&action=convert&voice=".$this->lang."&text=".$line);
  355. $fetched = ( !empty($this->exclude_chars) || isset($this->exclude_chars) ) ? substr($fetched, 0, $this->exclude_chars) : $fetched;
  356. file_put_contents($this->makePathURL($this->fileName_hashed), $fetched, FILE_APPEND);
  357. }
  358. }
  359.  
  360.  
  361. /**
  362. * Formatting URL of mp3
  363. *
  364. * @param - $hash - hashed (md5) text
  365. *
  366. */
  367. public function makeURL($hash) {
  368.  
  369. $location = BASE_URL . self::audioDir . $hash . '.mp3';
  370. return $location;
  371.  
  372. }
  373. public function makePathURL($hash) {
  374.  
  375. $location = ROOT_DIR . self::audioDir . $hash . '.mp3';
  376. return $location;
  377.  
  378. }
  379.  
  380. private function hash($hash) {
  381.  
  382. if ($hash) {
  383. $hash = $hash."_".$this->lang;
  384. $hashed = md5($hash);
  385. return $hashed;
  386. }
  387.  
  388. }
  389.  
  390. public function fetch($url) {
  391.  
  392. if ($this->fetcher == "curl") { $fetched = $this->curl($url); }
  393.  
  394. if ($this->fetcher == "fgc") { $fetched = $this->fgc($url); }
  395.  
  396. return $fetched;
  397. }
  398.  
  399.  
  400.  
  401. public function fgc($url) {
  402. $ret = @file_get_contents($url);
  403. return $ret;
  404. }
  405.  
  406. public function curl($url) {
  407. $curl = curl_init();
  408. $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
  409. curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
  410. curl_setopt($curl, CURLOPT_REFERER, "http://www.google.com/");
  411. curl_setopt($curl, CURLOPT_URL, $url);
  412. curl_setopt($curl, CURLOPT_HEADER, 0);
  413. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  414. $ret = curl_exec($curl);
  415. curl_close($curl);
  416.  
  417.  
  418. return $ret;
  419. }
  420. }
  421.  
  422.  
  423. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement