Skip to content

Commit a6ed86f

Browse files
authored
chore: upgrade texttospeech to new sample format (GoogleCloudPlatform#1641)
1 parent 73953b0 commit a6ed86f

File tree

8 files changed

+214
-206
lines changed

8 files changed

+214
-206
lines changed

texttospeech/src/list_voices.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,46 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/texttospeech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 1) {
28-
return print("Usage: php list_voices.php\n");
29-
}
24+
namespace Google\Cloud\Samples\TextToSpeech;
3025

3126
// [START tts_list_voices]
3227
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
3328

34-
// create client object
35-
$client = new TextToSpeechClient();
29+
function list_voices(): void
30+
{
31+
// create client object
32+
$client = new TextToSpeechClient();
3633

37-
// perform list voices request
38-
$response = $client->listVoices();
39-
$voices = $response->getVoices();
34+
// perform list voices request
35+
$response = $client->listVoices();
36+
$voices = $response->getVoices();
4037

41-
foreach ($voices as $voice) {
42-
// display the voice's name. example: tpc-vocoded
43-
printf('Name: %s' . PHP_EOL, $voice->getName());
38+
foreach ($voices as $voice) {
39+
// display the voice's name. example: tpc-vocoded
40+
printf('Name: %s' . PHP_EOL, $voice->getName());
4441

45-
// display the supported language codes for this voice. example: 'en-US'
46-
foreach ($voice->getLanguageCodes() as $languageCode) {
47-
printf('Supported language: %s' . PHP_EOL, $languageCode);
48-
}
42+
// display the supported language codes for this voice. example: 'en-US'
43+
foreach ($voice->getLanguageCodes() as $languageCode) {
44+
printf('Supported language: %s' . PHP_EOL, $languageCode);
45+
}
4946

50-
// SSML voice gender values from TextToSpeech\V1\SsmlVoiceGender
51-
$ssmlVoiceGender = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE',
52-
'NEUTRAL'];
47+
// SSML voice gender values from TextToSpeech\V1\SsmlVoiceGender
48+
$ssmlVoiceGender = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE',
49+
'NEUTRAL'];
5350

54-
// display the SSML voice gender
55-
$gender = $voice->getSsmlGender();
56-
printf('SSML voice gender: %s' . PHP_EOL, $ssmlVoiceGender[$gender]);
51+
// display the SSML voice gender
52+
$gender = $voice->getSsmlGender();
53+
printf('SSML voice gender: %s' . PHP_EOL, $ssmlVoiceGender[$gender]);
5754

58-
// display the natural hertz rate for this voice
59-
printf('Natural Sample Rate Hertz: %d' . PHP_EOL,
60-
$voice->getNaturalSampleRateHertz());
61-
}
55+
// display the natural hertz rate for this voice
56+
printf('Natural Sample Rate Hertz: %d' . PHP_EOL,
57+
$voice->getNaturalSampleRateHertz());
58+
}
6259

63-
$client->close();
60+
$client->close();
61+
}
6462
// [END tts_list_voices]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

texttospeech/src/synthesize_ssml.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/texttospeech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return print("Usage: php synthesize_ssml.php TEXT\n");
29-
}
30-
list($_, $ssml) = $argv;
24+
namespace Google\Cloud\Samples\TextToSpeech;
3125

3226
// [START tts_synthesize_ssml]
3327
use Google\Cloud\TextToSpeech\V1\AudioConfig;
@@ -37,29 +31,36 @@
3731
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
3832
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
3933

