Skip to content

Realtime Text-to-Speech

Streams text to the Soniox realtime Text-to-Speech WebSocket API. The default test path serializes generated messages without making a network call. Set SONIOX_RUN_REALTIME_TTS_EXAMPLE=1 to run the paid live example.

This example assumes using Soniox; is in scope and apiKey contains your Soniox API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var streamId = $"sdk-example-{Guid.NewGuid():N}";
var config = new TtsRealtime.TtsConfig
{
    ApiKey = GetOptionalEnvironmentVariable("SONIOX_API_KEY") ?? "test-key",
    StreamId = streamId,
    Model = SonioxClient.DefaultTtsModel,
    Language = SonioxClient.DefaultTtsLanguage,
    Voice = "Adrian",
    AudioFormat = SonioxClient.DefaultTtsAudioFormat,
    SampleRate = 24000,
    ReturnTimestamps = true,
    Speed = 1.1,
};
var textChunks = new[]
{
    new TtsRealtime.TtsText
    {
        StreamId = streamId,
        Text = "Hello from realtime ",
        TextEnd = false,
    },
    new TtsRealtime.TtsText
    {
        StreamId = streamId,
        Text = "Text-to-Speech.",
        TextEnd = true,
    },
};
var keepAlive = new TtsRealtime.TtsKeepAlive { KeepAlive = true };
var cancel = new TtsRealtime.TtsCancel { StreamId = streamId, Cancel = true };

if (!IsEnvironmentFlagEnabled(RunRealtimeTtsExampleFlag))
{
    var configJson = JsonSerializer.Serialize(
        config,
        typeof(TtsRealtime.TtsConfig),
        TtsRealtime.TtsRealtimeSourceGenerationContext.Default);
    var firstTextJson = JsonSerializer.Serialize(
        textChunks[0],
        typeof(TtsRealtime.TtsText),
        TtsRealtime.TtsRealtimeSourceGenerationContext.Default);
    var keepAliveJson = JsonSerializer.Serialize(
        keepAlive,
        typeof(TtsRealtime.TtsKeepAlive),
        TtsRealtime.TtsRealtimeSourceGenerationContext.Default);
    var cancelJson = JsonSerializer.Serialize(
        cancel,
        typeof(TtsRealtime.TtsCancel),
        TtsRealtime.TtsRealtimeSourceGenerationContext.Default);

    return;
}

using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(45));
await using var client = new TtsRealtime.SonioxTtsRealtimeClient();

await client.ConnectAsync(
    keepAliveInterval: TimeSpan.FromSeconds(15),
    connectTimeout: TimeSpan.FromSeconds(10),
    cancellationToken: cancellationTokenSource.Token);

config.ApiKey = GetRequiredEnvironmentVariable("SONIOX_API_KEY");
await client.SendTtsConfigAsync(config, cancellationTokenSource.Token);
await client.SendTtsTextAsync(textChunks[0], cancellationTokenSource.Token);
await client.SendTtsKeepAliveAsync(keepAlive, cancellationTokenSource.Token);
await client.SendTtsTextAsync(textChunks[1], cancellationTokenSource.Token);

var result = await CollectRealtimeTtsResultAsync(
    client: client,
    streamId: streamId,
    cancellationToken: cancellationTokenSource.Token);