Skip to content

Instantly share code, notes, and snippets.

@kshoji
Last active August 14, 2021 02:32
Show Gist options
  • Save kshoji/c4b7ea9b2af0be7f21d148ed54d9564c to your computer and use it in GitHub Desktop.
Save kshoji/c4b7ea9b2af0be7f21d148ed54d9564c to your computer and use it in GitHub Desktop.
MIDI Plugin for Mobile devices examples
public void OnMidiInputDeviceAttached(string deviceId)
{
// will be called when a new Input device connected
// Input device can receive MIDI events
}
public void OnMidiOutputDeviceAttached(string deviceId)
{
// will be called when a new Output device connected
// Output device can send MIDI events
Debug.Log($"MIDI device attached. deviceId: {deviceId}, name: {MidiManager.Instance.GetDeviceName(deviceId)}");
}
public void OnMidiInputDeviceDetached(string deviceId)
{
// will be called when a Input device disconnected
}
public void OnMidiOutputDeviceDetached(string deviceId)
{
// will be called when a Output device disconnected
Debug.Log($"MIDI device detached. deviceId: {deviceId}, name: {MidiManager.Instance.GetDeviceName(deviceId)}");
}
private void Awake()
{
// Register an GameObject to receive MIDI events
MidiManager.Instance.RegisterEventHandleObject(gameObject);
// Initialize MidiManager
MidiManager.Instance.InitializeMidi(() =>
{
// Start to scan Bluetooth LE MIDI devices
MidiManager.Instance.StartScanBluetoothMidiDevices(0); // 0: scans infinite
});
}
public class MidiSampleScene : MonoBehaviour, IMidiNoteOnEventHandler, IMidiDeviceEventHandler
{
// Event Handler called on MIDI Note On event received
public void OnMidiNoteOn(string deviceId, int group, int channel, int note, int velocity)
{
Debug.Log($"OnMidiNoteOn channel: {channel}, note: {note}, velocity: {velocity}");
}
...
}
if (GUILayout.Button("NoteOn"))
{
// Sending MIDI Note On event when the button clicked
MidiManager.Instance.SendMidiNoteOn(deviceIds[deviceIdIndex], 0, channel, noteNumber, velocity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment