diff --git a/texttospeech/snippets/pom.xml b/texttospeech/snippets/pom.xml new file mode 100644 index 00000000000..19a0cf5853b --- /dev/null +++ b/texttospeech/snippets/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + com.example.texttospeech + texttospeech-snippets + jar + Google Cloud Text-to-Speech Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/texttospeech + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + + + com.google.cloud + libraries-bom + 26.1.4 + pom + import + + + + + + + com.google.cloud + google-cloud-texttospeech + + + + + net.sourceforge.argparse4j + argparse4j + 0.9.0 + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.1.3 + test + + + + + + + diff --git a/texttospeech/snippets/resources/example.ssml b/texttospeech/snippets/resources/example.ssml new file mode 100644 index 00000000000..c27fd399967 --- /dev/null +++ b/texttospeech/snippets/resources/example.ssml @@ -0,0 +1,4 @@ +123 Street Ln, Small Town, IL 12345 USA +1 Jenny St & Number St, Tutone City, CA 86753 +1 Piazza del Fibonacci, 12358 Pisa, Italy + \ No newline at end of file diff --git a/texttospeech/snippets/resources/example.txt b/texttospeech/snippets/resources/example.txt new file mode 100644 index 00000000000..9cd7d74db36 --- /dev/null +++ b/texttospeech/snippets/resources/example.txt @@ -0,0 +1,3 @@ +123 Street Ln, Small Town, IL 12345 USA +1 Jenny St & Number St, Tutone City, CA 86753 +1 Piazza del Fibonacci, 12358 Pisa, Italy diff --git a/texttospeech/snippets/resources/expected_example.mp3 b/texttospeech/snippets/resources/expected_example.mp3 new file mode 100644 index 00000000000..407b85f7f5d Binary files /dev/null and b/texttospeech/snippets/resources/expected_example.mp3 differ diff --git a/texttospeech/snippets/resources/hello.ssml b/texttospeech/snippets/resources/hello.ssml new file mode 100644 index 00000000000..df7bf9eee37 --- /dev/null +++ b/texttospeech/snippets/resources/hello.ssml @@ -0,0 +1 @@ +Hello there. diff --git a/texttospeech/snippets/resources/hello.txt b/texttospeech/snippets/resources/hello.txt new file mode 100644 index 00000000000..495cc9fa8f9 --- /dev/null +++ b/texttospeech/snippets/resources/hello.txt @@ -0,0 +1 @@ +Hello there! diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java new file mode 100644 index 00000000000..fff4c7bbe59 --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java @@ -0,0 +1,69 @@ +/* + * 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.v1.ListVoicesRequest; +import com.google.cloud.texttospeech.v1.ListVoicesResponse; +import com.google.cloud.texttospeech.v1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1.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 List 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()); + } + return voices; + } + } + // [END tts_list_voices] +} diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoicesBeta.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoicesBeta.java new file mode 100644 index 00000000000..7d648639142 --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/ListAllSupportedVoicesBeta.java @@ -0,0 +1,72 @@ +/* + * 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.ListAllSupportedVoicesBeta' + */ +public class ListAllSupportedVoicesBeta { + + // [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(); + } +} diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/QuickstartSample.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/QuickstartSample.java new file mode 100644 index 00000000000..73841cb1e02 --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/QuickstartSample.java @@ -0,0 +1,73 @@ +/* + * 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.v1.AudioConfig; +import com.google.cloud.texttospeech.v1.AudioEncoding; +import com.google.cloud.texttospeech.v1.SsmlVoiceGender; +import com.google.cloud.texttospeech.v1.SynthesisInput; +import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; +import com.google.cloud.texttospeech.v1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1.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/snippets/src/main/java/com/example/texttospeech/QuickstartSampleBeta.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/QuickstartSampleBeta.java new file mode 100644 index 00000000000..0f520c86e85 --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/QuickstartSampleBeta.java @@ -0,0 +1,73 @@ +/* + * 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.QuickstartSampleBeta' + */ +public class QuickstartSampleBeta { + + /** 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/snippets/src/main/java/com/example/texttospeech/SsmlAddresses.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/SsmlAddresses.java new file mode 100644 index 00000000000..d5192429148 --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/SsmlAddresses.java @@ -0,0 +1,134 @@ +/* + * Copyright 2019 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_ssml_address_imports] +// Imports the Google Cloud client library +import com.google.cloud.texttospeech.v1.AudioConfig; +import com.google.cloud.texttospeech.v1.AudioEncoding; +import com.google.cloud.texttospeech.v1.SsmlVoiceGender; +import com.google.cloud.texttospeech.v1.SynthesisInput; +import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; +import com.google.cloud.texttospeech.v1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1.VoiceSelectionParams; +import com.google.common.html.HtmlEscapers; +import com.google.protobuf.ByteString; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +// [END tts_ssml_address_imports] + +/** + * Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java + * -Dexec.mainClass='com.example.texttospeech.SsmlAddresses + */ +public class SsmlAddresses { + + // [START tts_ssml_address_audio] + /** + * Generates synthetic audio from a String of SSML text. + * + *

Given a string of SSML text and an output file name, this function calls the Text-to-Speech + * API. The API returns a synthetic audio version of the text, formatted according to the SSML + * commands. This function saves the synthetic audio to the designated output file. + * + * @param ssmlText String of tagged SSML text + * @param outFile String name of file under which to save audio output + * @throws Exception on errors while closing the client + */ + public static void ssmlToAudio(String ssmlText, String outFile) throws Exception { + // Instantiates a client + try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { + // Set the ssml text input to synthesize + SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build(); + + // Build the voice request, select the language code ("en-US") and + // the ssml voice gender ("male") + VoiceSelectionParams voice = + VoiceSelectionParams.newBuilder() + .setLanguageCode("en-US") + .setSsmlGender(SsmlVoiceGender.MALE) + .build(); + + // Select the audio file type + 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(outFile)) { + out.write(audioContents.toByteArray()); + System.out.println("Audio content written to file " + outFile); + } + } + } + // [END tts_ssml_address_audio] + + // [START tts_ssml_address_ssml] + /** + * Generates SSML text from plaintext. + * + *

Given an input filename, this function converts the contents of the input text file into a + * String of tagged SSML text. This function formats the SSML String so that, when synthesized, + * the synthetic audio will pause for two seconds between each line of the text file. This + * function also handles special text characters which might interfere with SSML commands. + * + * @param inputFile String name of plaintext file + * @return a String of SSML text based on plaintext input. + * @throws IOException on files that don't exist + */ + public static String textToSsml(String inputFile) throws Exception { + + // Read lines of input file + String rawLines = new String(Files.readAllBytes(Paths.get(inputFile))); + + // Replace special characters with HTML Ampersand Character Codes + // These codes prevent the API from confusing text with SSML tags + // For example, '<' --> '<' and '&' --> '&' + String escapedLines = HtmlEscapers.htmlEscaper().escape(rawLines); + + // Convert plaintext to SSML + // Tag SSML so that there is a 2 second pause between each address + String expandedNewline = escapedLines.replaceAll("\\n", "\n"); + String ssml = "" + expandedNewline + ""; + + // Return the concatenated String of SSML + return ssml; + } + // [END tts_ssml_address_ssml] + + // [START tts_ssml_address_test] + public static void main(String... args) throws Exception { + // test example address file + String inputFile = "resources/example.txt"; + String outFile = "resources/example.mp3"; + + String ssml = textToSsml(inputFile); + ssmlToAudio(ssml, outFile); + } + // [END tts_ssml_address_test] +} diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFile.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFile.java new file mode 100644 index 00000000000..b67c04df69d --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFile.java @@ -0,0 +1,129 @@ +/* + * 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.v1.AudioConfig; +import com.google.cloud.texttospeech.v1.AudioEncoding; +import com.google.cloud.texttospeech.v1.SsmlVoiceGender; +import com.google.cloud.texttospeech.v1.SynthesisInput; +import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; +import com.google.cloud.texttospeech.v1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1.VoiceSelectionParams; +import com.google.protobuf.ByteString; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + * 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 ByteString 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\""); + return audioContents; + } + } + } + // [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 ByteString 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\""); + return audioContents; + } + } + } + // [END tts_synthesize_ssml_file] +} diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFileBeta.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFileBeta.java new file mode 100644 index 00000000000..bf367cb3b6b --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeFileBeta.java @@ -0,0 +1,155 @@ +/* + * 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 SynthesizeFileBeta { + + // [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); + } + } +} diff --git a/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeText.java b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeText.java new file mode 100644 index 00000000000..9e5f00484dc --- /dev/null +++ b/texttospeech/snippets/src/main/java/com/example/texttospeech/SynthesizeText.java @@ -0,0 +1,172 @@ +/* + * 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.v1.AudioConfig; +import com.google.cloud.texttospeech.v1.AudioEncoding; +import com.google.cloud.texttospeech.v1.SsmlVoiceGender; +import com.google.cloud.texttospeech.v1.SynthesisInput; +import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; +import com.google.cloud.texttospeech.v1.TextToSpeechClient; +import com.google.cloud.texttospeech.v1.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.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 ByteString 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\""); + return audioContents; + } + } + } + // [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 ByteString 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\""); + return audioContents; + } + } + } + // [END tts_synthesize_text_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., "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., " voices = listAllSupportedVoices.listAllSupportedVoices(); + + // Assert + assertThat(voices.isEmpty()).isFalse(); + String got = bout.toString(); + assertThat(got).contains("en-US"); + assertThat(got).contains("SSML Voice Gender: MALE"); + assertThat(got).contains("SSML Voice Gender: FEMALE"); + } +} diff --git a/texttospeech/snippets/src/test/java/com/example/texttospeech/SsmlAddressesIT.java b/texttospeech/snippets/src/test/java/com/example/texttospeech/SsmlAddressesIT.java new file mode 100644 index 00000000000..cbe8ec85c12 --- /dev/null +++ b/texttospeech/snippets/src/test/java/com/example/texttospeech/SsmlAddressesIT.java @@ -0,0 +1,76 @@ +/* + * Copyright 2019 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for SsmlAddresses sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SsmlAddressesIT { + + private static String OUTPUT = "output.mp3"; + private static String TEXT_FILE = "resources/example.txt"; + private static String SSML_FILE = "resources/example.ssml"; + + private ByteArrayOutputStream bout; + private PrintStream out; + private File outputFile; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @Test + public void testTextToSsml() throws Exception { + // Act + String ssml = SsmlAddresses.textToSsml(TEXT_FILE); + String expectedSsml = new String(Files.readAllBytes(Paths.get(SSML_FILE))); + + // Assert + assertThat(ssml).contains(expectedSsml); + } + + @Test + public void testSsmlToAudio() throws Exception { + // Act + String ssml = new String(Files.readAllBytes(Paths.get(SSML_FILE))); + SsmlAddresses.ssmlToAudio(ssml, OUTPUT); + + // Assert + outputFile = new File(OUTPUT); + assertThat(outputFile.isFile()).isTrue(); + String got = bout.toString(); + assertThat(got).contains("Audio content written to file output.mp3"); + + // After + outputFile.delete(); + } +} diff --git a/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeFileBetaIT.java b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeFileBetaIT.java new file mode 100644 index 00000000000..590dacc5e4f --- /dev/null +++ b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeFileBetaIT.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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for SynthesizeFileBeta sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SynthesizeFileBetaIT { + + private static String OUTPUT = "output.mp3"; + private static String TEXT_FILE = "resources/hello.txt"; + private static String SSML_FILE = "resources/hello.ssml"; + + 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 + SynthesizeFileBeta.synthesizeTextFile(TEXT_FILE); + + // 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 + SynthesizeFileBeta.synthesizeSsmlFile(SSML_FILE); + + // 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/snippets/src/test/java/com/example/texttospeech/SynthesizeFileIT.java b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeFileIT.java new file mode 100644 index 00000000000..1737d220a5d --- /dev/null +++ b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeFileIT.java @@ -0,0 +1,81 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.protobuf.ByteString; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for SynthesizeFile sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SynthesizeFileIT { + + private static String OUTPUT = "output.mp3"; + private static String TEXT_FILE = "resources/hello.txt"; + private static String SSML_FILE = "resources/hello.ssml"; + + 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 + ByteString audioContents = SynthesizeFile.synthesizeTextFile(TEXT_FILE); + + // Assert + assertThat(audioContents.isEmpty()).isFalse(); + 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 + ByteString audioContents = SynthesizeFile.synthesizeSsmlFile(SSML_FILE); + + // Assert + assertThat(audioContents.isEmpty()).isFalse(); + 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/snippets/src/test/java/com/example/texttospeech/SynthesizeTextBetaIT.java b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeTextBetaIT.java new file mode 100644 index 00000000000..91fb87b431c --- /dev/null +++ b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeTextBetaIT.java @@ -0,0 +1,91 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for SynthesizeTextBeta sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SynthesizeTextBetaIT { + + 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; + 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 + SynthesizeTextBeta.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 + SynthesizeTextBeta.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 + SynthesizeTextBeta.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/snippets/src/test/java/com/example/texttospeech/SynthesizeTextIT.java b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeTextIT.java new file mode 100644 index 00000000000..826052d7177 --- /dev/null +++ b/texttospeech/snippets/src/test/java/com/example/texttospeech/SynthesizeTextIT.java @@ -0,0 +1,95 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.protobuf.ByteString; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for SynthesizeText sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +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; + 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 + ByteString audioContents = SynthesizeText.synthesizeText(TEXT); + + // Assert + assertThat(audioContents.isEmpty()).isFalse(); + 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 + ByteString audioContents = SynthesizeText.synthesizeSsml(SSML); + + // Assert + assertThat(audioContents.isEmpty()).isFalse(); + 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 + ByteString audioContents = SynthesizeText.synthesizeTextWithAudioProfile(TEXT, EFFECTSPROFILE); + + // Assert + assertThat(audioContents.isEmpty()).isFalse(); + outputFile = new File(OUTPUT); + assertThat(outputFile.isFile()).isTrue(); + String got = bout.toString(); + assertThat(got).contains("Audio content written to file \"output.mp3\""); + } +}