1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cion\TextToSpeech\Converters; |
4
|
|
|
|
5
|
|
|
use Aws\Polly\PollyClient; |
6
|
|
|
use Aws\Result; |
7
|
|
|
use Cion\TextToSpeech\Contracts\Converter; |
8
|
|
|
use Cion\TextToSpeech\Traits\Sourceable; |
9
|
|
|
use Cion\TextToSpeech\Traits\Storable; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
|
12
|
|
|
class PollyConverter implements Converter |
13
|
|
|
{ |
14
|
|
|
use Storable, Sourceable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Client instance of Polly. |
18
|
|
|
* |
19
|
|
|
* @var \Aws\Polly\PollyClient |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Construct converter. |
25
|
|
|
* |
26
|
|
|
* @param PollyClient $client |
27
|
|
|
*/ |
28
|
|
|
public function __construct(PollyClient $client) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the Polly Client. |
35
|
|
|
* |
36
|
|
|
* @return \Aws\Polly\PollyClient |
37
|
|
|
*/ |
38
|
|
|
public function getClient(): PollyClient |
39
|
|
|
{ |
40
|
|
|
return $this->client; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Converts the text to speech. |
45
|
|
|
* |
46
|
|
|
* @param string $data |
47
|
|
|
* @param array $options |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function convert(string $data, array $options = null) |
51
|
|
|
{ |
52
|
|
|
$text = $this->getTextFromSource($data); |
53
|
|
|
|
54
|
|
|
if ($this->isTextAboveLimit($text)) |
55
|
|
|
{ |
56
|
|
|
$text = $this->getChunkText($text); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$result = $this->synthesizeSpeech($text, $options); |
60
|
|
|
|
61
|
|
|
if ($result instanceof Result) |
62
|
|
|
{ |
63
|
|
|
// Store audio file to disk |
64
|
|
|
return $this->store( |
65
|
|
|
$this->getTextFromSource($data), |
66
|
|
|
$this->getResultContent($result) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->store( |
71
|
|
|
$this->getTextFromSource($data), |
72
|
|
|
$this->mergeOutputs($result) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Request to Amazon Polly to synthesize speech. |
79
|
|
|
* |
80
|
|
|
* @param string|array $text |
81
|
|
|
* @param array $options |
82
|
|
|
* @return array|\Aws\Result; |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
protected function synthesizeSpeech($text, array $options = null) |
85
|
|
|
{ |
86
|
|
|
if (is_string($text)) |
87
|
|
|
{ |
88
|
|
|
return $this->client->synthesizeSpeech([ |
89
|
|
|
'VoiceId' => $this->voice($options), |
|
|
|
|
90
|
|
|
'OutputFormat' => $this->format($options), |
|
|
|
|
91
|
|
|
'TextType' => 'ssml', |
92
|
|
|
'Text' => '<speak>' . $text . '</speak>', |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$results = []; |
97
|
|
|
|
98
|
|
|
foreach ($text as $item) |
99
|
|
|
{ |
100
|
|
|
$result = $this->client->synthesizeSpeech([ |
101
|
|
|
'VoiceId' => $this->voice($options), |
|
|
|
|
102
|
|
|
'OutputFormat' => $this->format($options), |
|
|
|
|
103
|
|
|
'Text' => $item, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
array_push($results, $result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $results; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Merges the output from amazon polly |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
protected function mergeOutputs(array $results) |
118
|
|
|
{ |
119
|
|
|
$mergedResult = null; |
120
|
|
|
foreach ($results as $result) |
121
|
|
|
{ |
122
|
|
|
$mergedResult .= $this->getResultContent($result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $mergedResult; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks the length of the text if more than 3000 |
130
|
|
|
* |
131
|
|
|
* @param string $text |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
protected function isTextAboveLimit(string $text) |
135
|
|
|
{ |
136
|
|
|
return strlen($text) > 2000; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Chunk the given text into array |
141
|
|
|
* |
142
|
|
|
* @param string $text |
143
|
|
|
* @param int $size |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function getChunkText(string $text, int $size = 2000) |
147
|
|
|
{ |
148
|
|
|
return explode( "\n", wordwrap($text, $size)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the text to speech voice ID. |
153
|
|
|
* |
154
|
|
|
* @param array $options |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
protected function voice($options) |
158
|
|
|
{ |
159
|
|
|
$default = config('tts.services.polly.voice_id', 'Amy'); |
160
|
|
|
|
161
|
|
|
return Arr::get($options, 'voice', $default); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the audio format. |
166
|
|
|
* |
167
|
|
|
* @param array $options |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function format($options) |
171
|
|
|
{ |
172
|
|
|
$default = config('tts.output_format', 'mp3'); |
173
|
|
|
|
174
|
|
|
return Arr::get($options, 'format', $default); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the content of the result from AWS Polly. |
179
|
|
|
* |
180
|
|
|
* @param mixed $result |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
protected function getResultContent($result) |
184
|
|
|
{ |
185
|
|
|
return $result->get('AudioStream')->getContents(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.