Skip to content

[FEATURE REQ] Support streaming TTS responses. #533

@bmdub

Description

@bmdub

Describe the feature or improvement you are requesting

AudioClient.GenerateSpeechAsync() will only return after the entire generated speech has been received by the client.

To get true streaming, one must set PipelineMessage.BufferResponse = false (for this particular use-case only.)

One solution is to add the following method to AudioClient.cs:

public virtual async IAsyncEnumerable<BinaryData> GenerateSpeechStreamingAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
    Argument.AssertNotNull(text, nameof(text));

    options ??= new();
    CreateSpeechGenerationOptions(text, voice, ref options);

    using BinaryContent content = options.ToBinaryContent();
    var requestOptions = cancellationToken.ToRequestOptions();
    using PipelineMessage message = CreateCreateSpeechRequest(content, requestOptions);
    message.BufferResponse = false;
    var result = ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, requestOptions).ConfigureAwait(false));
    var stream = result.GetRawResponse().ContentStream;
    var buffer = ArrayPool<byte>.Shared.Rent(1024 * 16);
    try
    {
        int bytesRead;
        while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
        {
            yield return BinaryData.FromBytes(buffer.AsMemory(0, bytesRead));
        }
    }
    finally
    {
        ArrayPool<byte>.Shared.Return(buffer);
    }
}

A better solution may be to simply return the Stream and have the user handle the reading/buffering.

If you're interested in a PR, lmk. (I had a previous PR #426 which was sub-optimal, altering a generated code file.)

Additional context

No response

Metadata

Metadata

Labels

feature-requestCategory: A new feature or enhancement to an existing feature is being requested.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions