Skip to content

Text To Speech SSE Streaming

This example assumes using Cartesia; is in scope and apiKey contains your Cartesia 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
var handler = new TtsSseResponseHandler();
using var api = new CartesiaClient(
    "dummy-key",
    new HttpClient(handler)
    {
        BaseAddress = new Uri(CartesiaClient.DefaultBaseUrl),
    });

var request = new TTSSSERequest
{
    ModelId = TTSModel.Sonic35,
    Transcript = "Hello from Cartesia.",
    Voice = new TTSRequestVoiceSpecifier
    {
        Mode = TTSRequestVoiceSpecifierMode.Id,
        Id = "694f9389-aac1-45b6-b726-9d9369183238",
    },
    OutputFormat = new SSEOutputFormat
    {
        Container = SSEOutputFormatContainer.Raw,
        Encoding = RawEncoding.PcmS16le,
        SampleRate = 24000,
    },
    ContextId = "ctx-1",
};

var events = new List<CartesiaTtsSseEvent>();

// StreamTtsSseAsync yields events as the SSE response is read and decodes chunk audio bytes.
await foreach (var @event in api.StreamTtsSseAsync(request))
{
    events.Add(@event);
}