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

Update Audio Profiles to beta #1332

Merged
merged 6 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 3 additions & 31 deletions texttospeech/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,8 @@ To get started, [download][maven-download] and [install][maven-install] it.
[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](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/QuickstartSample.java)
## Snippets
To verify the snippets are running correctly, you can run the tests via:
```
mvn exec:java -DQuickstart
```

## List Voices
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)
```
mvn exec:java -DListVoices
```

## Synthesize Text
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)
```
mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"'
```

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)
```
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](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client/src/main/java/com/example/texttospeech/SynthesizeFile.java)
```
mvn exec:java -DSynthesizeFile -Dexec.args='--text resources/hello.txt'
```

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)
```
mvn exec:java -DSynthesizeFile -Dexec.args='--ssml resources/hello.ssml'
mvn clean verify
```
123 changes: 0 additions & 123 deletions texttospeech/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,127 +58,4 @@
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ListAllSupportedVoices {
* 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 {
public static List<Voice> listAllSupportedVoices() throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Builds the text to speech list voices request
Expand All @@ -65,11 +65,8 @@ public static void listAllSupportedVoices() throws Exception {
System.out.format("Natural Sample Rate Hertz: %s\n\n",
voice.getNaturalSampleRateHertz());
}
return voices;
}
}
// [END tts_list_voices]

public static void main(String[] args) throws Exception {
listAllSupportedVoices();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class SynthesizeFile {
* @param textFile the text file to be synthesized. (e.g., hello.txt)
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeTextFile(String textFile)
public static ByteString synthesizeTextFile(String textFile)
throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
Expand Down Expand Up @@ -83,6 +83,7 @@ public static void synthesizeTextFile(String textFile)
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
return audioContents;
}
}
}
Expand All @@ -95,7 +96,7 @@ public static void synthesizeTextFile(String textFile)
* @param ssmlFile the ssml document to be synthesized. (e.g., hello.ssml)
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeSsmlFile(String ssmlFile)
public static ByteString synthesizeSsmlFile(String ssmlFile)
throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
Expand Down Expand Up @@ -128,29 +129,9 @@ public static void synthesizeSsmlFile(String ssmlFile)
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
return audioContents;
}
}
}
// [END tts_synthesize_ssml_file]

public static void main(String... args) throws Exception {
ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
.defaultHelp(true)
.description("Synthesize a text file or ssml file.");
MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
group.addArgument("--text").help("The text file from which to synthesize speech.");
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");

try {
Namespace namespace = parser.parseArgs(args);

if (namespace.get("text") != null) {
synthesizeTextFile(namespace.getString("text"));
} else {
synthesizeSsmlFile(namespace.getString("ssml"));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@

import java.io.FileOutputStream;
import java.io.OutputStream;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
import net.sourceforge.argparse4j.inf.Namespace;

/**
* Google Cloud TextToSpeech API sample application.
Expand All @@ -48,7 +43,7 @@ public class SynthesizeText {
* @param text the raw text to be synthesized. (e.g., "Hello there!")
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeText(String text) throws Exception {
public static ByteString synthesizeText(String text) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the text input to be synthesized
Expand Down Expand Up @@ -78,11 +73,59 @@ public static void synthesizeText(String text) throws Exception {
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
return audioContents;
}
}
}
// [END tts_synthesize_text]

// [START tts_synthesize_text_audio_profile]
/**
* Demonstrates using the Text to Speech client with audio profiles to synthesize text or ssml
*
* @param text the raw text to be synthesized. (e.g., "Hello there!")
* @param effectsProfile audio profile to be used for synthesis. (e.g.,
* "telephony-class-application")
* @throws Exception on TextToSpeechClient Errors.
*/
public static ByteString synthesizeTextWithAudioProfile(String text, String effectsProfile)
throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();

// Build the voice request
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();

// Select the type of audio file you want returned and the audio profile
AudioConfig audioConfig =
AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.addEffectsProfileId(effectsProfile) // audio profile
.build();

// Perform the text-to-speech request
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();

// Write the response to the output file.
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
return audioContents;
}
}
}
// [END tts_synthesize_text_audio_profile]

// [START tts_synthesize_ssml]
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
Expand All @@ -93,7 +136,7 @@ public static void synthesizeText(String text) throws Exception {
* @param ssml the ssml document to be synthesized. (e.g., "<?xml...")
* @throws Exception on TextToSpeechClient Errors.
*/
public static void synthesizeSsml(String ssml) throws Exception {
public static ByteString synthesizeSsml(String ssml) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the ssml input to be synthesized
Expand Down Expand Up @@ -123,33 +166,9 @@ public static void synthesizeSsml(String ssml) throws Exception {
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
return audioContents;
}
}
}
// [END tts_synthesize_ssml]

public static void main(String... args) throws Exception {

ArgumentParser parser =
ArgumentParsers.newFor("SynthesizeText")
.build()
.defaultHelp(true)
.description("Synthesize a text or ssml.");

MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
group.addArgument("--text").help("The text file from which to synthesize speech.");
group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");

try {
Namespace namespace = parser.parseArgs(args);

if (namespace.get("text") != null) {
synthesizeText(namespace.getString("text"));
} else {
synthesizeSsml(namespace.getString("ssml"));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

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

import com.google.cloud.texttospeech.v1.Voice;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -55,9 +57,10 @@ public void tearDown() {
@Test
public void testListAllSupportedVoices() throws Exception {
// Act
listAllSupportedVoices.listAllSupportedVoices();
List<Voice> voices = listAllSupportedVoices.listAllSupportedVoices();

// Assert
assertThat(voices.isEmpty()).isFalse();
String got = bout.toString();
assertThat(got).contains("en-US");
assertThat(got).contains("SSML Voice Gender: MALE");
Expand Down
Loading