How to convert text to speech by Google API's
composer.json
1 2 3 4 5 6 7 8 9 |
{ "require": { "google/cloud-text-to-speech": "^1.3" } } |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
putenv("GOOGLE_APPLICATION_CREDENTIALS=/Applications/XAMPP/xamppfiles/htdocs/text-to-speech/credential.json"); require_once 'vendor/autoload.php'; use Google\Cloud\TextToSpeech\V1\AudioConfig; use Google\Cloud\TextToSpeech\V1\AudioEncoding; use Google\Cloud\TextToSpeech\V1\SynthesisInput; use Google\Cloud\TextToSpeech\V1\TextToSpeechClient; use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams; try { $textToSpeechClient = new TextToSpeechClient(); $input = new SynthesisInput(); $input->setText('<break time="3s" />Japan\'s national soccer <break time="3s" /> team won against Colombia!'); $voice = new VoiceSelectionParams(); $voice->setLanguageCode('en-US'); // optional $voice->setName('en-US-Standard-C'); $audioConfig = new AudioConfig(); $audioConfig->setAudioEncoding(AudioEncoding::MP3); $resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig); $resultData = $resp->getAudioContent(); file_put_contents('output.mp3', $resultData); echo 'okay'; // header('Content-length: ' . strlen($resultData)); // header('Content-Disposition: attachment; filename="text-to-speech.mp3"'); // header('X-Pad: avoid browser bug'); // header('Cache-Control: no-cache'); // echo $resultData; $textToSpeechClient->close(); } catch (Exception $e) { echo $e->getMessage(); } |
Open terminal and PHP <filepath of index.php>
More Stories
How to set EC2 Ubuntu default password
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu