Ejemplo n.º 1
0
        // Start the port and setup buffers to receives Sysex
        public string Start()
        {
            // Allocate buffer array
            if (buffers == null)
            {
                const int numBuffers = 4;
                const int bufferSize = 4096; // ZZZ Param?

                buffers = new MidiBuffer[numBuffers];

                // Allocate and prepare the buffers
                for (int b = 0; b < buffers.Length; b++)
                {
                    // Create buffer
                    buffers[b] = new MidiBuffer(bufferSize, (IntPtr)b);

                    // Prepare the buffer and give it to windows
                    EMMError st = buffers[b].AddMidiInBuffer(handle);
                    if (st != EMMError.NOERROR)
                    {
                        return(WindowsUtil.StrError(st));
                    }
                }
            }

            // Finally, start the port
            return(WindowsUtil.StrError(NativeMethods.midiInStart(handle)));
        }
Ejemplo n.º 2
0
        // Close the port
        public string Close()
        {
            EMMError result = NativeMethods.midiOutClose(handle);

            handle = IntPtr.Zero;
            return(WindowsUtil.StrError(result));
        }
Ejemplo n.º 3
0
        // Close the port
        public string Close()
        {
            Stop();
            Reset();
            EMMError result = NativeMethods.midiInClose(handle);

            handle            = IntPtr.Zero;
            MidiInputReceived = null;

            return(WindowsUtil.StrError(result));
        }
Ejemplo n.º 4
0
        // Open the input port.
        public string Open()
        {
            // Open the port
            var st = NativeMethods.midiOutOpen(
                out handle,
                Id,
                null,
                IntPtr.Zero,
                0);

            return(WindowsUtil.StrError(st));
        }
Ejemplo n.º 5
0
        // Reset the port and free the sysex buffers
        public string Reset()
        {
            EMMError st = NativeMethods.midiInReset(handle);

            if (st == EMMError.NOERROR && buffers != null)
            {
                // The buffers have now been released by the callback. Just free the array.
                buffers = null;
            }

            return(WindowsUtil.StrError(st));
        }
Ejemplo n.º 6
0
        // Open the input port. Midi events cause the MidiInputReceived event to be raised
        public string Open()
        {
            // Open the port
            EMMError st = NativeMethods.midiInOpen(
                out handle,
                id,
                midiInProc,
                IntPtr.Zero,
                NativeMethods.CALLBACK_FUNCTION);

            return(WindowsUtil.StrError(st));
        }
Ejemplo n.º 7
0
        // Send a message
        public string Send(MidiMessage m)
        {
            var st = EMMError.NOERROR;

            if (m is MidiSysexMessage)
            {
                // Sysex messages are sent using a buffer.
                var sysex  = m as MidiSysexMessage;
                var buffer = new MidiBuffer(sysex.Body);
                using (buffer)
                {
                    // Prepare the buffer
                    st = buffer.PrepareForMidiOut(handle);
                    if (st == EMMError.NOERROR)
                    {
                        // Send the sysex
                        st = NativeMethods.midiOutLongMsg(handle, buffer.Header, buffer.HeaderSize);
                    }
                    if (st == EMMError.NOERROR)
                    {
                        // Wait until it is sent
                        // Note: this is simple but weak... If performance becomes necessary
                        // we can declare a callback, not wait here (just queue the buffer) and
                        // unprepare it when we receive the appropriate message in the callback
                        while (!buffer.IsFree())
                        {
                            Thread.Sleep(1);
                        }

                        // unprepare the buffer
                        st = buffer.UnprepareForMidiOut(handle);
                    }
                }
            }
            else
            {
                uint sm = m.GetAsShortMessage();
                if (sm != uint.MaxValue)
                {
                    st = NativeMethods.midiOutShortMsg(handle, sm);
                }
            }

            return(WindowsUtil.StrError(st));
        }
Ejemplo n.º 8
0
 // Reset the port
 public string Reset()
 {
     return(WindowsUtil.StrError(NativeMethods.midiOutReset(handle)));
 }
Ejemplo n.º 9
0
 // Stop the port
 public string Stop()
 {
     return(WindowsUtil.StrError(NativeMethods.midiInStop(handle)));
 }