Skip to content

Commit 748be1b

Browse files
nirupa-kumarchingor13
authored andcommitted
samples: Adding TTS Beta samples : Audio profile (#1152)
* 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.
1 parent e545997 commit 748be1b

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeText.java

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,39 @@
3737
/**
3838
* Google Cloud TextToSpeech API sample application.
3939
* Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeText'
40-
* -Dexec.args='text "hello"'
40+
* -Dexec.args='--text "hello"'
4141
*/
4242
public class SynthesizeText {
4343

4444
// [START tts_synthesize_text]
4545
/**
4646
* Demonstrates using the Text to Speech client to synthesize text or ssml.
47+
*
4748
* @param text the raw text to be synthesized. (e.g., "Hello there!")
4849
* @throws Exception on TextToSpeechClient Errors.
4950
*/
50-
public static void synthesizeText(String text)
51-
throws Exception {
51+
public static void synthesizeText(String text) throws Exception {
5252
// Instantiates a client
5353
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
5454
// Set the text input to be synthesized
55-
SynthesisInput input = SynthesisInput.newBuilder()
56-
.setText(text)
57-
.build();
55+
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();
5856

5957
// Build the voice request
60-
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
61-
.setLanguageCode("en-US") // languageCode = "en_us"
62-
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
63-
.build();
58+
VoiceSelectionParams voice =
59+
VoiceSelectionParams.newBuilder()
60+
.setLanguageCode("en-US") // languageCode = "en_us"
61+
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
62+
.build();
6463

6564
// Select the type of audio file you want returned
66-
AudioConfig audioConfig = AudioConfig.newBuilder()
67-
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
68-
.build();
65+
AudioConfig audioConfig =
66+
AudioConfig.newBuilder()
67+
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
68+
.build();
6969

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

7474
// Get the audio contents from the response
7575
ByteString audioContents = response.getAudioContent();
@@ -87,34 +87,34 @@ public static void synthesizeText(String text)
8787
/**
8888
* Demonstrates using the Text to Speech client to synthesize text or ssml.
8989
*
90-
* Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
90+
* <p>Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
9191
* Example: <speak>Hello there.</speak>
92+
*
9293
* @param ssml the ssml document to be synthesized. (e.g., "<?xml...")
9394
* @throws Exception on TextToSpeechClient Errors.
9495
*/
95-
public static void synthesizeSsml(String ssml)
96-
throws Exception {
96+
public static void synthesizeSsml(String ssml) throws Exception {
9797
// Instantiates a client
9898
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
9999
// Set the ssml input to be synthesized
100-
SynthesisInput input = SynthesisInput.newBuilder()
101-
.setSsml(ssml)
102-
.build();
100+
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssml).build();
103101

104102
// Build the voice request
105-
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
106-
.setLanguageCode("en-US") // languageCode = "en_us"
107-
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
108-
.build();
103+
VoiceSelectionParams voice =
104+
VoiceSelectionParams.newBuilder()
105+
.setLanguageCode("en-US") // languageCode = "en_us"
106+
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
107+
.build();
109108

110109
// Select the type of audio file you want returned
111-
AudioConfig audioConfig = AudioConfig.newBuilder()
112-
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
113-
.build();
110+
AudioConfig audioConfig =
111+
AudioConfig.newBuilder()
112+
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
113+
.build();
114114

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

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

131131
public static void main(String... args) throws Exception {
132-
ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
133-
.defaultHelp(true)
134-
.description("Synthesize a text file or ssml file.");
132+
133+
ArgumentParser parser =
134+
ArgumentParsers.newFor("SynthesizeText")
135+
.build()
136+
.defaultHelp(true)
137+
.description("Synthesize a text or ssml.");
138+
135139
MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
136140
group.addArgument("--text").help("The text file from which to synthesize speech.");
137141
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");
@@ -148,4 +152,4 @@ public static void main(String... args) throws Exception {
148152
parser.handleError(e);
149153
}
150154
}
151-
}
155+
}

texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeTextIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testSynthesizeText() throws Exception {
7070
@Test
7171
public void testSynthesizeSsml() throws Exception {
7272
// Act
73-
SynthesizeText.synthesizeText(SSML);
73+
SynthesizeText.synthesizeSsml(SSML);
7474

7575
// Assert
7676
outputFile = new File(OUTPUT);

0 commit comments

Comments
 (0)