识别正在按下的Midi键

时间:2018-07-20 15:55:49

标签: c# .net midi

试图从Midi设备获取输入,但无法确定哪个键使信号下降。只有信号了。 有人,请帮助我确定按下Midi键时正在按下哪个Midi键

-

这是两个不同按键的示例。

黄色:按键速度

红色:按键压力

绿色:结束信号和按键编号

launchpad pro key press 1 launchpad pro key press 2

代码以查看midi信号

                midi.Open(chanel);
                midi.Start();

                int old = 0;

                while (runViewer)
                {
                    if (Console.KeyAvailable)
                    {
                        runViewer = false;
                    }
                    if (old != midi.p[0])
                    {
                        old = midi.p[0];
                        Console.WriteLine(old + " \t" + midi.p[1] + " \t" + midi.p[2] + " \t");
                    }
                }
                midi.Stop();

midi handeler

public class InputPort
{
    private NativeMethods.MidiInProc midiInProc;
    private IntPtr handle;

    public int[] p = new int[3];

    public InputPort()
    {
        midiInProc = new NativeMethods.MidiInProc(MidiProc);
        handle = IntPtr.Zero;
    }

    public static int InputCount
    {
        get { return NativeMethods.midiInGetNumDevs(); }
    }

    public bool Close()
    {
        bool result = NativeMethods.midiInClose(handle)
            == NativeMethods.MMSYSERR_NOERROR;
        handle = IntPtr.Zero;
        return result;
    }

    public bool Open(int id)
    {
        return NativeMethods.midiInOpen(
            out handle,
            id,
            midiInProc,
            IntPtr.Zero,
            NativeMethods.CALLBACK_FUNCTION)
                == NativeMethods.MMSYSERR_NOERROR;
    }

    public bool Start()
    {
        return NativeMethods.midiInStart(handle)
            == NativeMethods.MMSYSERR_NOERROR;
    }

    public bool Stop()
    {
        return NativeMethods.midiInStop(handle)
            == NativeMethods.MMSYSERR_NOERROR;
    }

    private void MidiProc(
        IntPtr hMidiIn,
        int wMsg,
        IntPtr dwInstance,
        int dwParam1,
        int dwParam2)
    {
        //Console.WriteLine("- "+ wMsg + " \t" + dwParam1 + " \t" + dwParam2);

        p = new int[] { dwParam1,dwParam2, (int)wMsg };

        // Receive messages here
    }
}

internal static class NativeMethods
{
    internal const int MMSYSERR_NOERROR = 0;
    internal const int CALLBACK_FUNCTION = 0x00030000;

    internal delegate void MidiInProc(
        IntPtr hMidiIn,
        int wMsg,
        IntPtr dwInstance,
        int dwParam1,
        int dwParam2);

    [DllImport("winmm.dll")]
    internal static extern int midiInGetNumDevs();

    [DllImport("winmm.dll")]
    internal static extern int midiInClose(
        IntPtr hMidiIn);

    [DllImport("winmm.dll")]
    internal static extern int midiInOpen(
        out IntPtr lphMidiIn,
        int uDeviceID,
        MidiInProc dwCallback,
        IntPtr dwCallbackInstance,
        int dwFlags);

    [DllImport("winmm.dll")]
    internal static extern int midiInStart(
        IntPtr hMidiIn);

    [DllImport("winmm.dll")]
    internal static extern int midiInStop(
        IntPtr hMidiIn);
}

0 个答案:

没有答案