Skip to content

Commit cc651fe

Browse files
committed
Merge branch 'master' into samples-texttospeech-beta
2 parents bdfd7de + cdd66e8 commit cc651fe

20 files changed

+1017
-116
lines changed

texttospeech/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>com.google.cloud</groupId>
3232
<artifactId>libraries-bom</artifactId>
33-
<version>8.0.0</version>
33+
<version>9.0.0</version>
3434
<type>pom</type>
3535
<scope>import</scope>
3636
</dependency>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<speak>123 Street Ln, Small Town, IL 12345 USA
2+
<break time='2s'/>1 Jenny St &amp; Number St, Tutone City, CA 86753
3+
<break time='2s'/>1 Piazza del Fibonacci, 12358 Pisa, Italy
4+
<break time='2s'/></speak>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123 Street Ln, Small Town, IL 12345 USA
2+
1 Jenny St & Number St, Tutone City, CA 86753
3+
1 Piazza del Fibonacci, 12358 Pisa, Italy
83.3 KB
Binary file not shown.

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package com.example.texttospeech;
1818

1919
// Imports the Google Cloud client library
20-
import com.google.cloud.texttospeech.v1beta1.ListVoicesRequest;
21-
import com.google.cloud.texttospeech.v1beta1.ListVoicesResponse;
22-
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
23-
import com.google.cloud.texttospeech.v1beta1.Voice;
20+
import com.google.cloud.texttospeech.v1.ListVoicesRequest;
21+
import com.google.cloud.texttospeech.v1.ListVoicesResponse;
22+
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
23+
import com.google.cloud.texttospeech.v1.Voice;
2424
import com.google.protobuf.ByteString;
2525
import java.util.List;
2626

