예제 #1
0
        //add device
        static void addDevice(uint port)
        {
            //create reference to RtMidi device
            IntPtr reference = MidiInternal.rtmidi_in_create_default();

            //get port count
            //not using GetPortCount to avoid creating another RtMididevice
            uint count = MidiInternal.rtmidi_get_port_count(reference);

            //check if port number is invalid
            if (port >= count)
            {
                //send error
                Debug.LogError(string.Format("Port Number {0} cannot be used for Midi Input!\nPort range 0-{1}", port,
                                             count - 1));
                //free reference
                freeHandle(reference);

                //quit
                return;
            }

            //get port name
            string name = MidiInternal.rtmidi_get_port_name(reference, port);

            //ignore types
            MidiInternal.rtmidi_in_ignore_types(reference, false, false, false);

            //add port to RtMidi device
            MidiInternal.rtmidi_open_port(reference, port, "LopeaMidi port: " + name);

            //create midi input handle
            MidiInDevice device = new MidiInDevice(port, reference, name);

            //add to array
            if (currdevices == null)
            {
                currdevices    = new MidiInDevice[1];
                currdevices[0] = device;
            }
            else
            {
                var newdevices = new MidiInDevice[currdevices.Length + 1];
                for (int i = 0; i < currdevices.Length; i++)
                {
                    newdevices[i] = currdevices[i];
                }
                newdevices[currdevices.Length] = device;
                currdevices = newdevices;
            }
        }
예제 #2
0
        //add device
        static MidiInDevice CreateDevice(uint port)
        {
            //create reference to RtMidi device
            IntPtr reference = MidiInternal.rtmidi_in_create_default();

            //get port name
            string name = MidiInternal.rtmidi_get_port_name(reference, port);

            //ignore types
            MidiInternal.rtmidi_in_ignore_types(reference, false, false, false);

            //add port to RtMidi device
            MidiInternal.rtmidi_open_port(reference, port, "LopeaMidi port: " + name);

            //create midi input handle
            return(new MidiInDevice(port, reference, name));
        }
예제 #3
0
        static void RestartDevices()
        {
            //cannot restart device if it hasn't started in the first place,
            if (!_initialized)
            {
                return;
            }

            var newPortCount = GetPortCount();

            //find the device removed/added
            for (uint i = 0; i < newPortCount; i++)
            {
                string name = MidiInternal.rtmidi_get_port_name(currdevices[0].ptr, i);
                for (int j = 0; j < currdevices.Length; j++)
                {
                    if (currdevices[j].name == name)
                    {
                        continue;
                    }
                    //once found, print message
                    else if (currdevices[j].name != name && j == currdevices.Length - 1)
                    {
                        if (portCount < newPortCount)
                        {
                            print("Midi Device Added: " + name);
                        }
                        else
                        {
                            print("Midi Device Removed: " + currdevices[j].name);
                        }
                    }
                }
            }
            //shutdown the system
            Shutdown();

            //reinitialize system
            Initialize();
        }
예제 #4
0
        /// <summary>
        /// Get the name of a vaild output device based on its device port number
        /// </summary>
        /// <param name="port">port number to get it's value from</param>
        /// <returns>string containing the name of the port, empty string if the port number is invalid.</returns>
        public static string GetPortName(uint port)
        {
            //if system has not been initialized,
            if (!_init)
            {
                //initialize system
                Initialize();

                //leave if the initialization failed,
                if (!_init)
                {
                    return(String.Empty);
                }
            }

            if (port > count)
            {
                return(string.Empty);
            }

            return(MidiInternal.rtmidi_get_port_name(OutPorts[0], port));
        }
예제 #5
0
 /// <summary>
 /// Gets the name of the port based on the port number given.
 /// </summary>
 /// <param name="port">port number to find the name of.</param>
 /// <returns></returns>
 public static string GetPortName(uint port)
 {
     //check if the value exists...
     if (port >= portCount)
     {
         //if not, print error and return nothing.
         Debug.LogError("MIDI port given does not exist when trying to find port name!");
         return(string.Empty);
     }
     //if the system has not been initialized...
     if (!_initialized)
     {
         //create a reference to the port
         IntPtr refPort = MidiInternal.rtmidi_in_create_default();
         //get name
         string name = MidiInternal.rtmidi_get_port_name(refPort, port);
         //free port
         freeHandle(refPort);
         return(name);
     }
     //system has already been initialized, get port name from device
     return(currdevices[port].name);
 }
예제 #6
0
        static void RestartDevices()
        {
            //cannot restart device if it hasn't started in the first place,
            if (!_initialized)
            {
                return;
            }

            var newPortCount = GetPortCount();

            if (newPortCount == 0)
            {
                for (uint i = 0; i < portCount; i++)
                {
                    print("Midi Device Removed: " + currdevices[i].name);
                }

                portCount = 0;

                Shutdown();
            }
            else
            {
                //find the device removed/added
                for (uint i = 0; i < newPortCount; i++)
                {
                    string name = MidiInternal.rtmidi_get_port_name(currdevices[0].ptr, i);
                    for (int j = 0; j < currdevices.Length; j++)
                    {
                        if (currdevices[j].name == name)
                        {
                            continue;
                        }
                        //once found, print message
                        if (currdevices[j].name != name && j == currdevices.Length - 1)
                        {
                            if (portCount < newPortCount)
                            {
                                print("Midi Device Added: " + name);
                            }
                            else
                            {
                                print("Midi Device Removed: " + currdevices[j].name);
                            }
                        }
                    }
                }

                portCount = newPortCount;

                for (uint i = 0; i < currdevices.Length; i++)
                {
                    removeDevice(i);
                }

                currdevices = null;
                currdevices = new MidiInDevice[portCount];
                for (uint i = 0; i < portCount; i++)
                {
                    currdevices[i] = CreateDevice(i);
                }
            }
        }