在C#中的MIDI端口获取信号 [英] Getting signals from a MIDI port in C#

查看:611
本文介绍了在C#中的MIDI端口获取信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我买了一个MIDI键盘作为生日礼物。我发现了一个程序(MidiPiano)从MIDI输入获得信号,并将其转换为音乐,但我宁愿喜欢写一个自己。

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 myself.

我在哪里可以找到文档做这样的任务呢?该MIDI协议是有据可查的,但不是MIDI端口。

Where can I find documentation for doing such a task? The MIDI protocol is well documented, but not the MIDI ports.

我查了一下两个项目从CodeProject(项目MIDI和C#MIDI工具包),但花了很多时间不深入我的目标。

I checked two projects from CodeProject (Project MIDI and C# MIDI Toolkit), but spent many hours without getting close to my goal.

到项目的引用将被罚款,但请,只有C#。

A reference to a project will be fine, but please, only C#.

推荐答案

您需要包装在所有的 http://msdn.microsoft.com/en-us/library/dd757277(VS.85)的.aspx

这不是太难了,如果你仅仅使用短消息,事情得到,如果你想要做的SysEx或输出流稍微复杂一点。

It's not too difficult if you're just using short messages, things get a little more complicated if you want to do SysEx or output streaming.

所有你需要的基本输入端口是获得有效输入标识(InputCount -1),传递一个有效的ID来打开,启动输入,通过一个委托接收消息...停止输入,然后终于关闭。这是如何这可能是acheived一个非常粗略的例子 - 你也许会需要做一些检查和小心,它的关闭之前的端口没有收集,密闭的回调已经发生,否则将冻结您的系统

All you need for a basic input Port is to get the valid input IDs (InputCount -1), pass a valid ID to Open, Start the input, receive the messages via a delegate... Stop the input and then finally close it. This is a very rough example of how this could be acheived - you will need to do some checking and be careful that the Port isn't collected before it's closed and the closed callback has happened or you will freeze your system!

祝你好运!

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

        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)
        {
            // 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);
    }
}

这篇关于在C#中的MIDI端口获取信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