Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions UI24RBridgeTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UI24RController;
using UI24RController.MIDIController;

Expand All @@ -26,7 +27,7 @@ static void Main(string[] args)
var controllersSetting = new List<UI24RController.ControllerSettings>();
configuration.GetSection("MidiControllers").Bind(controllersSetting);


var address = configuration["UI24R-Url"];
var midiInputDevice = configuration["MIDI-Input-Name"];
var midiOutputDevice = configuration["MIDI-Output-Name"];
Expand Down Expand Up @@ -55,7 +56,7 @@ static void Main(string[] args)
{
controllerSecond = MIDIControllerFactory.GetMidiController(protocol);
}

if (args.Length > 0)
WriteMIDIDeviceNames(controller);
else
Expand Down Expand Up @@ -156,7 +157,7 @@ static void Main(string[] args)
Console.WriteLine("Start bridge...");

BridgeSettings settings = new BridgeSettings(address, messageWriter);

if (syncID != null)
{
settings.SyncID = syncID;
Expand Down Expand Up @@ -251,27 +252,42 @@ static void Main(string[] args)
}
}
}

}
}
}
private static string PromptDeviceChoice(string promptText, IEnumerable<string> deviceNames)
{
var nameList = deviceNames.ToList();

AnsiConsole.WriteLine(promptText);
for (int i = 0; i < nameList.Count; i++)
AnsiConsole.MarkupLine($" [blue][[{i + 1}]][/]: {nameList[i]}");

int chosen = AnsiConsole.Prompt(
new TextPrompt<int>("Enter number:")
.DefaultValue(1)
.Validate(n => n >= 1 && n <= nameList.Count
? ValidationResult.Success()
: ValidationResult.Error($"Please enter a number between 1 and {nameList.Count}")));

return nameList[chosen - 1];
}

private static void CreateAppsettings(string fileName)
{
var controller = MIDIControllerFactory.GetMidiController("MC");
var inputDevicenames = controller.GetInputDeviceNames();
var outputDevicenames = controller.GetOutputDeviceNames();

AnsiConsole.WriteLine("appsettings.json is not found.");
AnsiConsole.WriteLine("Creating of the configuration file is starting.");
var address = AnsiConsole.Ask<string>(@"Please write the mixer address (eg: ws:\\192.168.3.12): "); //"UI24R-Url"
var midiInputDevice = AnsiConsole.Prompt(
new TextPrompt<string>("Choose primary input device. (It is case sensitive.)")
.AddChoices(inputDevicenames)); //configuration["MIDI-Input-Name"];
var midiInputDevice = PromptDeviceChoice(
"Choose primary input device:", inputDevicenames); //configuration["MIDI-Input-Name"];

var midiOutputDevice = AnsiConsole.Prompt(
new TextPrompt<string>("Choose primary output device. (It is case sensitive.)")
.AddChoices(outputDevicenames)); //configuration["MIDI-Output-Name"];
var midiOutputDevice = PromptDeviceChoice(
"Choose primary output device:", outputDevicenames); //configuration["MIDI-Output-Name"];

//var primaryIsExtender = configuration["PrimaryIsExtender"] == "true";
string primaryIsExtender = "false";
Expand All @@ -290,7 +306,7 @@ private static void CreateAppsettings(string fileName)
new TextPrompt<string>("Primary controller offset (show 1-8ch: 0 9-16ch: 1")
.AddChoices(["0", "1"])
.DefaultValue("0"));

var isAddSecondaryDevice = AnsiConsole.Prompt(
new TextPrompt<bool>("Do you want to add secondary device?")
.AddChoice(true)
Expand All @@ -304,17 +320,15 @@ private static void CreateAppsettings(string fileName)
string secondaryIsExtender = "false";
string secondaryChannelStart = "1";
//var secondaryMidiInputDevice = configuration["MIDI-Input-Name-Second"];
//var secondaryMidiOutputDevice = configuration["MIDI-Output-Name-Second"];
//var secondaryMidiOutputDevice = configuration["MIDI-Output-Name-Second"];
//var secondaryIsExtender = configuration["SecondaryIsExtender"] == "true";
if (isAddSecondaryDevice)
{
secondaryMidiInputDevice = AnsiConsole.Prompt(
new TextPrompt<string>("Choose secondary input device. (It is case sensitive.)")
.AddChoices(inputDevicenames)); //configuration["MIDI-Input-Name"];
secondaryMidiInputDevice = PromptDeviceChoice(
"Choose secondary input device:", inputDevicenames); //configuration["MIDI-Input-Name"];

secondaryMidiOutputDevice = AnsiConsole.Prompt(
new TextPrompt<string>("Choose secondary output device. (It is case sensitive.)")
.AddChoices(outputDevicenames)); //configuration["MIDI-Output-Name"];
secondaryMidiOutputDevice = PromptDeviceChoice(
"Choose secondary output device:", outputDevicenames); //configuration["MIDI-Output-Name"];

if (AnsiConsole.Prompt(
new TextPrompt<bool>("Is secondary device an extender?")
Expand Down Expand Up @@ -370,7 +384,7 @@ private static void CreateAppsettings(string fileName)
";

File.WriteAllText(fileName, settingsContent);


}

Expand Down
2 changes: 1 addition & 1 deletion UI24RBridgeTest/UI24RBridgeTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RuntimeIdentifiers>win-x64;win-x86;linux-x64</RuntimeIdentifiers>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
Expand Down
12 changes: 6 additions & 6 deletions UI24RController/MIDIController/BehringerUniversalMIDI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public void Dispose()
}
}

public bool ConnectInputDevice(string deviceName)
public async Task<bool> ConnectInputDevice(string deviceName)
{
var access = MidiAccessManager.Default;
var deviceNumber = access.Inputs.Where(i => i.Name == deviceName).FirstOrDefault();
if (deviceNumber != null)
{
_input = access.OpenInputAsync(deviceNumber.Id).Result;
_input = await access.OpenInputAsync(deviceNumber.Id);
_input.MessageReceived += (obj, e) =>
{
if (e.Data.Length>2)
Expand All @@ -104,7 +104,7 @@ public bool ConnectInputDevice(string deviceName)
return true;
}
else
return false;
return false;
}

private void ProcessMidiMessage(MidiReceivedEventArgs e)
Expand All @@ -122,13 +122,13 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
}
}

