languageCodes = voice.getLanguageCodesList().asByteStringList();
+ for (ByteString languageCode : languageCodes) {
+ System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
+ }
+
+ // Display the SSML Voice Gender
+ System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());
+
+ // Display the natural sample rate hertz for this voice. Example: 24000
+ System.out.format("Natural Sample Rate Hertz: %s\n\n",
+ voice.getNaturalSampleRateHertz());
+ }
+ }
+ }
+ // [END tts_list_voices]
+
+ public static void main(String[] args) throws Exception {
+ listAllSupportedVoices();
+ }
+}
\ No newline at end of file
diff --git a/texttospeech/beta/src/main/java/com/example/texttospeech/QuickstartSample.java b/texttospeech/beta/src/main/java/com/example/texttospeech/QuickstartSample.java
new file mode 100644
index 00000000000..d05c05f2dc9
--- /dev/null
+++ b/texttospeech/beta/src/main/java/com/example/texttospeech/QuickstartSample.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2018 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.
+ */
+
+package com.example.texttospeech;
+
+// [START tts_quickstart]
+// Imports the Google Cloud client library
+import com.google.cloud.texttospeech.v1beta1.AudioConfig;
+import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
+import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
+import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
+import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
+import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
+import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
+import com.google.protobuf.ByteString;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+/**
+ * Google Cloud TextToSpeech API sample application.
+ * Example usage: mvn package exec:java
+ * -Dexec.mainClass='com.example.texttospeech.QuickstartSample'
+ */
+public class QuickstartSample {
+
+ /**
+ * Demonstrates using the Text-to-Speech API.
+ */
+ public static void main(String... args) throws Exception {
+ // Instantiates a client
+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
+ // Set the text input to be synthesized
+ SynthesisInput input = SynthesisInput.newBuilder()
+ .setText("Hello, World!")
+ .build();
+
+ // Build the voice request, select the language code ("en-US") and the ssml voice gender
+ // ("neutral")
+ VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
+ .setLanguageCode("en-US")
+ .setSsmlGender(SsmlVoiceGender.NEUTRAL)
+ .build();
+
+ // Select the type of audio file you want returned
+ AudioConfig audioConfig = AudioConfig.newBuilder()
+ .setAudioEncoding(AudioEncoding.MP3)
+ .build();
+
+ // Perform the text-to-speech request on the text input with the selected voice parameters and
+ // audio file type
+ SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
+ audioConfig);
+
+ // Get the audio contents from the response
+ ByteString audioContents = response.getAudioContent();
+
+ // Write the response to the output file.
+ try (OutputStream out = new FileOutputStream("output.mp3")) {
+ out.write(audioContents.toByteArray());
+ System.out.println("Audio content written to file \"output.mp3\"");
+ }
+ }
+ }
+}
+// [END tts_quickstart]
diff --git a/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeFile.java b/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeFile.java
new file mode 100644
index 00000000000..8e22a1b05e9
--- /dev/null
+++ b/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeFile.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2018 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.
+ */
+
+package com.example.texttospeech;
+
+// Imports the Google Cloud client library
+import com.google.cloud.texttospeech.v1beta1.AudioConfig;
+import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
+import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
+import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
+import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
+import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
+import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
+import com.google.protobuf.ByteString;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import net.sourceforge.argparse4j.ArgumentParsers;
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
+import net.sourceforge.argparse4j.inf.Namespace;
+
+
+/**
+ * Google Cloud TextToSpeech API sample application.
+ * Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeFile'
+ * -Dexec.args='--text resources/hello.txt'
+ */
+public class SynthesizeFile {
+
+ // [START tts_synthesize_text_file]
+ /**
+ * Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
+ * @param textFile the text file to be synthesized. (e.g., hello.txt)
+ * @throws Exception on TextToSpeechClient Errors.
+ */
+ public static void synthesizeTextFile(String textFile)
+ throws Exception {
+ // Instantiates a client
+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
+ // Read the file's contents
+ String contents = new String(Files.readAllBytes(Paths.get(textFile)));
+ // Set the text input to be synthesized
+ SynthesisInput input = SynthesisInput.newBuilder()
+ .setText(contents)
+ .build();
+
+ // Build the voice request
+ 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();
+
+ // Perform the text-to-speech request
+ SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
+ audioConfig);
+
+ // Get the audio contents from the response
+ ByteString audioContents = response.getAudioContent();
+
+ // Write the response to the output file.
+ try (OutputStream out = new FileOutputStream("output.mp3")) {
+ out.write(audioContents.toByteArray());
+ System.out.println("Audio content written to file \"output.mp3\"");
+ }
+ }
+ }
+ // [END tts_synthesize_text_file]
+
+
+ // [START tts_synthesize_ssml_file]
+ /**
+ * Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
+ * @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml)
+ * @throws Exception on TextToSpeechClient Errors.
+ */
+ public static void synthesizeSsmlFile(String ssmlFile)
+ throws Exception {
+ // Instantiates a client
+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
+ // Read the file's contents
+ String contents = new String(Files.readAllBytes(Paths.get(ssmlFile)));
+ // Set the ssml input to be synthesized
+ SynthesisInput input = SynthesisInput.newBuilder()
+ .setSsml(contents)
+ .build();
+
+ // Build the voice request
+ 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();
+
+ // Perform the text-to-speech request
+ SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice,
+ audioConfig);
+
+ // Get the audio contents from the response
+ ByteString audioContents = response.getAudioContent();
+
+ // Write the response to the output file.
+ try (OutputStream out = new FileOutputStream("output.mp3")) {
+ out.write(audioContents.toByteArray());
+ System.out.println("Audio content written to file \"output.mp3\"");
+ }
+ }
+ }
+ // [END tts_synthesize_ssml_file]
+
+ public static void main(String... args) throws Exception {
+ ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
+ .defaultHelp(true)
+ .description("Synthesize a text file or ssml file.");
+ 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.");
+
+ try {
+ Namespace namespace = parser.parseArgs(args);
+
+ if (namespace.get("text") != null) {
+ synthesizeTextFile(namespace.getString("text"));
+ } else {
+ synthesizeSsmlFile(namespace.getString("ssml"));
+ }
+ } catch (ArgumentParserException e) {
+ parser.handleError(e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeText.java b/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeText.java
new file mode 100644
index 00000000000..028e67db5cd
--- /dev/null
+++ b/texttospeech/beta/src/main/java/com/example/texttospeech/SynthesizeText.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright 2018 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.
+ */
+
+package com.example.texttospeech;
+
+// Imports the Google Cloud client library
+import com.google.cloud.texttospeech.v1beta1.AudioConfig;
+import com.google.cloud.texttospeech.v1beta1.AudioEncoding;
+import com.google.cloud.texttospeech.v1beta1.SsmlVoiceGender;
+import com.google.cloud.texttospeech.v1beta1.SynthesisInput;
+import com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse;
+import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
+import com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams;
+import com.google.protobuf.ByteString;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import net.sourceforge.argparse4j.ArgumentParsers;
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
+import net.sourceforge.argparse4j.inf.Namespace;
+
+/**
+ * Google Cloud TextToSpeech API sample application.
+ * Example usage: mvn package exec:java-Dexec.mainClass='com.example.texttospeech.SynthesizeText'
+ * -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 {
+ // Instantiates a client
+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
+ // Set the text input to be synthesized
+ 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();
+
+ // Select the type of audio file you want returned
+ AudioConfig audioConfig =
+ AudioConfig.newBuilder()
+ .setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
+ .build();
+
+ // Perform the text-to-speech request
+ SynthesizeSpeechResponse response =
+ textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
+
+ // Get the audio contents from the response
+ ByteString audioContents = response.getAudioContent();
+
+ // Write the response to the output file.
+ try (OutputStream out = new FileOutputStream("output.mp3")) {
+ out.write(audioContents.toByteArray());
+ System.out.println("Audio content written to file \"output.mp3\"");
+ }
+ }
+ }
+ // [END tts_synthesize_text]
+
+ // [START tts_synthesize_text_with_audio_profile]
+ /**
+ * Demonstrates using the Text to Speech client with audio profiles to synthesize text or ssml
+ *
+ * @param text the raw text to be synthesized. (e.g., "Hello there!")
+ * @param effectsProfile audio profile to be used for synthesis. (e.g.,
+ * "telephony-class-application")
+ * @throws Exception on TextToSpeechClient Errors.
+ */
+ public static void synthesizeTextWithAudioProfile(String text, String effectsProfile)
+ throws Exception {
+ // Instantiates a client
+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
+ // Set the text input to be synthesized
+ 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();
+
+ // Select the type of audio file you want returned and the audio profile
+ AudioConfig audioConfig =
+ AudioConfig.newBuilder()
+ .setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
+ .addEffectsProfileId(effectsProfile) // audio profile
+ .build();
+
+ // Perform the text-to-speech request
+ SynthesizeSpeechResponse response =
+ textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
+
+ // Get the audio contents from the response
+ ByteString audioContents = response.getAudioContent();
+
+ // Write the response to the output file.
+ try (OutputStream out = new FileOutputStream("output.mp3")) {
+ out.write(audioContents.toByteArray());
+ System.out.println("Audio content written to file \"output.mp3\"");
+ }
+ }
+ }
+ // [END tts_synthesize_text_with_audio_profile]
+
+ // [START tts_synthesize_ssml]
+ /**
+ * 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/
+ * Example: Hello there.
+ *
+ * @param ssml the ssml document to be synthesized. (e.g., "Hello there.";
+ private static String EFFECTSPROFILE = "telephony-class-application";
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+ private File outputFile;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ outputFile.delete();
+ }
+
+ @Test
+ public void testSynthesizeText() throws Exception {
+ // Act
+ SynthesizeText.synthesizeText(TEXT);
+
+ // Assert
+ outputFile = new File(OUTPUT);
+ assertThat(outputFile.isFile()).isTrue();
+ String got = bout.toString();
+ assertThat(got).contains("Audio content written to file \"output.mp3\"");
+ }
+
+ @Test
+ public void testSynthesizeSsml() throws Exception {
+ // Act
+ SynthesizeText.synthesizeSsml(SSML);
+
+ // Assert
+ outputFile = new File(OUTPUT);
+ assertThat(outputFile.isFile()).isTrue();
+ String got = bout.toString();
+ assertThat(got).contains("Audio content written to file \"output.mp3\"");
+ }
+
+ @Test
+ public void testSynthesizeTextWithAudioProfile() throws Exception {
+ // Act
+ SynthesizeText.synthesizeTextWithAudioProfile(TEXT, EFFECTSPROFILE);
+
+ // Assert
+ outputFile = new File(OUTPUT);
+ assertThat(outputFile.isFile()).isTrue();
+ String got = bout.toString();
+ assertThat(got).contains("Audio content written to file \"output.mp3\"");
+ }
+}
diff --git a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java
index c447b69c028..3b7b8960824 100644
--- a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java
+++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java
@@ -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();
@@ -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/
+ *
Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
* Example: Hello there.
+ *
* @param ssml the ssml document to be synthesized. (e.g., "