Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Text-To-Speech Beta samples #1069

Merged
merged 13 commits into from
Mar 27, 2018
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<module>storage/xml-api/cmdline-sample</module>
<module>storage/xml-api/serviceaccount-appengine-sample</module>

<module>texttospeech/cloud-client</module>
<module>translate</module>
<module>translate/cloud-client</module>

Expand Down
75 changes: 75 additions & 0 deletions texttospeech/cloud-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Google Cloud Text-To-Speech API Java examples

The [Cloud Text To Speech API][texttospeech] enables you to generate and
customize synthesized speech from text or SSML.

These samples show how to list all supported voices, synthesize raw
text, and synthesize a file.

[texttospeech]: https://cloud.google.com/text-to-speech/
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java

## Prerequisites

### Download Maven

To get started, [download][maven-download] and [install][maven-install] it.

[maven]: https://maven.apache.org
[maven-download]: https://maven.apache.org/download.cgi
[maven-install]: https://maven.apache.org/install.html

### Setup

* Create a project with the [Google Cloud Console][cloud-console], and enable
the [TextToSpeech API][text-to-speech-api].
* [Set up][auth] authentication. For
example, from the Cloud Console, create a service account,
download its json credentials file, then set the appropriate environment
variable:

```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
```
* Build the samples
```
mvn clean package
```

[cloud-console]: https://console.cloud.google.com
[text-to-speech-api]: https://console.cloud.google.com/apis/api/texttospeech.googleapis.com/overview?project=_
[auth]: https://cloud.google.com/docs/authentication/getting-started

## Quckstart
Synthesize text to an output audio file. [Java Code](quickstart)
```
mvn exec:java -DQuickstart
```

## List Voices
This sample lists all the supported voices. [Java Code](list_voices)
```
mvn exec:java -DListVoices
```

## Synthesize Text
This sample synthesizes text to an output audio file. [Java Code](synthesize_text)
```
mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"'
```

This sample synthesizes ssml to an output audio file. [Java Code](synthesize_text)
```
mvn exec:java -DSynthesizeText -Dexec.args='--ssml "<speak>Hello there.</speak>"'
```

## Synthesize File
This sample synthesizes a text file to an output audio file. [Java Code](synthesize_file)
```
mvn exec:java -DSynthesizeFile -Dexec.args='--text resources/hello.txt'
```

This sample synthesizes a ssml file to an output audio file. [Java Code](synthesize_file)
```
mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml'
```
184 changes: 184 additions & 0 deletions texttospeech/cloud-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<!--
Copyright 2018, Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.texttospeech</groupId>
<artifactId>tts-samples</artifactId>
<packaging>jar</packaging>

<!-- Parent defines config for testing & linting. -->
<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- [START dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.41.0-beta</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
<version>0.8.1</version>
</dependency>
<!-- [END dependencies] -->

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.35</version>
<scope>test</scope>
</dependency>
</dependencies>


<profiles>
<!--Quickstart-->
<profile>
<id>Quickstart</id>
<activation>
<property>
<name>Quickstart</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.texttospeech.QuickstartSample</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!--ListVoices-->
<profile>
<id>ListVoices</id>
<activation>
<property>
<name>ListVoices</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.texttospeech.ListAllSupportedVoices</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!--SynthesizeFile-->
<profile>
<id>SynthesizeFile</id>
<activation>
<property>
<name>SynthesizeFile</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.texttospeech.SynthesizeFile</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!--SynthesizeText-->
<profile>
<id>SynthesizeText</id>
<activation>
<property>
<name>SynthesizeText</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.texttospeech.SynthesizeText</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
1 change: 1 addition & 0 deletions texttospeech/cloud-client/resources/hello.ssml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<speak>Hello there.</speak>
1 change: 1 addition & 0 deletions texttospeech/cloud-client/resources/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there!
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.texttospeech;

// Imports the Google Cloud client library
import com.google.cloud.texttospeech.v1beta1.ListVoicesRequest;
import com.google.cloud.texttospeech.v1beta1.ListVoicesResponse;
import com.google.cloud.texttospeech.v1beta1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1beta1.Voice;
import com.google.protobuf.ByteString;

import java.util.List;


/**
* Google Cloud TextToSpeech API sample application.
* Example usage: mvn package exec:java
* -Dexec.mainClass='com.example.texttospeech.ListAllSupportedVoices'
*/
public class ListAllSupportedVoices {

// [START tts_list_voices]
/**
* Demonstrates using the Text to Speech client to list the client's supported voices.
* @throws Exception on TextToSpeechClient Errors.
*/
public static void listAllSupportedVoices() throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Builds the text to speech list voices request
ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();

// Performs the list voices request
ListVoicesResponse response = textToSpeechClient.listVoices(request);
List<Voice> voices = response.getVoicesList();

for (Voice voice : voices) {
// Display the voice's name. Example: tpc-vocoded
System.out.format("Name: %s\n", voice.getName());

// Display the supported language codes for this voice. Example: "en-us"
List<ByteString> languageCodes = voice.getLanguageCodesList().asByteStringList();
for (ByteString languageCode : languageCodes) {
System.out.format("Supported Language: %s\n", languageCode.toStringUtf8());
}

// Display the SSML Voice Gender
System.out.format("SSML Voice Gender: %s\n", voice.getSsmlGender());

// Display the natural sample rate hertz for this voice. Example: 24000
System.out.format("Natural Sample Rate Hertz: %s\n\n",
voice.getNaturalSampleRateHertz());
}
}
}
// [END tts_list_voices]

public static void main(String[] args) throws Exception {
listAllSupportedVoices();
}
}
Loading