Skip to content

Commit f4c5c0c

Browse files
nnegreychingor13
authored andcommitted
samples: Add Text-To-Speech Beta samples (#1069)
* Add Text-To-Speech Beta samples * Update README * Clarification * Update doc page link * Tell the user an output file was created * Simplify SSML * Update based on feedback * Update missed audio encodings
1 parent 9df3b2e commit f4c5c0c

File tree

9 files changed

+690
-0
lines changed

9 files changed

+690
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<speak>Hello there.</speak>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there!
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
26+
import java.util.List;
27+
28+
29+
/**
30+
* Google Cloud TextToSpeech API sample application.
31+
* Example usage: mvn package exec:java
32+
* -Dexec.mainClass='com.example.texttospeech.ListAllSupportedVoices'
33+
*/
34+
public class ListAllSupportedVoices {
35+
36+
// [START tts_list_voices]
37+
/**
38+
* Demonstrates using the Text to Speech client to list the client's supported voices.
39+
* @throws Exception on TextToSpeechClient Errors.
40+
*/
41+
public static void listAllSupportedVoices() throws Exception {
42+
// Instantiates a client
43+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
44+
// Builds the text to speech list voices request
45+
ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();
46+
47+
// Performs the list voices request
48+
ListVoicesResponse response = textToSpeechClient.listVoices(request);
49+
List<Voice> voices = response.getVoicesList();
50+
51+
for (Voice voice : voices) {
52+
// Display the voice's name. Example: tpc-vocoded
53+
System.out.format("Name: %s\n", voice.getName());
54+
55+
// Display the supported language codes for this voice. Example: "en-us"
56+
List<ByteString> languageCodes = voice.getLanguageCodesList().asByteStringList();
57+
for (ByteString languageCode : languageCodes) {
58+
System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
59+
}
60+
61+
// Display the SSML Voice Gender
62+
System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());
63+
64+
// Display the natural sample rate hertz for this voice. Example: 24000
65+
System.out.format("Natural Sample Rate Hertz: %s\n\n",
66+
voice.getNaturalSampleRateHertz());
67+
}
68+
}
69+
}
70+
// [END tts_list_voices]
71+
72+
public static void main(String[] args) throws Exception {
73+
listAllSupportedVoices();
74+
}
75+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.
34+
* Example usage: mvn package exec:java
35+
* -Dexec.mainClass='com.example.texttospeech.QuickstartSample'
36+
*/
37+
public class QuickstartSample {
38+
39+
/**
40+
* Demonstrates using the Text-to-Speech API.
41+
*/
42+
public static void main(String... args) throws Exception {
43+
// Instantiates a client
44+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
45+
// Set the text input to be synthesized
46+
SynthesisInput input = SynthesisInput.newBuilder()
47+
.setText("Hello, World!")
48+
.build();
49+
50+
// Build the voice request, select the language code ("en-US") and the ssml voice gender
51+
// ("neutral")
52+
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
53+
.setLanguageCode("en-US")
54+
.setSsmlGender(SsmlVoiceGender.NEUTRAL)
55+
.build();
56+
57+
// Select the type of audio file you want returned
58+
AudioConfig audioConfig = AudioConfig.newBuilder()
59+
.setAudioEncoding(AudioEncoding.MP3)
60+
.build();
61+
62+
// Perform the text-to-speech request on the text input with the selected voice parameters and
63+
// audio file type
64+
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
65+
audioConfig);
66+
67+
// Get the audio contents from the response
68+
ByteString audioContents = response.getAudioContent();
69+
70+
// Write the response to the output file.
71+
try (OutputStream out = new FileOutputStream("output.mp3")) {
72+
out.write(audioContents.toByteArray());
73+
System.out.println("Audio content written to file \"output.mp3\"");
74+
}
75+
}
76+
}
77+
}
78+
// [END tts_quickstart]
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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.AudioConfig;
21+
import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
22+
import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
23+
import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
24+
import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
25+
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
26+
import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
27+
import com.google.protobuf.ByteString;
28+
29+
import java.io.FileOutputStream;
30+
import java.io.OutputStream;
31+
import java.nio.file.Files;
32+
import java.nio.file.Paths;
33+
import net.sourceforge.argparse4j.ArgumentParsers;
34+
import net.sourceforge.argparse4j.inf.ArgumentParser;
35+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
36+
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
37+
import net.sourceforge.argparse4j.inf.Namespace;
38+
39+
40+
/**
41+
* Google Cloud TextToSpeech API sample application.
42+
* Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeFile'
43+
* -Dexec.args='--text resources/hello.txt'
44+
*/
45+
public class SynthesizeFile {
46+
47+
// [START tts_synthesize_text_file]
48+
/**
49+
* Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
50+
* @param textFile the text file to be synthesized. (e.g., hello.txt)
51+
* @throws Exception on TextToSpeechClient Errors.
52+
*/
53+
public static void synthesizeTextFile(String textFile)
54+
throws Exception {
55+
// Instantiates a client
56+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
57+
// Read the file's contents
58+
String contents = new String(Files.readAllBytes(Paths.get(textFile)));
59+
// Set the text input to be synthesized
60+
SynthesisInput input = SynthesisInput.newBuilder()
61+
.setText(contents)
62+
.build();
63+
64+
// Build the voice request
65+
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
66+
.setLanguageCode("en-US") // languageCode = "en_us"
67+
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
68+
.build();
69+
70+
// Select the type of audio file you want returned
71+
AudioConfig audioConfig = AudioConfig.newBuilder()
72+
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
73+
.build();
74+
75+
// Perform the text-to-speech request
76+
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
77+
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("output.mp3")) {
84+
out.write(audioContents.toByteArray());
85+
System.out.println("Audio content written to file \"output.mp3\"");
86+
}
87+
}
88+
}
89+
// [END tts_synthesize_text_file]
90+
91+
92+
// [START tts_synthesize_ssml_file]
93+
/**
94+
* Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
95+
* @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml)
96+
* @throws Exception on TextToSpeechClient Errors.
97+
*/
98+
public static void synthesizeSsmlFile(String ssmlFile)
99+
throws Exception {
100+
// Instantiates a client
101+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
102+
// Read the file's contents
103+
String contents = new String(Files.readAllBytes(Paths.get(ssmlFile)));
104+
// Set the ssml input to be synthesized
105+
SynthesisInput input = SynthesisInput.newBuilder()
106+
.setSsml(contents)
107+
.build();
108+
109+
// Build the voice request
110+
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
111+
.setLanguageCode("en-US") // languageCode = "en_us"
112+
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
113+
.build();
114+
115+
// Select the type of audio file you want returned
116+
AudioConfig audioConfig = AudioConfig.newBuilder()
117+
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
118+
.build();
119+
120+
// Perform the text-to-speech request
121+
SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
122+
audioConfig);
123+
124+
// Get the audio contents from the response
125+
ByteString audioContents = response.getAudioContent();
126+
127+
// Write the response to the output file.
128+
try (OutputStream out = new FileOutputStream("output.mp3")) {
129+
out.write(audioContents.toByteArray());
130+
System.out.println("Audio content written to file \"output.mp3\"");
131+
}
132+
}
133+
}
134+
// [END tts_synthesize_ssml_file]
135+
136+
public static void main(String... args) throws Exception {
137+
ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
138+
.defaultHelp(true)
139+
.description("Synthesize a text file or ssml file.");
140+
MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
141+
group.addArgument("--text").help("The text file from which to synthesize speech.");
142+
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");
143+
144+
try {
145+
Namespace namespace = parser.parseArgs(args);
146+
147+
if (namespace.get("text") != null) {
148+
synthesizeTextFile(namespace.getString("text"));
149+
} else {
150+
synthesizeSsmlFile(namespace.getString("ssml"));
151+
}
152+
} catch (ArgumentParserException e) {
153+
parser.handleError(e);
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)