Skip to content

Commit

Permalink
samples: Adding TTS Beta samples : Audio profile (#1152)
Browse files Browse the repository at this point in the history
* Adding TTS Beta samples : Audio profile

* Updated based on comments : Adding TTS Beta samples - Audio profile

* Updated based on comments : TTS Beta samples - Audio profile

* Updated based on comments : TTS Beta samples - Audio profile

* Updates after review

* Updates after review

* Updates after review : Please let this be the last one :)

* Update to released client library

* Update SynthesizeText.java

Need to update the verification script to allow LLC.

* Update SynthesizeText.java

Need to update the verification script to allow LLC.
  • Loading branch information
nirupa-kumar authored and chingor13 committed Aug 17, 2020
1 parent e545997 commit 748be1b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,39 @@
/**
* Google Cloud TextToSpeech API sample application.
* Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeText'
* -Dexec.args='text "hello"'
* -Dexec.args='--text "hello"'
*/
public class SynthesizeText {

// [START tts_synthesize_text]
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
*
* @param text the raw text to be synthesized. (e.g., "Hello there!")
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeText(String text)
throws Exception {
public static void synthesizeText(String text) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder()
.setText(text)
.build();
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();

// Build the voice request
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();

// Select the type of audio file you want returned
AudioConfig audioConfig = AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();
AudioConfig audioConfig =
AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();

// Perform the text-to-speech request
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
audioConfig);
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
Expand All @@ -87,34 +87,34 @@ public static void synthesizeText(String text)
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
*
* Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
* <p>Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
* Example: <speak>Hello there.</speak>
*
* @param ssml the ssml document to be synthesized. (e.g., "<?xml...")
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeSsml(String ssml)
throws Exception {
public static void synthesizeSsml(String ssml) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the ssml input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder()
.setSsml(ssml)
.build();
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssml).build();

// Build the voice request
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();

// Select the type of audio file you want returned
AudioConfig audioConfig = AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();
AudioConfig audioConfig =
AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();

// Perform the text-to-speech request
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
audioConfig);
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
Expand All @@ -129,9 +129,13 @@ public static void synthesizeSsml(String ssml)
// [END tts_synthesize_ssml]

public static void main(String... args) throws Exception {
ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
.defaultHelp(true)
.description("Synthesize a text file or ssml file.");

ArgumentParser parser =
ArgumentParsers.newFor("SynthesizeText")
.build()
.defaultHelp(true)
.description("Synthesize a text or ssml.");

MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
group.addArgument("--text").help("The text file from which to synthesize speech.");
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");
Expand All @@ -148,4 +152,4 @@ public static void main(String... args) throws Exception {
parser.handleError(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testSynthesizeText() throws Exception {
@Test
public void testSynthesizeSsml() throws Exception {
// Act
SynthesizeText.synthesizeText(SSML);
SynthesizeText.synthesizeSsml(SSML);

// Assert
outputFile = new File(OUTPUT);
Expand Down

0 comments on commit 748be1b

Please sign in to comment.