Skip to content

Commit 45e3b3b

Browse files
Merge pull request #259 from dvonthenen/live-client-helper-funcs
Add Subscribe Interface to Live Client
2 parents c5476f1 + f564332 commit 45e3b3b

10 files changed

Lines changed: 273 additions & 116 deletions

File tree

Deepgram/Clients/Live/v1/Client.cs

Lines changed: 116 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
33
// SPDX-License-Identifier: MIT
44

5+
using System.Threading;
56
using Deepgram.Models.Authenticate.v1;
67
using Deepgram.Models.Live.v1;
78

@@ -17,6 +18,7 @@ public class Client : Attribute, IDisposable
1718

1819
private ClientWebSocket? _clientWebSocket;
1920
private CancellationTokenSource? _cancellationTokenSource;
21+
private readonly Mutex _mutex = new Mutex();
2022
#endregion
2123

2224
/// <param name="apiKey">Required DeepgramApiKey</param>
@@ -35,18 +37,18 @@ public Client(string? apiKey = null, DeepgramWsClientOptions? options = null)
3537
Log.Verbose("LiveClient", "LEAVE");
3638
}
3739

38-
#region Subscribe Events
40+
#region Event Handlers
3941
/// <summary>
4042
/// Fires when an event is received from the Deepgram API
4143
/// </summary>
42-
public event EventHandler<OpenResponse>? _openReceived;
43-
public event EventHandler<MetadataResponse>? _metadataReceived;
44-
public event EventHandler<ResultResponse>? _resultsReceived;
45-
public event EventHandler<UtteranceEndResponse>? _utteranceEndReceived;
46-
public event EventHandler<SpeechStartedResponse>? _speechStartedReceived;
47-
public event EventHandler<CloseResponse>? _closeReceived;
48-
public event EventHandler<UnhandledResponse>? _unhandledReceived;
49-
public event EventHandler<ErrorResponse>? _errorReceived;
44+
private event EventHandler<OpenResponse>? _openReceived;
45+
private event EventHandler<MetadataResponse>? _metadataReceived;
46+
private event EventHandler<ResultResponse>? _resultsReceived;
47+
private event EventHandler<UtteranceEndResponse>? _utteranceEndReceived;
48+
private event EventHandler<SpeechStartedResponse>? _speechStartedReceived;
49+
private event EventHandler<CloseResponse>? _closeReceived;
50+
private event EventHandler<UnhandledResponse>? _unhandledReceived;
51+
private event EventHandler<ErrorResponse>? _errorReceived;
5052
#endregion
5153

5254
/// <summary>
@@ -161,36 +163,111 @@ void StartKeepAliveBackgroundThread() => _ = Task.Factory.StartNew(
161163
Log.Verbose("LiveClient.Connect", "LEAVE");
162164
}
163165

