### Unity C# Plugin Initialization Source: https://github.com/arthurvasseur/usb-midi-android-plugin/blob/main/README.md Illustrates the initial setup for the USB MIDI plugin in a Unity C# script. It requires adding the `MidiManager` component to a GameObject and then registering the current script as an event handler using `MidiManager.Instance.RegisterEventHandler(this)`. This is typically done in the `Awake` and `Start` methods. ```C# private void Awake() { gameObject.AddComponent(); } private void Start() { MidiManager.Instance.RegisterEventHandler(this); } ``` -------------------------------- ### Java Android Plugin Initialization Source: https://github.com/arthurvasseur/usb-midi-android-plugin/blob/main/README.md Shows how to initialize the USB MIDI plugin within an Android Activity. It involves creating an instance of the `MyCallback` class and calling `UsbMidiController.getInstance().ctor()` to register the callback and the Android context. This setup should typically occur in the Activity's `onCreate` method. ```Java public class MainActivity extends AppCompatActivity { private MyCallback _myCallback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); _myCallback = new MyCallback(findViewById(R.id.textViewMidiLog)); UsbMidiController.getInstance().ctor(_myCallback, this); } } ``` -------------------------------- ### Unity C# MIDI Event Handling Source: https://github.com/arthurvasseur/usb-midi-android-plugin/blob/main/README.md Provides the implementation for the `IMidiEventHandler` interface in C# for Unity applications. This includes methods for handling raw MIDI data, note on/off events, and device attachment/detachment. Received MIDI data is logged to the Unity console and appended to a UI Text element. ```C# [SerializeField] private Text text; // Called for all midi commands, to receive raw midi data, including before NoteOn and NoteOff public void RawMidi(sbyte command, sbyte data1, sbyte data2) { string output = string.Format("MIDI command: {0:x2} {1:x2} {2:x2}", command, data1, data2); Debug.Log(output); text.text += output + Environment.NewLine; } // Called when a midi note is pressed public void NoteOn(int note, int velocity) { Debug.Log("Note On " + note + " velocity " + velocity); text.text += "Note On " + note + " velocity " + velocity + Environment.NewLine; } // Called when a midi note is released public void NoteOff(int note) { Debug.Log("Note off " + note); text.text += "Note off " + note + Environment.NewLine; } // Called when you plug a midi device public void DeviceAttached(string deviceName) { Debug.Log("Device Attached " + deviceName); text.text += "Device Attached " + deviceName + Environment.NewLine; } // Called when you unplug a midi device public void DeviceDetached(string deviceName) { Debug.Log("Device Detached " + deviceName); text.text += "Device Detached " + deviceName + Environment.NewLine; } ``` -------------------------------- ### Java Android MIDI Callback Implementation Source: https://github.com/arthurvasseur/usb-midi-android-plugin/blob/main/README.md Demonstrates implementing the `IMidiCallback` interface in Java for Android. This class handles MIDI events like RawMidi, NoteOn, NoteOff, DeviceAttached, and DeviceDetached by appending messages to a TextView. It requires a TextView instance for logging. ```Java public class MyCallback implements IMidiCallback { TextView _textView = null; public MyCallback(TextView textView) { _textView = textView; } @Override public void RawMidi(byte command, byte data1, byte data2) { _textView.append(" MIDI command : " + command + ", " + data1 + ", " + data2 + System.lineSeparator()); } @Override public void NoteOn(int note, int velocity) { _textView.append(" Note : " + note + " velocity : " + velocity + System.lineSeparator()); } @Override public void NoteOff(int note) { _textView.append(" Note : " + note + System.lineSeparator()); } @Override public void DeviceAttached(String name) { _textView.append(" DeviceAttached Name : " + name + System.lineSeparator()); } @Override public void DeviceDetached(String name) { _textView.append(" DeviceDetached Name : " + name + System.lineSeparator()); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.