Skip to content

Commit

Permalink
Merge branch 'feature/fatchannel'
Browse files Browse the repository at this point in the history
  • Loading branch information
oddbear committed Feb 4, 2022
2 parents cc009f5 + 6b091f7 commit cdc6355
Show file tree
Hide file tree
Showing 19 changed files with 491 additions and 61 deletions.
6 changes: 6 additions & 0 deletions Revelator.io24.Api/Models/SynchronizeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class SynchronizeModel

public Headphones HeadphonesSource { get; set; }

public bool ByPassDsp_MicL { get; set; }
public bool ByPassDsp_MicR { get; set; }

public SynchronizeModel(Synchronize model)
{
var children = model.Children;
Expand Down Expand Up @@ -80,6 +83,9 @@ public SynchronizeModel(Synchronize model)
HeadphonesSource = Headphones.MixA;
if (globalV.PhonesSrc == 1) //16256
HeadphonesSource = Headphones.MixB;

ByPassDsp_MicL = lineC.Ch1.Values.BypassDSP > 0;
ByPassDsp_MicR = lineC.Ch2.Values.BypassDSP > 0;
}
}
}
22 changes: 18 additions & 4 deletions Revelator.io24.Api/Services/CommunicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommunicationService : IDisposable
private Thread? _listeningThread;
private Thread? _writingThread;
private NetworkStream? _networkStream;
private ushort _monitorPort;

public CommunicationService()
{
Expand All @@ -34,11 +35,12 @@ public void Init(ushort tcpPort, ushort monitorPort)
if (_networkStream is not null)
return;

_monitorPort = monitorPort;

_tcpClient.Connect(IPAddress.Loopback, tcpPort);
_networkStream = _tcpClient.GetStream();

var welcomeMessage = CreateWelcomeMessage(monitorPort);
_networkStream.Write(welcomeMessage);
RequestCommunicationMessage();

_listeningThread = new Thread(Listener) { IsBackground = true };
_listeningThread.Start();
Expand All @@ -47,11 +49,20 @@ public void Init(ushort tcpPort, ushort monitorPort)
_writingThread.Start();
}

private byte[] CreateWelcomeMessage(ushort monitorPort)
private void RequestCommunicationMessage()
{
if (_networkStream is null)
return;

var welcomeMessage = CreateWelcomeMessage();
_networkStream.Write(welcomeMessage);
}

