Skip to content

Commit

Permalink
attempt to make shock for openshock through osc
Browse files Browse the repository at this point in the history
  • Loading branch information
krogenth committed Aug 18, 2024
1 parent c86c216 commit ae4c26b
Show file tree
Hide file tree
Showing 37 changed files with 1,583 additions and 36 deletions.
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<PackageVersion Include="Vosk" Version="0.3.38" />
<!-- Open Sound Control Framework -->
<PackageVersion Include="BuildSoft.OscCore" Version="1.2.1.1" />
<!-- OpenShock API -->
<PackageVersion Include="OpenShock.SDK.CSharp" Version="0.0.25" />
<PackageVersion Include="OpenShock.SDK.CSharp.Hub" Version="0.0.24" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions OpenShock.VoiceRecognizer.Common/Enums/BrowserProxyType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using OpenShock.VoiceRecognizer.Common.Utility;
using System.Text.Json.Serialization;

namespace OpenShock.VoiceRecognizer.Common.Enums;

[JsonConverter(typeof(TypedStringEnumConverter<BrowserProxyType>))]
public enum BrowserProxyType
{
Chrome,
Edge,
Opera,
Firefox,
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public void LoadFileConfiguration(ConfigurationFileFormat configurationFileForma
InputDeviceID.Value = configurationFileFormat.InputDeviceID;

public void LoadDefaultConfiguration() =>
InputDeviceID = new(AudioDevices.GetInputAudioDevices().First().ID);
InputDeviceID.Value = AudioDevices.GetInputAudioDevices().First().ID;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using OpenShock.VoiceRecognizer.Common;
using OpenShock.VoiceRecognizer.Common.Audio;
using OpenShock.VoiceRecognizer.Common.Enums;

namespace OpenShock.VoiceRecognizer.Configuration;

public class BrowserProxyConfigurationState
{
public ReactiveObject<BrowserProxyType> Proxy { get; set; }
public ReactiveObject<int> ProxyPort { get; set; }

public BrowserProxyConfigurationState()
{
Proxy = new(BrowserProxyType.Chrome);
ProxyPort = new(0);
}

public void LoadFileConfiguration(ConfigurationFileFormat configurationFileFormat)
{
Proxy.Value = configurationFileFormat.BrowserProxyType;
ProxyPort.Value = configurationFileFormat.BrowserProxyPort;
}

public void LoadDefaultConfiguration()
{
Proxy.Value = BrowserProxyType.Chrome;
ProxyPort.Value = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ public class ConfigurationFileFormat
public string InputDeviceID { get; set; } = string.Empty;
public string VoskModelDirectory { get; set; } = string.Empty;
public int OscListenPort { get; set; } = 0;
public ShockCollarType CollarType { get; }
public ShockCollarType CollarType { get; set; } = ShockCollarType.OpenShock;
public ObservableCollection<WordRecognition> Words { get; set; } = [];
/*public string OpenShockAPIKey { get; set; } = string.Empty;
public string OpenShockGroupName { get; set; } = string.Empty;*/
public string OpenShockSendHost { get; set; } = string.Empty;
public int OpenShockSendPort { get; set; } = 0;
public string OpenShockOscListenIntensityEndpoint { get; set; } = string.Empty;
public string OpenShockOscSendShockEndpoint { get; set; } = string.Empty;
public string OpenShockOscSendVibrateEndpoint { get; set; } = string.Empty;
public BrowserProxyType BrowserProxyType { get; set; } = BrowserProxyType.Chrome;
public int BrowserProxyPort { get; set; } = 0;

public ConfigurationFileFormat() { }

Expand All @@ -21,6 +30,15 @@ public ConfigurationFileFormat(ConfigurationState state)
OscListenPort = state.OSC.ListenPort.Value;
CollarType = state.Shock.CollarType.Value;
Words = state.Shock.Words.Value;
/*OpenShockAPIKey = state.OpenShock.APIKey.Value;
OpenShockGroupName = state.OpenShock.GroupName.Value;*/
OpenShockSendHost = state.OpenShock.SendHost.Value;
OpenShockSendPort = state.OpenShock.SendPort.Value;
OpenShockOscListenIntensityEndpoint = state.OpenShock.ExternalListenSetIntensityEndpoint;
OpenShockOscSendShockEndpoint = state.OpenShock.ExternalSendStartShockEndpoint;
OpenShockOscSendVibrateEndpoint = state.OpenShock.ExternalSendStartShockEndpoint;
BrowserProxyType = state.BrowserProxy.Proxy.Value;
BrowserProxyPort = state.BrowserProxy.ProxyPort.Value;
}

public static bool TryLoad(string filepath, out ConfigurationFileFormat? configurationFileFormat)
Expand Down
12 changes: 9 additions & 3 deletions OpenShock.VoiceRecognizer.Configuration/ConfigurationState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using OpenShock.VoiceRecognizer.Common.Enums;

namespace OpenShock.VoiceRecognizer.Configuration;
namespace OpenShock.VoiceRecognizer.Configuration;

public class ConfigurationState
{
Expand All @@ -10,6 +8,8 @@ public class ConfigurationState
public VoskConfigurationState Vosk { get; }
public OSCConfigurationState OSC { get; }
public ShockConfigurationState Shock { get; }
public OpenShockConfigurationState OpenShock { get; }
public BrowserProxyConfigurationState BrowserProxy { get; }

public static ConfigurationState? Instance { get; private set; } = null;

Expand All @@ -19,6 +19,8 @@ private ConfigurationState()
Vosk = new();
OSC = new();
Shock = new();
OpenShock = new();
BrowserProxy = new();
}

public void LoadConfiguration()
Expand All @@ -41,6 +43,8 @@ private void LoadConfigurationStateFromFile()
Vosk.LoadFileConfiguration(configurationFileFormat!);
OSC.LoadFileConfiguration(configurationFileFormat!);
Shock.LoadFileConfiguration(configurationFileFormat!);
OpenShock.LoadFileConfiguration(configurationFileFormat!);
BrowserProxy.LoadFileConfiguration(configurationFileFormat!);
}
}

Expand All @@ -50,6 +54,8 @@ private void LoadDefaultConfigurationState()
Vosk.LoadDefaultConfiguration();
OSC.LoadDefaultConfiguration();
Shock.LoadDefaultConfiguration();
OpenShock.LoadDefaultConfiguration();
BrowserProxy.LoadDefaultConfiguration();
}

