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

Adding TTS Beta samples : Audio profile #1152

Merged
merged 12 commits into from
Jul 20, 2018
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
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.52.0-beta</version>
<version>0.53.0-beta</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,42 @@
import net.sourceforge.argparse4j.inf.Namespace;

/**
* Google Cloud TextToSpeech API sample application.
* Example usage: mvn package exec:java -Dexec.mainClass='com.example.texttospeech.SynthesizeText'
* -Dexec.args='text "hello"'
* Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java
Copy link
Contributor

Choose a reason for hiding this comment

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

This can stay on a separate line.

* -Dexec.mainClass='com.example.texttospeech.SynthesizeText' -Dexec.args='--text "hello"
* "telephony-class-application"'
*/
public class SynthesizeText {

// [START tts_synthesize_text]
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
*
* @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 void synthesizeText(String text, String effectsprofile) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to create a 2nd method called synthesizeTextWithAudioProfile

See: https://github.com/GoogleCloudPlatform/python-docs-samples/pull/1538/files

// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder()
.setText(text)
.build();
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();
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
AudioConfig audioConfig = AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();
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);
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
Expand All @@ -87,34 +88,34 @@ public static void synthesizeText(String text)
/**
* Demonstrates using the Text to Speech client to synthesize text or ssml.
*
* Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
* <p>Note: ssml must be well-formed according to: (https://www.w3.org/TR/speech-synthesis/
* Example: <speak>Hello there.</speak>
*
* @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 void synthesizeSsml(String ssml) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the ssml input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder()
.setSsml(ssml)
.build();
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssml).build();

// Build the voice request
VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US") // languageCode = "en_us"
.setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE
.build();
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
AudioConfig audioConfig = AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();
AudioConfig audioConfig =
AudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.MP3) // MP3 audio.
.build();

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

// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
Expand All @@ -129,23 +130,34 @@ public static void synthesizeSsml(String ssml)
// [END tts_synthesize_ssml]

public static void main(String... args) throws Exception {
ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build()
.defaultHelp(true)
.description("Synthesize a text file or ssml file.");

ArgumentParser parser =
ArgumentParsers.newFor("SynthesizeText")
.build()
.defaultHelp(true)
.description("Synthesize a text with audio effect profiles or ssml.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Synthesize text or ssml

  • drop audio effect profiles.


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(2)
.metavar("TEXT", "EFFECTSPROFILE");
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 be removed for the non-beta client the EFFECTSPROFILE

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra () not needed.

synthesizeText(
namespace.getList("text").get(0).toString(),
namespace.getList("text").get(1).toString());

} else {
synthesizeSsml(namespace.getString("ssml"));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
}
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 All @@ -58,7 +59,7 @@ public void tearDown() {
@Test
public void testSynthesizeText() throws Exception {
// Act
SynthesizeText.synthesizeText(TEXT);
SynthesizeText.synthesizeText(TEXT,EFFECTSPROFILE);

// Assert
outputFile = new File(OUTPUT);
Expand All @@ -70,7 +71,7 @@ public void testSynthesizeText() throws Exception {
@Test
public void testSynthesizeSsml() throws Exception {
// Act
SynthesizeText.synthesizeText(SSML);
SynthesizeText.synthesizeSsml(SSML);

// Assert
outputFile = new File(OUTPUT);
Expand Down