⋯ 5 lines Show the 5 lines above this hunk @@ -6,6 +6,7 @@ use Aws\Polly\PollyClient; 6 6 use Aws \ Result ; 7 7 use Cion \ TextToSpeech \ Contracts \ Converter ; 8 8 use Cion \ TextToSpeech \ Traits \ HasLanguage ; 9 + use Cion \ TextToSpeech \ Traits \ HasSpeechMarks ; 9 10 use Cion \ TextToSpeech \ Traits \ Sourceable ; 10 11 use Cion \ TextToSpeech \ Traits \ SSMLable ; 11 12 use Cion \ TextToSpeech \ Traits \ Storable ; ⋯ 1 line Show the 1 line above this hunk @@ -13,7 +14,7 @@ use Illuminate\Support\Arr; 13 14 14 15 class PollyConverter implements Converter 15 16 { 16 - use Storable , Sourceable , HasLanguage , SSMLable ; 17 + use Storable , Sourceable , HasLanguage , SSMLable , HasSpeechMarks ; 17 18 18 19 19 20 ⋯ 27 lines Show the 27 lines above this hunk @@ -47,7 +48,7 @@ class PollyConverter implements Converter 47 48 48 49 49 50 50 - 51 + 51 52 52 53 public function convert ( string $data , array $options = null ) 53 54 { ⋯ 5 lines Show the 5 lines above this hunk @@ -59,6 +60,10 @@ class PollyConverter implements Converter 59 60 60 61 $result = $this - > synthesizeSpeech ( $text , $options ) ; 61 62 63 + if ( $this - > hasSpeechMarks ( ) ) { 64 + return $this - > formatToArray ( $this - > getResultContent ( $result ) ) ; 65 + } 66 + 62 67 if ( $result instanceof Result ) { 63 68 64 69 return $this - > store ( ⋯ 17 lines Show the 17 lines above this hunk @@ -82,26 +87,26 @@ class PollyConverter implements Converter 82 87 * / 83 88 protected function synthesizeSpeech ( $text , array $options = null ) 84 89 { 90 + $arguments = [ 91 + 'LanguageCode' = > $this - > getLanguage ( ) , 92 + 'VoiceId' = > $this - > voice ( $options ) , 93 + 'OutputFormat' = > $this - > format ( $options ) , 94 + 'TextType' = > $this - > textType ( ) , 95 + 'SpeechMarkTypes' = > $this - > speechMarks , 96 + ] ; 97 + 85 98 if ( is_string ( $text ) ) { 86 - return $this - > client - > synthesizeSpeech ( [ 87 - ' LanguageCode ' = > $this - > getLanguage ( ) , 88 - 'VoiceId' = > $this - > voice ( $options ) , 89 - 'OutputFormat' = > $this - > format ( $options ) , 90 - 'TextType' = > $this - > textType ( ) , 91 - 'Text' = > $text , 92 - ] ) ; 99 + return $this - > client - > synthesizeSpeech ( array_merge ( $arguments , [ 100 + ' Text ' = > $text , 101 + ] ) ) ; 93 102 } 94 103 95 104 $results = [ ] ; 96 105 97 106 foreach ( $text as $textItem ) { 98 - $result = $this - > client - > synthesizeSpeech ( [ 99 - ' LanguageCode ' = > $this - > getLanguage ( ) , 100 - 'VoiceId' = > $this - > voice ( $options ) , 101 - 'OutputFormat' = > $this - > format ( $options ) , 102 - 'TextType' = > $this - > textType ( ) , 103 - 'Text' = > $textItem , 104 - ] ) ; 107 + $result = $this - > client - > synthesizeSpeech ( array_merge ( $arguments , [ 108 + ' Text ' = > $textItem , 109 + ] ) ) ; 105 110 106 111 array_push ( $results , $result ) ; 107 112 } ⋯ 82 lines Show the 82 lines above this hunk @@ -190,6 +195,10 @@ class PollyConverter implements Converter 190 195 * / 191 196 protected function format ( $options ) 192 197 { 198 + if ( $this - > hasSpeechMarks ( ) ) { 199 + return 'json' ; 200 + } 201 + 193 202 $default = config ( 'tts.output_format' , 'mp3' ) ; 194 203 195 204 return Arr : : get ( $options , 'format' , $default ) ; ⋯ 9 lines Show the 9 lines above this hunk @@ -205,4 +214,31 @@ class PollyConverter implements Converter 205 214 { 206 215 return $result - > get ( 'AudioStream' ) - > getContents ( ) ; 207 216 } 217 + 218 + 219 + 220 + 221 + 222 + 223 + protected function hasSpeechMarks ( ) 224 + { 225 + return ! empty ( $this - > speechMarks ) ; 226 + } 227 + 228 + 229 + 230 + 231 + 232 + 233 + 234 + protected function formatToArray ( $json ) 235 + { 236 + $jsons = explode ( PHP_EOL , $json ) ; 237 + 238 + array_pop ( $jsons ) ; 239 + 240 + return collect ( $jsons ) - > map ( function ( $json ) { 241 + return json_decode ( $json , true ) ; 242 + } ) - > toArray ( ) ; 243 + } 208 244 }