diff --git a/pom.xml b/pom.xml index 6f8c58ab175..8711cd723ee 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,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..82d67b8bb3c --- /dev/null +++ b/texttospeech/cloud-client/README.md @@ -0,0 +1,75 @@ +# 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/text-to-speech/ +[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..bd5d5aa03ea --- /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.8 + 1.8 + 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..df7bf9eee37 --- /dev/null +++ b/texttospeech/cloud-client/resources/hello.ssml @@ -0,0 +1 @@ +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..da20617b81a --- /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 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/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..d05c05f2dc9 --- /dev/null +++ b/texttospeech/cloud-client/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/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..8e22a1b05e9 --- /dev/null +++ b/texttospeech/cloud-client/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/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..fb4f296c685 --- /dev/null +++ b/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java @@ -0,0 +1,151 @@ +/* + * 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_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 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.synthesizeText(SSML); + + // 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