MIDI Devices in WPF (I)

In this article I am going to discuss how to communicate with MIDI devices using C# and WPF framework. For the purposes of this article I use Windows.Devices.Midi (WinRT) API.

Using WinRT API in WPF app

WinRT MIDI API is designed for Windows Runtime apps, so it was introduced to be used in UWP applications. We can use it in WPF though. Assuming we are using Visual Studio 2022 and we have sample project already created, we just need to do one change in the project file. So, let’s right click on your project in Visual Studio and select Edit project file. In the project file, we need to specify the version of Windows, which we are targetting. I am using .NET 6, so normally the target framework of the WPF project should be set to “net6.0-windows”. To specify the version of Windows, we could change the target framework to e.g. “net6.0-windows10.0.19041.0”. The app would be targetting Windows 10, version 2004 then.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="DevExpress.Mvvm.CodeGenerators" Version="22.1.1" />
    <PackageReference Include="Prism.DryIoc" Version="8.1.97" />
  </ItemGroup>
</Project>

MIDI API

At the beginning we need to use two namespaces – Windows.Devices.Enumeration and Windows.Devices.Midi.

using Windows.Devices.Enumeration;
using Windows.Devices.Midi;

To find all input and output MIDI Devices we need to call FindAllAsync method of DeviceInformation object. As a parameter, we need to pass an query string. To get the query string, we need to use MidiInPort and MidiOutPort objects and call GetDeviceSelector method.

// Find all input MIDI devices
var midiInputQueryString = MidiInPort.GetDeviceSelector();
var midiInputDevices = await DeviceInformation.FindAllAsync(midiInputQueryString);

// Find all output MIDI devices
var midiOutportQueryString = MidiOutPort.GetDeviceSelector();
var midiOutputDevices = await DeviceInformation.FindAllAsync(midiOutportQueryString);

Now we got all MIDI devices connected to the computer. For all input devices, we need to get all the messages, which are coming from the devices. Firstly, we need to get the MidiInPort object using the Id of our input device and then we can simply subscribe to MessageReceived event.

var midiInPort = await MidiInPort.FromIdAsync(inputDev.Id);
midiInPort.MessageReceived += async (_, e) => { }

When the MessageReceived event is raised, we can become a few different types of MIDI message. Because I am using DJ controller for testing, it was sufficient for me to implement NoteOn, NoteOff and Control Change types. For more information about MIDI messages continue here.

midiInPort.MessageReceived += async (_, e) =>
{
    if (e.Message.Type == MidiMessageType.NoteOn)
    {
        var message = e.Message as MidiNoteOnMessage;
    }       
    else if (e.Message.Type == MidiMessageType.NoteOff)
    {
        var message = e.Message as MidiNoteOffMessage;
    } 
    else if (e.Message.Type == MidiMessageType.ControlChange)
    {
        var message = e.Message as MidiControlChangeMessage;
    }
};

I prepared a test app, where we can see and test how the API works. The test app consists of two listboxes (for displaying lists of input and output MIDI devices) and textbox, where MIDI messages are written to.

I used PRISM framework, MVVM Code Generators from DevExpress and MVVM design pattern in the app. You can find the project on my GitHub.

WPF Midi test app

Tip for productivity

In the test project I use MVVM Code Generators library from DevExpress. This library is automatically generating boilerplate code for your ViewModels using source generators and it is possible to use it with PRISM, DevExpress MVVM framework or MVVMLight. It is completely free to use, you can download it as NuGet package (you don’t have to own DevExpress WPF license).

Sources

4 thoughts on “MIDI Devices in WPF (I)”

  1. Very nice article, thank you sir! I will make this project too for my github! I have only one question – what is MIDI Device?

  2. Hello Sir,

    can I ask, when is the second part of the article coming? I am checking every month because I want to try finish the project for my resume.

    Kindly write the part 2

    1. Hello Rajesh,

      I am not planning to do it in the near future. I am currently writing the article about MQTT and Mosquitto Broker. I am also planning to write something about the UI Testing for WPF.

      Take a look to the sources of the article, especially the documentation from Microsoft (the first link), you can follow this article to implement the rest of the MIDI functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Accept