Skip to content

Commit

Permalink
Add the inactivity timeout parameter. Remove the keep-alive messages …
Browse files Browse the repository at this point in the history
…(as they don't work and stop the connection). And allow the sending of white space messages for testing.
  • Loading branch information
ocinon committed Oct 22, 2024
1 parent b44389f commit 5d4d0d0
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions ElevenLabs-DotNet/TextToSpeech/TextToSpeechWebSocketEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class TextToSpeechWebSocketEndpoint : ElevenLabsBaseEndPoint
private const string EnableSsmlParsingParameter = "enable_ssml_parsing";
private const string OptimizeStreamingLatencyParameter = "optimize_streaming_latency";
private const string OutputFormatParameter = "output_format";
private const string InactivityTimeoutParameter = "inactivity_timeout";

public TextToSpeechWebSocketEndpoint(ElevenLabsClient client) : base(client)
{
Expand Down Expand Up @@ -63,13 +64,17 @@ public TextToSpeechWebSocketEndpoint(ElevenLabsClient client) : base(client)
/// 4 - max latency optimizations, but also with text normalizer turned off for even more latency savings
/// (best latency, but can mispronounce eg numbers and dates).
/// </param>
/// <param name="inactivityTimeout">
/// The number of seconds that the connection can be inactive before it is automatically closed.
/// Defaults to 20 seconds, with a maximum allowed value of 180 seconds.
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken" />.</param>
/// <exception cref="ArgumentNullException">Raised when <paramref name="voice" /> is null or empty.</exception>
/// <exception cref="ArgumentNullException">Raised when <paramref name="partialClipCallback" /> is null.</exception>
public async Task StartTextToSpeechAsync(Voice voice, Func<VoiceClip, Task> partialClipCallback,
VoiceSettings voiceSettings = null, GenerationConfig generationConfig = null, Model model = null,
OutputFormat outputFormat = OutputFormat.MP3_44100_128, bool? enableLogging = null,
bool? enableSsmlParsing = null, int? optimizeStreamingLatency = null,
bool? enableSsmlParsing = null, int? optimizeStreamingLatency = null, int? inactivityTimeout = null,
CancellationToken cancellationToken = default)
{
if (voice == null ||
Expand Down Expand Up @@ -104,6 +109,11 @@ public async Task StartTextToSpeechAsync(Voice voice, Func<VoiceClip, Task> part
parameters.Add(OptimizeStreamingLatencyParameter, optimizeStreamingLatency.ToString());
}

if (inactivityTimeout.HasValue)
{
parameters.Add(InactivityTimeoutParameter, inactivityTimeout.ToString());
}

await client.WebSocketClient.ConnectAsync(
new Uri(GetWebSocketUrl($"/{voice.Id}/stream-input", parameters)), cancellationToken);

Expand Down Expand Up @@ -139,11 +149,6 @@ public async Task SendTextToSpeechAsync(string text, bool? flush = null, bool tr
throw new InvalidOperationException("WebSocket is not open!");
}

if (string.IsNullOrWhiteSpace(text))
{
throw new ArgumentNullException($"{nameof(text)} cannot be null or empty!");
}

TextToSpeechWebSocketRequest request = new(text, flush, tryTriggerGeneration);
await client.WebSocketClient.SendAsync(request.ToArraySegment(), WebSocketMessageType.Text, true,
cancellationToken);
Expand Down

0 comments on commit 5d4d0d0

Please sign in to comment.