Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use my Midi piano to interact with my Windows Form C# (DryWetMidi)

I've working on a little software in Visual Studio ( a Windows Form . NET in C# ). I Made a simply piano that changes its keys colors when I press some keyboard buttons (QWERTY)

enter image description here

Now I want to use my MIDI piano instead to do this event. In my actual code I've used a KeyDown(to show a colored image of the piano key over the original) and KeyUp(to hide it) to do this event, and I know that do that with a MIDI piano will be harder than this.

I've found the DryWetMidi API (by Melanchall) here (and I downloaded it using NuGet, and applied it into my proyect), that can do a lot of huge things with MIDI, but I just want to do these simples things:

  1. Recognize the MIDI devices that windows recognize.

  2. Let the user to choose his MIDI device in a "settings form"(that I already have in blank)

  3. Can play the MIDI piano, to activate the "changing color event"

  4. (don't need to recognize velocity)

  5. (don't need to play MIDI files)

  6. (don't need to sound, because I plan to use it at the same time with a Virtual instrument software)

The DryWetMidi documentation says "it's pretty easy to setup events receiving" using this:

using System;
using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Core;

// ...

using (var inputDevice = InputDevice.GetByName("MIDI Device"))
{
    inputDevice.EventReceived += OnEventReceived;
    inputDevice.StartEventsListening();
}

// ...

private void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
{
    MidiDevice midiDevice = (MidiDevice)sender;
    MidiEvent midiEvent = e.Event;
    Console.WriteLine($"Event received from '{midiDevice.Name}' at {DateTime.Now}: {midiEvent}");
}

...but it doesn't work for me (as you can see here): (yes, I already wrote those 3 lines in the beginning of my Form1.cs):

using System;

using Melanchall.DryWetMidi.Devices;

using Melanchall.DryWetMidi.Core;

These errors (which are underlined in red) do not allow me to continue coding my project, to reach the final goal of executing the key color changing event.

I'm principiant and I just write simple codes (as you can see), so probably I dont know if I have to put those codes on a new class document or form, to call them or another thing that could be obvious for you.

I want to clarify that I have already read all the documentation of the library that refers to the Device Connectivity, but I couldn't found the solution there.

I swear to you that I will really appreciate any help you can give me.

like image 599
iben Music Avatar asked Nov 04 '25 05:11

iben Music


1 Answers

I'm the author of DryWetMIDI.

You get these errors because you just copy/paste sample code. using block can be used within methods only.

If you task is "light up a key in your app on key press on MIDI device", your workflow should be:

  1. Populate the list of devices using InputDevice.GetAll
  2. When user selects a device from the list, save the selected device to varibale (for example, _inputDevice)
  3. Set event handler for EventReceived event of IInputDevice like in the snippet from the library docs you've shown, and don't forget to call StartEventsListening method
  4. When you receive NoteOnEvent, light up a key on your piano within your app
  5. When you receive NoteOffEvent, light out the key
  6. When user selects another device or app is closing, you must dispose _inputDevice with Dispose method; also remove event handler that you've set on step 3

Please read Input device article of the library docs.

like image 97
Maxim Avatar answered Nov 07 '25 14:11

Maxim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!