public void SaveConfigurationStateToFile() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public void LoadFileConfiguration(ConfigurationFileFormat configurationFileForma
ListenPort.Value = configurationFileFormat.OscListenPort;

public void LoadDefaultConfiguration() =>
ListenPort = new(9005);
ListenPort.Value = 9005;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using OpenShock.VoiceRecognizer.Common;

namespace OpenShock.VoiceRecognizer.Configuration;

public class OpenShockConfigurationState
{
/*public ReactiveObject<string> APIKey { get; private set; }
public ReactiveObject<string> GroupName { get; private set; }*/
public ReactiveObject<string> SendHost { get; private set; }
public ReactiveObject<int> SendPort { get; private set; }

public ReactiveObject<string> ExternalListenSetIntensityEndpoint { get; set; }
public ReactiveObject<string> ExternalSendStartShockEndpoint { get; set; }
public ReactiveObject<string> ExternalSendStartVibrationEndpoint { get; set; }

public OpenShockConfigurationState()
{
/*APIKey = new(string.Empty);
GroupName = new(string.Empty);*/
SendHost = new(string.Empty);
SendPort = new(9006);
ExternalListenSetIntensityEndpoint = new(string.Empty);
ExternalSendStartShockEndpoint = new(string.Empty);
ExternalSendStartVibrationEndpoint = new(string.Empty);
}

public void LoadFileConfiguration(ConfigurationFileFormat configurationFileFormat)
{
/*APIKey.Value = configurationFileFormat.OpenShockAPIKey;
GroupName.Value = configurationFileFormat.OpenShockGroupName;*/
SendHost.Value = configurationFileFormat.OpenShockSendHost;
SendPort.Value = configurationFileFormat.OpenShockSendPort;
ExternalListenSetIntensityEndpoint.Value = configurationFileFormat.OpenShockOscListenIntensityEndpoint;
ExternalSendStartShockEndpoint.Value = configurationFileFormat.OpenShockOscSendShockEndpoint;
ExternalSendStartVibrationEndpoint.Value = configurationFileFormat.OpenShockOscSendVibrateEndpoint;
}

public void LoadDefaultConfiguration()
{
/*APIKey.Value = string.Empty;
GroupName.Value = string.Empty;*/
SendHost.Value = "127.0.0.1";
SendPort.Value = 0;
ExternalListenSetIntensityEndpoint.Value = string.Empty;
ExternalSendStartShockEndpoint.Value = string.Empty;
ExternalSendStartVibrationEndpoint.Value = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public void LoadFileConfiguration(ConfigurationFileFormat configurationFileForma

public void LoadDefaultConfiguration()
{
CollarType = new(ShockCollarType.OpenShock);
Words = new([]);
CollarType.Value = ShockCollarType.OpenShock;
Words.Value = [];
}
}

public class WordRecognition
{
public string Word { get; set; } = string.Empty;
public ShockType Type { get; set; }
public float Delay { get; set; }
public float MinDelay { get; set; }
public float MaxDelay { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public void LoadFileConfiguration(ConfigurationFileFormat configurationFileForma
ModelDirectory.Value = configurationFileFormat.VoskModelDirectory;

public void LoadDefaultConfiguration() =>
ModelDirectory = new(AppDomain.CurrentDomain.BaseDirectory);
ModelDirectory.Value = AppDomain.CurrentDomain.BaseDirectory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net;

namespace OpenShock.VoiceRecognizer.Integrations.BrowserProxy;

public abstract class BaseBrowserProxy
{
protected HttpListener? _listener = null;
protected Thread? _thread = null;
protected bool _shouldExit = false;
protected int _port = 0;
protected List<string> _speechDetectionResults = [];
protected List<string> _speechDetectionOnEnd = [];
protected List<string> _pendingJavascript = [];

public BaseBrowserProxy() { }

public abstract void StartProxy(int port);

public abstract void StopProxy();

public abstract void DetectBrowser();

public abstract void CloseBrowser();

public void AddJavascriptCommand(string command) => _pendingJavascript.Add(command);

protected abstract void WorkerThread();
}
Loading

0 comments on commit ae4c26b

Please sign in to comment.