Skip to content

Commit 5da12f8

Browse files
gogascabshaffer
authored andcommitted
Add support for TTS Audio Profiles (GoogleCloudPlatform#850)
1 parent 5dfa97f commit 5da12f8

File tree

7 files changed

+195
-6
lines changed

7 files changed

+195
-6
lines changed

texttospeech/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,26 @@ Usage:
4141
Examples:
4242
php texttospeech.php synthesize-text -h
4343
php texttospeech.php synthesize-ssml -h
44+
php texttospeech.php synthesize-text-audio-profile -h
4445
php texttospeech.php synthesize-text "Hello there."
4546
php texttospeech.php synthesize-ssml "Hello there."
47+
php texttospeech.php synthesize-text-effects-profile "Hello there." "handset-class-device"
4648
```
4749

4850
### Synthesize file
4951
```
5052
Usage:
5153
php texttospeech.php synthesize-text-file
5254
php texttospeech.php synthesize-ssml-file
53-
55+
php texttospeech.php synthesize-text-effects-profile-file
56+
5457
Examples:
5558
php texttospeech.php synthesize-text-file -h
5659
php texttospeech.php synthesize-ssml-file -h
60+
php texttospeech.php synthesize-text-audio-profile-file -h
5761
php texttospeech.php synthesize-text-file resources/hello.txt
5862
php texttospeech.php synthesize-ssml-file resources/hello.ssml
63+
php texttospeech.php synthesize-text-effects-profile-file resources/hello.txt "handset-class-device"
5964
```
6065

6166
## The client library

texttospeech/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-text-to-speech": "^0.1",
3+
"google/cloud-text-to-speech": "^0.2.1",
44
"symfony/console": "^3.0"
55
},
66
"require-dev": {
@@ -12,7 +12,9 @@
1212
"src/synthesize_ssml.php",
1313
"src/synthesize_ssml_file.php",
1414
"src/synthesize_text.php",
15-
"src/synthesize_text_file.php"
15+
"src/synthesize_text_file.php",
16+
"src/synthesize_text_effects_profile.php",
17+
"src/synthesize_text_effects_profile_file.php"
1618
]
1719
}
1820
}

texttospeech/quickstart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
$client = new TextToSpeechClient();
3232

3333
// sets text to be synthesised
34-
$synthesis_input = (new SynthesisInput())
34+
$synthesisInputText = (new SynthesisInput())
3535
->setText('Hello, world!');
3636

3737
// build the voice request, select the language code ("en-US") and the ssml
@@ -40,13 +40,17 @@
4040
->setLanguageCode('en-US')
4141
->setSsmlGender(SsmlVoiceGender::FEMALE);
4242

43+
// Effects profile
44+
$effectsProfileId = "telephony-class-application";
45+
4346
// select the type of audio file you want returned
4447
$audioConfig = (new AudioConfig())
45-
->setAudioEncoding(AudioEncoding::MP3);
48+
->setAudioEncoding(AudioEncoding::MP3)
49+
->setEffectsProfileId(array($effectsProfileId));
4650

4751
// perform text-to-speech request on the text input with selected voice
4852
// parameters and audio file type
49-
$response = $client->synthesizeSpeech($synthesis_input, $voice, $audioConfig);
53+
$response = $client->synthesizeSpeech($synthesisInputText, $voice, $audioConfig);
5054
$audioContent = $response->getAudioContent();
5155

5256
// the response's audioContent is binary
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\TextToSpeech;
19+
20+
// [START tts_synthesize_text_audio_profile]
21+
use Google\Cloud\TextToSpeech\V1\AudioConfig;
22+
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
23+
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
24+
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
25+
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
26+
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
27+
28+
function synthesize_text_effects_profile($text, $effectsProfileId)
29+
{
30+
// create client object
31+
$client = new TextToSpeechClient();
32+
33+
$inputText = (new SynthesisInput())
34+
->setText($text);
35+
36+
// note: the voice can also be specified by name
37+
// names of voices can be retrieved with $client->listVoices()
38+
$voice = (new VoiceSelectionParams())
39+
->setLanguageCode('en-US')
40+
->setSsmlGender(SsmlVoiceGender::FEMALE);
41+
42+
// define effects profile id.
43+
$audioConfig = (new AudioConfig())
44+
->setAudioEncoding(AudioEncoding::MP3)
45+
->setEffectsProfileId(array($effectsProfileId));
46+
47+
$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
48+
$audioContent = $response->getAudioContent();
49+
50+
file_put_contents('output.mp3', $audioContent);
51+
print('Audio content written to "output.mp3"' . PHP_EOL);
52+
53+
$client->close();
54+
}
55+
// [END tts_synthesize_text_audio_profile]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\TextToSpeech;
19+
20+
// [START tts_synthesize_text_audio_profile_file]
21+
use Google\Cloud\TextToSpeech\V1\AudioConfig;
22+
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
23+
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
24+
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
25+
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
26+
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
27+
28+
function synthesize_text_effects_profile_file($path, $effectsProfileId)
29+
{
30+
// create client object
31+
$client = new TextToSpeechClient();
32+
33+
// get text from file
34+
$text = file_get_contents($path);
35+
$inputText = (new SynthesisInput())
36+
->setText($text);
37+
38+
// note: the voice can also be specified by name
39+
// names of voices can be retrieved with $client->listVoices()
40+
$voice = (new VoiceSelectionParams())
41+
->setLanguageCode('en-US')
42+
->setSsmlGender(SsmlVoiceGender::FEMALE);
43+
44+
$audioConfig = (new AudioConfig())
45+
->setAudioEncoding(AudioEncoding::MP3)
46+
->setEffectsProfileId(array($effectsProfileId));
47+
48+
$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
49+
$audioContent = $response->getAudioContent();
50+
51+
file_put_contents('output.mp3', $audioContent);
52+
print('Audio content written to "output.mp3"' . PHP_EOL);
53+
54+
$client->close();
55+
}
56+
// [END tts_synthesize_text_audio_profile_file]

texttospeech/test/textToSpeechTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public function testSynthesizeText()
5454
$this->assertGreaterThan(0,filesize('output.mp3'));
5555
unlink('output.mp3');
5656
}
57+
public function testSynthesizeTextEffectsProfile()
58+
{
59+
$output = $this->runCommand('synthesize-text-effects-profile', [
60+
'text' => 'hello there',
61+
'effects_profile_id' => 'telephony-class-application'
62+
]);
63+
$this->assertContains('Audio content written to', $output);
64+
$this->assertGreaterThan(0,filesize('output.mp3'));
65+
unlink('output.mp3');
66+
}
5767
public function testSynthesizeSsmlFile()
5868
{
5969
$path = __DIR__ . '/../resources/hello.ssml';
@@ -74,6 +84,17 @@ public function testSynthesizeTextFile()
7484
$this->assertGreaterThan(0,filesize('output.mp3'));
7585
unlink('output.mp3');
7686
}
87+
public function testSynthesizeTextEffectsProfileFile()
88+
{
89+
$path = __DIR__ . '/../resources/hello.txt';
90+
$output = $this->runCommand('synthesize-text-effects-profile-file', [
91+
'path' => $path,
92+
'effects_profile_id' => 'telephony-class-application'
93+
]);
94+
$this->assertContains('Audio content written to', $output);
95+
$this->assertGreaterThan(0,filesize('output.mp3'));
96+
unlink('output.mp3');
97+
}
7798
private function runCommand($commandName, array $args = [])
7899
{
79100
$application = require __DIR__ . '/../texttospeech.php';

texttospeech/texttospeech.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,26 @@
2828
$inputDefinition = new InputDefinition([
2929
new InputArgument('text', InputArgument::REQUIRED,
3030
'Text/SSML to synthesize.')
31+
32+
]);
33+
34+
$inputDefinitionEffectsProfile = new InputDefinition([
35+
new InputArgument('text', InputArgument::REQUIRED,
36+
'Text/SSML to synthesize.'),
37+
new InputArgument('effects_profile_id', InputArgument::REQUIRED,
38+
'Audio Profile.')
3139
]);
3240

3341
$inputDefinitionFile = new InputDefinition([
3442
new InputArgument('path', InputArgument::REQUIRED, 'File to synthesize.')
3543
]);
3644

45+
$inputDefinitionEffectsProfileFile = new InputDefinition([
46+
new InputArgument('path', InputArgument::REQUIRED, 'File to synthesize.'),
47+
new InputArgument('effects_profile_id', InputArgument::REQUIRED,
48+
'Audio Profile.')
49+
]);
50+
3751

3852
$application->add(new Command('list-voices'))
3953
->setDescription('List the available voices')
@@ -78,6 +92,22 @@
7892
})
7993
);
8094

95+
$application->add((new Command('synthesize-text-effects-profile'))
96+
->setDefinition($inputDefinitionEffectsProfile)
97+
->setDescription('Synthesizes speech from the input string of text using Audio Profiles')
98+
->setHelp(<<
99+
The %command.name% command synthesizes speech from the input string
100+
of text using Google Cloud Text-to-Speech API using Audio Profiles.
101+
php %command.full_name% "hello there" "wearable-class-device"
102+
EOF
103+
)
104+
->setCode(function ($input) {
105+
$text = $input->getArgument('text');
106+
$effectsProfileId = $input->getArgument('effects_profile_id');
107+
synthesize_text_effects_profile($text, $effectsProfileId);
108+
})
109+
);
110+
81111
$application->add((new Command('synthesize-ssml-file'))
82112
->setDefinition($inputDefinitionFile)
83113
->setDescription('Synthesizes speech from the input file of ssml')
@@ -108,6 +138,22 @@
108138
})
109139
);
110140

141+
$application->add((new Command('synthesize-text-effects-profile-file'))
142+
->setDefinition($inputDefinitionEffectsProfileFile)
143+
->setDescription('Synthesizes speech from the input file of text using Audio Profiles')
144+
->setHelp(<<
145+
The %command.name% command synthesizes speech from the input file
146+
of text using Google Cloud Text-to-Speech API using Audio Profiles.
147+
php %command.full_name% path/to/file.txt "wearable-class-device"
148+
EOF
149+
)
150+
->setCode(function ($input) {
151+
$path = $input->getArgument('path');
152+
$effectsProfileId = $input->getArgument('effects_profile_id');
153+
synthesize_text_effects_profile_file($path, $effectsProfileId);
154+
})
155+
);
156+
111157
// for testing
112158
if (getenv('PHPUNIT_TESTS') === '1') {
113159
return $application;

0 commit comments

Comments
 (0)