-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMidiNote.cs
163 lines (138 loc) · 5.68 KB
/
MidiNote.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using BarRaider.SdTools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using RtMidi.Core;
using RtMidi.Core.Devices;
using RtMidi.Core.Devices.Infos;
using RtMidi.Core.Enums;
using RtMidi.Core.Messages;
namespace MidiDevice
{
static class MidiDevice
{
public static IMidiOutputDevice outputDevice;
}
}
namespace streamdeck_totalmix
{
[PluginActionId("de.shells.totalmix.midinote.action")]
public class MidiNote : PluginBase
{
private class PluginSettings
{
public static PluginSettings CreateDefaultSettings()
{
string[] MidiDevicesPresent = MidiDeviceManager.Default.OutputDevices.Select(d => d.Name).ToArray();
PluginSettings instance = new PluginSettings
{
Channel = 1,
SelectedMidiAction = "1",
MidiNote = 54,
SelectedDevice = "0",
Devices = MidiDevicesPresent
};
return instance;
}
public static PluginSettings UpdateMidiDevices(int channel, string selectedmidiaction, int midinote, string selecteddevice)
{
string[] MidiDevicesPresent = MidiDeviceManager.Default.OutputDevices.Select(d => d.Name).ToArray();
PluginSettings instance = new PluginSettings
{
Channel = channel,
SelectedMidiAction = selectedmidiaction,
MidiNote = midinote,
SelectedDevice = selecteddevice,
Devices = MidiDevicesPresent
};
return instance;
}
[FilenameProperty]
[JsonProperty(PropertyName = "Channel")]
public int Channel { get; set; }
[JsonProperty(PropertyName = "SelectedMidiAction")]
public string SelectedMidiAction { get; set; }
[JsonProperty(PropertyName = "MidiNote")]
public int MidiNote { get; set; }
[JsonProperty(PropertyName = "SelectedDevice")]
public string SelectedDevice { get; set; }
[JsonProperty(PropertyName = "Devices")]
public string[] Devices { get; private set; }
}
#region Private Members
private PluginSettings settings;
#endregion
public MidiNote(SDConnection connection, InitialPayload payload) : base(connection, payload)
{
if (payload.Settings == null || payload.Settings.Count == 0)
{
this.settings = PluginSettings.CreateDefaultSettings();
Connection.SetSettingsAsync(JObject.FromObject(settings));
Logger.Instance.LogMessage(TracingLevel.INFO, $"OscToggle: Settings initially set: {this.settings}");
}
else
{
this.settings = payload.Settings.ToObject<PluginSettings>();
this.settings = PluginSettings.UpdateMidiDevices(this.settings.Channel, this.settings.SelectedMidiAction, this.settings.MidiNote, this.settings.SelectedDevice);
Connection.SetSettingsAsync(JObject.FromObject(settings));
}
}
public override void Dispose()
{
Logger.Instance.LogMessage(TracingLevel.INFO, "OscToggle: Destructor called");
}
public override void KeyPressed(KeyPayload payload)
{
this.settings = payload.Settings.ToObject<PluginSettings>();
Logger.Instance.LogMessage(TracingLevel.INFO, "OscToggle: Key Pressed");
IMidiOutputDeviceInfo outputDeviceInfo = MidiDeviceManager.Default.OutputDevices.ElementAt(Int32.Parse(this.settings.SelectedDevice));
MidiDevice.MidiDevice.outputDevice = outputDeviceInfo.CreateDevice();
if (!MidiDevice.MidiDevice.outputDevice.IsOpen)
{
MidiDevice.MidiDevice.outputDevice.Open();
}
this.MidiOn(this.settings.Channel, this.settings.MidiNote);
}
public override void KeyReleased(KeyPayload payload) {
this.MidiOff(this.settings.Channel, this.settings.MidiNote);
if (MidiDevice.MidiDevice.outputDevice != null)
{
MidiDevice.MidiDevice.outputDevice.Close();
}
}
public override void OnTick() { }
public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
Logger.Instance.LogMessage(TracingLevel.INFO, $"OscToggle: Payload Settings loaded: {payload.Settings}");
//Tools.AutoPopulateSettings(settings, payload.Settings);
Connection.SetSettingsAsync(JObject.FromObject(payload.Settings));
}
public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload) { }
#region Private Methods
private Task SaveSettings()
{
return Connection.SetSettingsAsync(JObject.FromObject(settings));
}
#endregion
public void MidiOn(int channel, int note)
{
var midiChannel = (Channel)(channel - 1);
var midiNote = (Key)note;
if (MidiDevice.MidiDevice.outputDevice.IsOpen)
{
MidiDevice.MidiDevice.outputDevice.Send(new NoteOnMessage(midiChannel, midiNote, 1));
}
}
public void MidiOff(int channel, int note)
{
var midiChannel = (Channel)(channel - 1);
var midiNote = (Key)note;
if (MidiDevice.MidiDevice.outputDevice.IsOpen)
{
MidiDevice.MidiDevice.outputDevice.Send(new NoteOffMessage(midiChannel, midiNote, 1));
}
}
}
}