Getting signals from a MIDI port in C#

后端 未结 6 1079
灰色年华
灰色年华 2020-12-10 06:52

I bought a MIDI keyboard for my birthday. I found a program (MidiPiano) that gets signals from the MIDI input and translates it into music, but I\'d rather like to write one

6条回答
  •  感动是毒
    2020-12-10 07:36

    With DryWetMIDI you can get events from a MIDI port with this code:

    namespace InputDeviceExample
    {
        class Program
        {
            private static IInputDevice _inputDevice;
    
            static void Main(string[] args)
            {
                _inputDevice = InputDevice.GetByName("Some MIDI device");
                _inputDevice.EventReceived += OnEventReceived;
                _inputDevice.StartEventsListening();
    
                Console.WriteLine("Input device is listening for events. Press any key to exit...");
                Console.ReadKey();
    
                (_inputDevice as IDisposable)?.Dispose();
            }
    
            private static void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
            {
                var midiDevice = (MidiDevice)sender;
                Console.WriteLine($"Event received from '{midiDevice.Name}' at {DateTime.Now}: {e.Event}");
            }
        }
    }
    

    You can find more info in the Devices section of the library docs.

提交回复
热议问题