public bool ConnectOutputDevice(string deviceName)
public async Task<bool> ConnectOutputDevice(string deviceName)
{
var access = MidiAccessManager.Default;
var deviceNumber = access.Outputs.Where(i => i.Name == deviceName).FirstOrDefault();
if (deviceNumber != null)
{
_output = access.OpenOutputAsync(deviceNumber.Id).Result;
_output = await access.OpenOutputAsync(deviceNumber.Id);
return true;
}
return false;
Expand Down Expand Up @@ -207,7 +207,7 @@ public void WriteTextToLCD(string text, int delay)
throw new NotImplementedException();
}

public bool ReConnectDevice()
public async Task<bool> ReConnectDevice()
{
throw new NotImplementedException();
}
Expand Down
7 changes: 4 additions & 3 deletions UI24RController/MIDIController/IMIDIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using System.Collections.Generic;
using UI24RController;
using UI24RController.MIDIController;
using System.Threading.Tasks;

public interface IMIDIController
{
#region Connection
string[] GetInputDeviceNames();
string[] GetOutputDeviceNames();
bool ConnectInputDevice(string deviceName);
bool ConnectOutputDevice(string deviceName);
Task<bool> ConnectInputDevice(string deviceName);
Task<bool> ConnectOutputDevice(string deviceName);

bool ReConnectDevice();
Task<bool> ReConnectDevice();

bool IsConnectionErrorOccured { get; }
bool IsConnected { get; }
Expand Down
28 changes: 14 additions & 14 deletions UI24RController/MIDIController/MC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Commons.Music.Midi;
using System.Linq;
using System.Threading.Tasks;
using Commons.Music.Midi.RtMidi;
using System.Threading;
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -38,7 +37,7 @@ public FaderState(double value)
/// Store every fader setted value of the faders, key is the channel number (z in the message)
/// </summary>
protected ConcurrentDictionary<byte, FaderState> faderValues = new ConcurrentDictionary<byte, FaderState>();

IMidiInput _input = null;
protected string _inputDeviceNumber;
protected string _inputDeviceName;
Expand Down Expand Up @@ -421,10 +420,10 @@ public void Dispose()
_output.Dispose();
_output = null;
}

}

public bool ConnectInputDevice(string deviceName)
public async Task<bool> ConnectInputDevice(string deviceName)
{
try
{
Expand All @@ -433,8 +432,8 @@ public bool ConnectInputDevice(string deviceName)
var deviceNumber = access.Inputs.Where(i => i.Name.ToUpper() == deviceName.ToUpper()).FirstOrDefault();
if (deviceNumber != null)
{

var input = access.OpenInputAsync(deviceNumber.Id).Result;
var input = await access.OpenInputAsync(deviceNumber.Id);

_input = input;
_inputDeviceNumber = deviceNumber.Id;
_input.MessageReceived += (obj, e) =>
Expand All @@ -459,7 +458,7 @@ public bool ConnectInputDevice(string deviceName)
}
return false;
}
public bool ConnectOutputDevice(string deviceName)
public async Task<bool> ConnectOutputDevice(string deviceName)
{
try
{
Expand All @@ -468,7 +467,7 @@ public bool ConnectOutputDevice(string deviceName)
var deviceNumber = access.Outputs.Where(i => i.Name.ToUpper() == deviceName.ToUpper()).FirstOrDefault();
if (deviceNumber != null)
{
var output = access.OpenOutputAsync(deviceNumber.Id).Result;
var output = await access.OpenOutputAsync(deviceNumber.Id);
_outputDeviceNumber = deviceNumber.Id;
_output = output;
_isConnected = true;
Expand Down Expand Up @@ -504,10 +503,11 @@ private void StartPingThread()
_pingThread.Start();

}
public bool ReConnectDevice()
public async Task<bool> ReConnectDevice()
{
return ConnectInputDevice(_inputDeviceName) &&
ConnectOutputDevice(_outputDeviceName);
var inputOk = await ConnectInputDevice(_inputDeviceName);
var outputOk = await ConnectOutputDevice(_outputDeviceName);
return inputOk && outputOk;
}

public string[] GetInputDeviceNames()
Expand All @@ -531,7 +531,7 @@ protected void Send(byte[] mevent, int offset, int length, long timestamp)

}
}
catch
catch
{
OnConnectionErrorEvent();
}
Expand All @@ -540,7 +540,7 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
{
var message = e.Data;

if (message[0] == 0x90) //button pressed, released, fader released
if (message[0] == 0x90) //button pressed, released, fader released
{
if (message.MIDIEqual(0x90, 0x00, 0x00, 0xff, 0x00, 0xff) && (message[1] >= 0x68) && (message[1] <= 0x70)) //release fader (0x90 [0x68-0x70] 0x00)
{
Expand Down Expand Up @@ -795,7 +795,7 @@ private void ProcessMidiMessage(MidiReceivedEventArgs e)
int lower = message[1]; // lower 7 bit

var faderValue = (upper + lower) / 16383.0;

faderValues[channelNumber].Value = faderValue;
OnFaderEvent(channelNumber, faderValue);
if (!faderValues[channelNumber].IsTouched)
Expand Down
Loading