private byte[] CreateWelcomeMessage()
{
var list = new List<byte>();

var welcomeMessage = WelcomeMessage.Create(monitorPort);
var welcomeMessage = WelcomeMessage.Create(_monitorPort);
list.AddRange(welcomeMessage);

var jsonMessage = ClientInfoMessage.Create();
Expand Down Expand Up @@ -171,6 +182,9 @@ private void Json(string json)
return;
case "SubscriptionReply":
return;
case "SubscriptionLost":
RequestCommunicationMessage();
return;
default:
Log.Warning("[{className}] Unknown json id {messageType}", nameof(CommunicationService), id);
return;
Expand Down
3 changes: 3 additions & 0 deletions Revelator.io24.Api/Services/RoutingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class RoutingModel

public Headphones HeadphonesSource { get; set; } = Headphones.Unknown;

public bool FatChannel_MicL { get; set; }
public bool FatChannel_MicR { get; set; }

public bool GetValueByRoute(string route)
{
var properties = typeof(RoutingModel).GetProperties();
Expand Down
31 changes: 26 additions & 5 deletions Revelator.io24.Api/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class UpdateService
public UpdateService(CommunicationService communicationService)
{
_communicationService = communicationService;
_communicationService.RouteChange += _communicationService_RouteChange;
_communicationService.Synchronize += _communicationService_Synchronize;
_communicationService.RouteChange += RouteChange;
_communicationService.Synchronize += Synchronize;
}

private void _communicationService_Synchronize(SynchronizeModel synchronizeModel)
private void Synchronize(SynchronizeModel synchronizeModel)
{
Routing.Main_MicL = synchronizeModel.Mic_L.Main_Assigned;
Routing.MixA_MicL = synchronizeModel.Mic_L.MixA_Assigned;
Expand Down Expand Up @@ -50,10 +50,13 @@ private void _communicationService_Synchronize(SynchronizeModel synchronizeModel

Routing.HeadphonesSource = synchronizeModel.HeadphonesSource;

Routing.FatChannel_MicL = !synchronizeModel.ByPassDsp_MicL;
Routing.FatChannel_MicR = !synchronizeModel.ByPassDsp_MicR;

RoutingUpdated?.Invoke(this, EventArgs.Empty);
}

private void _communicationService_RouteChange(string route, ushort state)
private void RouteChange(string route, ushort state)
{
if (route == "global/phonesSrc")
{
Expand All @@ -64,6 +67,24 @@ private void _communicationService_RouteChange(string route, ushort state)
return;
}

if (route == "line/ch1/bypassDSP")
{
Routing.FatChannel_MicL = state == 0;

RoutingUpdated?.Invoke(this, EventArgs.Empty);

return;
}

if (route == "line/ch2/bypassDSP")
{
Routing.FatChannel_MicR = state == 0;

RoutingUpdated?.Invoke(this, EventArgs.Empty);

return;
}

var properties = typeof(RoutingModel).GetProperties();
foreach (var property in properties)
{
Expand All @@ -86,7 +107,7 @@ private void _communicationService_RouteChange(string route, ushort state)
}
}

public void SetRouteValue(string route, uint value)
public void SetRouteValue(string route, float value)
{
var message = new List<byte>();

Expand Down
108 changes: 108 additions & 0 deletions Revelator.io24.StreamDeck/Actions/FatChannelToggleAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Revelator.io24.Api.Enums;
using Revelator.io24.Api.Services;
using Revelator.io24.StreamDeck.Settings;
using SharpDeck;
using SharpDeck.Events.Received;
using System.Diagnostics;

namespace Revelator.io24.StreamDeck.Actions
{
[StreamDeckAction("com.oddbear.revelator.io24.fatchanneltoggle")]
public class FatChannelToggleAction : StreamDeckAction
{
private readonly UpdateService _updateService;
private FatChannelToggleSettings _settings;

public FatChannelToggleAction(UpdateService updateService)
{
_updateService = updateService;
}

private int _count;

protected override async Task OnDidReceiveSettings(ActionEventArgs<ActionPayload> args)
{
await base.OnDidReceiveSettings(args);

await UpdateSettings(args.Payload);
}

protected override async Task OnWillAppear(ActionEventArgs<AppearancePayload> args)
{
await base.OnWillAppear(args);

_count++;
_updateService.RoutingUpdated += RoutingUpdated;

await UpdateSettings(args.Payload);
}

protected override async Task OnWillDisappear(ActionEventArgs<AppearancePayload> args)
{
await base.OnWillDisappear(args);

_count--;
_updateService.RoutingUpdated -= RoutingUpdated;
}

private async Task UpdateSettings(ActionPayload payload)
{
_settings = payload
.Settings["settingsModel"]
?.ToObject<FatChannelToggleSettings>() ?? new FatChannelToggleSettings();

await StateUpdated();
}

protected override async Task OnKeyDown(ActionEventArgs<KeyPayload> args)
{
await base.OnKeyDown(args);

if (_settings is null)
return;

var action = Action();
_updateService.SetRouteValue(_settings.Route, action);
}

private float Action()
{
return _settings.Action switch
{
"On" => 0.0f,
"Off" => 1.0f,
_ => GetCurrentState() ? 1.0f : 0.0f,
};
}

private bool GetCurrentState()
=> _settings.Route == "line/ch1/bypassDSP"
? _updateService.Routing.FatChannel_MicL
: _updateService.Routing.FatChannel_MicR;

private async void RoutingUpdated(object? sender, EventArgs e)
{
try
{
await StateUpdated();
}
catch (Exception)
{
//Event
}
}

private int? _lastState = null;

private async Task StateUpdated()
{
var state = GetCurrentState() ? 0 : 1;
if (state == _lastState)
return;

Trace.WriteLine($"Fat state: {state}, count: {_count}, context: {base.Context}");
await SetStateAsync(state);
_lastState = state;
}
}
}
16 changes: 14 additions & 2 deletions Revelator.io24.StreamDeck/Actions/HeadphonesSourceAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Revelator.io24.Api.Services;
using Revelator.io24.Api.Enums;
using Revelator.io24.Api.Services;
using Revelator.io24.StreamDeck.Settings;
using SharpDeck;
using SharpDeck.Events.Received;
Expand Down Expand Up @@ -53,7 +54,18 @@ protected override async Task OnKeyDown(ActionEventArgs<KeyPayload> args)
return;

var setting = _settings.Microphone;
_updateService.SetRouteValue("global/phonesSrc", (ushort)setting);
switch(setting)
{
case Headphones.Main:
_updateService.SetRouteValue("global/phonesSrc", 0.0f);
break;
case Headphones.MixA:
_updateService.SetRouteValue("global/phonesSrc", 0.5f);
break;
case Headphones.MixB:
_updateService.SetRouteValue("global/phonesSrc", 1.0f);
break;
}
}

private async void RoutingUpdated(object? sender, EventArgs e)
Expand Down
Loading

0 comments on commit cdc6355

Please sign in to comment.