Skip to content

Commit

Permalink
Switch to LucHeart.CoreOSC for OSC, using NuGet
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 30, 2024
1 parent 63faa9a commit 3c76f14
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

8 changes: 1 addition & 7 deletions ButterSTT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ButterSTT", "ButterSTT\ButterSTT.csproj", "{C8328FE0-794A-4FCE-B8ED-82449F22C8BD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreOSC", "Dependencies\CoreOSC-VRC-UTF8\CoreOSC\CoreOSC.csproj", "{4919CC8D-D8F3-423F-944C-7E375D0E16EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ButterSTT.Tests", "ButterSTT.Tests\ButterSTT.Tests.csproj", "{FCE266B7-2036-499B-92FB-263C8A39D570}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ButterSTT.Tests", "ButterSTT.Tests\ButterSTT.Tests.csproj", "{FCE266B7-2036-499B-92FB-263C8A39D570}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,10 +16,6 @@ Global
{C8328FE0-794A-4FCE-B8ED-82449F22C8BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8328FE0-794A-4FCE-B8ED-82449F22C8BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8328FE0-794A-4FCE-B8ED-82449F22C8BD}.Release|Any CPU.Build.0 = Release|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Release|Any CPU.Build.0 = Release|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
7 changes: 2 additions & 5 deletions ButterSTT/ButterSTT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

<ItemGroup>
<PackageReference Include="AprilAsr" Version="0.0.1.7" />
<PackageReference Include="CSharpier.MsBuild" Version="0.27.1">
<PackageReference Include="CSharpier.MsBuild" Version="0.27.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="LucHeart.CoreOSC" Version="1.2.1" />
<PackageReference Include="NAudio" Version="2.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dependencies\CoreOSC-VRC-UTF8\CoreOSC\CoreOSC.csproj" />
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions ButterSTT/Config/STTConfig.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using System.Text.Json.Serialization;

namespace ButterSTT.Config
Expand All @@ -16,10 +17,7 @@ public record STTConfig
public int MicrophoneDeviceNumber { get; set; } = 0;

[JsonPropertyName("osc_address")]
public string OSCAddress { get; set; } = "127.0.0.1";

[JsonPropertyName("osc_port")]
public int OSCPort { get; set; } = 9000;
public string OSCAddress { get; set; } = "127.0.0.1:9000";

[JsonPropertyName("osc_chatbox_ratelimit_s")]
public double OSCChatboxRateLimitS { get; set; } = 1.3;
Expand All @@ -39,7 +37,10 @@ public record STTConfig
[JsonPropertyName("hard_word_time_s")]
public double HardWordTimeS { get; set; } = 16.0;

// TimeSpan converters
// Converter utility properties
[JsonIgnore]
public IPEndPoint OSCEndpoint => IPEndPoint.Parse(OSCAddress);

[JsonIgnore]
public TimeSpan OSCChatboxRateLimit => TimeSpan.FromSeconds(OSCChatboxRateLimitS);

Expand Down
46 changes: 21 additions & 25 deletions ButterSTT/MessageSystem/OSCMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
using System.Diagnostics;
using System.Net;
using ButterSTT.Config;
using CoreOSC;
using LucHeart.CoreOSC;

namespace ButterSTT.MessageSystem
{
public class OSCMessageHandler
public class OSCMessageHandler : IDisposable
{
public TimeSpan RateLimit = STTConfig.Default.OSCChatboxRateLimit;

public readonly MessageQueue MessageQueue;
private OSCHandler _oscHandler;
private OscSender _oscSender;

private CancellationTokenSource? _messageLoopCancelSource;
private Task? _messageLoop;

private string _lastMessage = "";

public OSCMessageHandler(
MessageQueue? messageQueue = null,
string? oscAddress = null,
int? oscPort = null
)
public OSCMessageHandler(MessageQueue? messageQueue = null, IPEndPoint? ipEndPoint = null)
{
MessageQueue = messageQueue ?? new();
_oscHandler = MakeOSCHandler(oscAddress, oscPort);
_oscSender = new(ipEndPoint ?? STTConfig.Default.OSCEndpoint);
}

private static OSCHandler MakeOSCHandler(string? oscAddress = null, int? oscPort = null)
public void SetOSCEndpoint(IPEndPoint? ipEndPoint = null)
{
return new(
oscAddress ?? STTConfig.Default.OSCAddress,
oscPort ?? STTConfig.Default.OSCPort
);
}

public void SetOSCEndpoint(string? oscAddress = null, int? oscPort = null)
{
_oscHandler = MakeOSCHandler(oscAddress, oscPort);
_oscSender.Dispose();
_oscSender = new(ipEndPoint ?? STTConfig.Default.OSCEndpoint);
}

public bool IsLoopRunning => _messageLoop != null && !_messageLoop.IsCompleted;
Expand Down Expand Up @@ -73,33 +63,33 @@ private async Task MessageLoop(CancellationToken cancelToken)
{
timer.Restart();

SendMessage();
await SendMessage(cancelToken);

var timeout = RateLimit - timer.Elapsed;
if (timeout.TotalMilliseconds > 0)
await Task.Delay(timeout, cancelToken);
}
}

private void SendMessage()
private async Task SendMessage(CancellationToken cancelToken = default)
{
try
{
var message = MessageQueue.GetCurrentMessage();
if (string.IsNullOrWhiteSpace(message) || message == _lastMessage)
return;

var chatbox = OSCHandler.MakeChatboxInput(message);
var chatbox = OSCUtils.MakeChatboxInput(message);
if (MessageQueue.IsFinished)
{
// Just send the message, no more typing
_oscHandler.OSCSender.Send(chatbox);
await _oscSender.SendAsync(chatbox);
}
else
{
// Still typing the message... Show as typing!
_oscHandler.OSCSender.Send(
new OscBundle(0, chatbox, OSCHandler.MakeChatboxTyping(true))
await _oscSender.SendAsync(
new OscBundle(0, chatbox, OSCUtils.MakeChatboxTyping(true))
);
}

Expand All @@ -110,5 +100,11 @@ private void SendMessage()
Console.WriteLine(ex);
}
}

public void Dispose()
{
_oscSender.Dispose();
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using CoreOSC;
using LucHeart.CoreOSC;

namespace ButterSTT.MessageSystem
{
public class OSCHandler
public static class OSCUtils
{
public readonly UDPSender OSCSender;

public OSCHandler(string address = "127.0.0.1", int port = 9000)
{
OSCSender = new UDPSender(address, port);
}

public static OscMessage MakeChatboxTyping(bool isTyping)
{
return new OscMessage("/chatbox/typing", isTyping);
Expand Down
6 changes: 1 addition & 5 deletions ButterSTT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@
WordTime = config.WordTime,
HardWordTime = config.HardWordTime,
};
var oscHandler = new OSCMessageHandler(
messageQueue,
oscAddress: config.OSCAddress,
oscPort: config.OSCPort
)
using var oscHandler = new OSCMessageHandler(messageQueue, config.OSCEndpoint)
{
RateLimit = config.OSCChatboxRateLimit
};
Expand Down
1 change: 0 additions & 1 deletion Dependencies/CoreOSC-VRC-UTF8
Submodule CoreOSC-VRC-UTF8 deleted from 1caa13

0 comments on commit 3c76f14

Please sign in to comment.