Skip to content

Commit

Permalink
Fix prefix growing message length & other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 31, 2024
1 parent da9dc85 commit 112355f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
7 changes: 5 additions & 2 deletions ButterSTT/Config/STTConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record STTConfig
public static readonly STTConfig Default = new();

[JsonPropertyName("config_version")]
public int ConfigVersion { get; set; } = 1;
public int ConfigVersion { get; set; } = 2;

[JsonPropertyName("models_path")]
public string ModelsPath { get; set; } = "Models";
Expand Down Expand Up @@ -40,11 +40,14 @@ public record STTConfig
public double WordTimeS { get; set; } = 0.3;

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

[JsonPropertyName("page_context")]
public int PageContext { get; set; } = 1;

[JsonPropertyName("use_page_prefix")]
public bool UsePagePrefix { get; set; } = true;

// Converter utilities
private static TimeSpan Seconds(double s) =>
s < 0d ? TimeSpan.MaxValue : TimeSpan.FromSeconds(s);
Expand Down
79 changes: 48 additions & 31 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class MessageQueue
/// </summary>
public int PageContext = STTConfig.Default.PageContext;

/// <summary>
/// Whether to use the message prefix from pagination.
/// </summary>
public bool UsePagePrefix = STTConfig.Default.UsePagePrefix;

private readonly object _syncParagraph = new();
public Paragraph _curParagraph;
private (int sentence, int word) _curIndex;
Expand Down Expand Up @@ -307,15 +312,23 @@ private void PaginateWords()
_curMessageLength -= _messageWordQueue.Dequeue().Text.Length;
removedAny = true;
}
if (_messageWordQueue.Count > 0 && removedAny)
if (_messageWordQueue.Count <= 0)
{
_pagePrefix = true;
_curMessageLength += 1;
if (removedAny && !hardExpired && UsePagePrefix)
{
_pagePrefix = true;
_curMessageLength = 1;
}
else
{
_pagePrefix = false;
_curMessageLength = 0;
}
}
else if (_messageWordQueue.Count <= 0 && _pagePrefix)
else if (removedAny && !_pagePrefix && UsePagePrefix)
{
_pagePrefix = false;
_curMessageLength -= 1;
_pagePrefix = true;
_curMessageLength += 1;
}
}

Expand Down Expand Up @@ -361,12 +374,9 @@ private void ProgressWordQueue()
.Select(w => w.ExpiryTime)
.LastOrDefault(DateTime.UtcNow);
var baseTime = DateTime.UtcNow > lastTime ? DateTime.UtcNow : lastTime;
var wordTime = ComputeExpiration(baseTime, WordTime);
_messageWordQueue.Enqueue(
new MessageWord(
word,
ComputeExpiration(baseTime, WordTime),
ComputeExpiration(baseTime, HardWordTime)
)
new MessageWord(word, wordTime, ComputeExpiration(wordTime, HardWordTime))
);
_curMessageLength += word.Length;
}
Expand All @@ -381,33 +391,40 @@ public string GetCurrentMessage()
ProgressWordQueue();

var message = string.Concat(_messageWordQueue.Select(w => w.Text));
var showPrefix = _pagePrefix;
var showSuffix = _wordQueue.Count > 0;

// If there's no queue and there's new words to display
if (_wordQueue.Count <= 0 && _curParagraph.Length > 0)
{
var availableLength = MessageLength - _curMessageLength;
var totalTaken = 0;
var paragraph = string.Concat(
ParagraphWordEnumerator()
.TakeWhile(w =>
{
if (totalTaken + w.Length <= availableLength)
var paragraphLength = ParagraphLengthFromIndex();
if (paragraphLength > 0)
{
var availableLength = MessageLength - _curMessageLength;
var totalTaken = 0;
var paragraph = string.Concat(
ParagraphWordEnumerator()
.TakeWhile(w =>
{
totalTaken += w.Length;
return true;
}
else
return false;
})
);
return (_pagePrefix ? "-" : "")
+ (message + paragraph).Trim()
+ (_curParagraph.Length > totalTaken ? "-" : "");
if (totalTaken + w.Length <= availableLength)
{
totalTaken += w.Length;
return true;
}
else
return false;
})
);
message += paragraph;
showSuffix = paragraphLength > totalTaken;
}
}

return (_pagePrefix ? "-" : "")
+ message.Trim()
+ (_wordQueue.Count > 0 ? "-" : "");
if (string.IsNullOrWhiteSpace(message))
return "";

message = message.Trim();
return (showPrefix ? "-" : "") + message + (showSuffix ? "-" : "");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ButterSTT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
WordTime = config.WordTime,
HardWordTime = config.HardWordTime,
PageContext = config.PageContext,
UsePagePrefix = config.UsePagePrefix,
};
using var oscHandler = new OSCMessageHandler(messageQueue, config.OSCEndpoint)
{
Expand Down

0 comments on commit 112355f

Please sign in to comment.