Skip to content

Commit

Permalink
Add pagination prefix & fix expiration timing
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 31, 2024
1 parent 78cb0e0 commit da9dc85
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ButterSTT.Tests/MessageSystem/MessageQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void PageDequeueTest()
queue.FinishCurrentParagraph();
curMessage = queue.GetCurrentMessage();
_output.WriteLine($"Third messages: \"{curMessage}\"");
Assert.Equal($"message. {thirdMessage}", curMessage);
Assert.Equal($"-message. {thirdMessage}", curMessage);
}

[Theory()]
Expand Down
4 changes: 2 additions & 2 deletions ButterSTT/Config/STTConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public record STTConfig
public int RealtimeQueuePadding { get; set; } = 24;

[JsonPropertyName("word_time_s")]
public double WordTimeS { get; set; } = 5.0;
public double WordTimeS { get; set; } = 0.3;

[JsonPropertyName("hard_word_time_s")]
public double HardWordTimeS { get; set; } = 16.0;
public double HardWordTimeS { get; set; } = 8.0;

[JsonPropertyName("page_context")]
public int PageContext { get; set; } = 1;
Expand Down
37 changes: 30 additions & 7 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class MessageQueue
private (int sentence, int word) _curIndex;
private bool _atCurEnd = false;

private bool _pagePrefix = false;

public Paragraph CurParagraph
{
get
Expand Down Expand Up @@ -263,9 +265,6 @@ public void QueueParagraphToFit(int padding = 0)
}
}

private static DateTime ComputeExpiration(TimeSpan span) =>
span >= TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.UtcNow + span;

private void ExpireWords()
{
var dequeueCount = 0;
Expand All @@ -292,6 +291,7 @@ private void PaginateWords()
// Wait until the last word on the full page is expired
if (!hardExpired && (!IsMessageFull || DateTime.UtcNow < lastWord.ExpiryTime))
return;
var removedAny = false;
while (
_messageWordQueue.Count > 0
&& (
Expand All @@ -305,6 +305,17 @@ private void PaginateWords()
)
{
_curMessageLength -= _messageWordQueue.Dequeue().Text.Length;
removedAny = true;
}
if (_messageWordQueue.Count > 0 && removedAny)
{
_pagePrefix = true;
_curMessageLength += 1;
}
else if (_messageWordQueue.Count <= 0 && _pagePrefix)
{
_pagePrefix = false;
_curMessageLength -= 1;
}
}

Expand All @@ -327,6 +338,11 @@ private void DequeueMessages()
}
}

private static DateTime ComputeExpiration(DateTime time, TimeSpan span) =>
time >= DateTime.MaxValue || span >= TimeSpan.MaxValue
? DateTime.MaxValue
: time + span;

private void ProgressWordQueue()
{
// Make sure there is enough room to fit a new word in the message,
Expand All @@ -341,11 +357,15 @@ private void ProgressWordQueue()
)
{
var word = _wordQueue.Dequeue();
var lastTime = _messageWordQueue
.Select(w => w.ExpiryTime)
.LastOrDefault(DateTime.UtcNow);
var baseTime = DateTime.UtcNow > lastTime ? DateTime.UtcNow : lastTime;
_messageWordQueue.Enqueue(
new MessageWord(
word,
ComputeExpiration(WordTime),
ComputeExpiration(HardWordTime)
ComputeExpiration(baseTime, WordTime),
ComputeExpiration(baseTime, HardWordTime)
)
);
_curMessageLength += word.Length;
Expand Down Expand Up @@ -380,11 +400,14 @@ public string GetCurrentMessage()
return false;
})
);
return (message + paragraph).Trim()
return (_pagePrefix ? "-" : "")
+ (message + paragraph).Trim()
+ (_curParagraph.Length > totalTaken ? "-" : "");
}

return message.Trim() + (_wordQueue.Count > 0 ? "-" : "");
return (_pagePrefix ? "-" : "")
+ message.Trim()
+ (_wordQueue.Count > 0 ? "-" : "");
}
}
}
Expand Down

0 comments on commit da9dc85

Please sign in to comment.