164-
//// TODO: convienence method for subscribing to events
165-
//public void On<T>(T e, EventHandler<T> eventHandler) {
166-
// switch (e)
167-
// {
168-
// case OpenResponse open:
169-
// OpenReceived += (sender, e) => eventHandler;
170-
// break;
171-
// case MetadataResponse metadata:
172-
// MetadataReceived += (sender, e) => eventHandler;
173-
// break;
174-
// case ResultResponse result:
175-
// ResultsReceived += (sender, e) => eventHandler;
176-
// break;
177-
// case UtteranceEndResponse utteranceEnd:
178-
// UtteranceEndReceived += (sender, e) => eventHandler;
179-
// break;
180-
// case SpeechStartedResponse speechStarted:
181-
// SpeechStartedReceived += (sender, e) => eventHandler;
182-
// break;
183-
// case CloseResponse close:
184-
// CloseReceived += (sender, e) => eventHandler;
185-
// break;
186-
// case UnhandledResponse unhandled:
187-
// UnhandledReceived += (sender, e) => eventHandler;
188-
// break;
189-
// case ErrorResponse error:
190-
// ErrorReceived += (sender, e) => eventHandler;
191-
// break;
192-
// }
193-
//}
166+
#region Subscribe Event
167+
/// <summary>
168+
/// Subscribe to an Open event from the Deepgram API
169+
/// </summary>
170+
/// <param name="eventHandler"></param>
171+
/// <returns>True if successful</returns>
172+
public bool Subscribe(EventHandler<OpenResponse> eventHandler)
173+
{
174+
lock (_mutex)
175+
{
176+
_openReceived += (sender, e) => eventHandler(sender, e);
177+
}
178+
179+
return true;
180+
}
181+
182+
/// <summary>
183+
/// Subscribe to a Metadata event from the Deepgram API
184+
/// </summary>
185+
/// <param name="eventHandler"></param>
186+
/// <returns>True if successful</returns>
187+
public bool Subscribe(EventHandler<MetadataResponse> eventHandler)
188+
{
189+
lock (_mutex)
190+
{
191+
_metadataReceived += (sender, e) => eventHandler(sender, e);
192+
}
193+
return true;
194+
}
195+
196+
/// <summary>
197+
/// Subscribe to a Results event from the Deepgram API
198+
/// </summary>
199+
/// <returns>True if successful</returns>
200+
public bool Subscribe(EventHandler<ResultResponse> eventHandler)
201+
{
202+
lock (_mutex)
203+
{
204+
_resultsReceived += (sender, e) => eventHandler(sender, e);
205+
}
206+
return true;
207+
}
208+
209+
/// <summary>
210+
/// Subscribe to an UtteranceEnd event from the Deepgram API
211+
/// </summary>
212+
/// <returns>True if successful</returns>
213+
public bool Subscribe(EventHandler<UtteranceEndResponse> eventHandler)
214+
{
215+
lock (_mutex)
216+
{
217+
_utteranceEndReceived += (sender, e) => eventHandler(sender, e);
218+
}
219+
return true;
220+
}
221+
222+
/// <summary>
223+
/// Subscribe to a SpeechStarted event from the Deepgram API
224+
/// </summary>
225+
/// <returns>True if successful</returns>
226+
public bool Subscribe(EventHandler<SpeechStartedResponse> eventHandler)
227+
{
228+
_speechStartedReceived += (sender, e) => eventHandler(sender, e);
229+
return true;
230+
}
231+
232+
/// <summary>
233+
/// Subscribe to a Close event from the Deepgram API
234+
/// </summary>
235+
/// <returns>True if successful</returns>
236+
public bool Subscribe(EventHandler<CloseResponse> eventHandler)
237+
{
238+
lock (_mutex)
239+
{
240+
_closeReceived += (sender, e) => eventHandler(sender, e);
241+
}
242+
return true;
243+
}
244+
245+
/// <summary>
246+
/// Subscribe to an Unhandled event from the Deepgram API
247+
/// </summary>
248+
/// <returns>True if successful</returns>
249+
public bool Subscribe(EventHandler<UnhandledResponse> eventHandler)
250+
{
251+
lock (_mutex)
252+
{
253+
_unhandledReceived += (sender, e) => eventHandler(sender, e);
254+
}
255+
return true;
256+
}
257+
258+
/// <summary>
259+
/// Subscribe to an Error event from the Deepgram API
260+
/// </summary>
261+
/// <returns>True if successful</returns>
262+
public bool Subscribe(EventHandler<ErrorResponse> eventHandler)
263+
{
264+
lock (_mutex)
265+
{
266+
_errorReceived += (sender, e) => eventHandler(sender, e);
267+
}
268+
return true;
269+
}
270+
#endregion
194271

195272
/// <summary>
196273
/// Sends a binary message over the WebSocket connection.

Deepgram/LiveClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Deepgram.Clients.Live.v1;
66
using Deepgram.Models.Authenticate.v1;
7+
using Deepgram.Models.Live.v1;
78

89
namespace Deepgram;
910

clean-up.ps1

Lines changed: 0 additions & 16 deletions
This file was deleted.

clean-up.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ set -o xtrace
77

88
rm -rf ./.vs
99
rm -rf ./dist
10-
rm -rf ./Deepgram/obj
11-
rm -rf ./Deepgram/bin
1210

