I am trying to get Polly to read something for me, using PHP.
I have created a new project, installed Amazon composer require aws/aws-sdk-php
and then created a file with code from SDK documentation example and modified a few minor things such as changing credential from default to value, var_dump to var_export and finally saved the content of the stream to file
<?php require 'vendor/autoload.php'; use AwsExceptionAwsException; use AwsPollyPollyClient; use AwsCredentialsCredentials; // Create a PollyClient $client = new AwsPollyPollyClient([ //'profile' => 'default', 'credentials' => new Credentials('XXXXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'), 'version' => '2016-06-10', 'region' => 'us-east-2' ]); try { $result = $client->synthesizeSpeech([ 'Text' => 'Hello', 'OutputFormat' => 'json', //json|mp3|ogg_vorbis|pcm 'VoiceId' => 'Joanna', ]); var_export($result); $data = $result->get('AudioStream')->getContents(); echo "nn"; var_export($data); $file = fopen('test.txt','w+'); fwrite($file,$data); fclose($file); } catch (AwsException $e) { echo $e->getMessage() . "n"; }
The result I’m getting is following
AwsResult::__set_state(array( 'data' => array ( 'AudioStream' => GuzzleHttpPsr7Stream::__set_state(array( 'stream' => NULL, 'size' => NULL, 'seekable' => true, 'readable' => true, 'writable' => true, 'uri' => 'php://temp', 'customMetadata' => array (), )), 'ContentType' => 'application/x-json-stream', 'RequestCharacters' => '5', '@metadata' => array ( 'statusCode' => 200, 'effectiveUri' => 'https://polly.us-east-2.amazonaws.com/v1/speech', 'headers' => array ( 'x-amzn-requestid' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'x-amzn-requestcharacters' => '5', 'content-type' => 'application/x-json-stream', 'transfer-encoding' => 'chunked', 'date' => 'Sat, 18 Sep 2021 05:11:20 GMT', ), 'transferStats' => array ( 'http' => array ( 0 => array (), ), ), ), ), 'monitoringEvents' => array (), )) ''
As you can see the size of the AudioStream is null (nothing in it) and also the created file is also empty since there is nothing in the stream to read.
If I change a credential to an invalid string, I get errors, and with the valid credential, the status code is 200, which makes me believe that my request is successful. I changed voiceId to any other valid or invalid id and even changed the region with others with valid values getting status 200 and with invalid ones getting error messages, but I’m still not getting anything out of polly, it doesn’t feel like talking!
Note: When I run $arr_voices = $polly->describeVoices();
, I can read list of the voices without error.
Note: I had the same issue with .NET SDK too, which makes me think either there is something wrong with my request or some error message is missing from API.
Question
What I’m doing wrong?
Advertisement
Answer
You’re not doing anything wrong, but it only outputs JSON if you’re looking for speech marks. Try switching to an audio output format like MP3 as shown below.
$result = $client->synthesizeSpeech([ 'Text' => 'Hello', 'OutputFormat' => 'mp3', //json|mp3|ogg_vorbis|pcm 'VoiceId' => 'Joanna', ]);
If you’re looking for speech marks- metadata on the speech that will be synthesized- you need to specify SpeechMarkTypes as shown here https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-polly-2016-06-10.html#synthesizespeech