1+ /*
2+ * Copyright 2018 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+ // Imports the Google Cloud client library
20+ import com .google .cloud .texttospeech .v1beta1 .AudioConfig ;
21+ import com .google .cloud .texttospeech .v1beta1 .AudioEncoding ;
22+ import com .google .cloud .texttospeech .v1beta1 .SsmlVoiceGender ;
23+ import com .google .cloud .texttospeech .v1beta1 .SynthesisInput ;
24+ import com .google .cloud .texttospeech .v1beta1 .SynthesizeSpeechResponse ;
25+ import com .google .cloud .texttospeech .v1beta1 .TextToSpeechClient ;
26+ import com .google .cloud .texttospeech .v1beta1 .VoiceSelectionParams ;
27+ import com .google .protobuf .ByteString ;
28+
29+ import java .io .FileOutputStream ;
30+ import java .io .OutputStream ;
31+ import java .nio .file .Files ;
32+ import java .nio .file .Paths ;
33+ import net .sourceforge .argparse4j .ArgumentParsers ;
34+ import net .sourceforge .argparse4j .inf .ArgumentParser ;
35+ import net .sourceforge .argparse4j .inf .ArgumentParserException ;
36+ import net .sourceforge .argparse4j .inf .MutuallyExclusiveGroup ;
37+ import net .sourceforge .argparse4j .inf .Namespace ;
38+
39+
40+ /**
41+ * Google Cloud TextToSpeech API sample application.
42+ * Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeFile'
43+ * -Dexec.args='--text resources/hello.txt'
44+ */
45+ public class SynthesizeFile {
46+
47+ // [START tts_synthesize_text_file]
48+ /**
49+ * Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
50+ * @param textFile the text file to be synthesized. (e.g., hello.txt)
51+ * @throws Exception on TextToSpeechClient Errors.
52+ */
53+ public static void synthesizeTextFile (String textFile )
54+ throws Exception {
55+ // Instantiates a client
56+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient .create ()) {
57+ // Read the file's contents
58+ String contents = new String (Files .readAllBytes (Paths .get (textFile )));
59+ // Set the text input to be synthesized
60+ SynthesisInput input = SynthesisInput .newBuilder ()
61+ .setText (contents )
62+ .build ();
63+
64+ // Build the voice request
65+ VoiceSelectionParams voice = VoiceSelectionParams .newBuilder ()
66+ .setLanguageCode ("en-US" ) // languageCode = "en_us"
67+ .setSsmlGender (SsmlVoiceGender .FEMALE ) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
68+ .build ();
69+
70+ // Select the type of audio file you want returned
71+ AudioConfig audioConfig = AudioConfig .newBuilder ()
72+ .setAudioEncoding (AudioEncoding .MP3 ) // MP3 audio.
73+ .build ();
74+
75+ // Perform the text-to-speech request
76+ SynthesizeSpeechResponse response = textToSpeechClient .synthesizeSpeech (input , voice ,
77+ audioConfig );
78+
79+ // Get the audio contents from the response
80+ ByteString audioContents = response .getAudioContent ();
81+
82+ // Write the response to the output file.
83+ try (OutputStream out = new FileOutputStream ("output.mp3" )) {
84+ out .write (audioContents .toByteArray ());
85+ System .out .println ("Audio content written to file \" output.mp3\" " );
86+ }
87+ }
88+ }
89+ // [END tts_synthesize_text_file]
90+
91+
92+ // [START tts_synthesize_ssml_file]
93+ /**
94+ * Demonstrates using the Text to Speech client to synthesize a text file or ssml file.
95+ * @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml)
96+ * @throws Exception on TextToSpeechClient Errors.
97+ */
98+ public static void synthesizeSsmlFile (String ssmlFile )
99+ throws Exception {
100+ // Instantiates a client
101+ try (TextToSpeechClient textToSpeechClient = TextToSpeechClient .create ()) {
102+ // Read the file's contents
103+ String contents = new String (Files .readAllBytes (Paths .get (ssmlFile )));
104+ // Set the ssml input to be synthesized
105+ SynthesisInput input = SynthesisInput .newBuilder ()
106+ .setSsml (contents )
107+ .build ();
108+
109+ // Build the voice request
110+ VoiceSelectionParams voice = VoiceSelectionParams .newBuilder ()
111+ .setLanguageCode ("en-US" ) // languageCode = "en_us"
112+ .setSsmlGender (SsmlVoiceGender .FEMALE ) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
113+ .build ();
114+
115+ // Select the type of audio file you want returned
116+ AudioConfig audioConfig = AudioConfig .newBuilder ()
117+ .setAudioEncoding (AudioEncoding .MP3 ) // MP3 audio.
118+ .build ();
119+
120+ // Perform the text-to-speech request
121+ SynthesizeSpeechResponse response = textToSpeechClient .synthesizeSpeech (input , voice ,
122+ audioConfig );
123+
124+ // Get the audio contents from the response
125+ ByteString audioContents = response .getAudioContent ();
126+
127+ // Write the response to the output file.
128+ try (OutputStream out = new FileOutputStream ("output.mp3" )) {
129+ out .write (audioContents .toByteArray ());
130+ System .out .println ("Audio content written to file \" output.mp3\" " );
131+ }
132+ }
133+ }
134+ // [END tts_synthesize_ssml_file]
135+
136+ public static void main (String ... args ) throws Exception {
137+ ArgumentParser parser = ArgumentParsers .newFor ("SynthesizeFile" ).build ()
138+ .defaultHelp (true )
139+ .description ("Synthesize a text file or ssml file." );
140+ MutuallyExclusiveGroup group = parser .addMutuallyExclusiveGroup ().required (true );
141+ group .addArgument ("--text" ).help ("The text file from which to synthesize speech." );
142+ group .addArgument ("--ssml" ).help ("The ssml file from which to synthesize speech." );
143+
144+ try {
145+ Namespace namespace = parser .parseArgs (args );
146+
147+ if (namespace .get ("text" ) != null ) {
148+ synthesizeTextFile (namespace .getString ("text" ));
149+ } else {
150+ synthesizeSsmlFile (namespace .getString ("ssml" ));
151+ }
152+ } catch (ArgumentParserException e ) {
153+ parser .handleError (e );
154+ }
155+ }
156+ }
0 commit comments