-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapExpression_Form.cs
151 lines (122 loc) · 4.87 KB
/
LeapExpression_Form.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
using M;
using System.Linq;
namespace LeapExpression;
public partial class MainWindow : Form
{
ControllerLeap clMyLeap;
private FormHelper helper = new FormHelper();
public MainWindow()
{
InitializeComponent();
/// Leap Motion Controller Handle
clMyLeap = new ControllerLeap();
clMyLeap.MotionEvent += HandleMotionEvent;
// Leap Motion Program Constants
Program.leapMotionHandle = new LeapMotionHandle();
Program.lastLocation = new LeapMotionHandle.LastLocation();
// MIDI Devices Dropdown
// Inputs
String[] midiInputDevices = MidiDevice.Inputs.Where(x => x != null)
.Select(x => x.Name)
.ToArray();
MidiInputComboBox.Items.Clear();
MidiInputComboBox.Items.AddRange(midiInputDevices);
MidiInputComboBox.SelectedIndex = 0;
MidiInputComboBox.DropDown += new System.EventHandler(MidiInputsComboBox_DropDownOpen);
MidiInputComboBox.DropDownClosed += new System.EventHandler(MidiInputsComboBox_DropDownUpdate);
// Outputs
String[] midiOutputDevices = MidiDevice.Outputs.Where(x => x != null)
.Select(x => x.Name)
.ToArray();
MidiOutputComboBox.Items.Clear();
MidiOutputComboBox.Items.AddRange(midiOutputDevices);
MidiOutputComboBox.SelectedIndex = 0;
MidiOutputComboBox.DropDown += new System.EventHandler(MidiOutputsComboBox_DropDownOpen);
MidiOutputComboBox.DropDownClosed += new System.EventHandler(MidiOutputsComboBox_DropDownUpdate);
}
~MainWindow()
{
if (clMyLeap != null)
clMyLeap.MotionEvent -= HandleMotionEvent;
}
/// <summary>
/// Motion event handle for Leap Controller.
/// </summary>
private void HandleMotionEvent(object sender, Controller.MotionEventArgs e)
{
var mLeftRight = e.msState.fValue[(int)Controller.Motions.mLeftRight];
if (MidiInterop.isReflecting)
{
tbValue.Text = Program.leapMotionHandle.SendMotionCC(mLeftRight, e.msState.fValue[(int)Controller.Motions.mForwardBack], e.msState.fValue[(int)Controller.Motions.mDistance]).ToString();
}
if (MidiInterop.framesSinceNotePressed < MidiInterop.waitFrames) // If set amount of frames have not passed
{
Console.Out.WriteLine($"Note Pressed At: ${mLeftRight}");
Program.lastLocation.x = mLeftRight;
// Give time to adjust hand
MidiInterop.framesSinceNotePressed++;
Console.Out.WriteLine($"framesSinceNotePressed {MidiInterop.framesSinceNotePressed}");
if (MidiInterop.framesSinceNotePressed >= MidiInterop.waitFrames) { // After set amount of frames
Console.Out.WriteLine($"Last Location Set ${Program.lastLocation.x}");
}
}
}
/// <summary>
/// Update MIDI input dropdown menu on open.
/// </summary>
private void MidiInputsComboBox_DropDownOpen(object sender, System.EventArgs e)
{
String[] midiInputDevices = MidiDevice.Inputs.Where(x => x != null)
.Select(x => x.Name)
.ToArray();
MidiInputComboBox.Items.Clear();
MidiInputComboBox.Items.AddRange(midiInputDevices);
Console.Out.WriteLine("opened input dropdown: " + MidiInputComboBox.Text);
}
/// <summary>
/// Get MIDI input dropdown result.
/// </summary>
private void MidiInputsComboBox_DropDownUpdate(object sender, System.EventArgs e)
{
MidiInterop.isReflecting = false;
MidiInterop.pauseReflectingEvent.Set();
Dictionary<String, MidiInputDevice> inputDevices = helper.GetMidiDevices(MidiDevice.Inputs);
var midiInput = MidiInputComboBox.SelectedIndex.ToString();
var midiDevice = inputDevices[midiInput];
Console.Out.WriteLine($"selected input device: {midiInput} = {midiDevice.Name} | Index: {midiDevice.Index}");
MidiInterop.inputDeviceID = midiDevice.Index;
}
/// <summary>
/// Update MIDI input dropdown menu on open.
/// </summary>
private void MidiOutputsComboBox_DropDownOpen(object sender, System.EventArgs e)
{
String[] midiOutputDevices = MidiDevice.Outputs.Where(x => x != null)
.Select(x => x.Name)
.ToArray();
MidiOutputComboBox.Items.Clear();
MidiOutputComboBox.Items.AddRange(midiOutputDevices);
Console.Out.WriteLine("opened output dropdown: " + MidiOutputComboBox.Text);
}
/// <summary>
/// Get MIDI output dropdown result.
/// </summary>
private void MidiOutputsComboBox_DropDownUpdate(object sender, System.EventArgs e)
{
MidiInterop.isReflecting = false;
MidiInterop.pauseReflectingEvent.Set();
var midiOutput = MidiOutputComboBox.SelectedIndex.ToString();
Dictionary<String, MidiOutputDevice> outputDevices = helper.GetMidiDevices(MidiDevice.Outputs);
var midiDevice = outputDevices[midiOutput];
Console.Out.WriteLine($"selected output device: {midiOutput} = {midiDevice.Name} | Index: {midiDevice.Index}");
MidiInterop.outputDeviceID = midiDevice.Index;
}
/// <summary>
/// Retrieve MIDI input device.
/// </summary>
public String GetMidiInputDevice() => MidiInputComboBox.Text;
/// <summary>
/// Retrieve MIDI output device.
/// </summary>
public String GetMidiOutputDevice() => MidiOutputComboBox.Text;
}