Skip to content
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

Streaming text to speech? #45

Closed
nate-simpson opened this issue Jun 11, 2024 · 2 comments
Closed

Streaming text to speech? #45

nate-simpson opened this issue Jun 11, 2024 · 2 comments

Comments

@nate-simpson
Copy link

I see in the official docs at https://platform.openai.com/docs/guides/text-to-speech the following:

The Speech API provides support for real time audio streaming using chunk transfer encoding. This means that the audio is able to be played before the full file has been generated and made accessible.

There's python code for it, apparently, but it seems to be unsupported in this library so far. If it's supported, can you point me there? Otherwise, it would be really nice to have, as the latency with any vaguely long chunk of text for TTS is intolerably long for offering the ability to read back chat replies.

@KrzysztofCwalina
Copy link
Collaborator

KrzysztofCwalina commented Jun 11, 2024

I have not tried it, but there is an overload of the AudioClient.GenerateSppechFromText that might work for you:

public virtual ClientResult GenerateSpeechFromText(BinaryContent content, RequestOptions options = null);

to call this in streaming mode:

RequestOptions options = new() { BufferResponse = false };

var json = BinaryData.FromObjectAsJson(new {
            model = "tts-1",
            input = "Today is a wonderful day to build something people love!",
            voice = "alloy"
});

AudioClient client = ...;
var result = client.GenerateSpeechFromText(BinaryContent.Create(json), options);
PipelineResponse response = result.GetRawResponse();
using Stream stream = response.ContentStream; // very important to dispose the stream

@nate-simpson
Copy link
Author

This seems to work nicely. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants