Skip to content

Commit 3cbb96c

Browse files
nnegreynirupa-kumar
authored andcommitted
Update Audio Profiles to beta (GoogleCloudPlatform#1332)
* Update Audio Profiles to beta * Remove CLI * Update README.md
1 parent d8a9225 commit 3cbb96c

File tree

8 files changed

+88
-219
lines changed

8 files changed

+88
-219
lines changed

texttospeech/cloud-client/README.md

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,8 @@ To get started, [download][maven-download] and [install][maven-install] it.
4040
[text-to-speech-api]: https://console.cloud.google.com/apis/api/texttospeech.googleapis.com/overview?project=_
4141
[auth]: https://cloud.google.com/docs/authentication/getting-started
4242

43-
## Quckstart
44-
Synthesize text to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java)
43+
## Snippets
44+
To verify the snippets are running correctly, you can run the tests via:
4545
```
46-
mvn exec:java -DQuickstart
47-
```
48-
49-
## List Voices
50-
This sample lists all the supported voices. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java)
51-
```
52-
mvn exec:java -DListVoices
53-
```
54-
55-
## Synthesize Text
56-
This sample synthesizes text to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java)
57-
```
58-
mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"'
59-
```
60-
61-
This sample synthesizes ssml to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java)
62-
```
63-
mvn exec:java -DSynthesizeText -Dexec.args='--ssml "<speak>Hello there.</speak>"'
64-
```
65-
66-
## Synthesize File
67-
This sample synthesizes a text file to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java)
68-
```
69-
mvn exec:java -DSynthesizeFile -Dexec.args='--text resources/hello.txt'
70-
```
71-
72-
This sample synthesizes a ssml file to an output audio file. [Java Code](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java)
73-
```
74-
mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml'
46+
mvn clean verify
7547
```

texttospeech/cloud-client/pom.xml

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -58,127 +58,4 @@
5858
<scope>test</scope>
5959
</dependency>
6060
</dependencies>
61-
62-
63-
<profiles>
64-
<!--Quickstart-->
65-
<profile>
66-
<id>Quickstart</id>
67-
<activation>
68-
<property>
69-
<name>Quickstart</name>
70-
</property>
71-
</activation>
72-
<build>
73-
<plugins>
74-
<plugin>
75-
<groupId>org.codehaus.mojo</groupId>
76-
<artifactId>exec-maven-plugin</artifactId>
77-
<version>1.6.0</version>
78-
<executions>
79-
<execution>
80-
<goals>
81-
<goal>java</goal>
82-
</goals>
83-
</execution>
84-
</executions>
85-
<configuration>
86-
<mainClass>com.example.texttospeech.QuickstartSample</mainClass>
87-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
88-
</configuration>
89-
</plugin>
90-
</plugins>
91-
</build>
92-
</profile>
93-
94-
<!--ListVoices-->
95-
<profile>
96-
<id>ListVoices</id>
97-
<activation>
98-
<property>
99-
<name>ListVoices</name>
100-
</property>
101-
</activation>
102-
<build>
103-
<plugins>
104-
<plugin>
105-
<groupId>org.codehaus.mojo</groupId>
106-
<artifactId>exec-maven-plugin</artifactId>
107-
<version>1.6.0</version>
108-
<executions>
109-
<execution>
110-
<goals>
111-
<goal>java</goal>
112-
</goals>
113-
</execution>
114-
</executions>
115-
<configuration>
116-
<mainClass>com.example.texttospeech.ListAllSupportedVoices</mainClass>
117-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
118-
</configuration>
119-
</plugin>
120-
</plugins>
121-
</build>
122-
</profile>
123-
124-
<!--SynthesizeFile-->
125-
<profile>
126-
<id>SynthesizeFile</id>
127-
<activation>
128-
<property>
129-
<name>SynthesizeFile</name>
130-
</property>
131-
</activation>
132-
<build>
133-
<plugins>
134-
<plugin>
135-
<groupId>org.codehaus.mojo</groupId>
136-
<artifactId>exec-maven-plugin</artifactId>
137-
<version>1.6.0</version>
138-
<executions>
139-
<execution>
140-
<goals>
141-
<goal>java</goal>
142-
</goals>
143-
</execution>
144-
</executions>
145-
<configuration>
146-
<mainClass>com.example.texttospeech.SynthesizeFile</mainClass>
147-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
148-
</configuration>
149-
</plugin>
150-
</plugins>
151-
</build>
152-
</profile>
153-
154-
<!--SynthesizeText-->
155-
<profile>
156-
<id>SynthesizeText</id>
157-
<activation>
158-
<property>
159-
<name>SynthesizeText</name>
160-
</property>
161-
</activation>
162-
<build>
163-
<plugins>
164-
<plugin>
165-
<groupId>org.codehaus.mojo</groupId>
166-
<artifactId>exec-maven-plugin</artifactId>
167-
<version>1.6.0</version>
168-
<executions>
169-
<execution>
170-
<goals>
171-
<goal>java</goal>
172-
</goals>
173-
</execution>
174-
</executions>
175-
<configuration>
176-
<mainClass>com.example.texttospeech.SynthesizeText</mainClass>
177-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
178-
</configuration>
179-
</plugin>
180-
</plugins>
181-
</build>
182-
</profile>
183-
</profiles>
18461
</project>

