Beispiel #1
0
        /// <summary>
        /// Get array of available RtMidi API's (if any)
        /// </summary>
        /// <returns>The available apis.</returns>
        public static IEnumerable <RtMidiApi> GetAvailableApis()
        {
            var apisPtr = IntPtr.Zero;

            try
            {
                // Get number of API's
                var count = RtMidiC.GetCompiledApi(IntPtr.Zero, 0);
                if (count <= 0)
                {
                    return(new RtMidiApi[0]);
                }

                // Get array of available API's
                var enumSize = RtMidiC.Utility.SizeofRtMidiApi();
                apisPtr = Marshal.AllocHGlobal(count * enumSize);
                RtMidiC.GetCompiledApi(apisPtr, (uint)count);

                // Convert to managed enum types
                switch (enumSize)
                {
                case 1:
                    var bytes = new byte[count];
                    Marshal.Copy(apisPtr, bytes, 0, bytes.Length);
                    return(bytes.Select(b => (RtMidiApi)b));

                case 2:
                    var shorts = new short[count];
                    Marshal.Copy(apisPtr, shorts, 0, shorts.Length);
                    return(shorts.Select(s => (RtMidiApi)s));

                case 4:
                    var ints = new int[count];
                    Marshal.Copy(apisPtr, ints, 0, ints.Length);
                    return(ints.Select(i => (RtMidiApi)i));

                case 8:
                    var longs = new long[count];
                    Marshal.Copy(apisPtr, longs, 0, longs.Length);
                    return(longs.Select(l => (RtMidiApi)l));

                default:
                    Log.Error("Unexpected size {Size} of RtMidiApi enum", enumSize);
                    throw new NotSupportedException($"Unexpected size of RtMidiApi enum {enumSize}");
                }
            }
            catch (Exception e)
            {
                Log.Error(e, "Unexpected exception occurred while listing available RtMidi API's");

                return(new RtMidiApi[0]);
            }
            finally
            {
                if (apisPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(apisPtr);
                }
            }
        }
Beispiel #2
0
        public bool Open()
        {
            if (IsOpen)
            {
                return(false);
            }

            if (!EnsureDevice())
            {
                return(false);
            }

            try {
                Program.DebugLog($"Fetching port name, for port {_portNumber}");
                string portName = RtMidiC.GetPortName(Handle, _portNumber);
                CheckForError();

                Program.DebugLog($"Opening port {_portNumber} using name {portName}");
                RtMidiC.OpenPort(Handle, _portNumber, portName);
                CheckForError();

                IsOpen = true;

                return(true);
            } catch (Exception) {
                Program.Log($"Unable to open port number {_portNumber}");
                return(false);
            }
        }
        public bool Open()
        {
            if (_isOpen)
            {
                return(false);
            }

            if (!EnsureDevice())
            {
                Log.Debug("Could not create device handle, cannot open port {PortNumber}", _portNumber);
                return(false);
            }

            try
            {
                Log.Debug("Fetching port name, for port {PortNumber}", _portNumber);
                var portName = RtMidiC.GetPortName(_handle, _portNumber);
                CheckForError();

                Log.Debug("Opening port {PortNumber} using name {PortName}", _portNumber, portName);
                RtMidiC.OpenPort(_handle, _portNumber, portName);
                CheckForError();

                _isOpen = true;

                return(true);
            }
            catch (Exception e)
            {
                Log.Error(e, "Unable to open port number {PortNumber}", _portNumber);
                return(false);
            }
        }
Beispiel #4
0
        public static IEnumerable <RtMidiApi> GetAvailableApis()
        {
            IntPtr apisPtr = IntPtr.Zero;

            try {
                // Get number of APIs
                int count = RtMidiC.GetCompiledApi(IntPtr.Zero, 0);
                if (count <= 0)
                {
                    return(new RtMidiApi[0]);
                }

                // Get array of available APIs
                int enumSize = RtMidiC.Utility.SizeofRtMidiApi();
                apisPtr = Marshal.AllocHGlobal(count * enumSize);
                RtMidiC.GetCompiledApi(apisPtr, (uint)count);

                // Convert to managed Enum types
                switch (enumSize)
                {
                case 1:
                    byte[] bytes = new byte[count];
                    Marshal.Copy(apisPtr, bytes, 0, bytes.Length);
                    return(bytes.Cast <RtMidiApi>());

                case 2:
                    short[] shorts = new short[count];
                    Marshal.Copy(apisPtr, shorts, 0, shorts.Length);
                    return(shorts.Cast <RtMidiApi>());

                case 4:
                    int[] ints = new int[count];
                    Marshal.Copy(apisPtr, ints, 0, ints.Length);
                    return(ints.Cast <RtMidiApi>());

                case 8:
                    long[] longs = new long[count];
                    Marshal.Copy(apisPtr, longs, 0, longs.Length);
                    return(longs.Cast <RtMidiApi>());

                default:
                    throw new NotSupportedException($"Unexpected size of RtMidiApi enum {enumSize}");
                }
            } catch (Exception) {
                Console.Error.WriteLine("Unexpected exception occurred while listing available RtMidi APIs");
                return(new RtMidiApi[0]);
            } finally {
                if (apisPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(apisPtr);
                }
            }
        }
Beispiel #5
0
        internal string GetPortName(uint portNumber)
        {
            if (!EnsureDevice())
            {
                return(null);
            }

            try {
                string name = RtMidiC.GetPortName(Handle, portNumber);
                CheckForError();
                return(name);
            } catch (Exception) {
                Program.Log($"Error while getting port {portNumber} name");
                return(null);
            }
        }
Beispiel #6
0
        internal uint GetPortCount()
        {
            if (!EnsureDevice())
            {
                return(0);
            }

            try {
                uint count = RtMidiC.GetPortCount(Handle);
                CheckForError();
                return(count);
            } catch (Exception) {
                Program.Log("Error while getting number of ports");
                return(0);
            }
        }
Beispiel #7
0
        public void Close()
        {
            if (!IsOpen)
            {
                return;
            }

            try {
                Program.DebugLog($"Closing port number {_portNumber}");
                RtMidiC.ClosePort(Handle);
                CheckForError();

                IsOpen = false;
            } catch (Exception) {
                Program.Log($"Unable to close port number {_portNumber}");
            }
        }
        /// <summary>
        /// Get number of available ports for this device type
        /// </summary>
        /// <returns>Number of ports</returns>
        internal uint GetPortCount()
        {
            if (!EnsureDevice())
            {
                return(0);
            }

            try
            {
                var count = RtMidiC.GetPortCount(_handle);
                CheckForError();
                return(count);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while getting number of ports");
                return(0);
            }
        }
        /// <summary>
        /// Get name of port, for this device type
        /// </summary>
        /// <returns>The port name.</returns>
        /// <param name="portNumber">Port number.</param>
        internal string GetPortName(uint portNumber)
        {
            if (!EnsureDevice())
            {
                return(null);
            }

            try
            {
                var name = RtMidiC.GetPortName(_handle, portNumber);
                CheckForError();
                return(name);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while getting port {PortNumber} name", portNumber);
                return(null);
            }
        }
        public void Close()
        {
            if (!_isOpen)
            {
                return;
            }

            try
            {
                Log.Debug("Closing port number {PortNumber}", _portNumber);
                RtMidiC.ClosePort(_handle);
                CheckForError();

                _isOpen = false;
            }
            catch (Exception e)
            {
                Log.Error(e, "Unable to close port number {PortNumber}", _portNumber);
            }
        }