@@ -36,7 +36,7 @@ public class ListAllSupportedVoices {
3636
*
3737
* @throws Exception on TextToSpeechClient Errors.
3838
*/
39-
public static void listAllSupportedVoices() throws Exception {
39+
public static List<Voice> listAllSupportedVoices() throws Exception {
4040
// Instantiates a client
4141
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
4242
// Builds the text to speech list voices request
@@ -62,11 +62,8 @@ public static void listAllSupportedVoices() throws Exception {
6262
// Display the natural sample rate hertz for this voice. Example: 24000
6363
System.out.format("Natural Sample Rate Hertz: %s\n\n", voice.getNaturalSampleRateHertz());
6464
}
65+
return voices;
6566
}
6667
}
6768
// [END tts_list_voices]
68-
69-
public static void main(String[] args) throws Exception {
70-
listAllSupportedVoices();
71-
}
7269
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.texttospeech;
18+
19+
// Imports the Google Cloud client library
20+
import com.google.cloud.texttospeech.v1beta1.ListVoicesRequest;
21+
import com.google.cloud.texttospeech.v1beta1.ListVoicesResponse;
22+
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
23+
import com.google.cloud.texttospeech.v1beta1.Voice;
24+
import com.google.protobuf.ByteString;
25+
import java.util.List;
26+
27+
/**
28+
* Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java
29+
* -Dexec.mainClass='com.example.texttospeech.ListAllSupportedVoicesBeta'
30+
*/
31+
public class ListAllSupportedVoicesBeta {
32+
33+
// [START tts_list_voices]
34+
/**
35+
* Demonstrates using the Text to Speech client to list the client's supported voices.
36+
*
37+
* @throws Exception on TextToSpeechClient Errors.
38+
*/
39+
public static void listAllSupportedVoices() throws Exception {
40+
// Instantiates a client
41+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
42+
// Builds the text to speech list voices request
43+
ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();
44+
45+
// Performs the list voices request
46+
ListVoicesResponse response = textToSpeechClient.listVoices(request);
47+
List<Voice> voices = response.getVoicesList();
48+
49+
for (Voice voice : voices) {
50+
// Display the voice's name. Example: tpc-vocoded
51+
System.out.format("Name: %s\n", voice.getName());
52+
53+
// Display the supported language codes for this voice. Example: "en-us"
54+
List<ByteString> languageCodes = voice.getLanguageCodesList().asByteStringList();
55+
for (ByteString languageCode : languageCodes) {
56+
System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
57+
}
58+
59+
// Display the SSML Voice Gender
60+
System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());
61+
62+
// Display the natural sample rate hertz for this voice. Example: 24000
63+
System.out.format("Natural Sample Rate Hertz: %s\n\n", voice.getNaturalSampleRateHertz());
64+
}
65+
}
66+
}
67+
// [END tts_list_voices]
68+
69+
public static void main(String[] args) throws Exception {
70+
listAllSupportedVoices();
71+
}
72+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
// [START tts_quickstart]
2020
// Imports the Google Cloud client library
21-
import com.google.cloud.texttospeech.v1beta1.AudioConfig;
22-
import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
23-
import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
24-
import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
25-
import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
26-
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
27-
import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
21+
import com.google.cloud.texttospeech.v1.AudioConfig;
22+
import com.google.cloud.texttospeech.v1.AudioEncoding;
23+
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
24+
import com.google.cloud.texttospeech.v1.SynthesisInput;
25+
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
26+
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
27+
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
2828
import com.google.protobuf.ByteString;
2929
import java.io.FileOutputStream;
3030
import java.io.OutputStream;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.texttospeech;
18+
19+
// [START tts_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.texttospeech.v1beta1.AudioConfig;
22+
import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
23+
import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
24+
import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
25+
import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
26+
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
27+
import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
28+
import com.google.protobuf.ByteString;
29+
import java.io.FileOutputStream;
30+
import java.io.OutputStream;
31+
32+
/**
33+
* Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java
34+
* -Dexec.mainClass='com.example.texttospeech.QuickstartSampleBeta'
35+
*/
36+
public class QuickstartSampleBeta {
37+
38+
/** Demonstrates using the Text-to-Speech API. */
39+
public static void main(String... args) throws Exception {
40+
// Instantiates a client
41+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
42+
// Set the text input to be synthesized
43+
SynthesisInput input = SynthesisInput.newBuilder().setText("Hello, World!").build();
44+
45+
// Build the voice request, select the language code ("en-US") and the ssml voice gender
46+
// ("neutral")
47+
VoiceSelectionParams voice =
48+
VoiceSelectionParams.newBuilder()
49+
.setLanguageCode("en-US")
50+
.setSsmlGender(SsmlVoiceGender.NEUTRAL)
51+
.build();
52+
53+
// Select the type of audio file you want returned
54+
AudioConfig audioConfig =
55+
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
56+
57+
// Perform the text-to-speech request on the text input with the selected voice parameters and
58+
// audio file type
59+
SynthesizeSpeechResponse response =
60+
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
61+
62+
// Get the audio contents from the response
63+
ByteString audioContents = response.getAudioContent();
64+
65+
// Write the response to the output file.
66+
try (OutputStream out = new FileOutputStream("output.mp3")) {
67+
out.write(audioContents.toByteArray());
68+
System.out.println("Audio content written to file \"output.mp3\"");
69+
}
70+
}
71+
}
72+
}
73+
// [END tts_quickstart]
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.texttospeech;
18+
19+
// [START tts_ssml_address_imports]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.texttospeech.v1.AudioConfig;
22+
import com.google.cloud.texttospeech.v1.AudioEncoding;
23+
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
24+
import com.google.cloud.texttospeech.v1.SynthesisInput;
25+
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
26+
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
27+
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
28+
import com.google.common.html.HtmlEscapers;
29+
import com.google.protobuf.ByteString;
30+
import java.io.FileOutputStream;
31+
import java.io.IOException;
32+
import java.io.OutputStream;
33+
import java.nio.file.Files;
34+
import java.nio.file.Paths;
35+
36+
// [END tts_ssml_address_imports]
37+
38+
/**
39+
* Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java
40+
* -Dexec.mainClass='com.example.texttospeech.SsmlAddresses
41+
*/
42+
public class SsmlAddresses {
43+
44+
// [START tts_ssml_address_audio]
45+
/**
46+
* Generates synthetic audio from a String of SSML text.
47+
*
48+
* <p>Given a string of SSML text and an output file name, this function calls the Text-to-Speech
49+
* API. The API returns a synthetic audio version of the text, formatted according to the SSML
50+
* commands. This function saves the synthetic audio to the designated output file.
51+
*
52+
* @param ssmlText String of tagged SSML text
53+
* @param outFile String name of file under which to save audio output
54+
* @throws Exception on errors while closing the client
55+
*/
56+
public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
57+
// Instantiates a client
58+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
59+
// Set the ssml text input to synthesize
60+
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
61+
62+
// Build the voice request, select the language code ("en-US") and
63+
// the ssml voice gender ("male")
64+
VoiceSelectionParams voice =
65+
VoiceSelectionParams.newBuilder()
66+
.setLanguageCode("en-US")
67+
.setSsmlGender(SsmlVoiceGender.MALE)
68+
.build();
69+
70+
// Select the audio file type
71+
AudioConfig audioConfig =
72+
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
73+
74+
// Perform the text-to-speech request on the text input with the selected voice parameters and
75+
// audio file type
76+
SynthesizeSpeechResponse response =
77+
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
78+
79+
// Get the audio contents from the response
80+
ByteString audioContents = response.getAudioContent();
81+
82+
// Write the response to the output file
83+
try (OutputStream out = new FileOutputStream(outFile)) {
84+
out.write(audioContents.toByteArray());
85+
System.out.println("Audio content written to file " + outFile);
86+
}
87+
}
88+
}
89+
// [END tts_ssml_address_audio]
90+
91+
// [START tts_ssml_address_ssml]
92+
/**
93+
* Generates SSML text from plaintext.
94+
*
95+
* <p>Given an input filename, this function converts the contents of the input text file into a
96+
* String of tagged SSML text. This function formats the SSML String so that, when synthesized,
97+
* the synthetic audio will pause for two seconds between each line of the text file. This
98+
* function also handles special text characters which might interfere with SSML commands.
99+
*
100+
* @param inputFile String name of plaintext file
101+
* @return a String of SSML text based on plaintext input.
102+
* @throws IOException on files that don't exist
103+
*/
104+
public static String textToSsml(String inputFile) throws Exception {
105+
106+
// Read lines of input file
107+
String rawLines = new String(Files.readAllBytes(Paths.get(inputFile)));
108+
109+
// Replace special characters with HTML Ampersand Character Codes
110+
// These codes prevent the API from confusing text with SSML tags
111+
// For example, '<' --> '&lt;' and '&' --> '&amp;'
112+
String escapedLines = HtmlEscapers.htmlEscaper().escape(rawLines);
113+
114+
// Convert plaintext to SSML
115+
// Tag SSML so that there is a 2 second pause between each address
116+
String expandedNewline = escapedLines.replaceAll("\\n", "\n<break time='2s'/>");
117+
String ssml = "<speak>" + expandedNewline + "</speak>";
118+
119+
// Return the concatenated String of SSML
120+
return ssml;
121+
}
122+
// [END tts_ssml_address_ssml]
123+
124+
// [START tts_ssml_address_test]
125+
public static void main(String... args) throws Exception {
126+
// test example address file
127+
String inputFile = "resources/example.txt";
128+
String outFile = "resources/example.mp3";
129+
130+
String ssml = textToSsml(inputFile);
131+
ssmlToAudio(ssml, outFile);
132+
}
133+
// [END tts_ssml_address_test]
134+
}

0 commit comments

Comments
 (0)