texttospeech/cloud-client/src/main/java/com/example/texttospeech/ListAllSupportedVoices.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ListAllSupportedVoices {
3838
* Demonstrates using the Text to Speech client to list the client's supported voices.
3939
* @throws Exception on TextToSpeechClient Errors.
4040
*/
41-
public static void listAllSupportedVoices() throws Exception {
41+
public static List<Voice> listAllSupportedVoices() throws Exception {
4242
// Instantiates a client
4343
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
4444
// Builds the text to speech list voices request
@@ -65,11 +65,8 @@ public static void listAllSupportedVoices() throws Exception {
6565
System.out.format("Natural Sample Rate Hertz: %s\n\n",
6666
voice.getNaturalSampleRateHertz());
6767
}
68+
return voices;
6869
}
6970
}
7071
// [END tts_list_voices]
71-
72-
public static void main(String[] args) throws Exception {
73-
listAllSupportedVoices();
74-
}
7572
}

texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SynthesizeFile {
5050
* @param textFile the text file to be synthesized. (e.g., hello.txt)
5151
* @throws Exception on TextToSpeechClient Errors.
5252
*/
53-
public static void synthesizeTextFile(String textFile)
53+
public static ByteString synthesizeTextFile(String textFile)
5454
throws Exception {
5555
// Instantiates a client
5656
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
@@ -83,6 +83,7 @@ public static void synthesizeTextFile(String textFile)
8383
try (OutputStream out = new FileOutputStream("output.mp3")) {
8484
out.write(audioContents.toByteArray());
8585
System.out.println("Audio content written to file \"output.mp3\"");
86+
return audioContents;
8687
}
8788
}
8889
}
@@ -95,7 +96,7 @@ public static void synthesizeTextFile(String textFile)
9596
* @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml)
9697
* @throws Exception on TextToSpeechClient Errors.
9798
*/
98-
public static void synthesizeSsmlFile(String ssmlFile)
99+
public static ByteString synthesizeSsmlFile(String ssmlFile)
99100
throws Exception {
100101
// Instantiates a client
101102
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
@@ -128,29 +129,9 @@ public static void synthesizeSsmlFile(String ssmlFile)
128129
try (OutputStream out = new FileOutputStream("output.mp3")) {
129130
out.write(audioContents.toByteArray());
130131
System.out.println("Audio content written to file \"output.mp3\"");
132+
return audioContents;
131133
}
132134
}
133135
}
134136
// [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-
}
156137
}

texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeText.java

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828

2929
import java.io.FileOutputStream;
3030
import java.io.OutputStream;
31-
import net.sourceforge.argparse4j.ArgumentParsers;
32-
import net.sourceforge.argparse4j.inf.ArgumentParser;
33-
import net.sourceforge.argparse4j.inf.ArgumentParserException;
34-
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
35-
import net.sourceforge.argparse4j.inf.Namespace;
3631