40-
/** Uncomment and populate these variables in your code */
41-
// $ssml = 'SSML to synthesize';
42-
43-
// create client object
44-
$client = new TextToSpeechClient();
34+
/**
35+
* @param string $ssml SSML to synthesize
36+
*/
37+
function synthesize_ssml(string $ssml): void
38+
{
39+
// create client object
40+
$client = new TextToSpeechClient();
4541

46-
$input_text = (new SynthesisInput())
47-
->setSsml($ssml);
42+
$input_text = (new SynthesisInput())
43+
->setSsml($ssml);
4844

49-
// note: the voice can also be specified by name
50-
// names of voices can be retrieved with $client->listVoices()
51-
$voice = (new VoiceSelectionParams())
52-
->setLanguageCode('en-US')
53-
->setSsmlGender(SsmlVoiceGender::FEMALE);
45+
// note: the voice can also be specified by name
46+
// names of voices can be retrieved with $client->listVoices()
47+
$voice = (new VoiceSelectionParams())
48+
->setLanguageCode('en-US')
49+
->setSsmlGender(SsmlVoiceGender::FEMALE);
5450

55-
$audioConfig = (new AudioConfig())
56-
->setAudioEncoding(AudioEncoding::MP3);
51+
$audioConfig = (new AudioConfig())
52+
->setAudioEncoding(AudioEncoding::MP3);
5753

58-
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
59-
$audioContent = $response->getAudioContent();
54+
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
55+
$audioContent = $response->getAudioContent();
6056

61-
file_put_contents('output.mp3', $audioContent);
62-
print('Audio content written to "output.mp3"' . PHP_EOL);
57+
file_put_contents('output.mp3', $audioContent);
58+
print('Audio content written to "output.mp3"' . PHP_EOL);
6359

64-
$client->close();
60+
$client->close();
61+
}
6562
// [END tts_synthesize_ssml]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

texttospeech/src/synthesize_ssml_file.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/texttospeech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return print("Usage: php synthesize_ssml_file.php FILE\n");
29-
}
30-
list($_, $path) = $argv;
24+
namespace Google\Cloud\Samples\TextToSpeech;
3125

3226
// [START tts_synthesize_ssml_file]
3327
use Google\Cloud\TextToSpeech\V1\AudioConfig;
@@ -37,31 +31,38 @@
3731
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
3832
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
3933

40-
/** Uncomment and populate these variables in your code */
41-
// $path = 'Path to file to synthesize';
42-
43-
// create client object
44-
$client = new TextToSpeechClient();
34+
/**
35+
* @param string $path Path to file to synthesize
36+
*/
37+
function synthesize_ssml_file(string $path): void
38+
{
39+
// create client object
40+
$client = new TextToSpeechClient();
4541

46-
// get ssml from file
47-
$ssml = file_get_contents($path);
48-
$input_text = (new SynthesisInput())
49-
->setSsml($ssml);
42+
// get ssml from file
43+
$ssml = file_get_contents($path);
44+
$input_text = (new SynthesisInput())
45+
->setSsml($ssml);
5046

51-
// note: the voice can also be specified by name
52-
// names of voices can be retrieved with $client->listVoices()
53-
$voice = (new VoiceSelectionParams())
54-
->setLanguageCode('en-US')
55-
->setSsmlGender(SsmlVoiceGender::FEMALE);
47+
// note: the voice can also be specified by name
48+
// names of voices can be retrieved with $client->listVoices()
49+
$voice = (new VoiceSelectionParams())
50+
->setLanguageCode('en-US')
51+
->setSsmlGender(SsmlVoiceGender::FEMALE);
5652

57-
$audioConfig = (new AudioConfig())
58-
->setAudioEncoding(AudioEncoding::MP3);
53+
$audioConfig = (new AudioConfig())
54+
->setAudioEncoding(AudioEncoding::MP3);
5955

60-
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
61-
$audioContent = $response->getAudioContent();
56+
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
57+
$audioContent = $response->getAudioContent();
6258

63-
file_put_contents('output.mp3', $audioContent);
64-
print('Audio content written to "output.mp3"' . PHP_EOL);
59+
file_put_contents('output.mp3', $audioContent);
60+
print('Audio content written to "output.mp3"' . PHP_EOL);
6561

66-
$client->close();
62+
$client->close();
63+
}
6764
// [END tts_synthesize_ssml_file]
65+
66+
// The following 2 lines are only needed to run the samples
67+
require_once __DIR__ . '/../../testing/sample_helpers.php';
68+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

texttospeech/src/synthesize_text.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/texttospeech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return print("Usage: php synthesize_text.php TEXT\n");
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\TextToSpeech;
3125

3226
// [START tts_synthesize_text]
3327
use Google\Cloud\TextToSpeech\V1\AudioConfig;
@@ -37,29 +31,36 @@
3731
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
3832
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
3933

