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 c616592 commit 11214ce
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions ElevenLabs-DotNet/TextToSpeech/TextToSpeechWebSocketEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -64,13 +65,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 @@ -105,6 +110,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 All @@ -115,18 +125,6 @@ await client.WebSocketClient.ConnectAsync(
TextToSpeechWebSocketFirstMessageRequest firstMessageRequest = new(voiceSettings, generationConfig);
await client.WebSocketClient.SendAsync(firstMessageRequest.ToArraySegment(), WebSocketMessageType.Text, true,
cancellationToken);

// send a space every 10 seconds to keep the connection alive
_ = Task.Run(async () =>
{
while (client.WebSocketClient.State == WebSocketState.Open)
{
await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
TextToSpeechWebSocketRequest request = new(" ");
await client.WebSocketClient.SendAsync(request.ToArraySegment(), WebSocketMessageType.Text, false,
cancellationToken);
}
}, cancellationToken);
}

/// <summary>
Expand All @@ -152,11 +150,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 11214ce

Please sign in to comment.