|
| 1 | +/* |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.texttospeech; |
| 18 | + |
| 19 | +// [START tts_ssml_address_imports] |
| 20 | +// Imports the Google Cloud client library |
| 21 | +import com.google.cloud.texttospeech.v1.AudioConfig; |
| 22 | +import com.google.cloud.texttospeech.v1.AudioEncoding; |
| 23 | +import com.google.cloud.texttospeech.v1.SsmlVoiceGender; |
| 24 | +import com.google.cloud.texttospeech.v1.SynthesisInput; |
| 25 | +import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse; |
| 26 | +import com.google.cloud.texttospeech.v1.TextToSpeechClient; |
| 27 | +import com.google.cloud.texttospeech.v1.VoiceSelectionParams; |
| 28 | +import com.google.common.html.HtmlEscapers; |
| 29 | +import com.google.protobuf.ByteString; |
| 30 | +import java.io.FileOutputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.OutputStream; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Paths; |
| 35 | +// [END tts_ssml_address_imports] |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Google Cloud TextToSpeech API sample application. |
| 40 | + * Example usage: mvn package exec:java |
| 41 | + * -Dexec.mainClass='com.example.texttospeech.SsmlAddresses |
| 42 | + */ |
| 43 | +public class SsmlAddresses { |
| 44 | + |
| 45 | + // [START tts_ssml_address_audio] |
| 46 | + /** |
| 47 | + * Generates synthetic audio from a String of SSML text. |
| 48 | + * |
| 49 | + * Given a string of SSML text and an output file name, this function |
| 50 | + * calls the Text-to-Speech API. The API returns a synthetic audio |
| 51 | + * version of the text, formatted according to the SSML commands. This |
| 52 | + * function saves the synthetic audio to the designated output file. |
| 53 | + * |
| 54 | + * @param ssmlText: String of tagged SSML text |
| 55 | + * @param outfile: String name of file under which to save audio output |
| 56 | + * @throws Exception on errors while closing the client |
| 57 | + * |
| 58 | + */ |
| 59 | + public static void ssmlToAudio(String ssmlText, String outFile) |
| 60 | + throws Exception { |
| 61 | + // Instantiates a client |
| 62 | + try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { |
| 63 | + // Set the ssml text input to synthesize |
| 64 | + SynthesisInput input = SynthesisInput.newBuilder() |
| 65 | + .setSsml(ssmlText) |
| 66 | + .build(); |
| 67 | + |
| 68 | + // Build the voice request, select the language code ("en-US") and |
| 69 | + // the ssml voice gender ("male") |
| 70 | + VoiceSelectionParams voice = VoiceSelectionParams.newBuilder() |
| 71 | + .setLanguageCode("en-US") |
| 72 | + .setSsmlGender(SsmlVoiceGender.MALE) |
| 73 | + .build(); |
| 74 | + |
| 75 | + // Select the audio file type |
| 76 | + AudioConfig audioConfig = AudioConfig.newBuilder() |
| 77 | + .setAudioEncoding(AudioEncoding.MP3) |
| 78 | + .build(); |
| 79 | + |
| 80 | + // Perform the text-to-speech request on the text input with the selected voice parameters and |
| 81 | + // audio file type |
| 82 | + SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, |
| 83 | + audioConfig); |
| 84 | + |
| 85 | + // Get the audio contents from the response |
| 86 | + ByteString audioContents = response.getAudioContent(); |
| 87 | + |
| 88 | + // Write the response to the output file |
| 89 | + try (OutputStream out = new FileOutputStream(outFile)) { |
| 90 | + out.write(audioContents.toByteArray()); |
| 91 | + System.out.println("Audio content written to file " + outFile); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + // [END tts_ssml_address_audio] |
| 96 | + |
| 97 | + // [START tts_ssml_address_ssml] |
| 98 | + /** |
| 99 | + * Generates SSML text from plaintext. |
| 100 | + * |
| 101 | + * Given an input filename, this function converts the contents of the input text file |
| 102 | + * into a String of tagged SSML text. This function formats the SSML String so that, |
| 103 | + * when synthesized, the synthetic audio will pause for two seconds between each line |
| 104 | + * of the text file. This function also handles special text characters which might |
| 105 | + * interfere with SSML commands. |
| 106 | + * |
| 107 | + * @param inputfile: String name of plaintext file |
| 108 | + * @throws IOException on files that don't exist |
| 109 | + * @return a String of SSML text based on plaintext input. |
| 110 | + * |
| 111 | + */ |
| 112 | + public static String textToSsml(String inputFile) |
| 113 | + throws Exception { |
| 114 | + |
| 115 | + // Read lines of input file |
| 116 | + String rawLines = new String(Files.readAllBytes(Paths.get(inputFile))); |
| 117 | + |
| 118 | + // Replace special characters with HTML Ampersand Character Codes |
| 119 | + // These codes prevent the API from confusing text with SSML tags |
| 120 | + // For example, '<' --> '<' and '&' --> '&' |
| 121 | + String escapedLines = HtmlEscapers.htmlEscaper().escape(rawLines); |
| 122 | + |
| 123 | + // Convert plaintext to SSML |
| 124 | + // Tag SSML so that there is a 2 second pause between each address |
| 125 | + String expandedNewline = escapedLines.replaceAll("\\n","\n<break time='2s'/>"); |
| 126 | + String ssml = "<speak>" + expandedNewline + "</speak>"; |
| 127 | + |
| 128 | + // Return the concatenated String of SSML |
| 129 | + return ssml; |
| 130 | + } |
| 131 | + // [START tts_ssml_address_ssml] |
| 132 | + |
| 133 | + // [START tts_ssml_address_test] |
| 134 | + public static void main(String... args) throws Exception { |
| 135 | + // test example address file |
| 136 | + String inputFile = "resources/example.txt"; |
| 137 | + String outFile = "resources/example.mp3"; |
| 138 | + |
| 139 | + String ssml = textToSsml(inputFile); |
| 140 | + ssmlToAudio(ssml, outFile); |
| 141 | + } |
| 142 | + // [END tts_ssml_address_test] |
| 143 | +} |