-
Notifications
You must be signed in to change notification settings - Fork 2
/
MidiNoteAction.cs
56 lines (51 loc) · 1.9 KB
/
MidiNoteAction.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
using System.Threading.Tasks;
using RtMidi.Core.Enums;
using RtMidi.Core.Messages;
using Serilog;
using StreamDeckLib;
using StreamDeckLib.Messages;
using StreamDeckMidiPlugin2.Models;
namespace StreamDeckMidiPlugin2
{
[ActionUuid(Uuid = "com.sorskoot.midi.action")]
public class MidiNoteAction : BaseMidiAction<MidiNoteModel>
{
public override Task OnKeyDown(StreamDeckEventPayload args)
{
Log.Information("OnKeyDown");
this.MidiOn(this.SettingsModel.Channel, this.SettingsModel.Note);
return Task.CompletedTask;
}
public override Task OnKeyUp(StreamDeckEventPayload args)
{
this.MidiOff(this.SettingsModel.Channel, this.SettingsModel.Note);
return base.OnKeyUp(args);
}
public void MidiOn(int channel, int note)
{
var midiChannel = (Channel)(channel - 1);
var midiNote = (Key)note;
if (MidiDevice.OutputDevices[this.GetSelectedDeviceName(this.SettingsModel.SelectedDevice)].IsOpen)
{
MidiDevice.OutputDevices[this.GetSelectedDeviceName(this.SettingsModel.SelectedDevice)].Send(new NoteOnMessage(midiChannel, midiNote, 127));
}
else
{
Log.Warning("midi output device not connected");
}
}
public void MidiOff(int channel, int note)
{
var midiChannel = (Channel)(channel - 1);
var midiNote = (Key)note;
if (MidiDevice.OutputDevices[this.GetSelectedDeviceName(this.SettingsModel.SelectedDevice)].IsOpen)
{
MidiDevice.OutputDevices[this.GetSelectedDeviceName(this.SettingsModel.SelectedDevice)].Send(new NoteOffMessage(midiChannel, midiNote, 127));
}
else
{
Log.Warning("midi output device not connected");
}
}
}
}