Live Streaming Audio Transcription
An overview of the Deepgram .NET SDK and Deepgram speech-to-text live streaming.
The .NET SDK
implements a LiveClient at the root of the Deepgram
namespace. This encapsulates a websocket connection to the Deepgram API and returns a class that emits events received from the Deepgram API for consumption by your application.
Installing the SDK
# Install the Deepgram .NET SDK
# https://github.com/deepgram/deepgram-dotnet-sdk
dotnet add package Deepgram
# Optionally, install the Deepgram Microphone package
dotnet add package Deepgram.Microphone
Initiating a Connection
The Connect()
method initiates a connection with the Deepgram API via websocket. You may pass in options for the transcription as well.
// Set "DEEPGRAM_API_KEY" environment variable to your Deepgram API Key
var liveClient = ClientFactory.CreateListenWebSocketClient();
// Start the connection
var liveSchema = new LiveSchema()
{
Model = "nova-2",
};
await liveClient.Connect(liveSchema);
Events
The following events are fired by the LiveTranscriptionClient
:
Event | Description | Data |
---|---|---|
OpenResponse | The websocket connection to Deepgram has been opened. | OpenResponse object |
ResultResponse | Deepgram has responded with a transcription | ResultResponse object |
MetadataResponse | Information about the websocket connection | MetadataResponse object |
SpeechStartedResponse | A SpeechStarted event was fired | SpeechStartedResponse object |
UtteranceEndResponse | An UtteranceEndResponse event was fired | UtteranceEndResponse object |
CloseResponse | The websocket connection to Deepgram has been closed. | CloseResponse object |
ErrorResponse | An error was encountered | ErrorResponse object |
UnhandledResponse | An unhandled response message was received | UnhandledResponse object |
OpenResponse Event
Subscribe to the OpenResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<OpenResponse>((sender, e) =>
{
Console.WriteLine($"\n\n----> OpenResponse received");
}));
ResultResponse Event
Subscribe to the ResultResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<ResultResponse>((sender, e) =>
{
Console.WriteLine($"----> Speaker: {e.Channel.Alternatives[0].Transcript}");
}));
MetadataResponse Event
Subscribe to the MetadataResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<MetadataResponse>((sender, e) =>
{
Console.WriteLine($"----> MetadataResponse received");
}));
SpeechStartedResponse Event
Subscribe to the SpeechStartedResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<SpeechStartedResponse>((sender, e) =>
{
Console.WriteLine($"----> SpeechStartedResponse received");
}));
UtteranceEndResponse Event
Subscribe to the UtteranceEndResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<UtteranceEndResponse>((sender, e) =>
{
Console.WriteLine($"----> UtteranceEndResponse received");
}));
CloseResponse Event
Subscribe to the CloseResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<CloseResponse>((sender, e) =>
{
Console.WriteLine($"----> CloseResponse received");
}));
ErrorResponse Event
Subscribe to the ErrorResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<ErrorResponse>((sender, e) =>
{
Console.WriteLine($"----> ErrorResponse received");
}));
UnhandledResponse Event
Subscribe to the UnhandledResponse event by calling the Subscribe()
function.
liveClient.Subscribe(new EventHandler<UnhandledResponse>((sender, e) =>
{
Console.WriteLine($"----> UnhandledResponse received");
}));
Functions
These functions provide capabilities that make up the LiveClient API.
Sending Data
The Send
method sends raw audio data to the Deepgram API.
liveClient.Send(AUDIO_STREAM_DATA);
Get Websocket State
The State
method returns the ready state of the websocket connection to Deepgram.
var websocketState = liveClient.State();
Stop Transcribing
The Stop
method sends a signal to the Deepgram API that you have finished streaming audio, and it should conclude its transcription efforts. Upon sending the final transcript back, it will close the websocket connection.
await liveClient.Stop()
Updated about 2 months ago