Skip to content

Commit

Permalink
Osc code restructure, osc afk timer
Browse files Browse the repository at this point in the history
  • Loading branch information
PaciStardust committed Dec 16, 2022
1 parent 3ce8f57 commit 4cde510
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 125 deletions.
25 changes: 23 additions & 2 deletions OscMultitool/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,18 @@ public class ConfigOscModel
{
//Routing
public string Ip { get; set; } = "127.0.0.1";
public int Port { get; set; } = 9000;
public int PortListen { get; set; } = 9001;
public int Port
{
get { return _port; }
set { _port = MinMax(value, -1, 65535); }
}
private int _port = 9000;
public int PortListen
{
get { return _portListen; }
set { _portListen = MinMax(value, -1, 65535); }
}
private int _portListen = 9001;
public List<OscRoutingFilterModel> RoutingFilters { get; set; } = new();

//Addresses
Expand All @@ -176,13 +186,24 @@ public class ConfigOscModel
public string AddressEnableTts { get; set; } = "/avatar/parameters/ToolEnableTts";
public string AddressEnableAutoMute { get; set; } = "/avatar/parameters/ToolEnableAutoMute";
public string AddressListeningIndicator { get; set; } = "/avatar/parameters/MicListening";
public string AddressGameMute { get; set; } = "/avatar/parameters/MuteSelf";
public string AddressGameAfk { get; set; } = "/avatar/parameters/AFK";
public string AddressAddTextbox { get; set; } = "/hoscy/message";
public string AddressAddTts { get; set; } = "/hoscy/tts";
public string AddressAddNotification { get; set; } = "/hoscy/notification";

//Counters
public bool ShowCounterNotifications { get; set; } = false;
public List<CounterModel> Counters { get; set; } = new();

//AFK
public bool ShowAfkDuration { get; set; } = false;
public int AfkDuration
{
get { return _afkDuration; }
set { _afkDuration = MinMax(value, 5000, 300000); }
}
private int _afkDuration = 30000;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions OscMultitool/Hoscy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<UseWPF>true</UseWPF>
<ApplicationIcon>Resources\hoscy_circle.ico</ApplicationIcon>
<Platforms>AnyCPU;x64</Platforms>
<AssemblyVersion>0.5</AssemblyVersion>
<FileVersion>0.5</FileVersion>
<AssemblyVersion>0.5.1</AssemblyVersion>
<FileVersion>0.5.1</FileVersion>
<RepositoryUrl>https://github.com/PaciStardust/HOSCY</RepositoryUrl>
<PackageProjectUrl>https://github.com/PaciStardust/HOSCY</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion OscMultitool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Hoscy.OscControl;
using Hoscy.Services.OscControl;
using Hoscy.Services.Api;
using Hoscy.Ui;
using Hoscy.Ui.Controls;
Expand Down
2 changes: 1 addition & 1 deletion OscMultitool/Services/OscControl/Osc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using VRC.OSCQuery;
using System.Linq;

namespace Hoscy.OscControl
namespace Hoscy.Services.OscControl
{
/// <summary>
/// Static class for osc-related things
Expand Down
167 changes: 167 additions & 0 deletions OscMultitool/Services/OscControl/OscDataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using Hoscy.Services.Speech;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Hoscy.Services.OscControl
{
internal static class OscDataHandler
{
#region Basic Handling
/// <summary>
/// Internal parsing for osc in case it triggers a command
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="arguments">Arguments of packet</param>
internal static void Handle(string address, object[] arguments)
{
if (arguments.Length == 0 || arguments[0] == null)
return;

Type type = arguments[0].GetType();

if (type == typeof(bool))
HandleToolDataBool(address, (bool)arguments[0]);
else if (type == typeof(string))
HandleToolDataString(address, (string)arguments[0]);
}

/// <summary>
/// Handles all internal osc commands of type bool
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="value">Bool value</param>
private static void HandleToolDataBool(string address, bool value)
{
if (Config.Speech.MuteOnVrcMute && address == Config.Osc.AddressGameMute)
Recognition.SetListening(!value);

if (address == Config.Osc.AddressGameAfk)
SetAfkTimer(value);

if (!value) //Options below will be triggered with an osc button so this avoids triggering twice
return;

//Checking counters
CheckForCounters(address);

if (address == Config.Osc.AddressManualMute)
Recognition.SetListening(!Recognition.IsRecognizerListening);

if (address == Config.Osc.AddressManualSkipBox)
Textbox.Clear();

if (address == Config.Osc.AddressManualSkipSpeech)
Synthesizing.Skip();

if (address == Config.Osc.AddressEnableAutoMute)
{
bool newValue = !Config.Speech.MuteOnVrcMute;
Logger.Info("'Mute on VRC mute' has been changed via OSC => " + newValue);
Config.Speech.MuteOnVrcMute = !Config.Speech.MuteOnVrcMute;
}

if (address == Config.Osc.AddressEnableTextbox)
{
bool newValue = !Config.Speech.UseTextbox;
Logger.Info("'Textbox on Speech' has been changed via OSC => " + newValue);
Config.Speech.UseTextbox = newValue;
}

if (address == Config.Osc.AddressEnableTts)
{
bool newValue = !Config.Speech.UseTts;
Logger.Info("'TTS on Speech' has been changed via OSC => " + newValue);
Config.Speech.UseTts = !Config.Speech.UseTts;
}

if (address == Config.Osc.AddressEnableReplacements)
{
bool newValue = !Config.Speech.UseReplacements;
Logger.Info("'Replacements and Shortcuts for Speech' has been changed via OSC => " + newValue);
Config.Speech.UseReplacements = !Config.Speech.UseReplacements;
}
}

/// <summary>
/// Handles all internal osc commands of type string
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="value">String value</param>
private static void HandleToolDataString(string address, string value)
{
if (string.IsNullOrWhiteSpace(value))
return;

if (address == Config.Osc.AddressAddTextbox || address == Config.Osc.AddressAddTts)
{
var tProcessor = new TextProcessor()
{
ReplaceCaseInsensitive = true,
TriggerReplace = true,
TriggerCommands = true,
UseTextbox = address == Config.Osc.AddressAddTextbox,
UseTts = address == Config.Osc.AddressAddTts,
AllowTranslate = Config.Api.TranslationAllowExternal
};
tProcessor.Process(value);
}

if (address == Config.Osc.AddressAddNotification)
Textbox.Notify(value, NotificationType.External);
}
#endregion

#region Functionality
/// <summary>
/// Checks for counter increases
/// </summary>
/// <param name="address">Osc Address</param>
private static void CheckForCounters(string address)
{
foreach (var counter in Config.Osc.Counters)
{
if (counter.Parameter != address)
continue;

counter.Count++;
Logger.Debug($"Counter \"{counter.Count}\" ({counter.Parameter}) increased to {counter.Count}");
Textbox.Notify(counter.ToString(), NotificationType.Counter);
break;
}
}

private static Timer? _afkTimer;
private static DateTime _afkStarted = DateTime.Now;
private static void SetAfkTimer(bool mode)
{
if (Config.Osc.ShowAfkDuration && mode && _afkTimer == null)
{
Textbox.Notify("User now AFK", NotificationType.Afk);
_afkStarted = DateTime.Now;

_afkTimer = new(Config.Osc.AfkDuration);
_afkTimer.Elapsed += AfkTimerElapsed;
_afkTimer.Start();

Logger.Log("AFK Timer started");
return;
}
else if (!mode && _afkTimer != null)
{
Textbox.Notify("User no longer AFK", NotificationType.Afk);
_afkTimer.Stop();
_afkTimer.Dispose();
_afkTimer = null;
Logger.Log("AFK Timer stopped");
}
}

private static void AfkTimerElapsed(object? sender, ElapsedEventArgs e)
=> Textbox.Notify("User AFK since " + (e.SignalTime.AddMilliseconds(500) - _afkStarted).ToString(@"hh\:mm\:ss"), NotificationType.Afk);
#endregion
}
}
113 changes: 2 additions & 111 deletions OscMultitool/Services/OscControl/OscListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using VRC.OSCQuery;
using System.Threading.Tasks;

namespace Hoscy.OscControl
namespace Hoscy.Services.OscControl
{
public class OscListener
{
Expand Down Expand Up @@ -118,121 +118,12 @@ public void Stop()
/// <param name="arguments">Arguments of packet</param>
private void HandleData(string address, object[] arguments)
{
HandleToolData(address, arguments);
OscDataHandler.Handle(address, arguments);

foreach (var filter in _filters)
if (filter.Matches(address))
filter.Send(address, arguments);
}

/// <summary>
/// Internal parsing for osc in case it triggers a command
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="arguments">Arguments of packet</param>
private static void HandleToolData(string address, object[] arguments)
{
if (arguments.Length == 0 || arguments[0] == null)
return;

Type type = arguments[0].GetType();

if (type == typeof(bool))
HandleToolDataBool(address, (bool)arguments[0]);
else if (type == typeof(string))
HandleToolDataString(address, (string)arguments[0]);
}

/// <summary>
/// Handles all internal osc commands of type bool
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="value">Bool value</param>
private static void HandleToolDataBool(string address, bool value)
{
if (Config.Speech.MuteOnVrcMute && address == "/avatar/parameters/MuteSelf")
Recognition.SetListening(!value);

if (!value) //Options below will be triggered with an osc button so this avoids triggering twice
return;

//Checking counters
foreach(var counter in Config.Osc.Counters)
{
if (counter.Parameter != address)
continue;

counter.Count++;
Logger.Debug($"Counter \"{counter.Count}\" ({counter.Parameter}) increased to {counter.Count}");
Textbox.Notify(counter.ToString(), NotificationType.Counter);
break;
}

if (address == Config.Osc.AddressManualMute)
Recognition.SetListening(!Recognition.IsRecognizerListening);

if (address == Config.Osc.AddressManualSkipBox)
Textbox.Clear();

if (address == Config.Osc.AddressManualSkipSpeech)
Synthesizing.Skip();

if (address == Config.Osc.AddressEnableAutoMute)
{
bool newValue = !Config.Speech.MuteOnVrcMute;
Logger.Info("'Mute on VRC mute' has been changed via OSC => " + newValue);
Config.Speech.MuteOnVrcMute = !Config.Speech.MuteOnVrcMute;
}

if (address == Config.Osc.AddressEnableTextbox)
{
bool newValue = !Config.Speech.UseTextbox;
Logger.Info("'Textbox on Speech' has been changed via OSC => " + newValue);
Config.Speech.UseTextbox = newValue;
}

if (address == Config.Osc.AddressEnableTts)
{
bool newValue = !Config.Speech.UseTts;
Logger.Info("'TTS on Speech' has been changed via OSC => " + newValue);
Config.Speech.UseTts = !Config.Speech.UseTts;
}

if (address == Config.Osc.AddressEnableReplacements)
{
bool newValue = !Config.Speech.UseReplacements;
Logger.Info("'Replacements and Shortcuts for Speech' has been changed via OSC => " + newValue);
Config.Speech.UseReplacements = !Config.Speech.UseReplacements;
}
}

/// <summary>
/// Handles all internal osc commands of type string
/// </summary>
/// <param name="address">Target address of packet</param>
/// <param name="value">String value</param>
private static void HandleToolDataString(string address, string value)
{
if (string.IsNullOrWhiteSpace(value))
return;

if (address == Config.Osc.AddressAddTextbox || address == Config.Osc.AddressAddTts)
{
var tProcessor = new TextProcessor()
{
ReplaceCaseInsensitive = true,
TriggerReplace = true,
TriggerCommands = true,
UseTextbox = address == Config.Osc.AddressAddTextbox,
UseTts = address == Config.Osc.AddressAddTts,
AllowTranslate = Config.Api.TranslationAllowExternal
};
tProcessor.Process(value);
}

if (address == Config.Osc.AddressAddNotification)
Textbox.Notify(value, NotificationType.External);
}
#endregion

#region OscQuery
Expand Down
2 changes: 1 addition & 1 deletion OscMultitool/Services/OscControl/OscPacket.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Hoscy.OscControl
namespace Hoscy.Services.OscControl
{
public readonly struct OscPacket
{
Expand Down
2 changes: 1 addition & 1 deletion OscMultitool/Services/OscControl/OscRoutingFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Hoscy.OscControl
namespace Hoscy.Services.OscControl
{
public readonly struct OscRoutingFilter
{
Expand Down
2 changes: 1 addition & 1 deletion OscMultitool/Services/Speech/Recognizers/RecognizerBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Hoscy.OscControl;
using Hoscy.Services.OscControl;
using Hoscy.Services.Speech.Recognizers;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down
Loading

0 comments on commit 4cde510

Please sign in to comment.