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); }
// Update is called once per frame void Update() { list = new List <byte>(); for (int i = 0; i < header.Length; i++) { list.Add(header[i]); } list.Add(15); list.Add(0); for (int i = 0; i < circles.Count; i++) { if (circles[i].timer >= 100) { circles.RemoveAt(i); } var data = circles[i]; data.timer += Time.deltaTime * 30; circles[i] = data; } for (int i = 0, y = 0; y < 10; y++) { for (int x = 0; x < 10; x++, i++) { //reset color colors[i] = Color.black; for (int j = 0; j < circles.Count; j++) { float dist = Vector2.Distance(new Vector2(x, y), circles[j].center); Color col = gradient.Evaluate(Mathf.Sin(dist + Time.time * 30) / 2 + 0.5f); if (Mathf.Abs(dist - circles[j].timer) < 1) { colors[i] = col; } } //Color col = gradient.Evaluate(Mathf.Sin((x + y + Time.time) * Mathf.PI)); // checkerboard pattern list.Add((byte)(colors[i].r * 62)); list.Add((byte)(colors[i].g * 62)); list.Add((byte)(colors[i].b * 62)); } } list.Add(247); MidiInternal.rtmidi_out_send_message(o, list.ToArray(), list.Count); //store pointers IntPtr message = IntPtr.Zero; IntPtr size = IntPtr.Zero; //allocate pointers size = Marshal.AllocHGlobal(4); message = Marshal.AllocHGlobal(1024); // useful code right here while (true) { //get data from Midi device MidiInternal.rtmidi_in_get_message(ptr, message, size); //store size of message int s = Marshal.ReadInt32(size); //check if no message is sent if (s == 0) { //de-allocate and exit loop Marshal.FreeHGlobal(message); Marshal.FreeHGlobal(size); break; } //change center position byte[] data = new byte[s]; for (int i = 0; i < data.Length; i++) { data[i] = Marshal.ReadByte(message, i); } //change center changeCenter(data); } }
//On every frame, get current status of each midi device and store all its values. void Update() { //Loop based on Keijiro Takahashi's implementation. //https://github.com/keijiro/jp.keijiro.rtmidi/ if (_initialized) { //check if a device has been added/removed. if (portCount != GetPortCount()) { RestartDevices(); //if there are no devices available, quit if (!_initialized) { return; } } //allocate memory for messages IntPtr messages = Marshal.AllocHGlobal(1024); IntPtr size = Marshal.AllocHGlobal(4); //loop for every device active for (int i = 0; i < currdevices.Length; i++) { int currsize = 0; //loop indefinitely while (true) { //write max size for parameter (max size for a midi message is 1024 bytes) Marshal.WriteInt32(size, 1024); //get message and store timestamp double timestamp = MidiInternal.rtmidi_in_get_message(currdevices[i].ptr, messages, size); //parse size currsize = Marshal.ReadInt32(size); //if the message is empty, quit if (currsize == 0) { break; } //store messages in array byte[] m = new byte[currsize]; Marshal.Copy(messages, m, 0, currsize); // //parse message // //store new data MidiData data; //get status byte byte status = m[0]; //store data data = new MidiData((float)timestamp, //time since last midi message (MidiStatus)((status >> 4)), //midi type (8-15 defined) (status & 0x0F), //channel bit (0-15) m[1], //data1 byte (stores midi note ID usually) (currsize == 2) ? byte.MinValue : m[2], //data2 byte (sometimes this is 0 due to the length of the message) m, //raw data of the message i); //device port ID //some devices for whatever reason have both note on and off to be the same value //so note on/off status is based on velocity if (data.status == MidiStatus.NoteOn || data.status == MidiStatus.NoteOff) { data.status = (data.data2 != 0) ? MidiStatus.NoteOn : MidiStatus.NoteOff; } //add data to the device currdevices[i].AddData(data); } } //deallocate pointers Marshal.FreeHGlobal(size); Marshal.FreeHGlobal(messages); } }