|
34 | 34 | import java.nio.file.Paths; |
35 | 35 | // [END tts_ssml_address_imports] |
36 | 36 |
|
37 | | - |
38 | 37 | /** |
39 | | - * Google Cloud TextToSpeech API sample application. |
40 | | - * Example usage: mvn package exec:java |
41 | | - * -Dexec.mainClass='com.example.texttospeech.SsmlAddresses |
| 38 | + * Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java |
| 39 | + * -Dexec.mainClass='com.example.texttospeech.SsmlAddresses |
42 | 40 | */ |
43 | 41 | public class SsmlAddresses { |
44 | 42 |
|
45 | 43 | // [START tts_ssml_address_audio] |
46 | 44 | /** |
47 | 45 | * Generates synthetic audio from a String of SSML text. |
48 | 46 | * |
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. |
| 47 | + * <p>Given a string of SSML text and an output file name, this function calls the Text-to-Speech |
| 48 | + * API. The API returns a synthetic audio version of the text, formatted according to the SSML |
| 49 | + * commands. This function saves the synthetic audio to the designated output file. |
53 | 50 | * |
54 | | - * @param ssmlText: String of tagged SSML text |
55 | | - * @param outfile: String name of file under which to save audio output |
| 51 | + * @param ssmlText String of tagged SSML text |
| 52 | + * @param outFile String name of file under which to save audio output |
56 | 53 | * @throws Exception on errors while closing the client |
57 | | - * |
58 | 54 | */ |
59 | | - public static void ssmlToAudio(String ssmlText, String outFile) |
60 | | - throws Exception { |
| 55 | + public static void ssmlToAudio(String ssmlText, String outFile) throws Exception { |
61 | 56 | // Instantiates a client |
62 | 57 | try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { |
63 | 58 | // Set the ssml text input to synthesize |
64 | | - SynthesisInput input = SynthesisInput.newBuilder() |
65 | | - .setSsml(ssmlText) |
66 | | - .build(); |
| 59 | + SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build(); |
67 | 60 |
|
68 | 61 | // Build the voice request, select the language code ("en-US") and |
69 | 62 | // the ssml voice gender ("male") |
70 | | - VoiceSelectionParams voice = VoiceSelectionParams.newBuilder() |
71 | | - .setLanguageCode("en-US") |
72 | | - .setSsmlGender(SsmlVoiceGender.MALE) |
73 | | - .build(); |
| 63 | + VoiceSelectionParams voice = |
| 64 | + VoiceSelectionParams.newBuilder() |
| 65 | + .setLanguageCode("en-US") |
| 66 | + .setSsmlGender(SsmlVoiceGender.MALE) |
| 67 | + .build(); |
74 | 68 |
|
75 | 69 | // Select the audio file type |
76 | | - AudioConfig audioConfig = AudioConfig.newBuilder() |
77 | | - .setAudioEncoding(AudioEncoding.MP3) |
78 | | - .build(); |
| 70 | + AudioConfig audioConfig = |
| 71 | + AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build(); |
79 | 72 |
|
80 | 73 | // Perform the text-to-speech request on the text input with the selected voice parameters and |
81 | 74 | // audio file type |
82 | | - SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, |
83 | | - audioConfig); |
| 75 | + SynthesizeSpeechResponse response = |
| 76 | + textToSpeechClient.synthesizeSpeech(input, voice, audioConfig); |
84 | 77 |
|
85 | 78 | // Get the audio contents from the response |
86 | 79 | ByteString audioContents = response.getAudioContent(); |
@@ -98,19 +91,16 @@ public static void ssmlToAudio(String ssmlText, String outFile) |
98 | 91 | /** |
99 | 92 | * Generates SSML text from plaintext. |
100 | 93 | * |
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. |
| 94 | + * <p>Given an input filename, this function converts the contents of the input text file into a |
| 95 | + * String of tagged SSML text. This function formats the SSML String so that, when synthesized, |
| 96 | + * the synthetic audio will pause for two seconds between each line of the text file. This |
| 97 | + * function also handles special text characters which might interfere with SSML commands. |
106 | 98 | * |
107 | | - * @param inputfile: String name of plaintext file |
108 | | - * @throws IOException on files that don't exist |
| 99 | + * @param inputFile String name of plaintext file |
109 | 100 | * @return a String of SSML text based on plaintext input. |
110 | | - * |
| 101 | + * @throws IOException on files that don't exist |
111 | 102 | */ |
112 | | - public static String textToSsml(String inputFile) |
113 | | - throws Exception { |
| 103 | + public static String textToSsml(String inputFile) throws Exception { |
114 | 104 |
|
115 | 105 | // Read lines of input file |
116 | 106 | String rawLines = new String(Files.readAllBytes(Paths.get(inputFile))); |
@@ -122,7 +112,7 @@ public static String textToSsml(String inputFile) |
122 | 112 |
|
123 | 113 | // Convert plaintext to SSML |
124 | 114 | // Tag SSML so that there is a 2 second pause between each address |
125 | | - String expandedNewline = escapedLines.replaceAll("\\n","\n<break time='2s'/>"); |
| 115 | + String expandedNewline = escapedLines.replaceAll("\\n", "\n<break time='2s'/>"); |
126 | 116 | String ssml = "<speak>" + expandedNewline + "</speak>"; |
127 | 117 |
|
128 | 118 | // Return the concatenated String of SSML |
|