3732
/**
3833
* Google Cloud TextToSpeech API sample application.
@@ -48,7 +43,7 @@ public class SynthesizeText {
4843
* @param text the raw text to be synthesized. (e.g., "Hello there!")
4944
* @throws Exception on TextToSpeechClient Errors.
5045
*/
51-
public static void synthesizeText(String text) throws Exception {
46+
public static ByteString synthesizeText(String text) throws Exception {
5247
// Instantiates a client
5348
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
5449
// Set the text input to be synthesized
@@ -78,11 +73,59 @@ public static void synthesizeText(String text) throws Exception {
7873
try (OutputStream out = new FileOutputStream("output.mp3")) {
7974
out.write(audioContents.toByteArray());
8075
System.out.println("Audio content written to file \"output.mp3\"");
76+
return audioContents;
8177
}
8278
}
8379
}
8480
// [END tts_synthesize_text]
8581

82+
// [START tts_synthesize_text_audio_profile]
83+
/**
84+
* Demonstrates using the Text to Speech client with audio profiles to synthesize text or ssml
85+
*
86+
* @param text the raw text to be synthesized. (e.g., "Hello there!")
87+
* @param effectsProfile audio profile to be used for synthesis. (e.g.,
88+
* "telephony-class-application")
89+
* @throws Exception on TextToSpeechClient Errors.
90+
*/
91+
public static ByteString synthesizeTextWithAudioProfile(String text, String effectsProfile)
92+
throws Exception {
93+
// Instantiates a client
94+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
95+
// Set the text input to be synthesized
96+
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();
97+
98+
// Build the voice request
99+
VoiceSelectionParams voice =
100+
VoiceSelectionParams.newBuilder()
101+
.setLanguageCode("en-US") // languageCode = "en_us"
102+
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
103+
.build();
104+
105+
// Select the type of audio file you want returned and the audio profile
106+
AudioConfig audioConfig =
107+
AudioConfig.newBuilder()
108+
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
109+
.addEffectsProfileId(effectsProfile) // audio profile
110+
.build();
111+
112+
// Perform the text-to-speech request
113+
SynthesizeSpeechResponse response =
114+
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
115+
116+
// Get the audio contents from the response
117+
ByteString audioContents = response.getAudioContent();
118+
119+
// Write the response to the output file.
120+
try (OutputStream out = new FileOutputStream("output.mp3")) {
121+
out.write(audioContents.toByteArray());
122+
System.out.println("Audio content written to file \"output.mp3\"");
123+
return audioContents;
124+
}
125+
}
126+
}
127+
// [END tts_synthesize_text_audio_profile]
128+
86129
// [START tts_synthesize_ssml]
87130
/**
88131
* Demonstrates using the Text to Speech client to synthesize text or ssml.
@@ -93,7 +136,7 @@ public static void synthesizeText(String text) throws Exception {
93136
* @param ssml the ssml document to be synthesized. (e.g., "<?xml...")
94137
* @throws Exception on TextToSpeechClient Errors.
95138
*/
96-
public static void synthesizeSsml(String ssml) throws Exception {
139+
public static ByteString synthesizeSsml(String ssml) throws Exception {
97140
// Instantiates a client
98141
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
99142
// Set the ssml input to be synthesized
@@ -123,33 +166,9 @@ public static void synthesizeSsml(String ssml) throws Exception {
123166
try (OutputStream out = new FileOutputStream("output.mp3")) {
124167
out.write(audioContents.toByteArray());
125168
System.out.println("Audio content written to file \"output.mp3\"");
169+
return audioContents;
126170
}
127171
}
128172
}
129173
// [END tts_synthesize_ssml]
130-
131-
public static void main(String... args) throws Exception {
132-
133-
ArgumentParser parser =
134-
ArgumentParsers.newFor("SynthesizeText")
135-
.build()
136-
.defaultHelp(true)
137-
.description("Synthesize a text or ssml.");
138-
139-
MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
140-
group.addArgument("--text").help("The text file from which to synthesize speech.");
141-
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");
142-
143-
try {
144-
Namespace namespace = parser.parseArgs(args);
145-
146-
if (namespace.get("text") != null) {
147-
synthesizeText(namespace.getString("text"));
148-
} else {
149-
synthesizeSsml(namespace.getString("ssml"));
150-
}
151-
} catch (ArgumentParserException e) {
152-
parser.handleError(e);
153-
}
154-
}
155174
}

texttospeech/cloud-client/src/test/java/com/example/texttospeech/ListAllSupportedVoicesIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.cloud.texttospeech.v1.Voice;
2122
import java.io.ByteArrayOutputStream;
2223
import java.io.PrintStream;
2324

25+
import java.util.List;
2426
import org.junit.After;
2527
import org.junit.Before;
2628
import org.junit.Test;
@@ -55,9 +57,10 @@ public void tearDown() {
5557
@Test
5658
public void testListAllSupportedVoices() throws Exception {
5759
// Act
58-
listAllSupportedVoices.listAllSupportedVoices();
60+
List<Voice> voices = listAllSupportedVoices.listAllSupportedVoices();
5961

6062
// Assert
63+
assertThat(voices.isEmpty()).isFalse();
6164
String got = bout.toString();
6265
assertThat(got).contains("en-US");
6366
assertThat(got).contains("SSML Voice Gender: MALE");

0 commit comments

Comments
 (0)