/
NoteCallback.cs
46 lines (41 loc) · 1.66 KB
/
NoteCallback.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
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Layouts;
// NoteCallback.cs - This script shows how to define a callback to get notified
// on MIDI note-on/off events.
sealed class NoteCallback : MonoBehaviour
{
void Start()
{
InputSystem.onDeviceChange += (device, change) =>
{
if (change != InputDeviceChange.Added) return;
var midiDevice = device as Minis.MidiDevice;
if (midiDevice == null) return;
midiDevice.onWillNoteOn += (note, velocity) => {
// Note that you can't use note.velocity because the state
// hasn't been updated yet (as this is "will" event). The note
// object is only useful to specify the target note (note
// number, channel number, device name, etc.) Use the velocity
// argument as an input note velocity.
Debug.Log(string.Format(
"Note On #{0} ({1}) vel:{2:0.00} ch:{3} dev:'{4}'",
note.noteNumber,
note.shortDisplayName,
velocity,
(note.device as Minis.MidiDevice)?.channel,
note.device.description.product
));
};
midiDevice.onWillNoteOff += (note) => {
Debug.Log(string.Format(
"Note Off #{0} ({1}) ch:{2} dev:'{3}'",
note.noteNumber,
note.shortDisplayName,
(note.device as Minis.MidiDevice)?.channel,
note.device.description.product
));
};
};
}
}