13-
# Deepgram.Tests
14-
rm -rf ./Deepgram.Tests/bin
15-
rm -rf ./Deepgram.Tests/obj
16-
17-
# Deepgram.Microphone
18-
rm -rf ./Deepgram.Microphone/bin
19-
rm -rf ./Deepgram.Microphone/obj
11+
# delete all compile actifacts
12+
find ./ -type d -iname obj -print0 | xargs -0 rm -rf
13+
find ./ -type d -iname bin -print0 | xargs -0 rm -rf
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\Deepgram\Deepgram.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Using Include="Deepgram" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+
// SPDX-License-Identifier: MIT
4+
5+
using System.Text.Json;
6+
7+
using Deepgram.Logger;
8+
using Deepgram.Models.Manage.v1;
9+
10+
namespace SampleApp
11+
{
12+
class Program
13+
{
14+
static async Task Main(string[] args)
15+
{
16+
// Initialize Library with default logging
17+
// Normal logging is "Info" level
18+
Library.Initialize();
19+
// OR very chatty logging
20+
//Library.Initialize(LogLevel.Debug); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose
21+
22+
// Set "DEEPGRAM_API_KEY" environment variable to your Deepgram API Key
23+
var deepgramClient = new ManageClient();
24+
25+
var response = await deepgramClient.GetProjects();
26+
if (response == null)
27+
{
28+
Console.WriteLine("No projects found.");
29+
return;
30+
}
31+
32+
Console.WriteLine(JsonSerializer.Serialize(response));
33+
34+
//var projectId = "";
35+
//foreach (var project in response.Projects)
36+
//{
37+
// Console.WriteLine($"Project ID: {project.ProjectId}");
38+
// projectId = project.ProjectId;
39+
// break;
40+
//}
41+
42+
//var balanacesResponse = deepgramClient.GetBalances(projectId);
43+
//if (balanacesResponse == null || balanacesResponse.Balances == null)
44+
//{
45+
// Console.WriteLine("No balance found.");
46+
// return;
47+
//}
48+
49+
//Console.WriteLine("\n\nBalances:");
50+
//Console.WriteLine(JsonSerializer.Serialize(balanacesResponse));
51+
//Console.WriteLine("\n\n");
52+
53+
//string balanceId = "";
54+
//foreach (var balance in balanacesResponse.Balances)
55+
//{
56+
// Console.WriteLine($"Balance ID: {balance.BalanceId}");
57+
// balanceId = balance.BalanceId;
58+
// break;
59+
//}
60+
61+
//var balanaceResponse = deepgramClient.GetBalance(projectId, balanceId);
62+
//if (balanaceResponse == null)
63+
//{
64+
// Console.WriteLine("No balance found.");
65+
// return;
66+
//}
67+
68+
//Console.WriteLine("\n\nBalances:");
69+
//Console.WriteLine(JsonSerializer.Serialize(balanacesResponse));
70+
//Console.WriteLine("\n\n");
71+
72+
Console.WriteLine("Press any key to exit.");
73+
Console.ReadKey();
74+
75+
// Teardown Library
76+
Library.Terminate();
77+
}
78+
}
79+
}

examples/streaming/file/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ static async Task Main(string[] args)
2121
var liveClient = new LiveClient();
2222

2323
// Subscribe to the EventResponseReceived event
24-
liveClient._resultsReceived += (sender, e) =>
24+
liveClient.Subscribe(new EventHandler<ResultResponse>((sender, e) =>
2525
{
2626
if (e.Channel.Alternatives[0].Transcript == "")
2727
{
2828
return;
2929
}
3030

31-
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
32-
Console.WriteLine($"Speaker: {e.Channel.Alternatives[0].Transcript}");
33-
};
31+
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Transcription));
32+
Console.WriteLine($"\n\n\n----> Speaker: {e.Channel.Alternatives[0].Transcript}\n\n\n");
33+
}));
3434

3535
// Start the connection
3636
var liveSchema = new LiveSchema()

0 commit comments

Comments
 (0)