-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e9c8cd
commit 5e9355c
Showing
6 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? "-" : "")}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
ButterSTT/OSCHandler.cs → ButterSTT/MessageSystem/OSCHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using CoreOSC; | ||
|
||
namespace ButterSTT | ||
namespace ButterSTT.MessageSystem | ||
{ | ||
public class OSCHandler | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters