Skip to content

Commit

Permalink
Merge pull request #249 from GuerrillaCoder/release-v3
Browse files Browse the repository at this point in the history
Adds support for SpeechStarted
  • Loading branch information
davidvonthenen authored Mar 29, 2024
2 parents 38ad5bd + 011c3c2 commit b6b1670
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
32 changes: 29 additions & 3 deletions Deepgram/Clients/LiveTranscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Deepgram.Utilities;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Deepgram.Clients
{
Expand Down Expand Up @@ -55,6 +56,11 @@ public LiveTranscriptionClient(Credentials credentials)
/// </summary>
public event EventHandler<TranscriptReceivedEventArgs> TranscriptReceived;

/// <summary>
/// Fires if vad_events enabled and speech start is detected
/// </summary>
public event EventHandler<SpeechStartedEventArgs> SpeechStarted;

/// <summary>
/// Retrieves the connection state of the WebSocket
/// </summary>
Expand Down Expand Up @@ -264,12 +270,32 @@ private async Task Receive()
if (result.MessageType == WebSocketMessageType.Text)
{
var text = Encoding.UTF8.GetString(ms.ToArray());

if (text != null)
{
var transcript = JsonConvert.DeserializeObject<LiveTranscriptionResult>(text);
if (transcript != null)
//get value of type property
JObject jsonObject = JObject.Parse(text);
string typeValue = jsonObject["type"]?.ToString();

switch (typeValue)
{
TranscriptReceived?.Invoke(null, new TranscriptReceivedEventArgs(transcript));
case "Results":
var transcript = JsonConvert.DeserializeObject<LiveTranscriptionResult>(text);
if (transcript != null)
{
TranscriptReceived?.Invoke(null, new TranscriptReceivedEventArgs(transcript));
}
break;
case "SpeechStarted":
var started = JsonConvert.DeserializeObject<LiveTranscriptionSpeechStarted>(text);
if (started != null)
{
SpeechStarted?.Invoke(null, new SpeechStartedEventArgs(started));
}
break;
default:
// add more...
break;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Deepgram/Interfaces/ILiveTranscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public interface ILiveTranscriptionClient : IDisposable
/// Fires when a transcript is received from the Deepgram API
/// </summary>
event EventHandler<TranscriptReceivedEventArgs> TranscriptReceived;

/// <summary>
/// Fires if vad_events set to true and speech detected
/// </summary>
event EventHandler<SpeechStartedEventArgs> SpeechStarted;

/// <summary>
/// Retrieves the connection state of the WebSocket
Expand Down
3 changes: 3 additions & 0 deletions Deepgram/Models/LiveTranscriptionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,8 @@ public class LiveTranscriptionOptions
/// </summary>
[JsonProperty("tag")]
public string[] Tag { get; set; }

[JsonProperty("vad_events")]
public bool VadEvents { get; set; }
}
}
3 changes: 3 additions & 0 deletions Deepgram/Models/LiveTranscriptionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ public class LiveTranscriptionResult
/// </summary>
[JsonProperty("tags")]
public string[] Tags { get; set; }

[JsonProperty("type")]
public string Type { get; set; }
}
}
26 changes: 26 additions & 0 deletions Deepgram/Models/LiveTranscriptionSpeechStarted.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System;

namespace Deepgram.Models
{
public class LiveTranscriptionSpeechStarted
{
/// <summary>
/// Timestamp since start of first recognition that speech started.
/// </summary>
[JsonProperty("timestamp")]
public decimal Timestamp { get; set; }

/// <summary>
/// The channel field is interpreted as [A,B], where A is the channel index, and B is the total number of channels. The above example is channel 0 of single-channel audio.
/// </summary>
[JsonProperty("channel")]
public int[] Channel { get; set; }

/// <summary>
/// The type field is always SpeechStarted for this event
/// </summary>
[JsonProperty("type")]
public string Type { get; set; }
}
}
18 changes: 18 additions & 0 deletions Deepgram/Models/SpeechStartedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Deepgram.Models
{
public class SpeechStartedEventArgs : EventArgs
{
public SpeechStartedEventArgs(LiveTranscriptionSpeechStarted speechStarted)
{
SpeechStarted = speechStarted;
}

public LiveTranscriptionSpeechStarted SpeechStarted { get; set; }
}
}

0 comments on commit b6b1670

Please sign in to comment.