Skip to content

Commit 6fc6f51

Browse files
authored
Add Text-To-Speech Beta samples (GoogleCloudPlatform#1069)
* Add Text-To-Speech Beta samples * Update README * Clarification * Update doc page link * Tell the user an output file was created * Simplify SSML * Update based on feedback * Update missed audio encodings
1 parent 37e74e9 commit 6fc6f51

File tree

12 files changed

+950
-0
lines changed

12 files changed

+950
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<module>storage/xml-api/cmdline-sample</module>
8989
<module>storage/xml-api/serviceaccount-appengine-sample</module>
9090

91+
<module>texttospeech/cloud-client</module>
9192
<module>translate</module>
9293
<module>translate/cloud-client</module>
9394

texttospeech/cloud-client/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Google Cloud Text-To-Speech API Java examples
2+
3+
The [Cloud Text To Speech API][texttospeech] enables you to generate and
4+
customize synthesized speech from text or SSML.
5+
6+
These samples show how to list all supported voices, synthesize raw
7+
text, and synthesize a file.
8+
9+
[texttospeech]: https://cloud.google.com/text-to-speech/
10+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
11+
12+
## Prerequisites
13+
14+
### Download Maven
15+
16+
To get started, [download][maven-download] and [install][maven-install] it.
17+
18+
[maven]: https://maven.apache.org
19+
[maven-download]: https://maven.apache.org/download.cgi
20+
[maven-install]: https://maven.apache.org/install.html
21+
22+
### Setup
23+
24+
* Create a project with the [Google Cloud Console][cloud-console], and enable
25+
the [TextToSpeech API][text-to-speech-api].
26+
* [Set up][auth] authentication. For
27+
example, from the Cloud Console, create a service account,
28+
download its json credentials file, then set the appropriate environment
29+
variable:
30+
31+
```bash
32+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
33+
```
34+
* Build the samples
35+
```
36+
mvn clean package
37+
```
38+
39+
[cloud-console]: https://console.cloud.google.com
40+
[text-to-speech-api]: https://console.cloud.google.com/apis/api/texttospeech.googleapis.com/overview?project=_
41+
[auth]: https://cloud.google.com/docs/authentication/getting-started
42+
43+
## Quckstart
44+
Synthesize text to an output audio file. [Java Code](quickstart)
45+
```
46+
mvn exec:java -DQuickstart
47+
```
48+
49+
## List Voices
50+
This sample lists all the supported voices. [Java Code](list_voices)
51+
```
52+
mvn exec:java -DListVoices
53+
```
54+
55+
## Synthesize Text
56+
This sample synthesizes text to an output audio file. [Java Code](synthesize_text)
57+
```
58+
mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"'
59+
```
60+
61+
This sample synthesizes ssml to an output audio file. [Java Code](synthesize_text)
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](synthesize_file)
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](synthesize_file)
73+
```
74+
mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml'
75+
```

texttospeech/cloud-client/pom.xml

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<!--
2+
Copyright 2018, Google Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<project>
14+
<modelVersion>4.0.0</modelVersion>
15+
<groupId>com.example.texttospeech</groupId>
16+
<artifactId>tts-samples</artifactId>
17+
<packaging>jar</packaging>
18+
19+
<!-- Parent defines config for testing & linting. -->
20+
<parent>
21+
<artifactId>doc-samples</artifactId>
22+
<groupId>com.google.cloud</groupId>
23+
<version>1.0.0</version>
24+
<relativePath>../..</relativePath>
25+
</parent>
26+
27+
<properties>
28+
<maven.compiler.target>1.8</maven.compiler.target>
29+
<maven.compiler.source>1.8</maven.compiler.source>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
</properties>
32+
33+
<dependencies>
34+
<!-- [START dependencies] -->
35+
<dependency>
36+
<groupId>com.google.cloud</groupId>
37+
<artifactId>google-cloud-texttospeech</artifactId>
38+
<version>0.41.0-beta</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>net.sourceforge.argparse4j</groupId>
42+
<artifactId>argparse4j</artifactId>
43+
<version>0.8.1</version>
44+
</dependency>
45+
<!-- [END dependencies] -->
46+
47+
<!-- Test dependencies -->
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.12</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.truth</groupId>
56+
<artifactId>truth</artifactId>
57+
<version>0.35</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</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>
184+
</project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<speak>Hello there.</speak>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there!
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.ListVoicesRequest;
21+
import com.google.cloud.texttospeech.v1beta1.ListVoicesResponse;
22+
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
23+
import com.google.cloud.texttospeech.v1beta1.Voice;
24+
import com.google.protobuf.ByteString;
25+
26+
import java.util.List;
27+
28+
29+
/**
30+
* Google Cloud TextToSpeech API sample application.
31+
* Example usage: mvn package exec:java
32+
* -Dexec.mainClass='com.example.texttospeech.ListAllSupportedVoices'
33+
*/
34+
public class ListAllSupportedVoices {
35+
36+
// [START tts_list_voices]
37+
/**
38+
* Demonstrates using the Text to Speech client to list the client's supported voices.
39+
* @throws Exception on TextToSpeechClient Errors.
40+
*/
41+
public static void listAllSupportedVoices() throws Exception {
42+
// Instantiates a client
43+
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
44+
// Builds the text to speech list voices request
45+
ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();
46+
47+
// Performs the list voices request
48+
ListVoicesResponse response = textToSpeechClient.listVoices(request);
49+
List<Voice> voices = response.getVoicesList();
50+
51+
for (Voice voice : voices) {
52+
// Display the voice's name. Example: tpc-vocoded
53+
System.out.format("Name: %s\n", voice.getName());
54+
55+
// Display the supported language codes for this voice. Example: "en-us"
56+
List<ByteString> languageCodes = voice.getLanguageCodesList().asByteStringList();
57+
for (ByteString languageCode : languageCodes) {
58+
System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
59+
}
60+
61+
// Display the SSML Voice Gender
62+
System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());
63+
64+
// Display the natural sample rate hertz for this voice. Example: 24000
65+
System.out.format("Natural Sample Rate Hertz: %s\n\n",
66+
voice.getNaturalSampleRateHertz());
67+
}
68+
}
69+
}
70+
// [END tts_list_voices]
71+
72+
public static void main(String[] args) throws Exception {
73+
listAllSupportedVoices();
74+
}
75+
}

0 commit comments

Comments
 (0)