Text to Speech REST

An overview of the Deepgram .NET SDK and Deepgram text-to-speech.

Installing the SDK

To begin using Deepgram’s Text-to-Speech functionality, you need to install the Deepgram .NET SDK in your existing project. You can do this using the following command:

C#
1# Install the Deepgram .NET SDK
2# https://github.com/deepgram/deepgram-dotnet-sdk
3
4dotnet add package Deepgram
5
6# Optionally, install the Deepgram Microphone package
7dotnet add package Deepgram.Microphone

Make a Deepgram Text-to-Speech Request

C#
1using Deepgram.Models.Speak.v1.REST;
2
3namespace SampleApp
4{
5 class Program
6 {
7 static async Task Main(string[] args)
8 {
9 // Initialize Library with default logging
10 // Normal logging is "Info" level
11 Library.Initialize();
12
13 // use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable
14 var deepgramClient = ClientFactory.CreateSpeakRESTClient();
15
16 var response = await deepgramClient.ToFile(
17 new TextSource("Hello World!"),
18 "test.mp3",
19 new SpeakSchema()
20 {
21 Model = "aura-asteria-en",
22 });
23
24 //Console.WriteLine(response);
25 Console.WriteLine(response);
26 Console.ReadKey();
27
28 // Teardown Library
29 Library.Terminate();
30 }
31 }
32}

Audio Output Streaming

Deepgram’s TTS API allows you to start playing the audio as soon as the first byte is received. This section provides examples to help you stream the audio output efficiently.

Single Text Source Payload

C#
1using Deepgram.Models.Speak.v1.REST;
2
3namespace SampleApp
4{
5 class Program
6 {
7 static async Task Main(string[] args)
8 {
9 // Initialize Library with default logging
10 // Normal logging is "Info" level
11 Library.Initialize();
12
13 // use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable
14 var deepgramClient = ClientFactory.CreateSpeakRESTClient();
15
16 // STEP 3: send/process the desired text to Deepgram to convert to Speech
17 var response = await deepgramClient.Stream(
18 new TextSource("Hello World!"),
19 new SpeakSchema()
20 {
21 Model = "aura-asteria-en",
22 });
23
24 await foreach (var audioSegment in response.AudioStream)
25 {
26 // Process the audio segment received
27 }
28
29 // Teardown Library
30 Library.Terminate();
31 }
32 }
33}

Chunk Text Source Payload

C#
1using Deepgram.Models.Speak.v1.REST;
2
3namespace SampleApp
4{
5 class Program
6 {
7 static async Task Main(string[] args)
8 {
9 // Initialize Library with default logging
10 // Normal logging is "Info" level
11 Library.Initialize();
12
13 // use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable
14 var deepgramClient = ClientFactory.CreateSpeakRESTClient();
15
16 var inputText = "Your long text goes here...";
17
18 // STEP 3: process the desired text in segments
19 // Send each seqment to Deepgram to convert to Speech
20 var segments = SegmentTextBySentence(inputText);
21
22 foreach (var segment in segments)
23 {
24 var response = await deepgramClient.Stream(
25 new TextSource(segment),
26 new SpeakSchema()
27 {
28 Model = "aura-asteria-en",
29 });
30
31 await foreach (var audioSegment in response.AudioStream)
32 {
33 // Process the audio segment received
34 }
35 }
36
37 // Teardown Library
38 Library.Terminate();
39 }
40
41 static string[] SegmentTextBySentence(string text)
42 {
43 return Regex.Matches(text, @"[^.!?]+[.!?]")
44 .Cast()
45 .Select(m => m.Value)
46 .ToArray();
47 }
48 }
49}

Where To Find Additional Examples

The SDK repository has a good collection text-to-speech examples. You can find links to them in the README.

Built with