Skip to content

Commit

Permalink
ElevenLabs-DotNet 2.0.3 (#30)
Browse files Browse the repository at this point in the history
- Fixed text to speech streaming

---------

Co-authored-by: Stephen Hodgson <[email protected]>
Co-authored-by: Stephen Hodgson <[email protected]>
  • Loading branch information
3 people authored Nov 15, 2023
1 parent a87c1fe commit 8637b61
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
30 changes: 27 additions & 3 deletions ElevenLabs-DotNet-Tests/Test_Fixture_03_TextToSpeechEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -16,9 +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.Id);
}

[Test]
public async Task Test_02_StreamTextToSpeech()
{
Assert.NotNull(ElevenLabsClient.TextToSpeechEndpoint);
var voice = (await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync()).FirstOrDefault();
Assert.NotNull(voice);
var partialClips = new Queue<VoiceClip>();
var defaultVoiceSettings = await ElevenLabsClient.VoicesEndpoint.GetDefaultVoiceSettingsAsync();
var voiceClip = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings,

partialClipCallback: async partialClip =>
{
Assert.IsNotNull(partialClip);
partialClips.Enqueue(partialClip);
await Task.CompletedTask;
});

Assert.NotNull(partialClips);
Assert.IsNotEmpty(partialClips);
Assert.NotNull(voiceClip);
Console.WriteLine(voiceClip.Id);
}
}
}
6 changes: 4 additions & 2 deletions ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
<Company>RageAgainstThePixel</Company>
<Copyright>2023</Copyright>
<PackageTags>ElevenLabs, AI, ML, Voice, TTS</PackageTags>
<Version>2.0.2</Version>
<PackageReleaseNotes>Version 2.0.2
<Version>2.0.3</Version>
<PackageReleaseNotes>Version 2.0.3
- Fixed text to speech streaming
Version 2.0.2
- Added the u-law format
Version 2.0.1
- Pass some cancellation tokens to internals
Expand Down
8 changes: 7 additions & 1 deletion ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -87,7 +88,12 @@ public async Task<VoiceClip> TextToSpeechAsync(string text, Voice voice, VoiceSe
parameters.Add(OptimizeStreamingLatencyParameter, optimizeStreamingLatency.ToString());
}

var response = await Api.Client.PostAsync(GetUrl($"/{voice.Id}{(partialClipCallback == null ? string.Empty : "/stream")}", parameters), payload, cancellationToken).ConfigureAwait(false);
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();

Expand Down

0 comments on commit 8637b61

Please sign in to comment.