Working with MIDI in Windows Store app (Win 8.1)
My goal is to receive MIDI messages in Windows Store apps. Microsoft has provided an API called Microsoft.WindowsPreview.MidiRT
(as a nuget package).
I was able to get the MIDI port, but no event MessageReceived
was raised even though I press keys on my MIDI keyboard and other MIDI programs show that the computer is receiving 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?
source to share
I managed to get it to work. I changed platfrom to x64 and now it works (I used it for x86). There is also a problem (and it is even bigger): I want to integrate this with Unity3d, but Unity3d does not allow creating x64 applications for Windows, on the other hand, x86 MIDI assembly does not work on x64 machines.
Added:
Even though this API depends on your architecture, the new Windows 10 api reportedly doesn't do this, so it should be easier if you're targeting Win10.
source to share
Possibly related: Your device's observer code does not match the normal pattern. Here's what you need to do:
DeviceWatcher midiWatcher;
void MonitorMidiChanges()
{
if (midiWatcher != null)
return;
var selector = MidiInPort.GetDeviceSelector();
midiWatcher = DeviceInformation.CreateWatcher(selector);
midiWatcher.Added += (s, a) => Debug.WriteLine("Midi Port named '{0}' with Id {1} was added", a.Name, a.Id);
midiWatcher.Updated += (s, a) => Debug.WriteLine("Midi Port with Id {1} was updated", a.Id);
midiWatcher.Removed += (s, a) => Debug.WriteLine("Midi Port with Id {1} was removed", a.Id);
midiWatcher.EnumerationCompleted += (s, a) => Debug.WriteLine("Initial enumeration complete; watching for changes...");
midiWatcher.Start();
}
source to share