Google Cloud Text-to-Speech v1 API - Class TextToSpeechClient (3.8.0)

public abstract class TextToSpeechClient

Reference documentation and code samples for the Google Cloud Text-to-Speech v1 API class TextToSpeechClient.

TextToSpeech client wrapper, for convenient use.

Inheritance

object > TextToSpeechClient

Derived Types

Namespace

Google.Cloud.TextToSpeech.V1

Assembly

Google.Cloud.TextToSpeech.V1.dll

Remarks

Service that implements Google Cloud Text-to-Speech API.

Properties

DefaultEndpoint

public static string DefaultEndpoint { get; }

The default endpoint for the TextToSpeech service, which is a host of "texttospeech.googleapis.com" and a port of 443.

Property Value
Type Description
string

DefaultScopes

public static IReadOnlyList<string> DefaultScopes { get; }

The default TextToSpeech scopes.

Property Value
Type Description
IReadOnlyListstring
Remarks

The default TextToSpeech scopes are:

GrpcClient

public virtual TextToSpeech.TextToSpeechClient GrpcClient { get; }

The underlying gRPC TextToSpeech client

Property Value
Type Description
TextToSpeechTextToSpeechClient

ServiceMetadata

public static ServiceMetadata ServiceMetadata { get; }

The service metadata associated with this client type.

Property Value
Type Description
ServiceMetadata

Methods

Create()

public static TextToSpeechClient Create()

Synchronously creates a TextToSpeechClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use TextToSpeechClientBuilder.

Returns
Type Description
TextToSpeechClient

The created TextToSpeechClient.

CreateAsync(CancellationToken)

public static Task<TextToSpeechClient> CreateAsync(CancellationToken cancellationToken = default)

Asynchronously creates a TextToSpeechClient using the default credentials, endpoint and settings. To specify custom credentials or other settings, use TextToSpeechClientBuilder.

Parameter
Name Description
cancellationToken CancellationToken

The CancellationToken to use while creating the client.

Returns
Type Description
TaskTextToSpeechClient

The task representing the created TextToSpeechClient.

ListVoices(ListVoicesRequest, CallSettings)

public virtual ListVoicesResponse ListVoices(ListVoicesRequest request, CallSettings callSettings = null)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
request ListVoicesRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
ListVoicesResponse

The RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
// Initialize request argument(s)
ListVoicesRequest request = new ListVoicesRequest { LanguageCode = "", };
// Make the request
ListVoicesResponse response = textToSpeechClient.ListVoices(request);

ListVoices(string, CallSettings)

public virtual ListVoicesResponse ListVoices(string languageCode, CallSettings callSettings = null)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
languageCode string

Optional. Recommended. BCP-47 language tag. If not specified, the API will return all supported voices. If specified, the ListVoices call will only return voices that can be used to synthesize this language_code. For example, if you specify "en-NZ", all "en-NZ" voices will be returned. If you specify "no", both "no-\*" (Norwegian) and "nb-\*" (Norwegian Bokmal) voices will be returned.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
ListVoicesResponse

The RPC response.

Example
TextToSpeechClient client = TextToSpeechClient.Create();
ListVoicesResponse response = client.ListVoices("en");
foreach (Voice voice in response.Voices)
{
    Console.WriteLine($"{voice.Name} ({voice.SsmlGender}); Language codes: {string.Join(", ", voice.LanguageCodes)}");
}

ListVoicesAsync(ListVoicesRequest, CallSettings)

public virtual Task<ListVoicesResponse> ListVoicesAsync(ListVoicesRequest request, CallSettings callSettings = null)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
request ListVoicesRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
TaskListVoicesResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
ListVoicesRequest request = new ListVoicesRequest { LanguageCode = "", };
// Make the request
ListVoicesResponse response = await textToSpeechClient.ListVoicesAsync(request);

ListVoicesAsync(ListVoicesRequest, CancellationToken)

public virtual Task<ListVoicesResponse> ListVoicesAsync(ListVoicesRequest request, CancellationToken cancellationToken)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
request ListVoicesRequest

The request object containing all of the parameters for the API call.

cancellationToken CancellationToken

A CancellationToken to use for this RPC.

Returns
Type Description
TaskListVoicesResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
ListVoicesRequest request = new ListVoicesRequest { LanguageCode = "", };
// Make the request
ListVoicesResponse response = await textToSpeechClient.ListVoicesAsync(request);

ListVoicesAsync(string, CallSettings)

public virtual Task<ListVoicesResponse> ListVoicesAsync(string languageCode, CallSettings callSettings = null)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
languageCode string

