Skip to content

Commit

Permalink
Start basic message queueing
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 28, 2024
1 parent 1e9c8cd commit 5e9355c
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
107 changes: 107 additions & 0 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using ButterSTT.TextProcessing.TextParts;

namespace ButterSTT.MessageSystem
{
public class MessageQueue
{
public int MessageLength = 144;
public TimeSpan WordTime = TimeSpan.FromSeconds(3);

public Paragraph CurParagraph;
private (int sentence, int word) CurIndex;

private readonly Queue<string> WordQueue = new();
private readonly Queue<MessageWord> MessageWordQueue = new();
private int CurMessageLength;

private void InternLimitWordIndex()
{
CurIndex.word = CurParagraph.Sentences[CurIndex.sentence].Words.Length - 1;
}

public void LimitParagraphIndex()
{
if (CurParagraph.Sentences.Length <= CurIndex.sentence)
{
// Move to the end of the last known position
CurIndex.sentence = CurParagraph.Sentences.Length - 1;
InternLimitWordIndex();
}
else if (CurParagraph.Sentences[CurIndex.sentence].Length <= CurIndex.word)
{
InternLimitWordIndex();
}
}

public void FinishCurrentParagraph()
{
// Limit the index if length has changed since last known
LimitParagraphIndex();

// Queue all words after the current displayed ones
for (var s = CurIndex.sentence; s < CurParagraph.Sentences.Length; s++)
{
var wordCount = CurParagraph.Sentences[s].Words.Length;
for (var w = CurIndex.word; w < wordCount; w++)
{
var word = CurParagraph.Sentences[s].Words[w];
WordQueue.Enqueue(
$"{word.Text}{(w + 1 >= wordCount && !word.Text.EndsWith(' ') ? " " : "")}"
);
}
// Reset word index to 0 for following sentences
CurIndex.word = 0;
}

// Reset states
CurParagraph = default;
CurIndex = default;
}

public string GetCurrentMessage()
{
// Remove expired words if more space is needed
if (WordQueue.Count > 0 || CurParagraph.Length > 0)
{
while (
MessageWordQueue.TryPeek(out var expiredWord)
&& DateTime.Now - expiredWord.DisplayTime > WordTime
)
{
CurMessageLength -= MessageWordQueue.Dequeue().Text.Length;
}
}

// Make sure there is enough room to fit a new word in the message and
// allow space for a dash after the current text if there is already more
while (
WordQueue.TryPeek(out var newWord)
&& CurMessageLength + newWord.Length + (WordQueue.Count > 1 ? 1 : 0) < MessageLength
)
{
var word = WordQueue.Dequeue();
MessageWordQueue.Enqueue(new MessageWord(word, DateTime.Now));
CurMessageLength += word.Length;
}

// If there's no queue and there's new words to display
if (WordQueue.Count <= 0 && CurParagraph.Length > 0)
{
// If there's no message to display besides the one in progress, display what we have now
if (MessageWordQueue.Count <= 0)
{
return CurParagraph
.Sentences.SelectMany(x => x.Words, (x, y) => y.Text)
.Aggregate("", (x, y) => x + y)
.Trim();
}
}

var message = MessageWordQueue
.Select(w => w.Text)
.Aggregate("", (x, y) => x + y)
.Trim();
return $"{message}{(WordQueue.Count > 0 ? "-" : "")}";
}
}
}
14 changes: 14 additions & 0 deletions ButterSTT/MessageSystem/MessageWord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ButterSTT.MessageSystem
{
public readonly struct MessageWord
{
public readonly string Text;
public readonly DateTime DisplayTime;

public MessageWord(string text, DateTime displayTime)
{
Text = text;
DisplayTime = displayTime;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CoreOSC;

namespace ButterSTT
namespace ButterSTT.MessageSystem
{
public class OSCHandler
{
Expand Down
6 changes: 6 additions & 0 deletions ButterSTT/SpeechToTextHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using AprilAsr;
using ButterSTT.MessageSystem;
using ButterSTT.TextProcessing;
using CoreOSC;
using NAudio.Wave;
Expand All @@ -22,6 +23,7 @@ public class SpeechToTextHandler : IDisposable
// Output
private readonly StringBuilder consoleOutput = new();
private readonly StringBuilder aprilOutput = new();
private readonly MessageQueue messageQueue = new();
private readonly OSCHandler oscHandler = new();

private DateTime lastMessage = DateTime.Now;
Expand Down Expand Up @@ -145,6 +147,10 @@ private void OnAprilTokens(AprilResultKind result, AprilToken[] tokens)
? EnglishCapitalization.Capitalize(aprilOutput.ToString().Trim())
: "";

messageQueue.CurParagraph = EnglishTextParser.ParseParagraph(aprilOutputString);
if (result == AprilResultKind.FinalRecognition)
messageQueue.FinishCurrentParagraph();

try
{
if (tokens.Length > 0 && !string.IsNullOrWhiteSpace(aprilOutputString))
Expand Down
2 changes: 2 additions & 0 deletions ButterSTT/TextProcessing/TextParts/Paragraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ namespace ButterSTT.TextProcessing.TextParts
public readonly struct Paragraph
{
public readonly Sentence[] Sentences;
public readonly int Length;

public Paragraph(Sentence[] sentences)
{
Sentences = sentences;
Length = sentences.Sum(s => s.Length);
}
}
}
2 changes: 2 additions & 0 deletions ButterSTT/TextProcessing/TextParts/Sentence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ namespace ButterSTT.TextProcessing.TextParts
public readonly struct Sentence
{
public readonly Word[] Words;
public readonly int Length;

public Sentence(Word[] words)
{
Words = words;
Length = words.Sum(w => w.Text.Length);
}
}
}

0 comments on commit 5e9355c

Please sign in to comment.