|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AudioManager\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use AudioManager\Adapter\Options\OptionsInterface; |
|
6
|
|
|
use Aws\Polly\PollyClient; |
|
7
|
|
|
use AudioManager\Adapter\Options\Polly as Options; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Polly |
|
11
|
|
|
* @package AudioManager\Adapter |
|
12
|
|
|
* @method Options getOptions() |
|
13
|
|
|
*/ |
|
14
|
|
|
class Polly extends AbstractAdapter implements AdapterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Get audio for text from adapter |
|
18
|
|
|
* @param string $text |
|
19
|
|
|
* @return mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
public function read($text) |
|
22
|
|
|
{ |
|
23
|
|
|
$initializeOptions = $this->getOptions()->getInitializeOptions(); |
|
24
|
|
|
$pollyClient = new PollyClient([ |
|
25
|
|
|
'version' => $initializeOptions->getVersion(), |
|
26
|
|
|
'region' => $initializeOptions->getRegion(), |
|
27
|
|
|
'credentials' => [ |
|
28
|
|
|
'key' => $initializeOptions->getCredentials()->getKey(), |
|
29
|
|
|
'secret' => $initializeOptions->getCredentials()->getSecret() |
|
30
|
|
|
] |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
|
|
$synthesizeOptions = [ |
|
34
|
|
|
'OutputFormat' => $this->getOptions()->getOutputFormat(), |
|
35
|
|
|
'Text' => $text, |
|
36
|
|
|
'TextType' => $this->getOptions()->getTextType(), |
|
37
|
|
|
'VoiceId' => $this->getOptions()->getVoiceId(), |
|
38
|
|
|
'SampleRate' => $this->getOptions()->getSampleRate(), |
|
39
|
|
|
]; |
|
40
|
|
|
if (!empty($this->getOptions()->getLexiconNames())) { |
|
41
|
|
|
$synthesizeOptions['LexiconNames'] = $this->getOptions()->getLexiconNames(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$awsResult = $pollyClient->synthesizeSpeech($synthesizeOptions); |
|
45
|
|
|
$this->setHeaders($awsResult->get('@metadata')); |
|
46
|
|
|
$stream = $awsResult->get('AudioStream'); |
|
47
|
|
|
return $stream->getContents(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Init options |
|
52
|
|
|
* @return OptionsInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function initOptions() |
|
55
|
|
|
{ |
|
56
|
|
|
return new Options(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getHeaders() |
|
60
|
|
|
{ |
|
61
|
|
|
$headers = parent::getHeaders(); |
|
62
|
|
|
return [ |
|
63
|
|
|
'http_code' => isset($headers['statusCode']) ? $headers['statusCode']: 500, |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|