Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion texttospeech/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,26 @@ Usage:
Examples:
php texttospeech.php synthesize-text -h
php texttospeech.php synthesize-ssml -h
php texttospeech.php synthesize-text-audio-profile -h
php texttospeech.php synthesize-text "Hello there."
php texttospeech.php synthesize-ssml "<speak>Hello there.</speak>"
php texttospeech.php synthesize-text-effects-profile "Hello there." "handset-class-device"
```

### Synthesize file
```
Usage:
php texttospeech.php synthesize-text-file <FILE_PATH>
php texttospeech.php synthesize-ssml-file <FILE_PATH>

php texttospeech.php synthesize-text-effects-profile-file <FILE_PATH> <AUDIO_PROFILE>

Examples:
php texttospeech.php synthesize-text-file -h
php texttospeech.php synthesize-ssml-file -h
php texttospeech.php synthesize-text-audio-profile-file -h
php texttospeech.php synthesize-text-file resources/hello.txt
php texttospeech.php synthesize-ssml-file resources/hello.ssml
php texttospeech.php synthesize-text-effects-profile-file resources/hello.txt "handset-class-device"
```

## The client library
Expand Down
6 changes: 4 additions & 2 deletions texttospeech/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-text-to-speech": "^0.1",
"google/cloud-text-to-speech": "^0.2.1",
"symfony/console": "^3.0"
},
"require-dev": {
Expand All @@ -12,7 +12,9 @@
"src/synthesize_ssml.php",
"src/synthesize_ssml_file.php",
"src/synthesize_text.php",
"src/synthesize_text_file.php"
"src/synthesize_text_file.php",
"src/synthesize_text_effects_profile.php",
"src/synthesize_text_effects_profile_file.php"
]
}
}
10 changes: 7 additions & 3 deletions texttospeech/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$client = new TextToSpeechClient();

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

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

// Effects profile
$effectsProfileId = "telephony-class-application";

// select the type of audio file you want returned
$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::MP3);
->setAudioEncoding(AudioEncoding::MP3)
->setEffectsProfileId(array($effectsProfileId));

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

// the response's audioContent is binary
Expand Down
55 changes: 55 additions & 0 deletions texttospeech/src/synthesize_text_effects_profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\TextToSpeech;

// [START tts_synthesize_text_audio_profile]
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

function synthesize_text_effects_profile($text, $effectsProfileId)
{
// create client object
$client = new TextToSpeechClient();

$inputText = (new SynthesisInput())
->setText($text);

// note: the voice can also be specified by name
// names of voices can be retrieved with $client->listVoices()
$voice = (new VoiceSelectionParams())
->setLanguageCode('en-US')
->setSsmlGender(SsmlVoiceGender::FEMALE);

// define effects profile id.
$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::MP3)
->setEffectsProfileId(array($effectsProfileId));

$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
$audioContent = $response->getAudioContent();

file_put_contents('output.mp3', $audioContent);
print('Audio content written to "output.mp3"' . PHP_EOL);

$client->close();
}
// [END tts_synthesize_text_audio_profile]
56 changes: 56 additions & 0 deletions texttospeech/src/synthesize_text_effects_profile_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\TextToSpeech;

// [START tts_synthesize_text_audio_profile_file]
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

function synthesize_text_effects_profile_file($path, $effectsProfileId)
{
// create client object
$client = new TextToSpeechClient();

// get text from file
$text = file_get_contents($path);
$inputText = (new SynthesisInput())
->setText($text);

// note: the voice can also be specified by name
// names of voices can be retrieved with $client->listVoices()
$voice = (new VoiceSelectionParams())
->setLanguageCode('en-US')
->setSsmlGender(SsmlVoiceGender::FEMALE);

$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::MP3)
->setEffectsProfileId(array($effectsProfileId));

$response = $client->synthesizeSpeech($inputText, $voice, $audioConfig);
$audioContent = $response->getAudioContent();

file_put_contents('output.mp3', $audioContent);
print('Audio content written to "output.mp3"' . PHP_EOL);

$client->close();
}
// [END tts_synthesize_text_audio_profile_file]
21 changes: 21 additions & 0 deletions texttospeech/test/textToSpeechTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public function testSynthesizeText()
$this->assertGreaterThan(0,filesize('output.mp3'));
unlink('output.mp3');
}
public function testSynthesizeTextEffectsProfile()
{
$output = $this->runCommand('synthesize-text-effects-profile', [
'text' => 'hello there',
'effects_profile_id' => 'telephony-class-application'
]);
$this->assertContains('Audio content written to', $output);
$this->assertGreaterThan(0,filesize('output.mp3'));
unlink('output.mp3');
}
public function testSynthesizeSsmlFile()
{
$path = __DIR__ . '/../resources/hello.ssml';
Expand All @@ -74,6 +84,17 @@ public function testSynthesizeTextFile()
$this->assertGreaterThan(0,filesize('output.mp3'));
unlink('output.mp3');
}
public function testSynthesizeTextEffectsProfileFile()
{
$path = __DIR__ . '/../resources/hello.txt';
$output = $this->runCommand('synthesize-text-effects-profile-file', [
'path' => $path,
'effects_profile_id' => 'telephony-class-application'
]);
$this->assertContains('Audio content written to', $output);
$this->assertGreaterThan(0,filesize('output.mp3'));
unlink('output.mp3');
}
private function runCommand($commandName, array $args = [])
{
$application = require __DIR__ . '/../texttospeech.php';
Expand Down
46 changes: 46 additions & 0 deletions texttospeech/texttospeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@
$inputDefinition = new InputDefinition([
new InputArgument('text', InputArgument::REQUIRED,
'Text/SSML to synthesize.')

]);

$inputDefinitionEffectsProfile = new InputDefinition([
new InputArgument('text', InputArgument::REQUIRED,
'Text/SSML to synthesize.'),
new InputArgument('effects_profile_id', InputArgument::REQUIRED,
'Audio Profile.')
]);

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

$inputDefinitionEffectsProfileFile = new InputDefinition([
new InputArgument('path', InputArgument::REQUIRED, 'File to synthesize.'),
new InputArgument('effects_profile_id', InputArgument::REQUIRED,
'Audio Profile.')
]);


$application->add(new Command('list-voices'))
->setDescription('List the available voices')
Expand Down Expand Up @@ -78,6 +92,22 @@
})
);

$application->add((new Command('synthesize-text-effects-profile'))
->setDefinition($inputDefinitionEffectsProfile)
->setDescription('Synthesizes speech from the input string of text using Audio Profiles')
->setHelp(<<<EOF
The <info>%command.name%</info> command synthesizes speech from the input string
of text using Google Cloud Text-to-Speech API using Audio Profiles.
<info>php %command.full_name% "hello there" "wearable-class-device"</info>
EOF
)
->setCode(function ($input) {
$text = $input->getArgument('text');
$effectsProfileId = $input->getArgument('effects_profile_id');
synthesize_text_effects_profile($text, $effectsProfileId);
})
);

$application->add((new Command('synthesize-ssml-file'))
->setDefinition($inputDefinitionFile)
->setDescription('Synthesizes speech from the input file of ssml')
Expand Down Expand Up @@ -108,6 +138,22 @@
})
);

$application->add((new Command('synthesize-text-effects-profile-file'))
->setDefinition($inputDefinitionEffectsProfileFile)
->setDescription('Synthesizes speech from the input file of text using Audio Profiles')
->setHelp(<<<EOF
The <info>%command.name%</info> command synthesizes speech from the input file
of text using Google Cloud Text-to-Speech API using Audio Profiles.
<info>php %command.full_name% path/to/file.txt "wearable-class-device"</info>
EOF
)
->setCode(function ($input) {
$path = $input->getArgument('path');
$effectsProfileId = $input->getArgument('effects_profile_id');
synthesize_text_effects_profile_file($path, $effectsProfileId);
})
);

// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
Expand Down