From 9f6ecfd76afed0b5ee129e674c6169e345c70c55 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 8 Feb 2019 13:15:14 -0800 Subject: [PATCH 1/3] Update Audio Profiles to beta --- texttospeech/cloud-client/README.md | 5 ++ texttospeech/cloud-client/pom.xml | 2 +- .../example/texttospeech/SynthesizeText.java | 66 +++++++++++++++++-- .../texttospeech/SynthesizeTextIT.java | 13 ++++ 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md index 5e66d14f1fe..1dd0ff74bcf 100644 --- a/texttospeech/cloud-client/README.md +++ b/texttospeech/cloud-client/README.md @@ -58,6 +58,11 @@ This sample synthesizes text to an output audio file. [Java Code](https://github mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"' ``` +This sample synthesizes text with an audio profile to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java) +``` +mvn exec:java -DSynthesizeText -Dexec.args='--text "hello" "telephony-class-application"' +``` + This sample synthesizes ssml to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java) ``` mvn exec:java -DSynthesizeText -Dexec.args='--ssml "Hello there."' diff --git a/texttospeech/cloud-client/pom.xml b/texttospeech/cloud-client/pom.xml index 43abed00b08..0a20eb21c61 100644 --- a/texttospeech/cloud-client/pom.xml +++ b/texttospeech/cloud-client/pom.xml @@ -35,7 +35,7 @@ com.google.cloud google-cloud-texttospeech - 0.70.0-beta + 0.80.0-beta net.sourceforge.argparse4j 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 3b7b8960824..7f9ddf7d978 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 @@ -83,6 +83,52 @@ public static void synthesizeText(String text) throws Exception { } // [END tts_synthesize_text] + // [START tts_synthesize_text_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_audio_profile] + // [START tts_synthesize_ssml] /** * Demonstrates using the Text to Speech client to synthesize text or ssml. @@ -134,17 +180,29 @@ public static void main(String... args) throws Exception { ArgumentParsers.newFor("SynthesizeText") .build() .defaultHelp(true) - .description("Synthesize a text or ssml."); + .description("Synthesize a text, text with audio effect profiles, or ssml."); MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true); - group.addArgument("--text").help("The text file from which to synthesize speech."); + group + .addArgument("--text") + .help("The text file from which to synthesize speech.") + .nargs("+") + .metavar("TEXT", "EFFECTSPROFILE(optional)"); group.addArgument("--ssml").help("The ssml file from which to synthesize speech."); try { Namespace namespace = parser.parseArgs(args); - if (namespace.get("text") != null) { - synthesizeText(namespace.getString("text")); + if ((namespace.get("text") != null)) { + if (namespace.getList("text").size() == 2) { + synthesizeTextWithAudioProfile( + namespace.getList("text").get(0).toString(), + namespace.getList("text").get(1).toString()); + + } else { + synthesizeText(namespace.getString("text")); + } + } else { synthesizeSsml(namespace.getString("ssml")); } diff --git a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java index dfee58f9303..3602e95ae5e 100644 --- a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java +++ b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java @@ -38,6 +38,7 @@ public class SynthesizeTextIT { private static String OUTPUT = "output.mp3"; private static String TEXT = "Hello there."; private static String SSML = "Hello there."; + private static String EFFECTSPROFILE = "telephony-class-application"; private ByteArrayOutputStream bout; private PrintStream out; @@ -78,4 +79,16 @@ public void testSynthesizeSsml() throws Exception { 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\""); + } } \ No newline at end of file From bc25c2f0aff2f68e4bed58a259395dc9847ab6c3 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 8 Feb 2019 13:55:25 -0800 Subject: [PATCH 2/3] Remove CLI --- texttospeech/cloud-client/README.md | 33 +---- texttospeech/cloud-client/pom.xml | 123 ------------------ .../texttospeech/ListAllSupportedVoices.java | 7 +- .../example/texttospeech/SynthesizeFile.java | 27 +--- .../example/texttospeech/SynthesizeText.java | 51 +------- .../ListAllSupportedVoicesIT.java | 5 +- .../texttospeech/SynthesizeFileIT.java | 7 +- .../texttospeech/SynthesizeTextIT.java | 10 +- 8 files changed, 31 insertions(+), 232 deletions(-) diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md index 1dd0ff74bcf..59331ebac4e 100644 --- a/texttospeech/cloud-client/README.md +++ b/texttospeech/cloud-client/README.md @@ -46,35 +46,8 @@ Synthesize text to an output audio file. [Java Code](https://github.com/GoogleCl mvn exec:java -DQuickstart ``` -## List Voices -This sample lists all the supported voices. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java) +## Snippets +To verify the snippets are running correctly, you can run the tests via: ``` -mvn exec:java -DListVoices -``` - -## Synthesize Text -This sample synthesizes text to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java) -``` -mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"' -``` - -This sample synthesizes text with an audio profile to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java) -``` -mvn exec:java -DSynthesizeText -Dexec.args='--text "hello" "telephony-class-application"' -``` - -This sample synthesizes ssml to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java) -``` -mvn exec:java -DSynthesizeText -Dexec.args='--ssml "Hello there."' -``` - -## Synthesize File -This sample synthesizes a text file to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java) -``` -mvn exec:java -DSynthesizeFile -Dexec.args='--text resources/hello.txt' -``` - -This sample synthesizes a ssml file to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java) -``` -mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml' +mvn clean verify ``` diff --git a/texttospeech/cloud-client/pom.xml b/texttospeech/cloud-client/pom.xml index 0a20eb21c61..97a6344a582 100644 --- a/texttospeech/cloud-client/pom.xml +++ b/texttospeech/cloud-client/pom.xml @@ -58,127 +58,4 @@ test - - - - - - Quickstart - - - Quickstart - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.texttospeech.QuickstartSample - false - - - - - - - - - ListVoices - - - ListVoices - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.texttospeech.ListAllSupportedVoices - false - - - - - - - - - SynthesizeFile - - - SynthesizeFile - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.texttospeech.SynthesizeFile - false - - - - - - - - - SynthesizeText - - - SynthesizeText - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.texttospeech.SynthesizeText - false - - - - - - diff --git a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java index 795bae7b852..690091731f8 100644 --- a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java @@ -38,7 +38,7 @@ public class ListAllSupportedVoices { * Demonstrates using the Text to Speech client to list the client's supported voices. * @throws Exception on TextToSpeechClient Errors. */ - public static void listAllSupportedVoices() throws Exception { + public static List listAllSupportedVoices() throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { // Builds the text to speech list voices request @@ -65,11 +65,8 @@ public static void listAllSupportedVoices() throws Exception { System.out.format("Natural Sample Rate Hertz: %s\n\n", voice.getNaturalSampleRateHertz()); } + return voices; } } // [END tts_list_voices] - - public static void main(String[] args) throws Exception { - listAllSupportedVoices(); - } } \ No newline at end of file diff --git a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java index 2b1ab08d07f..382ee7a4cc4 100644 --- a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java @@ -50,7 +50,7 @@ public class SynthesizeFile { * @param textFile the text file to be synthesized. (e.g., hello.txt) * @throws Exception on TextToSpeechClient Errors. */ - public static void synthesizeTextFile(String textFile) + public static ByteString synthesizeTextFile(String textFile) throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { @@ -83,6 +83,7 @@ public static void synthesizeTextFile(String textFile) try (OutputStream out = new FileOutputStream("output.mp3")) { out.write(audioContents.toByteArray()); System.out.println("Audio content written to file \"output.mp3\""); + return audioContents; } } } @@ -95,7 +96,7 @@ public static void synthesizeTextFile(String textFile) * @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml) * @throws Exception on TextToSpeechClient Errors. */ - public static void synthesizeSsmlFile(String ssmlFile) + public static ByteString synthesizeSsmlFile(String ssmlFile) throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { @@ -128,29 +129,9 @@ public static void synthesizeSsmlFile(String ssmlFile) try (OutputStream out = new FileOutputStream("output.mp3")) { out.write(audioContents.toByteArray()); System.out.println("Audio content written to file \"output.mp3\""); + return audioContents; } } } // [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/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java index 7f9ddf7d978..3c495ff15c7 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 @@ -28,11 +28,6 @@ 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. @@ -48,7 +43,7 @@ public class SynthesizeText { * @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 ByteString synthesizeText(String text) throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { // Set the text input to be synthesized @@ -78,6 +73,7 @@ public static void synthesizeText(String text) throws Exception { try (OutputStream out = new FileOutputStream("output.mp3")) { out.write(audioContents.toByteArray()); System.out.println("Audio content written to file \"output.mp3\""); + return audioContents; } } } @@ -92,7 +88,7 @@ public static void synthesizeText(String text) throws Exception { * "telephony-class-application") * @throws Exception on TextToSpeechClient Errors. */ - public static void synthesizeTextWithAudioProfile(String text, String effectsProfile) + public static ByteString synthesizeTextWithAudioProfile(String text, String effectsProfile) throws Exception { // Instantiates a client try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { @@ -124,6 +120,7 @@ public static void synthesizeTextWithAudioProfile(String text, String effectsPro try (OutputStream out = new FileOutputStream("output.mp3")) { out.write(audioContents.toByteArray()); System.out.println("Audio content written to file \"output.mp3\""); + return audioContents; } } } @@ -139,7 +136,7 @@ public static void synthesizeTextWithAudioProfile(String text, String effectsPro * @param ssml the ssml document to be synthesized. (e.g., " voices = listAllSupportedVoices.listAllSupportedVoices(); // Assert + assertThat(voices.isEmpty()).isFalse(); String got = bout.toString(); assertThat(got).contains("en-US"); assertThat(got).contains("SSML Voice Gender: MALE"); diff --git a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeFileIT.java b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeFileIT.java index d3b68b498da..7c232d15ee4 100644 --- a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeFileIT.java +++ b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeFileIT.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; import java.io.File; @@ -58,9 +59,10 @@ public void tearDown() { @Test public void testSynthesizeText() throws Exception { // Act - SynthesizeFile.synthesizeTextFile(TEXT_FILE); + ByteString audioContents = SynthesizeFile.synthesizeTextFile(TEXT_FILE); // Assert + assertThat(audioContents.isEmpty()).isFalse(); outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); String got = bout.toString(); @@ -70,9 +72,10 @@ public void testSynthesizeText() throws Exception { @Test public void testSynthesizeSsml() throws Exception { // Act - SynthesizeFile.synthesizeSsmlFile(SSML_FILE); + ByteString audioContents = SynthesizeFile.synthesizeSsmlFile(SSML_FILE); // Assert + assertThat(audioContents.isEmpty()).isFalse(); outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); String got = bout.toString(); diff --git a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java index 3602e95ae5e..eb326afd6b3 100644 --- a/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java +++ b/texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; import java.io.File; @@ -59,9 +60,10 @@ public void tearDown() { @Test public void testSynthesizeText() throws Exception { // Act - SynthesizeText.synthesizeText(TEXT); + ByteString audioContents = SynthesizeText.synthesizeText(TEXT); // Assert + assertThat(audioContents.isEmpty()).isFalse(); outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); String got = bout.toString(); @@ -71,9 +73,10 @@ public void testSynthesizeText() throws Exception { @Test public void testSynthesizeSsml() throws Exception { // Act - SynthesizeText.synthesizeSsml(SSML); + ByteString audioContents = SynthesizeText.synthesizeSsml(SSML); // Assert + assertThat(audioContents.isEmpty()).isFalse(); outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); String got = bout.toString(); @@ -83,9 +86,10 @@ public void testSynthesizeSsml() throws Exception { @Test public void testSynthesizeTextWithAudioProfile() throws Exception { // Act - SynthesizeText.synthesizeTextWithAudioProfile(TEXT, EFFECTSPROFILE); + ByteString audioContents = SynthesizeText.synthesizeTextWithAudioProfile(TEXT, EFFECTSPROFILE); // Assert + assertThat(audioContents.isEmpty()).isFalse(); outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); String got = bout.toString(); From 6116848e7e567460a7a2e357f29ae0e934af2834 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Fri, 8 Feb 2019 15:19:48 -0800 Subject: [PATCH 3/3] Update README.md --- texttospeech/cloud-client/README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md index 59331ebac4e..875aca870ad 100644 --- a/texttospeech/cloud-client/README.md +++ b/texttospeech/cloud-client/README.md @@ -40,12 +40,6 @@ To get started, [download][maven-download] and [install][maven-install] it. [text-to-speech-api]: https://console.cloud.google.com/apis/api/texttospeech.googleapis.com/overview?project=_ [auth]: https://cloud.google.com/docs/authentication/getting-started -## Quckstart -Synthesize text to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java) -``` -mvn exec:java -DQuickstart -``` - ## Snippets To verify the snippets are running correctly, you can run the tests via: ```