Receiving signals from a MIDI port in C #

I bought a MIDI keyboard for my birthday. I found a program (MidiPiano) that receives signals from a MIDI input and translates it into music, but I would rather write it myself.

Where can I find documentation to accomplish such a task? The MIDI protocol is well documented, but not MIDI ports.

I checked two projects from CodeProject (Project MIDI and C # MIDI Toolkit), but spent many hours without getting closer to my goal.

The link to the project will be fine, but please only C #.

+7
c # midi
source share
5 answers

You need to wrap all the necessary functions listed at http://msdn.microsoft.com/en-us/library/dd757277(VS.85).aspx

Not too complicated if you just use short messages, things get a little more complicated if you want to do SysEx or streaming output.

All you need for a basic input port is to get valid input identifiers (InputCount -1), pass a valid Open identifier, start an input, receive messages through a delegate ... Stop the input, and then finally close it. This is a very approximate example of how this can be achieved - you will need to do some checking and be careful that the port is not collected before it is closed, and a closed callback occurs or you lock your system!

Good luck

  namespace MIDI
 {
     public class InputPort
     {
         private NativeMethods.MidiInProc midiInProc;
         private IntPtr handle;

         public InputPort ()
         {
             midiInProc = new NativeMethods.MidiInProc (MidiProc);
             handle = IntPtr.Zero;
         }

         public static int InputCount
         {
             get {return NativeMethods.midiInGetNumDevs ();  }
         }

         public bool Close ()
         {
             bool result = NativeMethods.midiInClose (handle) 
                 == NativeMethods.MMSYSERR_NOERROR;
             handle = IntPtr.Zero;
             return result;
         }

         public bool Open (int id)
         {
             return NativeMethods.midiInOpen (
                 out handle,
                 id
                 midiInProc,
                 IntPtr.Zero,
                 NativeMethods.CALLBACK_FUNCTION)
                     == NativeMethods.MMSYSERR_NOERROR;
         }

         public bool Start ()
         {
             return NativeMethods.midiInStart (handle)
                 == NativeMethods.MMSYSERR_NOERROR;
         }

         public bool Stop ()
         {
             return NativeMethods.midiInStop (handle)
                 == NativeMethods.MMSYSERR_NOERROR;
         }

         private void MidiProc (IntPtr hMidiIn,
             int wMsg,
             IntPtr dwInstance,
             int dwParam1,
             int dwParam2)
         {
             // Receive messages here
         }
     }

     internal static class NativeMethods
     {
         internal const int MMSYSERR_NOERROR = 0;
         internal const int CALLBACK_FUNCTION = 0x00030000;

         internal delegate void MidiInProc (
             IntPtr hMidiIn,
             int wMsg,
             IntPtr dwInstance,
             int dwParam1,
             int dwParam2);

         [DllImport ("winmm.dll")]
         internal static extern int midiInGetNumDevs ();

         [DllImport ("winmm.dll")]
         internal static extern int midiInClose (
             IntPtr hMidiIn);

         [DllImport ("winmm.dll")]
         internal static extern int midiInOpen (
             out IntPtr lphMidiIn,
             int uDeviceID,
             MidiInProc dwCallback,
             IntPtr dwCallbackInstance,
             int dwFlags);

         [DllImport ("winmm.dll")]
         internal static extern int midiInStart (
             IntPtr hMidiIn);

         [DllImport ("winmm.dll")]
         internal static extern int midiInStop (
             IntPtr hMidiIn);
     }
 } 
+6
source share

I couldn't find much either, but as Josh says, Carl definitely did a great job on MIDI, so maybe he will get an email ... he might even reply, but he’s usually a VB.Net guy, if anyone ported your stuff to C #?

For some other links that looked promising ...

If you are looking for a clean C # solution ... check out Alvas.Audio . This looks a little high for what you are asking for, but may be of some help.

Midi project is also possible

+2
source share

I'm just developing a C # midi communication system. The Sanford midi toolkit is good, but Leslie's implementation has many improvements, which makes the code less readable. My code is as simple as possible. If you want, you can join me. A few days later I publish a fully working midi monitor. Just take a look at http://puremidi.codeplex.com/

+2
source share

Use the C # Midi Toolkit to get MIDI information from your keyboard (connected to the MIDI port of your sound card). To create audio (music or tones) you need to create a continuous stream of digital audio data. You can start with the C # Synth Toolkit (the same author as the midi toolkit) to write your own synthesizer.

But you can also download a free (open source) sequencer program (such as audacity) and use VST plugins to do the job. There are many free plugins available on the Internet.

+1
source share

I solved this in a workaround by writing a Python script to send a UDP packet every time an ON / OFF message appears on the MIDI keyboard.

MIDI over TCP / IP / UDP, which should be received inUnity3D with C # /. NET

This way the platform-specific stuff will be wrapped, and I can write the C # agnostic code code to collect the UDP packet.

0
source share

All Articles