|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DayRev\Synthesizer\Provider; |
| 4 | + |
| 5 | +use Aws\Polly\PollyClient; |
| 6 | +use DayRev\Synthesizer\Provider; |
| 7 | + |
| 8 | +/** |
| 9 | + * Amazon Polly text to speech driver. |
| 10 | + * |
| 11 | + * @see http://docs.aws.amazon.com/polly/latest/dg/what-is.html |
| 12 | + */ |
| 13 | +class Amazon extends Provider |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Max text length that can be synthesized. |
| 17 | + * |
| 18 | + * @var int |
| 19 | + */ |
| 20 | + protected $max_text_length = 1500; |
| 21 | + |
| 22 | + /** |
| 23 | + * Builds the synthesized audio file URL for the text. |
| 24 | + * |
| 25 | + * @param string $text Text to synthesize. |
| 26 | + * |
| 27 | + * @return string |
| 28 | + */ |
| 29 | + public function buildAudioUrl(string $text): string |
| 30 | + { |
| 31 | + $this->params = array_merge($this->params, array( |
| 32 | + 'text' => $this->cleanText($text), |
| 33 | + )); |
| 34 | + |
| 35 | + return $this->getAudioUrl(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets the URL to the synthesized audio file. |
| 40 | + * |
| 41 | + * @return string |
| 42 | + */ |
| 43 | + protected function getAudioUrl(): string |
| 44 | + { |
| 45 | + $client = new PollyClient([ |
| 46 | + 'version' => 'latest', |
| 47 | + 'region' => $this->params['region'] ?? 'us-west-2', |
| 48 | + 'credentials' => [ |
| 49 | + 'key' => $this->params['key'], |
| 50 | + 'secret' => $this->params['secret'], |
| 51 | + ], |
| 52 | + ]); |
| 53 | + |
| 54 | + return $client->createSynthesizeSpeechPreSignedUrl([ |
| 55 | + 'OutputFormat' => $this->params['OutputFormat'] ?? 'mp3', |
| 56 | + 'SampleRate' => $this->params['SampleRate'] ?? '8000', |
| 57 | + 'Text' => $this->params['text'], |
| 58 | + 'TextType' => $this->params['TextType'] ?? 'text', |
| 59 | + 'VoiceId' => $this->params['VoiceId'] ?? 'Joanna', |
| 60 | + ]); |
| 61 | + } |
| 62 | +} |
0 commit comments