1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.texttospeech.v1; 18 19 import com.google.api.gax.core.BackgroundResource; 20 import com.google.api.gax.rpc.UnaryCallable; 21 import com.google.cloud.texttospeech.v1.stub.TextToSpeechStub; 22 import com.google.cloud.texttospeech.v1.stub.TextToSpeechStubSettings; 23 import java.io.IOException; 24 import java.util.concurrent.TimeUnit; 25 import javax.annotation.Generated; 26 27 // AUTO-GENERATED DOCUMENTATION AND CLASS. 28 /** 29 * Service Description: Service that implements Google Cloud Text-to-Speech API. 30 * 31 * <p>This class provides the ability to make remote calls to the backing service through method 32 * calls that map to API methods. Sample code to get started: 33 * 34 * <pre>{@code 35 * // This snippet has been automatically generated and should be regarded as a code template only. 36 * // It will require modifications to work: 37 * // - It may require correct/in-range values for request initialization. 38 * // - It may require specifying regional endpoints when creating the service client as shown in 39 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 40 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 41 * String languageCode = "languageCode-2092349083"; 42 * ListVoicesResponse response = textToSpeechClient.listVoices(languageCode); 43 * } 44 * }</pre> 45 * 46 * <p>Note: close() needs to be called on the TextToSpeechClient object to clean up resources such 47 * as threads. In the example above, try-with-resources is used, which automatically calls close(). 48 * 49 * <p>The surface of this class includes several types of Java methods for each of the API's 50 * methods: 51 * 52 * <ol> 53 * <li>A "flattened" method. With this type of method, the fields of the request type have been 54 * converted into function parameters. It may be the case that not all fields are available as 55 * parameters, and not every API method will have a flattened method entry point. 56 * <li>A "request object" method. This type of method only takes one parameter, a request object, 57 * which must be constructed before the call. Not every API method will have a request object 58 * method. 59 * <li>A "callable" method. This type of method takes no parameters and returns an immutable API 60 * callable object, which can be used to initiate calls to the service. 61 * </ol> 62 * 63 * <p>See the individual methods for example code. 64 * 65 * <p>Many parameters require resource names to be formatted in a particular way. To assist with 66 * these names, this class includes a format method for each type of name, and additionally a parse 67 * method to extract the individual identifiers contained within names that are returned. 68 * 69 * <p>This class can be customized by passing in a custom instance of TextToSpeechSettings to 70 * create(). For example: 71 * 72 * <p>To customize credentials: 73 * 74 * <pre>{@code 75 * // This snippet has been automatically generated and should be regarded as a code template only. 76 * // It will require modifications to work: 77 * // - It may require correct/in-range values for request initialization. 78 * // - It may require specifying regional endpoints when creating the service client as shown in 79 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 80 * TextToSpeechSettings textToSpeechSettings = 81 * TextToSpeechSettings.newBuilder() 82 * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 83 * .build(); 84 * TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(textToSpeechSettings); 85 * }</pre> 86 * 87 * <p>To customize the endpoint: 88 * 89 * <pre>{@code 90 * // This snippet has been automatically generated and should be regarded as a code template only. 91 * // It will require modifications to work: 92 * // - It may require correct/in-range values for request initialization. 93 * // - It may require specifying regional endpoints when creating the service client as shown in 94 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 95 * TextToSpeechSettings textToSpeechSettings = 96 * TextToSpeechSettings.newBuilder().setEndpoint(myEndpoint).build(); 97 * TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(textToSpeechSettings); 98 * }</pre> 99 * 100 * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over 101 * the wire: 102 * 103 * <pre>{@code 104 * // This snippet has been automatically generated and should be regarded as a code template only. 105 * // It will require modifications to work: 106 * // - It may require correct/in-range values for request initialization. 107 * // - It may require specifying regional endpoints when creating the service client as shown in 108 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 109 * TextToSpeechSettings textToSpeechSettings = TextToSpeechSettings.newHttpJsonBuilder().build(); 110 * TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(textToSpeechSettings); 111 * }</pre> 112 * 113 * <p>Please refer to the GitHub repository's samples for more quickstart code snippets. 114 */ 115 @Generated("by gapic-generator-java") 116 public class TextToSpeechClient implements BackgroundResource { 117 private final TextToSpeechSettings settings; 118 private final TextToSpeechStub stub; 119 120 /** Constructs an instance of TextToSpeechClient with default settings. */ create()121 public static final TextToSpeechClient create() throws IOException { 122 return create(TextToSpeechSettings.newBuilder().build()); 123 } 124 125 /** 126 * Constructs an instance of TextToSpeechClient, using the given settings. The channels are 127 * created based on the settings passed in, or defaults for any settings that are not set. 128 */ create(TextToSpeechSettings settings)129 public static final TextToSpeechClient create(TextToSpeechSettings settings) throws IOException { 130 return new TextToSpeechClient(settings); 131 } 132 133 /** 134 * Constructs an instance of TextToSpeechClient, using the given stub for making calls. This is 135 * for advanced usage - prefer using create(TextToSpeechSettings). 136 */ create(TextToSpeechStub stub)137 public static final TextToSpeechClient create(TextToSpeechStub stub) { 138 return new TextToSpeechClient(stub); 139 } 140 141 /** 142 * Constructs an instance of TextToSpeechClient, using the given settings. This is protected so 143 * that it is easy to make a subclass, but otherwise, the static factory methods should be 144 * preferred. 145 */ TextToSpeechClient(TextToSpeechSettings settings)146 protected TextToSpeechClient(TextToSpeechSettings settings) throws IOException { 147 this.settings = settings; 148 this.stub = ((TextToSpeechStubSettings) settings.getStubSettings()).createStub(); 149 } 150 TextToSpeechClient(TextToSpeechStub stub)151 protected TextToSpeechClient(TextToSpeechStub stub) { 152 this.settings = null; 153 this.stub = stub; 154 } 155 getSettings()156 public final TextToSpeechSettings getSettings() { 157 return settings; 158 } 159 getStub()160 public TextToSpeechStub getStub() { 161 return stub; 162 } 163 164 // AUTO-GENERATED DOCUMENTATION AND METHOD. 165 /** 166 * Returns a list of Voice supported for synthesis. 167 * 168 * <p>Sample code: 169 * 170 * <pre>{@code 171 * // This snippet has been automatically generated and should be regarded as a code template only. 172 * // It will require modifications to work: 173 * // - It may require correct/in-range values for request initialization. 174 * // - It may require specifying regional endpoints when creating the service client as shown in 175 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 176 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 177 * String languageCode = "languageCode-2092349083"; 178 * ListVoicesResponse response = textToSpeechClient.listVoices(languageCode); 179 * } 180 * }</pre> 181 * 182 * @param languageCode Optional. Recommended. 183 * [BCP-47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt) language tag. If not specified, the 184 * API will return all supported voices. If specified, the ListVoices call will only return 185 * voices that can be used to synthesize this language_code. For example, if you specify 186 * `"en-NZ"`, all `"en-NZ"` voices will be returned. If you specify `"no"`, both 187 * `"no-\\*"` (Norwegian) and `"nb-\\*"` (Norwegian Bokmal) voices will be returned. 188 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 189 */ listVoices(String languageCode)190 public final ListVoicesResponse listVoices(String languageCode) { 191 ListVoicesRequest request = 192 ListVoicesRequest.newBuilder().setLanguageCode(languageCode).build(); 193 return listVoices(request); 194 } 195 196 // AUTO-GENERATED DOCUMENTATION AND METHOD. 197 /** 198 * Returns a list of Voice supported for synthesis. 199 * 200 * <p>Sample code: 201 * 202 * <pre>{@code 203 * // This snippet has been automatically generated and should be regarded as a code template only. 204 * // It will require modifications to work: 205 * // - It may require correct/in-range values for request initialization. 206 * // - It may require specifying regional endpoints when creating the service client as shown in 207 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 208 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 209 * ListVoicesRequest request = 210 * ListVoicesRequest.newBuilder().setLanguageCode("languageCode-2092349083").build(); 211 * ListVoicesResponse response = textToSpeechClient.listVoices(request); 212 * } 213 * }</pre> 214 * 215 * @param request The request object containing all of the parameters for the API call. 216 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 217 */ listVoices(ListVoicesRequest request)218 public final ListVoicesResponse listVoices(ListVoicesRequest request) { 219 return listVoicesCallable().call(request); 220 } 221 222 // AUTO-GENERATED DOCUMENTATION AND METHOD. 223 /** 224 * Returns a list of Voice supported for synthesis. 225 * 226 * <p>Sample code: 227 * 228 * <pre>{@code 229 * // This snippet has been automatically generated and should be regarded as a code template only. 230 * // It will require modifications to work: 231 * // - It may require correct/in-range values for request initialization. 232 * // - It may require specifying regional endpoints when creating the service client as shown in 233 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 234 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 235 * ListVoicesRequest request = 236 * ListVoicesRequest.newBuilder().setLanguageCode("languageCode-2092349083").build(); 237 * ApiFuture<ListVoicesResponse> future = 238 * textToSpeechClient.listVoicesCallable().futureCall(request); 239 * // Do something. 240 * ListVoicesResponse response = future.get(); 241 * } 242 * }</pre> 243 */ listVoicesCallable()244 public final UnaryCallable<ListVoicesRequest, ListVoicesResponse> listVoicesCallable() { 245 return stub.listVoicesCallable(); 246 } 247 248 // AUTO-GENERATED DOCUMENTATION AND METHOD. 249 /** 250 * Synthesizes speech synchronously: receive results after all text input has been processed. 251 * 252 * <p>Sample code: 253 * 254 * <pre>{@code 255 * // This snippet has been automatically generated and should be regarded as a code template only. 256 * // It will require modifications to work: 257 * // - It may require correct/in-range values for request initialization. 258 * // - It may require specifying regional endpoints when creating the service client as shown in 259 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 260 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 261 * SynthesisInput input = SynthesisInput.newBuilder().build(); 262 * VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().build(); 263 * AudioConfig audioConfig = AudioConfig.newBuilder().build(); 264 * SynthesizeSpeechResponse response = 265 * textToSpeechClient.synthesizeSpeech(input, voice, audioConfig); 266 * } 267 * }</pre> 268 * 269 * @param input Required. The Synthesizer requires either plain text or SSML as input. 270 * @param voice Required. The desired voice of the synthesized audio. 271 * @param audioConfig Required. The configuration of the synthesized audio. 272 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 273 */ synthesizeSpeech( SynthesisInput input, VoiceSelectionParams voice, AudioConfig audioConfig)274 public final SynthesizeSpeechResponse synthesizeSpeech( 275 SynthesisInput input, VoiceSelectionParams voice, AudioConfig audioConfig) { 276 SynthesizeSpeechRequest request = 277 SynthesizeSpeechRequest.newBuilder() 278 .setInput(input) 279 .setVoice(voice) 280 .setAudioConfig(audioConfig) 281 .build(); 282 return synthesizeSpeech(request); 283 } 284 285 // AUTO-GENERATED DOCUMENTATION AND METHOD. 286 /** 287 * Synthesizes speech synchronously: receive results after all text input has been processed. 288 * 289 * <p>Sample code: 290 * 291 * <pre>{@code 292 * // This snippet has been automatically generated and should be regarded as a code template only. 293 * // It will require modifications to work: 294 * // - It may require correct/in-range values for request initialization. 295 * // - It may require specifying regional endpoints when creating the service client as shown in 296 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 297 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 298 * SynthesizeSpeechRequest request = 299 * SynthesizeSpeechRequest.newBuilder() 300 * .setInput(SynthesisInput.newBuilder().build()) 301 * .setVoice(VoiceSelectionParams.newBuilder().build()) 302 * .setAudioConfig(AudioConfig.newBuilder().build()) 303 * .build(); 304 * SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(request); 305 * } 306 * }</pre> 307 * 308 * @param request The request object containing all of the parameters for the API call. 309 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 310 */ synthesizeSpeech(SynthesizeSpeechRequest request)311 public final SynthesizeSpeechResponse synthesizeSpeech(SynthesizeSpeechRequest request) { 312 return synthesizeSpeechCallable().call(request); 313 } 314 315 // AUTO-GENERATED DOCUMENTATION AND METHOD. 316 /** 317 * Synthesizes speech synchronously: receive results after all text input has been processed. 318 * 319 * <p>Sample code: 320 * 321 * <pre>{@code 322 * // This snippet has been automatically generated and should be regarded as a code template only. 323 * // It will require modifications to work: 324 * // - It may require correct/in-range values for request initialization. 325 * // - It may require specifying regional endpoints when creating the service client as shown in 326 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 327 * try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) { 328 * SynthesizeSpeechRequest request = 329 * SynthesizeSpeechRequest.newBuilder() 330 * .setInput(SynthesisInput.newBuilder().build()) 331 * .setVoice(VoiceSelectionParams.newBuilder().build()) 332 * .setAudioConfig(AudioConfig.newBuilder().build()) 333 * .build(); 334 * ApiFuture<SynthesizeSpeechResponse> future = 335 * textToSpeechClient.synthesizeSpeechCallable().futureCall(request); 336 * // Do something. 337 * SynthesizeSpeechResponse response = future.get(); 338 * } 339 * }</pre> 340 */ 341 public final UnaryCallable<SynthesizeSpeechRequest, SynthesizeSpeechResponse> synthesizeSpeechCallable()342 synthesizeSpeechCallable() { 343 return stub.synthesizeSpeechCallable(); 344 } 345 346 @Override close()347 public final void close() { 348 stub.close(); 349 } 350 351 @Override shutdown()352 public void shutdown() { 353 stub.shutdown(); 354 } 355 356 @Override isShutdown()357 public boolean isShutdown() { 358 return stub.isShutdown(); 359 } 360 361 @Override isTerminated()362 public boolean isTerminated() { 363 return stub.isTerminated(); 364 } 365 366 @Override shutdownNow()367 public void shutdownNow() { 368 stub.shutdownNow(); 369 } 370 371 @Override awaitTermination(long duration, TimeUnit unit)372 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 373 return stub.awaitTermination(duration, unit); 374 } 375 } 376