40-
/** Uncomment and populate these variables in your code */
41-
// $text = 'Text to synthesize';
42-
43-
// create client object
44-
$client = new TextToSpeechClient();
34+
/**
35+
* @param string $text Text to synthesize
36+
*/
37+
function synthesize_text(string $text): void
38+
{
39+
// create client object
40+
$client = new TextToSpeechClient();
4541

46-
$input_text = (new SynthesisInput())
47-
->setText($text);
42+
$input_text = (new SynthesisInput())
43+
->setText($text);
4844

49-
// note: the voice can also be specified by name
50-
// names of voices can be retrieved with $client->listVoices()
51-
$voice = (new VoiceSelectionParams())
52-
->setLanguageCode('en-US')
53-
->setSsmlGender(SsmlVoiceGender::FEMALE);
45+
// note: the voice can also be specified by name
46+
// names of voices can be retrieved with $client->listVoices()
47+
$voice = (new VoiceSelectionParams())
48+
->setLanguageCode('en-US')
49+
->setSsmlGender(SsmlVoiceGender::FEMALE);
5450

55-
$audioConfig = (new AudioConfig())
56-
->setAudioEncoding(AudioEncoding::MP3);
51+
$audioConfig = (new AudioConfig())
52+
->setAudioEncoding(AudioEncoding::MP3);
5753

58-
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
59-
$audioContent = $response->getAudioContent();
54+
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
55+
$audioContent = $response->getAudioContent();
6056

61-
file_put_contents('output.mp3', $audioContent);
62-
print('Audio content written to "output.mp3"' . PHP_EOL);
57+
file_put_contents('output.mp3', $audioContent);
58+
print('Audio content written to "output.mp3"' . PHP_EOL);
6359

64-
$client->close();
60+
$client->close();
61+
}
6562
// [END tts_synthesize_text]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

texttospeech/src/synthesize_text_effects_profile.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/texttospeech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 3) {
28-
return print("Usage: php synthesize_text_effects_profile.php TEXT EFFECTS_PROFILE_ID\n");
29-
}
30-
list($_, $text, $effectsProfileId) = $argv;
24+
namespace Google\Cloud\Samples\TextToSpeech;
3125

3226
// [START tts_synthesize_text_audio_profile]
3327
use Google\Cloud\TextToSpeech\V1\AudioConfig;
@@ -37,32 +31,39 @@
3731
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
3832
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
3933

40-
/** Uncomment and populate these variables in your code */
41-
// $text = 'Text to synthesize';
42-
// $effectsProfileId = 'Audio Profile ID';
43-
44-
// create client object
45-
$client = new TextToSpeechClient();
34+
/**
35+
* @param string $text Text to synthesize
36+
* @param string $effectsProfileId Audio Profile ID
37+
*/
38+
function synthesize_text_effects_profile(string $text, string $effectsProfileId): void
39+
{
40+
// create client object
41+
$client = new TextToSpeechClient();
4642

47-
$inputText = (new SynthesisInput())
48-
->setText($text);
43+
$inputText = (new SynthesisInput())
44+
->setText($text);
4945

50-
// note: the voice can also be specified by name
51-
// names of voices can be retrieved with $client->listVoices()
52-
$voice = (new VoiceSelectionParams())
53-
->setLanguageCode('en-US')
54-
->setSsmlGender(SsmlVoiceGender::FEMALE);
46+
// note: the voice can also be specified by name
47+
// names of voices can be retrieved with $client->listVoices()
48+
$voice = (new VoiceSelectionParams())
49+
->setLanguageCode('en-US')
50+
->setSsmlGender(SsmlVoiceGender::FEMALE);
5551

56-
// define effects profile id.
57-
$audioConfig = (new AudioConfig())
58-
->setAudioEncoding(AudioEncoding::MP3)
59-
->setEffectsProfileId(array($effectsProfileId));
52+
// define effects profile id.
53+
$audioConfig = (new AudioConfig())
54+
->setAudioEncoding(AudioEncoding::MP3)
55+
->setEffectsProfileId(array($effectsProfileId));
6056

61-
$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
62-
$audioContent = $response->getAudioContent();
57+
$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
58+
$audioContent = $response->getAudioContent();
6359

64-
file_put_contents('output.mp3', $audioContent);
65-
print('Audio content written to "output.mp3"' . PHP_EOL);
60+
file_put_contents('output.mp3', $audioContent);
61+
print('Audio content written to "output.mp3"' . PHP_EOL);
6662

67-
$client->close();
63+
$client->close();
64+
}
6865
// [END tts_synthesize_text_audio_profile]
66+
67+
// The following 2 lines are only needed to run the samples
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)