Search code examples
c#audiowindows-store-appsmidi

Working with MIDI in Windows Store App (Win 8.1)


My goal is to receive MIDI messages in Windows Store Apps. Microsoft has delivered an API called Microsoft.WindowsPreview.MidiRT (as a nuget package).

I managed to get a midi port, but MessageReceived event is not arised, although I'm pressing keys on my MIDI keyboard, and other MIDI programs show me that PC receives these messages.

Here is my code:

public sealed partial class MainPage : Page
{
    private MidiInPort port;

    public MainPage()
    {
        this.InitializeComponent();
        DeviceWatcher watcher = DeviceInformation.CreateWatcher();
        watcher.Updated += watcher_Updated;
        watcher.Start();
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        port.Dispose();
    }

    async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
    {
        DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(MidiInPort.GetDeviceSelector());
        foreach (var item in deviceCollection)
        {
            Debug.WriteLine(item.Name);
            if (port == null)
            {
                port = await MidiInPort.FromIdAsync(item.Id);
                port.MessageReceived += port_MessageReceived;
            }
        }
    }

    void port_MessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args)
    {
        Debug.WriteLine(args.Message.Type);
    }
}

Any ideas?


Solution

  • I managed to make it work. I've changed the platfrom to x64, and now it works (I used to build it for x86). There is still a problem though (and it is even bigger): I want to integrate this with Unity3d, but Unity3d doesn't allow to build x64 windows apps, on the other hand x86 MIDI build doesn't work on x64 machines.

    Added:

    Although this API depends on your architecture, a new Windows 10 api reportedly does not, so it should be simpler, if you target Win10.