private static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj) { if (obj == PlayModeStateChange.ExitingEditMode && _initialized) { OnNextNote?.Invoke(new MidiID(-1, -1, -1, MidiStatus.Dummy)); Shutdown(); } }
private static void Update() { if (!_initialized) { return; } if (EditorApplication.isPlaying || EditorApplication.timeSinceStartup - startTime > 10 || devices.Length != MidiInput.GetPortCount()) { OnNextNote?.Invoke(new MidiID(-1, -1, -1, MidiStatus.Dummy)); Shutdown(); return; } IntPtr messages = Marshal.AllocHGlobal(1024); IntPtr size = Marshal.AllocHGlobal(4); for (int i = 0; i < devices.Length; i++) { while (true) { Marshal.WriteInt32(size, 1024); MidiInternal.rtmidi_in_get_message(devices[i], messages, size); if (Marshal.ReadInt32(size) == 0) { break; } byte[] m = new byte[Marshal.ReadInt32(size)]; Marshal.Copy(messages, m, 0, m.Length); var status = (MidiStatus)((m[0] >> 4)); if (status == MidiStatus.NoteOff) { status = MidiStatus.NoteOn; } OnNextNote?.Invoke(new MidiID(m[0] & 0x0F, m[1], i, status)); Shutdown(); Marshal.FreeHGlobal(size); Marshal.FreeHGlobal(messages); return; } } //deallocate pointers Marshal.FreeHGlobal(size); Marshal.FreeHGlobal(messages); }