Initial commit
This commit is contained in:
128
MidiManager.cs
Normal file
128
MidiManager.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using Midi.Devices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
|
||||
|
||||
namespace VirtualDJ_MidiDeviceBridge
|
||||
{
|
||||
internal static class MidiManager
|
||||
{
|
||||
public static void SetupDevice()
|
||||
{
|
||||
Globals.MidiInDevice.NoteOn += MidiDevice_NoteOn;
|
||||
Globals.MidiInDevice.NoteOff += MidiDevice_NoteOff;
|
||||
Globals.MidiInDevice.ControlChange += MidiDevice_ControlChange;
|
||||
}
|
||||
|
||||
private static void MidiDevice_NoteOn(Midi.Messages.NoteOnMessage msg)
|
||||
{
|
||||
string key = $"NOTEON_{(int)(msg.Pitch)}";
|
||||
|
||||
if ((int)msg.Pitch == Globals.Config.ShiftNoteId)
|
||||
{
|
||||
if (Globals.Config.ToggleShift && Globals.ShiftKeyHeld)
|
||||
Globals.ShiftKeyHeld = false;
|
||||
else
|
||||
Globals.ShiftKeyHeld = true;
|
||||
}
|
||||
|
||||
string cmd = null;
|
||||
Globals.Config.GetActiveMidiInCommands(Globals.ShiftKeyHeld).TryGetValue(key, out cmd);
|
||||
|
||||
if (cmd == null)
|
||||
{
|
||||
Console.WriteLine($"Unbound midi event: {key} (shift: {Globals.ShiftKeyHeld})");
|
||||
return;
|
||||
}
|
||||
|
||||
Globals.SendCommand($"0:(command){cmd}");
|
||||
}
|
||||
|
||||
private static void MidiDevice_NoteOff(Midi.Messages.NoteOffMessage msg)
|
||||
{
|
||||
string key = $"NOTEOFF_{(int)(msg.Pitch)}";
|
||||
|
||||
if ((int)msg.Pitch == Globals.Config.ShiftNoteId)
|
||||
{
|
||||
if (!Globals.Config.ToggleShift)
|
||||
Globals.ShiftKeyHeld = false;
|
||||
|
||||
Globals.MidiOutDevice.SilenceAllNotes();
|
||||
}
|
||||
|
||||
string cmd = null;
|
||||
Globals.Config.GetActiveMidiInCommands(Globals.ShiftKeyHeld).TryGetValue(key, out cmd);
|
||||
|
||||
if (cmd == null)
|
||||
{
|
||||
Console.WriteLine($"Unbound midi event: {key} (shift: {Globals.ShiftKeyHeld})");
|
||||
return;
|
||||
}
|
||||
|
||||
Globals.SendCommand($"0:(command){cmd}");
|
||||
}
|
||||
|
||||
private static void MidiDevice_ControlChange(Midi.Messages.ControlChangeMessage msg)
|
||||
{
|
||||
string key = $"CONTROL_{(int)(msg.Control)}";
|
||||
string cmd = null;
|
||||
|
||||
float normalizedValue = msg.Value / 128.0f * 100f;
|
||||
|
||||
Globals.Config.GetActiveMidiInCommands(Globals.ShiftKeyHeld).TryGetValue(key, out cmd);
|
||||
|
||||
if (cmd == null)
|
||||
{
|
||||
Console.WriteLine($"Unbound midi event: {key} (value: {normalizedValue.ToString(CultureInfo.InvariantCulture)}) (shift: {Globals.ShiftKeyHeld})");
|
||||
return;
|
||||
}
|
||||
|
||||
cmd = cmd.Replace("%val%", normalizedValue.ToString(CultureInfo.InvariantCulture) + "%");
|
||||
Globals.SendCommand($"0:(command){cmd}");
|
||||
}
|
||||
|
||||
public static void Work()
|
||||
{
|
||||
Globals.MidiInDevice.StartReceiving(null);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
|
||||
foreach(var cmd in Globals.Config.GetActiveMidiOutShiftCommandsSysEx(Globals.ShiftKeyHeld))
|
||||
{
|
||||
try
|
||||
{
|
||||
string result = Globals.SendCommand($"1:(string){cmd}");
|
||||
result = result.Substring(2); //skip 0:
|
||||
//result = result.Substring(1, result.Length - 3); //skip [ ... ]
|
||||
|
||||
string[] values = result.Split(',');
|
||||
byte[] sysExBytes = new byte[values.Length];
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (values[i].StartsWith("0x"))
|
||||
sysExBytes[i] = byte.Parse(values[i].Substring(2), NumberStyles.HexNumber);
|
||||
else
|
||||
sysExBytes[i] = byte.Parse(values[i], NumberStyles.Integer);
|
||||
}
|
||||
|
||||
Globals.MidiOutDevice.SendSysEx(sysExBytes);
|
||||
}
|
||||
catch (DeviceException ex)
|
||||
{
|
||||
Console.WriteLine("Device lost, you're on your own now!");
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to read sysex midi out command!\nCommand: {cmd}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user