1
|
|
|
<?php |
2
|
|
|
namespace TTS; |
3
|
|
|
|
4
|
|
|
use TTS\Driver\Google; |
5
|
|
|
use TTS\Exceptions\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TextToSpeech |
9
|
|
|
* @package TTS |
10
|
|
|
*/ |
11
|
|
|
class TextToSpeech |
12
|
|
|
{ |
13
|
|
|
private $cacheDir = null; |
14
|
|
|
private $driver = null; |
15
|
|
|
private $voice = 'famale'; |
|
|
|
|
16
|
|
|
|
17
|
|
|
private $lines = array(); |
18
|
|
|
private $files = array(); |
19
|
|
|
|
20
|
|
|
protected $text = ''; |
21
|
|
|
|
22
|
|
|
public function __construct($options = null) |
23
|
|
|
{ |
24
|
|
|
if ($options['cacheDir']) { |
25
|
|
|
$this->cacheDir = $options['cacheDir']; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (isset($options['driver'])) { |
29
|
|
|
$adapterClass = __NAMESPACE__. '\\Driver\\' . $options['driver']; |
30
|
|
|
} else { |
31
|
|
|
$adapterClass = Google::class; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->driver = new $adapterClass('ru_RU', '0.5'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function setText($text) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$text = trim(str_replace(["\r", "\n", '"', "'", '«', '»'], ' ', $text)); |
40
|
|
|
$text = preg_replace('/([ ]{2,})/', ' ', $text); |
41
|
|
|
$text = mb_strtolower($text); |
42
|
|
|
|
43
|
|
|
$this->text = $text; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function clear() |
48
|
|
|
{ |
49
|
|
|
$this->lines = array(); |
50
|
|
|
$this->files = array(); |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function process() |
55
|
|
|
{ |
56
|
|
|
$this->textToFile($this->text); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function textToFile($text) |
62
|
|
|
{ |
63
|
|
|
if (!$text) { |
64
|
|
|
return array(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$cacheDir = $this->getCacheDir(); |
68
|
|
|
if (!is_dir($cacheDir) || !is_writable($cacheDir)) { |
69
|
|
|
throw new Exception('Can not write to ' . $cacheDir); |
70
|
|
|
} |
71
|
|
|
$cacheDir = dirname($this->prepareFileName($text)); |
72
|
|
|
if (!is_dir($cacheDir)) { |
73
|
|
|
mkdir($cacheDir, 0755, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$fileName = $this->prepareFileName($text); |
77
|
|
|
if (!is_file($fileName)) { |
78
|
|
|
$content = $this->driver->make($text, $fileName); |
79
|
|
|
file_put_contents($fileName, $content); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->files[] = $fileName; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getTempFile() |
89
|
|
|
{ |
90
|
|
|
return tempnam(sys_get_temp_dir(), 'tts'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getFiles() |
94
|
|
|
{ |
95
|
|
|
return $this->files; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getCacheDir() |
99
|
|
|
{ |
100
|
|
|
return $this->cacheDir |
101
|
|
|
. '/' . strtolower($this->driver->getName()) |
102
|
|
|
. '/' .$this->driver->getVoice() . '/'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function prepareFileName($text) |
106
|
|
|
{ |
107
|
|
|
$fileName = $this->prepareUiq($text); |
108
|
|
|
return $this->getCacheDir() . '/' . substr($fileName, 0, 2) . '/' . substr( |
109
|
|
|
$fileName, |
110
|
|
|
2, |
111
|
|
|
2 |
112
|
|
|
) . '/' . $fileName . '.mp3'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function prepareUiq($text) |
116
|
|
|
{ |
117
|
|
|
return sha1($text); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.