Repository navigation
Expand file tree
/
Copy pathMidiListener.cs
More file actions
118 lines (100 loc) · 3.3 KB
/
Copy pathMidiListener.cs
File metadata and controls
118 lines (100 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace MidiShortcuts;
/// <summary>
/// Decodifica note on/off y control change en claves "note_N" / "cc_N"
/// (sin distinguir canal) y dispara Trigger solo al presionar. Abre el
/// dispositivo por winmm o por WinRT (Bluetooth) segun donde aparezca.
/// </summary>
public sealed class MidiListener : IDisposable
{
private readonly MidiInputDevice _device = new();
private WinRtMidiInput? _winrt;
private readonly HashSet<string> _pressed = new();
private readonly object _sync = new();
private volatile bool _learning;
/// <summary>Se dispara con la clave (ej. "note_60") al presionar un pad/control.</summary>
public event Action<string>? Trigger;
/// <summary>Se dispara una sola vez al capturar un pad en modo "aprender".</summary>
public event Action<string>? Learned;
/// <summary>Se dispara con cualquier pulsacion, para mostrar "ultimo pad".</summary>
public event Action<string>? AnyMessage;
/// <summary>Se dispara si el dispositivo se cierra solo (ej. corte de Bluetooth).</summary>
public event Action? Disconnected;
public string? LastError { get; private set; }
public bool IsOpen => _winrt?.IsOpen ?? _device.IsOpen;
public MidiListener()
{
_device.MessageReceived += OnMessage;
_device.DeviceClosed += () => Disconnected?.Invoke();
}
public bool Start(string portName)
{
var endpoint = MidiEndpoints.Find(portName);
if (endpoint == null)
{
LastError = "El dispositivo MIDI ya no esta disponible.";
return false;
}
bool ok;
if (endpoint.WinRtId != null)
{
_winrt = new WinRtMidiInput();
_winrt.MessageReceived += OnMessage;
ok = _winrt.Open(endpoint.WinRtId);
}
else
{
ok = _device.Open(endpoint.WinMmIndex);
}
if (!ok) LastError = "No se pudo abrir el puerto MIDI (puede estar en uso por otra instancia).";
return ok;
}
public void StartLearning() => _learning = true;
private void OnMessage(byte status, byte data1, byte data2)
{
int type = status & 0xF0;
string? key = type switch
{
0x90 or 0x80 => $"note_{data1}",
0xB0 => $"cc_{data1}",
_ => null
};
if (key == null) return;
bool isPress = type switch
{
0x90 => data2 > 0,
0xB0 => data2 > 0,
_ => false
};
bool isRelease = type switch
{
0x90 => data2 == 0,
0x80 => true,
0xB0 => data2 == 0,
_ => false
};
if (isPress) AnyMessage?.Invoke(key);
if (_learning)
{
if (isPress)
{
_learning = false;
Logger.Log($"Pad capturado: {key}");
Learned?.Invoke(key);
}
return;
}
bool fire = false;
lock (_sync)
{
if (isPress) fire = _pressed.Add(key);
else if (isRelease) _pressed.Remove(key);
}
// Fuera del lock para no bloquear otros mensajes mientras corre la accion.
if (fire) Trigger?.Invoke(key);
}
public void Dispose()
{
_device.Dispose();
_winrt?.Dispose();
}
}