melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices
https://melanchall.github.io/drywetmidi
MIT License
545 stars 75 forks source link

Incoming events - how to get sustain pedal data? #47

Closed Yorgg closed 5 years ago

Yorgg commented 5 years ago

I am listening to the InputDevice and want to get the noteName and velocity from NoteOn, NoteOff events, and also the sustain pedal event (on piano).

I know the EventType, but don't know what to do next:

        if (e.EventType == DryWetMidi.Smf.MidiEventType.NoteOn)
        {
            // -> NoteName ???
            // -> velocity ???
        }

thank you

Yorgg commented 5 years ago

I just realized I can do a cast: (I'm new to c#)

 if (e.EventType == Melanchall.DryWetMidi.Smf.MidiEventType.NoteOn)
        {
            var note = e as NoteOnEvent;

            Debug.Log(note.Velocity);
            Debug.Log($"{note.GetNoteName()}{note.GetNoteOctave()}");
        }

I have one question remaining: how to get the sustain pedal data?

melanchall commented 5 years ago

Hi,

According to MIDI standard, sustain pedal on/off is a control change event. So you can use this code:

var controlChangeEvent = e as ControlChangeEvent;
if (controlChangeEvent != null && ControlUtilities.GetControlName(controlChangeEvent) == ControlName.DamperPedal)
{
    var controlValue = controlChangeEvent.ControlValue;
    var sustainOn = controlValue < 64;
}

Values from 0 to 63 correspond to sustain is turned on.

Yorgg commented 5 years ago

Great thanks.