From ac6a732ec936cef3534bc87bfb7b4b5c0230990e Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 14 Oct 2021 15:20:40 -0700 Subject: [PATCH] samples: add voice selection (#333) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * samples: add voice selection * added imports * add ssmlvoicegender * lint fix * lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Added comments * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .../dialogflow/cx/DetectIntentStream.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java index 125581c83e0..b09609aea45 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java @@ -23,13 +23,18 @@ import com.google.cloud.dialogflow.cx.v3beta1.AudioEncoding; import com.google.cloud.dialogflow.cx.v3beta1.AudioInput; import com.google.cloud.dialogflow.cx.v3beta1.InputAudioConfig; +import com.google.cloud.dialogflow.cx.v3beta1.OutputAudioConfig; +import com.google.cloud.dialogflow.cx.v3beta1.OutputAudioEncoding; import com.google.cloud.dialogflow.cx.v3beta1.QueryInput; import com.google.cloud.dialogflow.cx.v3beta1.QueryResult; import com.google.cloud.dialogflow.cx.v3beta1.SessionName; import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient; import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings; +import com.google.cloud.dialogflow.cx.v3beta1.SsmlVoiceGender; import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentRequest; import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentResponse; +import com.google.cloud.dialogflow.cx.v3beta1.SynthesizeSpeechConfig; +import com.google.cloud.dialogflow.cx.v3beta1.VoiceSelectionParams; import com.google.protobuf.ByteString; import java.io.FileInputStream; import java.io.IOException; @@ -78,11 +83,33 @@ public static void detectIntentStream( BidiStream bidiStream = sessionsClient.streamingDetectIntentCallable().call(); + // Specify sssml name and gender + VoiceSelectionParams voiceSelection = + // Voices that are available https://cloud.google.com/text-to-speech/docs/voices + VoiceSelectionParams.newBuilder() + .setName("en-GB-Standard-A") + .setSsmlGender(SsmlVoiceGender.SSML_VOICE_GENDER_FEMALE) + .build(); + + SynthesizeSpeechConfig speechConfig = + SynthesizeSpeechConfig.newBuilder().setVoice(voiceSelection).build(); + + // Setup audio config + OutputAudioConfig audioConfig = + // Output enconding explanation + // https://cloud.google.com/dialogflow/cx/docs/reference/rpc/google.cloud.dialogflow.cx.v3#outputaudioencoding + OutputAudioConfig.newBuilder() + .setAudioEncoding(OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_UNSPECIFIED) + .setAudioEncodingValue(1) + .setSynthesizeSpeechConfig(speechConfig) + .build(); + // The first request must **only** contain the audio configuration: bidiStream.send( StreamingDetectIntentRequest.newBuilder() .setSession(session.toString()) .setQueryInput(queryInput) + .setOutputAudioConfig(audioConfig) .build()); try (FileInputStream audioStream = new FileInputStream(audioFilePath)) {