Skip to content

Commit

Permalink
Merge pull request #255 from dvonthenen/refactor-live-client-part2
Browse files Browse the repository at this point in the history
Refactor LiveClient
  • Loading branch information
davidvonthenen authored Mar 29, 2024
2 parents fc257a1 + d36caea commit c841605
Show file tree
Hide file tree
Showing 19 changed files with 377 additions and 186 deletions.
1 change: 0 additions & 1 deletion Deepgram/Abstractions/AbstractRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT

using Deepgram.Encapsulations;
using Deepgram.Models.Analyze.v1;
using Deepgram.Models.Authenticate.v1;

namespace Deepgram.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;
namespace Deepgram.Abstractions;

public class NoopSchema
{
Expand Down
266 changes: 223 additions & 43 deletions Deepgram/Clients/Live/v1/Client.cs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Deepgram/Clients/Live/v1/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Clients.Live.v1;

/// <summary>
/// Headers of interest in the return values from the Deepgram Speak API.
/// </summary>
public static class Constants
{
// WS buffer size
public const int BufferSize = 1024 * 16;
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Models.Live.v1;

namespace Deepgram.Clients.Live.v1;

public class ResponseEventArgs(EventResponse response) : EventArgs
public class ResponseEvent<T>(T? response) : EventArgs
{
public EventResponse Response { get; set; } = response;
public T? Response { get; set; } = response;
}

10 changes: 0 additions & 10 deletions Deepgram/Models/Analyze/v1/NoopSchema.cs

This file was deleted.

15 changes: 15 additions & 0 deletions Deepgram/Models/Live/v1/CloseResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;

public record CloseResponse
{
/// <summary>
/// TODO
/// </summary>
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LiveType? Type { get; set; } = LiveType.Close;
}
33 changes: 33 additions & 0 deletions Deepgram/Models/Live/v1/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;

public record ErrorResponse
{
/// <summary>
/// Error Description
/// </summary>
[JsonPropertyName("description")]
public string? Description { get; set; } = "";

/// <summary>
/// Error Message
/// </summary>
[JsonPropertyName("message")]
public string? Message { get; set; } = "";

/// <summary>
/// Error Variant
/// </summary>
[JsonPropertyName("variant")]
public string? Variant { get; set; } = "";

/// <summary>
/// TODO
/// </summary>
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LiveType? Type { get; set; } = LiveType.Error;
}
33 changes: 0 additions & 33 deletions Deepgram/Models/Live/v1/EventResponse.cs

This file was deleted.

4 changes: 4 additions & 0 deletions Deepgram/Models/Live/v1/LiveType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ namespace Deepgram.Models.Live.v1;

public enum LiveType
{
Open,
Metadata,
Results,
UtteranceEnd,
SpeechStarted,
Close,
Unhandled,
Error,
}
15 changes: 15 additions & 0 deletions Deepgram/Models/Live/v1/OpenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;

public record OpenResponse
{
/// <summary>
/// TODO
/// </summary>
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LiveType? Type { get; set; } = LiveType.Open;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;
public record TranscriptionResponse
public record ResultResponse
{
/// <summary>
/// TODO
Expand Down
21 changes: 21 additions & 0 deletions Deepgram/Models/Live/v1/UnhandledResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

namespace Deepgram.Models.Live.v1;

public record UnhandledResponse
{
/// <summary>
/// Raw JSON
/// </summary>
[JsonPropertyName("raw")]
public string? Raw { get; set; } = "";

/// <summary>
/// TODO
/// </summary>
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LiveType? Type { get; set; } = LiveType.Unhandled;
}
10 changes: 0 additions & 10 deletions Deepgram/Models/Manage/v1/NoopSchema.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Deepgram/Models/OnPrem/v1/NoopSchema.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Deepgram/Models/PreRecorded/v1/NoopSchema.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Deepgram/Models/Speak/v1/NoopSchema.cs

This file was deleted.

63 changes: 37 additions & 26 deletions examples/streaming/file/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,47 @@ static async Task Main(string[] args)
var liveClient = new LiveClient();

// Subscribe to the EventResponseReceived event
liveClient.EventResponseReceived += (sender, e) =>
liveClient._resultsReceived += (sender, e) =>
{
if (e.Response.Transcription != null)
if (e.Channel.Alternatives[0].Transcript == "")
{
if (e.Response.Transcription.Channel.Alternatives[0].Transcript == "")
{
Console.WriteLine("Empty transcription received.");
return;
}
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
Console.WriteLine($"Speaker: {e.Response.Transcription.Channel.Alternatives[0].Transcript}");
}
else if (e.Response.SpeechStarted != null)
{
Console.WriteLine("SpeechStarted received: " + JsonSerializer.Serialize(e.Response.SpeechStarted));
}
else if (e.Response.UtteranceEnd != null)
{
Console.WriteLine("UtteranceEnd received: " + JsonSerializer.Serialize(e.Response.UtteranceEnd));
}
else if (e.Response.MetaData != null)
{
Console.WriteLine("Metadata received: " + JsonSerializer.Serialize(e.Response.MetaData));
}
else if (e.Response.Error != null)
{
Console.WriteLine("Error: " + JsonSerializer.Serialize(e.Response.Error.Message));
Console.WriteLine("Empty transcription received.");
return;
}
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
Console.WriteLine($"Speaker: {e.Channel.Alternatives[0].Transcript}");
};
//liveClient.EventResponseReceived += (sender, e) =>
//{
// if (e.Response.Transcription != null)
// {
// if (e.Response.Transcription.Channel.Alternatives[0].Transcript == "")
// {
// Console.WriteLine("Empty transcription received.");
// return;
// }

// // Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
// Console.WriteLine($"Speaker: {e.Response.Transcription.Channel.Alternatives[0].Transcript}");
// }
// else if (e.Response.SpeechStarted != null)
// {
// Console.WriteLine("SpeechStarted received: " + JsonSerializer.Serialize(e.Response.SpeechStarted));
// }
// else if (e.Response.UtteranceEnd != null)
// {
// Console.WriteLine("UtteranceEnd received: " + JsonSerializer.Serialize(e.Response.UtteranceEnd));
// }
// else if (e.Response.MetaData != null)
// {
// Console.WriteLine("Metadata received: " + JsonSerializer.Serialize(e.Response.MetaData));
// }
// else if (e.Response.Error != null)
// {
// Console.WriteLine("Error: " + JsonSerializer.Serialize(e.Response.Error.Message));
// }
//};

// Start the connection
var liveSchema = new LiveSchema()
Expand Down
37 changes: 10 additions & 27 deletions examples/streaming/microphone/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,16 @@ static async Task Main(string[] args)
var liveClient = new LiveClient();

// Subscribe to the EventResponseReceived event
liveClient.EventResponseReceived += (sender, e) =>
liveClient._resultsReceived += (sender, e) =>
{
if (e.Response.Transcription != null)
if (e.Channel.Alternatives[0].Transcript == "")
{
if (e.Response.Transcription.Channel.Alternatives[0].Transcript == "")
{
Console.WriteLine("Empty transcription received.");
return;
}
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
Console.WriteLine($"Speaker: {e.Response.Transcription.Channel.Alternatives[0].Transcript}");
}
else if (e.Response.SpeechStarted != null)
{
Console.WriteLine("SpeechStarted received: " + JsonSerializer.Serialize(e.Response.SpeechStarted));
}
else if (e.Response.UtteranceEnd != null)
{
Console.WriteLine("UtteranceEnd received: " + JsonSerializer.Serialize(e.Response.UtteranceEnd));
}
else if (e.Response.MetaData != null)
{
Console.WriteLine("Metadata received: " + JsonSerializer.Serialize(e.Response.MetaData));
}
else if (e.Response.Error != null)
{
Console.WriteLine("Error: " + JsonSerializer.Serialize(e.Response.Error.Message));
Console.WriteLine("Empty transcription received.");
return;
}
// Console.WriteLine("Transcription received: " + JsonSerializer.Serialize(e.Response.Transcription));
Console.WriteLine($"Speaker: {e.Channel.Alternatives[0].Transcript}");
};

// Start the connection
Expand All @@ -61,7 +42,9 @@ static async Task Main(string[] args)
var microphone = new Microphone(liveClient.Send);
microphone.Start();

Thread.Sleep(3600000);
// Wait for the user to press a key
Console.WriteLine("Press any key to stop the microphone streaming...");
Console.ReadKey();

// Stop the connection
await liveClient.Stop();
Expand Down

0 comments on commit c841605

Please sign in to comment.