-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from 1 commit
659d146
6c81453
9959361
18d679d
9c0ca8a
7c1ddae
c4d8ec0
3b92678
ffe276e
e1292ad
f412b1f
88f53cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to create a 2nd method called 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(); | ||
|
@@ -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(); | ||
|
@@ -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."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Synthesize text 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(2) | ||
.metavar("TEXT", "EFFECTSPROFILE"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra |
||
synthesizeText( | ||
namespace.getList("text").get(0).toString(), | ||
namespace.getList("text").get(1).toString()); | ||
|
||
} else { | ||
synthesizeSsml(namespace.getString("ssml")); | ||
} | ||
} catch (ArgumentParserException e) { | ||
parser.handleError(e); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.