diff --git a/ElevenLabs-DotNet-Tests/Test_Fixture_03_TextToSpeechEndpoint.cs b/ElevenLabs-DotNet-Tests/Test_Fixture_03_TextToSpeechEndpoint.cs index df2b351..3cef1c2 100644 --- a/ElevenLabs-DotNet-Tests/Test_Fixture_03_TextToSpeechEndpoint.cs +++ b/ElevenLabs-DotNet-Tests/Test_Fixture_03_TextToSpeechEndpoint.cs @@ -2,6 +2,7 @@ using NUnit.Framework; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -16,30 +17,32 @@ public async Task Test_01_TextToSpeech() var voice = (await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync()).FirstOrDefault(); Assert.NotNull(voice); var defaultVoiceSettings = await ElevenLabsClient.VoicesEndpoint.GetDefaultVoiceSettingsAsync(); - var clipPath = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings); - Assert.NotNull(clipPath); - Console.WriteLine(clipPath); + var voiceClip = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings); + Assert.NotNull(voiceClip); + Console.WriteLine(voiceClip); } [Test] - public async Task Test_02_TextToSpeechPartial() + public async Task Test_02_StreamTextToSpeech() { Assert.NotNull(ElevenLabsClient.TextToSpeechEndpoint); var voice = (await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync()).FirstOrDefault(); Assert.NotNull(voice); - bool callbackCalled = false; + var partialClips = new Queue(); var defaultVoiceSettings = await ElevenLabsClient.VoicesEndpoint.GetDefaultVoiceSettingsAsync(); - var clipPath = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings, - - partialClipCallback: (partialClip) => + var voiceClip = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings, + + partialClipCallback: async partialClip => { - callbackCalled = true; - return null; + Assert.IsNotNull(partialClip); + partialClips.Enqueue(partialClip); + await Task.CompletedTask; }); - - Assert.NotNull(clipPath); - Assert.IsTrue(callbackCalled); - Console.WriteLine(clipPath); + + Assert.NotNull(partialClips); + Assert.IsNotEmpty(partialClips); + Assert.NotNull(voiceClip); + Console.WriteLine(voiceClip.Id); } } } \ No newline at end of file diff --git a/ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs b/ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs index 7b23250..df9845e 100644 --- a/ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs +++ b/ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs @@ -87,11 +87,13 @@ public async Task TextToSpeechAsync(string text, Voice voice, VoiceSe { parameters.Add(OptimizeStreamingLatencyParameter, optimizeStreamingLatency.ToString()); } - var postRequest = new HttpRequestMessage(HttpMethod.Post, GetUrl($"/{voice.Id}{(partialClipCallback == null ? string.Empty : "/stream")}", parameters)) - { - Content = payload, - }; - var response = await Api.Client.SendAsync(postRequest, partialClipCallback == null ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead, cancellationToken); + + using var postRequest = new HttpRequestMessage(HttpMethod.Post, GetUrl($"/{voice.Id}{(partialClipCallback == null ? string.Empty : "/stream")}", parameters)); + postRequest.Content = payload; + var requestOption = partialClipCallback == null + ? HttpCompletionOption.ResponseContentRead + : HttpCompletionOption.ResponseHeadersRead; + var response = await Api.Client.SendAsync(postRequest, requestOption, cancellationToken); await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false); var clipId = response.Headers.GetValues(HistoryItemId).FirstOrDefault();