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 1 commit
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
5 changes: 5 additions & 0 deletions texttospeech/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ This sample synthesizes text to an output audio file. [Java Code](https://github
mvn exec:java -DSynthesizeText -Dexec.args='--text "hello"'
```

This sample synthesizes text with an audio profile 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" "telephony-class-application"'
```

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>"'
Expand Down
2 changes: 1 addition & 1 deletion texttospeech/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.70.0-beta</version>
<version>0.80.0-beta</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,52 @@ public static void synthesizeText(String text) throws Exception {
}
// [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 void 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\"");
}
}
}
// [END tts_synthesize_text_audio_profile]

// [START tts_synthesize_ssml]
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
Expand Down Expand Up @@ -134,17 +180,29 @@ public static void main(String... args) throws Exception {
ArgumentParsers.newFor("SynthesizeText")
.build()
.defaultHelp(true)
.description("Synthesize a text or ssml.");
.description("Synthesize a text, text with audio effect profiles, or ssml.");

MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
group.addArgument("--text").help("The text file from which to synthesize speech.");
group
.addArgument("--text")
.help("The text file from which to synthesize speech.")
.nargs("+")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a different argument? .addArgument("--effect-profile").required(false) or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll just drop the CLI entirely like we did for Dialogflow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.metavar("TEXT", "EFFECTSPROFILE(optional)");
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"));
if ((namespace.get("text") != null)) {
if (namespace.getList("text").size() == 2) {
synthesizeTextWithAudioProfile(
namespace.getList("text").get(0).toString(),
namespace.getList("text").get(1).toString());

} else {
synthesizeText(namespace.getString("text"));
}

} else {
synthesizeSsml(namespace.getString("ssml"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SynthesizeTextIT {
private static String OUTPUT = "output.mp3";
private static String TEXT = "Hello there.";
private static String SSML = "<speak>Hello there.</speak>";
private static String EFFECTSPROFILE = "telephony-class-application";

private ByteArrayOutputStream bout;
private PrintStream out;
Expand Down Expand Up @@ -78,4 +79,16 @@ public void testSynthesizeSsml() throws Exception {
String got = bout.toString();
assertThat(got).contains("Audio content written to file \"output.mp3\"");
}

@Test
public void testSynthesizeTextWithAudioProfile() throws Exception {
// Act
SynthesizeText.synthesizeTextWithAudioProfile(TEXT, EFFECTSPROFILE);

// Assert
outputFile = new File(OUTPUT);
assertThat(outputFile.isFile()).isTrue();
String got = bout.toString();
assertThat(got).contains("Audio content written to file \"output.mp3\"");
}
}