Skip to content

Commit 451b3ad

Browse files
crowdusnnegrey
authored andcommitted
Java samples for SSML Addresses tutorial (GoogleCloudPlatform#1556)
* TODO tests * TODO add comments * finished Java samples for SSML Addresses * updated comments
1 parent eb80831 commit 451b3ad

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<speak>123 Street Ln, Small Town, IL 12345 USA
2+
<break time='2s'/>1 Jenny St &amp; Number St, Tutone City, CA 86753
3+
<break time='2s'/>1 Piazza del Fibonacci, 12358 Pisa, Italy
4+
<break time='2s'/></speak>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123 Street Ln, Small Town, IL 12345 USA
2+
1 Jenny St & Number St, Tutone City, CA 86753
3+
1 Piazza del Fibonacci, 12358 Pisa, Italy
Binary file not shown.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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, '<' --> '&lt;' and '&' --> '&amp;'
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.protobuf.ByteString;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.File;
24+
import java.io.PrintStream;
25+
import java.nio.file.Files;
26+
import java.nio.file.Paths;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.JUnit4;
32+
33+
/**
34+
* Tests for SsmlAddresses sample.
35+
*/
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
38+
public class SsmlAddressesIT {
39+
40+
private static String OUTPUT = "output.mp3";
41+
private static String TEXT_FILE = "resources/example.txt";
42+
private static String SSML_FILE = "resources/example.ssml";
43+
44+
private ByteArrayOutputStream bout;
45+
private PrintStream out;
46+
private File outputFile;
47+
48+
@Before
49+
public void setUp() {
50+
bout = new ByteArrayOutputStream();
51+
out = new PrintStream(bout);
52+
System.setOut(out);
53+
}
54+
55+
@Test
56+
public void testTextToSsml() throws Exception {
57+
// Act
58+
String ssml = SsmlAddresses.textToSsml(TEXT_FILE);
59+
String expectedSsml = new String(Files.readAllBytes(Paths.get(SSML_FILE)));
60+
61+
// Assert
62+
assertThat(ssml).contains(expectedSsml);
63+
}
64+
65+
@Test
66+
public void testSsmlToAudio() throws Exception {
67+
// Act
68+
String ssml = new String(Files.readAllBytes(Paths.get(SSML_FILE)));
69+
SsmlAddresses.ssmlToAudio(ssml, OUTPUT);
70+
71+
// Assert
72+
outputFile = new File(OUTPUT);
73+
assertThat(outputFile.isFile()).isTrue();
74+
String got = bout.toString();
75+
assertThat(got).contains("Audio content written to file output.mp3");
76+
77+
// After
78+
outputFile.delete();
79+
}
80+
}

0 commit comments

Comments
 (0)