{"id":3576,"date":"2021-01-28T10:28:32","date_gmt":"2021-01-28T10:28:32","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3576"},"modified":"2021-01-28T10:28:35","modified_gmt":"2021-01-28T10:28:35","slug":"using-midi-with-naudio","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/01\/28\/using-midi-with-naudio\/","title":{"rendered":"Using MIDI with NAudio"},"content":{"rendered":"\n<p>This is a quick and small program in C# to get started with MIDI programming using NAudio. There are other libraries for .NET which can be used for MIDI programming but NAudio has the reputation of being a very stable and updated library, so I have gone along with it.<\/p>\n\n\n\n<p>This program only does two things:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enumerate MIDI devices<\/li><li>Track MIDI IN messages from a MIDI keyboard. <\/li><\/ul>\n\n\n\n<p>Given below is the output when no MIDI keyboard is attached. The Microsoft GS WaveTable Synth is a default MIDI output device which comes as part of Windows 10. We are looking for an MIDI input device which can be queried for MIDI messages. In this case , there is none:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"263\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen1.png\" alt=\"\" class=\"wp-image-3578\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen1.png 760w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen1-620x215.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen1-300x104.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p>Next we attach a MIDI keyboard and we assume that the first device found in the list of MIDI IN devices is our target device. Here I have attached an Impact ix61  MIDI Controller keyboard and it shows its MIDI device names in the list.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"274\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen2.png\" alt=\"\" class=\"wp-image-3580\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen2.png 710w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen2-620x239.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen2-300x116.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p>Now we create a MIDI Input device object in NAudio and start tracking its events. As each event is received, it is dumped on the Console. Here I am pressing keys on the MIDI keyboard.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"518\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen3.png\" alt=\"\" class=\"wp-image-3581\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen3.png 718w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen3-620x447.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/01\/screen3-300x216.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<p>The source code is given below. Create a Windows .NET Console project and use Nuget to add a reference to NAudio.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing NAudio.Midi;\n\nnamespace MidiTest\n{\n    class Program\n    {\n        static int mInDeviceIndex = -1;\n        static int mOutDeviceIndex = -1;\n        static MidiIn mMidiIn = null;\n        static Boolean mReady = false;\n\n\n        static void Main(string&#x5B;] args)\n        {\n            listDevices();\n            if (mReady)\n            {\n                handleMidiMessages();\n                Console.ReadLine();\n\n                mMidiIn.Stop();\n                mMidiIn.Dispose();\n            }\n        }\n\n        public static void listDevices()\n        {\n            Console.WriteLine(&quot;MIDI In Devices&quot;);\n            Console.WriteLine(&quot;===============&quot;);\n            for (int device = 0; device &lt; MidiIn.NumberOfDevices; device++)\n            {\n                mReady = true; \/\/  some midi in device exists\n                Console.WriteLine(MidiIn.DeviceInfo(device).ProductName);\n            }\n            mInDeviceIndex = 0;\n            Console.WriteLine(&quot;\\n\\n\\nMIDI Out Devices&quot;);\n            Console.WriteLine(&quot;=====================&quot;);\n            for (int device = 0; device &lt; MidiOut.NumberOfDevices; device++)\n            {\n                Console.WriteLine(MidiOut.DeviceInfo(device).ProductName);\n            }\n            mOutDeviceIndex = 1;\n        }\n\n        public static void handleMidiMessages()\n        {\n            mMidiIn = new MidiIn(mInDeviceIndex);\n            mMidiIn.MessageReceived += midiIn_MessageReceived;\n            mMidiIn.ErrorReceived += midiIn_ErrorReceived;\n            mMidiIn.Start();\n\n\n        }\n\n        static void midiIn_ErrorReceived(object sender, MidiInMessageEventArgs e)\n        {\n            Console.WriteLine(String.Format(&quot;Time {0} Message 0x{1:X8} Event {2}&quot;,\n                e.Timestamp, e.RawMessage, e.MidiEvent));\n        }\n\n        static void midiIn_MessageReceived(object sender, MidiInMessageEventArgs e)\n        {\n            Console.WriteLine(String.Format(&quot;Time {0} Message 0x{1:X8} Event {2}&quot;,\n                e.Timestamp, e.RawMessage, e.MidiEvent));\n        }\n    }\n}\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is a quick and small program in C# to get started with MIDI programming using NAudio. There are other libraries for .NET which can <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/01\/28\/using-midi-with-naudio\/\" title=\"Using MIDI with NAudio\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3585,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[362,363,15],"tags":[],"class_list":["post-3576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-midi-programming","category-windows"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3576"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3576\/revisions"}],"predecessor-version":[{"id":3584,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3576\/revisions\/3584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3585"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}