Beispiel #1
0
 public void AddData(MidiData data)
 {
     //handle any special values
     if (isSpecial(data.status))
     {
         int index = 11 - (int)data.status;
         if (!activedata.ContainsKey(index))
         {
             activedata.Add(index, data);
         }
         else
         {
             activedata[index] = data;
         }
     }
     else
     {
         if (activedata.ContainsKey(data.data1))
         {
             activedata[data.data1] = data;
         }
         else
         {
             activedata.Add(data.data1, data);
         }
     }
 }
Beispiel #2
0
        public static void SendData(uint port, MidiData data)
        {
            //cannot do anything with a dummy
            if (data.status == MidiStatus.Dummy)
            {
                return;
            }

            SendRawData(port, data.rawData);
        }
Beispiel #3
0
        //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);
            }
        }