Skip to content

Add GenerateSpeechStreamingAsync() for streaming TTS responses. #426

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/Custom/Audio/AudioClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Buffers;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -108,9 +111,47 @@ public virtual async Task<ClientResult<BinaryData>> GenerateSpeechAsync(string t

using BinaryContent content = options;
ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
await result.GetRawResponse().BufferContentAsync(cancellationToken).ConfigureAwait(false);
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

/// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
/// <remarks>
/// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
/// via <see cref="SpeechGenerationOptions.ResponseFormat"/>.
/// </remarks>
/// <param name="text"> The text to generate audio for. </param>
/// <param name="voice"> The voice to use in the generated audio. </param>
/// <param name="options"> The options to configure the audio generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception>
/// <returns> Streaming chunks of the generated audio in the specified output format. </returns>
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;
ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).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);
}
}

/// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
/// <remarks>
/// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
Expand All @@ -130,7 +171,8 @@ public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpe
CreateSpeechGenerationOptions(text, voice, ref options);

using BinaryContent content = options;
ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions()); ;
ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions());
result.GetRawResponse().BufferContent(cancellationToken);
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

Expand Down
1 change: 1 addition & 0 deletions src/Generated/AudioClient.RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal virtual PipelineMessage CreateCreateSpeechRequest(BinaryContent content
{
PipelineMessage message = Pipeline.CreateMessage();
message.ResponseClassifier = PipelineMessageClassifier200;
message.BufferResponse = false;
PipelineRequest request = message.Request;
request.Method = "POST";
ClientUriBuilder uri = new ClientUriBuilder();
Expand Down