Optional. Recommended. BCP-47 language tag. If not specified, the API will return all supported voices. If specified, the ListVoices call will only return voices that can be used to synthesize this language_code. For example, if you specify "en-NZ", all "en-NZ" voices will be returned. If you specify "no", both "no-\*" (Norwegian) and "nb-\*" (Norwegian Bokmal) voices will be returned.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
TaskListVoicesResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
string languageCode = "";
// Make the request
ListVoicesResponse response = await textToSpeechClient.ListVoicesAsync(languageCode);

ListVoicesAsync(string, CancellationToken)

public virtual Task<ListVoicesResponse> ListVoicesAsync(string languageCode, CancellationToken cancellationToken)

Returns a list of Voice supported for synthesis.

Parameters
Name Description
languageCode string

Optional. Recommended. BCP-47 language tag. If not specified, the API will return all supported voices. If specified, the ListVoices call will only return voices that can be used to synthesize this language_code. For example, if you specify "en-NZ", all "en-NZ" voices will be returned. If you specify "no", both "no-\*" (Norwegian) and "nb-\*" (Norwegian Bokmal) voices will be returned.

cancellationToken CancellationToken

A CancellationToken to use for this RPC.

Returns
Type Description
TaskListVoicesResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
string languageCode = "";
// Make the request
ListVoicesResponse response = await textToSpeechClient.ListVoicesAsync(languageCode);

ShutdownDefaultChannelsAsync()

public static Task ShutdownDefaultChannelsAsync()

Shuts down any channels automatically created by Create() and CreateAsync(CancellationToken). Channels which weren't automatically created are not affected.

Returns
Type Description
Task

A task representing the asynchronous shutdown operation.

Remarks

After calling this method, further calls to Create() and CreateAsync(CancellationToken) will create new channels, which could in turn be shut down by another call to this method.

StreamingSynthesize(CallSettings, BidirectionalStreamingSettings)

public virtual TextToSpeechClient.StreamingSynthesizeStream StreamingSynthesize(CallSettings callSettings = null, BidirectionalStreamingSettings streamingSettings = null)

Performs bidirectional streaming speech synthesis: receive audio while sending text.

Parameters
Name Description
callSettings CallSettings

If not null, applies overrides to this RPC call.

streamingSettings BidirectionalStreamingSettings

If not null, applies streaming overrides to this RPC call.

Returns
Type Description
TextToSpeechClientStreamingSynthesizeStream

The client-server stream.

Example
// Create client
TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
// Initialize streaming call, retrieving the stream object
using TextToSpeechClient.StreamingSynthesizeStream response = textToSpeechClient.StreamingSynthesize();

// Sending requests and retrieving responses can be arbitrarily interleaved
// Exact sequence will depend on client/server behavior

// Create task to do something with responses from server
Task responseHandlerTask = Task.Run(async () =>
{
    // Note that C# 8 code can use await foreach
    AsyncResponseStream<StreamingSynthesizeResponse> responseStream = response.GetResponseStream();
    while (await responseStream.MoveNextAsync())
    {
        StreamingSynthesizeResponse responseItem = responseStream.Current;
        // Do something with streamed response
    }
    // The response stream has completed
});

// Send requests to the server
bool done = false;
while (!done)
{
    // Initialize a request
    StreamingSynthesizeRequest request = new StreamingSynthesizeRequest
    {
        StreamingConfig = new StreamingSynthesizeConfig(),
    };
    // Stream a request to the server
    await response.WriteAsync(request);
    // Set "done" to true when sending requests is complete
}

// Complete writing requests to the stream
await response.WriteCompleteAsync();
// Await the response handler
// This will complete once all server responses have been processed
await responseHandlerTask;

SynthesizeSpeech(SynthesisInput, VoiceSelectionParams, AudioConfig, CallSettings)

public virtual SynthesizeSpeechResponse SynthesizeSpeech(SynthesisInput input, VoiceSelectionParams voice, AudioConfig audioConfig, CallSettings callSettings = null)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
input SynthesisInput

Required. The Synthesizer requires either plain text or SSML as input.

voice VoiceSelectionParams

Required. The desired voice of the synthesized audio.

audioConfig AudioConfig

Required. The configuration of the synthesized audio.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
SynthesizeSpeechResponse

The RPC response.

Example
TextToSpeechClient client = TextToSpeechClient.Create();
// The input can be provided as text or SSML.
SynthesisInput input = new SynthesisInput
{
    Text = "This is a demonstration of the Google Cloud Text-to-Speech API"
};
// You can specify a particular voice, or ask the server to pick based
// on specified criteria.
VoiceSelectionParams voiceSelection = new VoiceSelectionParams
{
    LanguageCode = "en-US",
    SsmlGender = SsmlVoiceGender.Female
};
// The audio configuration determines the output format and speaking rate.
AudioConfig audioConfig = new AudioConfig
{
    AudioEncoding = AudioEncoding.Mp3
};
SynthesizeSpeechResponse response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);
using (Stream output = File.Create("sample.mp3"))
{
    // response.AudioContent is a ByteString. This can easily be converted into
    // a byte array or written to a stream.
    response.AudioContent.WriteTo(output);
}

SynthesizeSpeech(SynthesizeSpeechRequest, CallSettings)

public virtual SynthesizeSpeechResponse SynthesizeSpeech(SynthesizeSpeechRequest request, CallSettings callSettings = null)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
request SynthesizeSpeechRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
SynthesizeSpeechResponse

The RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
// Initialize request argument(s)
SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
{
    Input = new SynthesisInput(),
    Voice = new VoiceSelectionParams(),
    AudioConfig = new AudioConfig(),
    AdvancedVoiceOptions = new AdvancedVoiceOptions(),
};
// Make the request
SynthesizeSpeechResponse response = textToSpeechClient.SynthesizeSpeech(request);

SynthesizeSpeechAsync(SynthesisInput, VoiceSelectionParams, AudioConfig, CallSettings)

public virtual Task<SynthesizeSpeechResponse> SynthesizeSpeechAsync(SynthesisInput input, VoiceSelectionParams voice, AudioConfig audioConfig, CallSettings callSettings = null)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
input SynthesisInput

Required. The Synthesizer requires either plain text or SSML as input.

voice VoiceSelectionParams

Required. The desired voice of the synthesized audio.

audioConfig AudioConfig

Required. The configuration of the synthesized audio.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
TaskSynthesizeSpeechResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
SynthesisInput input = new SynthesisInput();
VoiceSelectionParams voice = new VoiceSelectionParams();
AudioConfig audioConfig = new AudioConfig();
// Make the request
SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(input, voice, audioConfig);

SynthesizeSpeechAsync(SynthesisInput, VoiceSelectionParams, AudioConfig, CancellationToken)

public virtual Task<SynthesizeSpeechResponse> SynthesizeSpeechAsync(SynthesisInput input, VoiceSelectionParams voice, AudioConfig audioConfig, CancellationToken cancellationToken)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
input SynthesisInput

Required. The Synthesizer requires either plain text or SSML as input.

voice VoiceSelectionParams

Required. The desired voice of the synthesized audio.

audioConfig AudioConfig

Required. The configuration of the synthesized audio.

cancellationToken CancellationToken

A CancellationToken to use for this RPC.

Returns
Type Description
TaskSynthesizeSpeechResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
SynthesisInput input = new SynthesisInput();
VoiceSelectionParams voice = new VoiceSelectionParams();
AudioConfig audioConfig = new AudioConfig();
// Make the request
SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(input, voice, audioConfig);

SynthesizeSpeechAsync(SynthesizeSpeechRequest, CallSettings)

public virtual Task<SynthesizeSpeechResponse> SynthesizeSpeechAsync(SynthesizeSpeechRequest request, CallSettings callSettings = null)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
request SynthesizeSpeechRequest

The request object containing all of the parameters for the API call.

callSettings CallSettings

If not null, applies overrides to this RPC call.

Returns
Type Description
TaskSynthesizeSpeechResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
{
    Input = new SynthesisInput(),
    Voice = new VoiceSelectionParams(),
    AudioConfig = new AudioConfig(),
    AdvancedVoiceOptions = new AdvancedVoiceOptions(),
};
// Make the request
SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(request);

SynthesizeSpeechAsync(SynthesizeSpeechRequest, CancellationToken)

public virtual Task<SynthesizeSpeechResponse> SynthesizeSpeechAsync(SynthesizeSpeechRequest request, CancellationToken cancellationToken)

Synthesizes speech synchronously: receive results after all text input has been processed.

Parameters
Name Description
request SynthesizeSpeechRequest

The request object containing all of the parameters for the API call.

cancellationToken CancellationToken

A CancellationToken to use for this RPC.

Returns
Type Description
TaskSynthesizeSpeechResponse

A Task containing the RPC response.

Example
// Create client
TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();
// Initialize request argument(s)
SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
{
    Input = new SynthesisInput(),
    Voice = new VoiceSelectionParams(),
    AudioConfig = new AudioConfig(),
    AdvancedVoiceOptions = new AdvancedVoiceOptions(),
};
// Make the request
SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(request);