From 1aee24863a430e0c0eacd7a345dcc23214e14b82 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 20 Mar 2018 13:01:40 -0700 Subject: [PATCH 1/8] Add Text-To-Speech Beta samples --- pom.xml | 1 + texttospeech/cloud-client/README.md | 81 ++++++++ texttospeech/cloud-client/pom.xml | 184 ++++++++++++++++++ .../cloud-client/resources/hello.ssml | 7 + texttospeech/cloud-client/resources/hello.txt | 1 + .../texttospeech/ListAllSupportedVoices.java | 75 +++++++ .../texttospeech/QuickstartSample.java | 77 ++++++++ .../example/texttospeech/SynthesizeFile.java | 160 +++++++++++++++ .../example/texttospeech/SynthesizeText.java | 162 +++++++++++++++ .../ListAllSupportedVoicesIT.java | 66 +++++++ .../texttospeech/SynthesizeFileIT.java | 65 +++++++ .../texttospeech/SynthesizeTextIT.java | 71 +++++++ 12 files changed, 950 insertions(+) create mode 100644 texttospeech/cloud-client/README.md create mode 100644 texttospeech/cloud-client/pom.xml create mode 100644 texttospeech/cloud-client/resources/hello.ssml create mode 100644 texttospeech/cloud-client/resources/hello.txt create mode 100644 texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java create mode 100644 texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java create mode 100644 texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java create mode 100644 texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java create mode 100644 texttospeech/cloud-client/src/test/java/com/example/texttospeech/ListAllSupportedVoicesIT.java create mode 100644 texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeFileIT.java create mode 100644 texttospeech/cloud-client/src/test/java/com/example/texttospeech/SynthesizeTextIT.java diff --git a/pom.xml b/pom.xml index 0a15493d0ca..639ef88a583 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,7 @@ storage/xml-api/cmdline-sample storage/xml-api/serviceaccount-appengine-sample + texttospeech/cloud-client translate translate/cloud-client diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md new file mode 100644 index 00000000000..7649405aaab --- /dev/null +++ b/texttospeech/cloud-client/README.md @@ -0,0 +1,81 @@ +# Google Cloud Text-To-Speech API Java examples + +The [Cloud Text To Speech API][texttospeech] enables you to generate and +customize synthesized speech from text or SSML. + +These samples show how to list all supported voices, synthesize raw +text, and synthesize a file. + +[texttospeech]: https://cloud.google.com/texttospeech/ +[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java + +## Prerequisites + +### Download Maven + +To get started, [download][maven-download] and [install][maven-install] it. + +[maven]: https://maven.apache.org +[maven-download]: https://maven.apache.org/download.cgi +[maven-install]: https://maven.apache.org/install.html + +### Setup + +* Create a project with the [Google Cloud Console][cloud-console], and enable + the [TextToSpeech API][text-to-speech-api]. +* [Set up][auth] authentication. For + example, from the Cloud Console, create a service account, + download its json credentials file, then set the appropriate environment + variable: + + ```bash + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json + ``` +* Build the samples + ``` + mvn clean package + ``` + +[cloud-console]: https://console.cloud.google.com +[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](quickstart) +``` +mvn exec:java -DQuickstart +``` + +## List Voices +This sample lists all the supported voices. [Java Code](list_voices) +``` +mvn exec:java -DListVoices +``` + +## Synthesize Text +This sample synthesizes text to an output audio file. [Java Code](synthesize_text) +``` +mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"' +``` + +This sample synthesizes ssml to an output audio file. [Java Code](synthesize_text) +``` +mvn exec:java -DSynthesizeText -Dexec.args='--ssml \ + \ + Hello there. \ +' +``` + +## Synthesize File +This sample synthesizes a text file to an output audio file. [Java Code](synthesize_file) +``` +mvn exec:java -DSynthesizeFile -Dexec.args='--text resources/hello.txt' +``` + +This sample synthesizes a ssml file to an output audio file. [Java Code](synthesize_file) +``` +mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml' +``` \ No newline at end of file diff --git a/texttospeech/cloud-client/pom.xml b/texttospeech/cloud-client/pom.xml new file mode 100644 index 00000000000..d2229221a17 --- /dev/null +++ b/texttospeech/cloud-client/pom.xml @@ -0,0 +1,184 @@ + + + 4.0.0 + com.example.texttospeech + tts-samples + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../.. + + + + 1.7 + 1.7 + UTF-8 + + + + + + com.google.cloud + google-cloud-texttospeech + 0.41.0-beta + + + net.sourceforge.argparse4j + argparse4j + 0.8.1 + + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.35 + 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 + + + + + + + \ No newline at end of file diff --git a/texttospeech/cloud-client/resources/hello.ssml b/texttospeech/cloud-client/resources/hello.ssml new file mode 100644 index 00000000000..3b0ecdb4b03 --- /dev/null +++ b/texttospeech/cloud-client/resources/hello.ssml @@ -0,0 +1,7 @@ + + + Hello there. + diff --git a/texttospeech/cloud-client/resources/hello.txt b/texttospeech/cloud-client/resources/hello.txt new file mode 100644 index 00000000000..495cc9fa8f9 --- /dev/null +++ b/texttospeech/cloud-client/resources/hello.txt @@ -0,0 +1 @@ +Hello there! 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 new file mode 100644 index 00000000000..e498c333f41 --- /dev/null +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java @@ -0,0 +1,75 @@ +/* + * 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.ListVoicesRequest; +import com.google.cloud.texttospeech.v1beta1.ListVoicesResponse; +import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1beta1.Voice; +import com.google.protobuf.ByteString; + +import java.util.List; + + +/** + * Google Cloud TextToSpeech API sample application. + * Example usage: mvn package exec:java + * -Dexec.mainClass='com.example.texttospeech.ListAllSupportedVoices' + */ +public class ListAllSupportedVoices { + + // [START tts_list_voices] + /** + * 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 { + // Instantiates a client + try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { + // Builds the text to speech list voices request + ListVoicesRequest request = ListVoicesRequest.getDefaultInstance(); + + // Performs the list voices request + ListVoicesResponse response = textToSpeechClient.listVoices(request); + List voices = response.getVoicesList(); + + for (Voice voice : voices) { + // Display the voice's name. Example: tpc-vocoded + System.out.format("Name: %s\n", voice.getName()); + + // Display the supported language codes for this voice. Example: "en-us" + List languageCodes = voice.getLanguageCodesList().asByteStringList(); + for (ByteString languageCode : languageCodes) { + System.out.format("Supported Language: %s\n", languageCode.toStringUtf8()); + } + + // Display the supported ssml-gender for this voice. Example: FEMALE + System.out.format("SSML 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/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java new file mode 100644 index 00000000000..b9556f7c9b9 --- /dev/null +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java @@ -0,0 +1,77 @@ +/* + * 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()); + } + } + } +} +// [END tts_quickstart] 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 new file mode 100644 index 00000000000..c21a4349d55 --- /dev/null +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java @@ -0,0 +1,160 @@ +/* + * 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(); + + // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. + AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. + + // Select the type of audio file you want returned + AudioConfig audioConfig = AudioConfig.newBuilder() + .setAudioEncoding(audioEncoding) + .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()); + } + } + } + // [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(); + + // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. + AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. + + // Select the type of audio file you want returned + AudioConfig audioConfig = AudioConfig.newBuilder() + .setAudioEncoding(audioEncoding) + .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()); + } + } + } + // [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 new file mode 100644 index 00000000000..a777e12130a --- /dev/null +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java @@ -0,0 +1,162 @@ +/* + * 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(); + + // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. + AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. + + // Select the type of audio file you want returned + AudioConfig audioConfig = AudioConfig.newBuilder() + .setAudioEncoding(audioEncoding) + .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()); + } + } + } + // [END tts_synthesize_text] + + // [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., "\n" + + "\n" + + "Hello there.\n" + + ""; + + private File outputFile; + + @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(); + } + + @Test + public void testSynthesizeSsml() throws Exception { + // Act + SynthesizeText.synthesizeText(SSML); + + // Assert + outputFile = new File(OUTPUT); + assertThat(outputFile.isFile()).isTrue(); + } +} \ No newline at end of file From b31962325c9aa0b0bc62bdd7addc96b28906bf86 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 22 Mar 2018 17:34:44 -0700 Subject: [PATCH 2/8] Update README --- texttospeech/cloud-client/README.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md index 7649405aaab..8431ef33d0f 100644 --- a/texttospeech/cloud-client/README.md +++ b/texttospeech/cloud-client/README.md @@ -60,13 +60,7 @@ mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"' This sample synthesizes ssml to an output audio file. [Java Code](synthesize_text) ``` -mvn exec:java -DSynthesizeText -Dexec.args='--ssml \ - \ - Hello there. \ -' +mvn exec:java -DSynthesizeText -Dexec.args='--ssml "Hello there."' ``` ## Synthesize File From c35c992fefa6932d3c4d9d5fda17a18632b4c298 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 14:06:50 -0700 Subject: [PATCH 3/8] Clarification --- .../java/com/example/texttospeech/ListAllSupportedVoices.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 e498c333f41..da20617b81a 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 @@ -58,8 +58,8 @@ public static void listAllSupportedVoices() throws Exception { System.out.format("Supported Language: %s\n", languageCode.toStringUtf8()); } - // Display the supported ssml-gender for this voice. Example: FEMALE - System.out.format("SSML gender: %s\n", voice.getSsmlGender()); + // 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", From 9d5477e7ac7926bb39e90be535c9651ff2f383ae Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 14:52:47 -0700 Subject: [PATCH 4/8] Update doc page link --- texttospeech/cloud-client/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/texttospeech/cloud-client/README.md b/texttospeech/cloud-client/README.md index 8431ef33d0f..82d67b8bb3c 100644 --- a/texttospeech/cloud-client/README.md +++ b/texttospeech/cloud-client/README.md @@ -6,7 +6,7 @@ customize synthesized speech from text or SSML. These samples show how to list all supported voices, synthesize raw text, and synthesize a file. -[texttospeech]: https://cloud.google.com/texttospeech/ +[texttospeech]: https://cloud.google.com/text-to-speech/ [google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java ## Prerequisites From 53c6bfd6d51301ed136f5032c1cc5cf8ede69e11 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 14:59:28 -0700 Subject: [PATCH 5/8] Tell the user an output file was created --- .../main/java/com/example/texttospeech/QuickstartSample.java | 1 + .../src/main/java/com/example/texttospeech/SynthesizeFile.java | 2 ++ .../src/main/java/com/example/texttospeech/SynthesizeText.java | 2 ++ 3 files changed, 5 insertions(+) diff --git a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java index b9556f7c9b9..d05c05f2dc9 100644 --- a/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java @@ -70,6 +70,7 @@ public static void main(String... args) throws Exception { // 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\""); } } } 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 c21a4349d55..c87c7316949 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 @@ -85,6 +85,7 @@ public static void synthesizeTextFile(String textFile) // 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\""); } } } @@ -132,6 +133,7 @@ public static void synthesizeSsmlFile(String ssmlFile) // 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\""); } } } 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 a777e12130a..777fe58a116 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 @@ -80,6 +80,7 @@ public static void synthesizeText(String text) // 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\""); } } } @@ -134,6 +135,7 @@ public static void synthesizeSsml(String ssml) // 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\""); } } } From b4f4431b21ccdd4a85b75d97000b7f02f60d6392 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 16:40:53 -0700 Subject: [PATCH 6/8] Simplify SSML --- .../cloud-client/resources/hello.ssml | 8 +------ .../example/texttospeech/SynthesizeText.java | 9 +------ .../ListAllSupportedVoicesIT.java | 4 ++-- .../texttospeech/SynthesizeFileIT.java | 16 +++++++++++++ .../texttospeech/SynthesizeTextIT.java | 24 +++++++++++++------ 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/texttospeech/cloud-client/resources/hello.ssml b/texttospeech/cloud-client/resources/hello.ssml index 3b0ecdb4b03..df7bf9eee37 100644 --- a/texttospeech/cloud-client/resources/hello.ssml +++ b/texttospeech/cloud-client/resources/hello.ssml @@ -1,7 +1 @@ - - - Hello there. - +Hello there. 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 777fe58a116..9d1603871d5 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 @@ -91,14 +91,7 @@ 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/ - * Example: - * - * - * Hello there. - * + * Example: Hello there. * @param ssml the ssml document to be synthesized. (e.g., "\n" - + "\n" - + "Hello there.\n" - + ""; + private static String SSML = "Hello there."; + 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(); @@ -57,6 +63,8 @@ public void testSynthesizeText() throws Exception { // Assert outputFile = new File(OUTPUT); assertThat(outputFile.isFile()).isTrue(); + String got = bout.toString(); + assertThat(got).contains("Audio content written to file \"output.mp3\""); } @Test @@ -67,5 +75,7 @@ public void testSynthesizeSsml() throws Exception { // 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 00ef55880e837d2379e33fe11aa2d75c70cdf760 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 26 Mar 2018 14:36:13 -0700 Subject: [PATCH 7/8] Update based on feedback --- texttospeech/cloud-client/pom.xml | 4 ++-- .../main/java/com/example/texttospeech/SynthesizeFile.java | 5 +---- .../main/java/com/example/texttospeech/SynthesizeText.java | 5 +---- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/texttospeech/cloud-client/pom.xml b/texttospeech/cloud-client/pom.xml index d2229221a17..bd5d5aa03ea 100644 --- a/texttospeech/cloud-client/pom.xml +++ b/texttospeech/cloud-client/pom.xml @@ -25,8 +25,8 @@ - 1.7 - 1.7 + 1.8 + 1.8 UTF-8 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 c87c7316949..5485987459b 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 @@ -115,12 +115,9 @@ public static void synthesizeSsmlFile(String ssmlFile) .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE .build(); - // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. - AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. - // Select the type of audio file you want returned AudioConfig audioConfig = AudioConfig.newBuilder() - .setAudioEncoding(audioEncoding) + .setAudioEncoding(AudioEncoding.MP3) // MP3 audio. .build(); // Perform the text-to-speech request 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 9d1603871d5..c64dee1f3d6 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 @@ -110,12 +110,9 @@ public static void synthesizeSsml(String ssml) .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE .build(); - // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. - AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. - // Select the type of audio file you want returned AudioConfig audioConfig = AudioConfig.newBuilder() - .setAudioEncoding(audioEncoding) + .setAudioEncoding(AudioEncoding.MP3) // MP3 audio. .build(); // Perform the text-to-speech request From 7ac2e409d30a03ea3127ee70bdd00b8ff04d20a5 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 26 Mar 2018 18:53:44 -0700 Subject: [PATCH 8/8] Update missed audio encodings --- .../main/java/com/example/texttospeech/SynthesizeFile.java | 5 +---- .../main/java/com/example/texttospeech/SynthesizeText.java | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) 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 5485987459b..8e22a1b05e9 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 @@ -67,12 +67,9 @@ public static void synthesizeTextFile(String textFile) .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE .build(); - // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. - AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. - // Select the type of audio file you want returned AudioConfig audioConfig = AudioConfig.newBuilder() - .setAudioEncoding(audioEncoding) + .setAudioEncoding(AudioEncoding.MP3) // MP3 audio. .build(); // Perform the text-to-speech request 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 c64dee1f3d6..fb4f296c685 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 @@ -62,12 +62,9 @@ public static void synthesizeText(String text) .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE .build(); - // Returns [google.rpc.Code.INVALID_ARGUMENT] when not specified. - AudioEncoding audioEncoding = AudioEncoding.MP3; // MP3 audio. - // Select the type of audio file you want returned AudioConfig audioConfig = AudioConfig.newBuilder() - .setAudioEncoding(audioEncoding) + .setAudioEncoding(AudioEncoding.MP3) // MP3 audio. .build(); // Perform the text-to-speech request