如何使用Midi网络类在C#中读取打击乐器音符值

如何使用Midi网络类在C#中读取打击乐器音符值,c#,.net,midi,C#,.net,Midi,我正在开发一个VB.net应用程序,它需要从外部鼓垫读取MIDI音符值,鼓垫在通道10上发送值。此值将用于控制我的应用程序的各个方面 我计划使用这里的MIDI.net类:读取打击乐器的音符值,并查看其中一个示例,该示例读取由外部键盘播放的音符并显示它们 不幸的是,我不能把它转换成我想要的东西,尽管我觉得我已经很接近了 是否有熟悉此库并能发现我的错误的用户?C#不是我的主要语言(事实上,VB.net也不是!),但如果我能在C#中运行一些东西,那么我可以将其转换为VB.net,以便在我的应用程序中使

我正在开发一个VB.net应用程序,它需要从外部鼓垫读取MIDI音符值,鼓垫在通道10上发送值。此值将用于控制我的应用程序的各个方面

我计划使用这里的MIDI.net类:读取打击乐器的音符值,并查看其中一个示例,该示例读取由外部键盘播放的音符并显示它们

不幸的是,我不能把它转换成我想要的东西,尽管我觉得我已经很接近了

是否有熟悉此库并能发现我的错误的用户?C#不是我的主要语言(事实上,VB.net也不是!),但如果我能在C#中运行一些东西,那么我可以将其转换为VB.net,以便在我的应用程序中使用

或者,有没有其他更简单的方法来做我想做的事情

以下是我目前的代码:

using System;
using Midi;
using System.Threading;
using System.Collections.Generic;

namespace MidiExamples
{    
    public class Example05 : ExampleBase
    {
        public Example05()
            : base("Example05.cs", "Prints notes and chords as they are played.")
        { }

        public class Summarizer
        {
            public Summarizer(InputDevice inputDevice)
            {
                this.inputDevice = inputDevice;
                percussionPressed = new Dictionary<Percussion, string>();
                inputDevice.NoteOn += new InputDevice.NoteOnHandler(this.NoteOn);
                inputDevice.NoteOff += new InputDevice.NoteOffHandler(this.NoteOff);
            }

            private void PrintStatus()
            {
                Console.Clear();
                Console.WriteLine("Play notes and chords on the MIDI input device, and watch");
                Console.WriteLine("their names printed here.  Press any QUERTY key to quit.");
                Console.WriteLine();

                // Print the currently pressed notes.
                List<Percussion> percussion = new List<Percussion>(percussionPressed.Keys);
                percussion.Sort();
                Console.Write("Notes: ");
                for (int i = 0; i < percussion.Count; ++i)
                {
                    //Pitch pitch = pitches[i];
                    Percussion percussionNote = percussion[i];
                    if (i > 0)
                    {
                        Console.Write(", ");
                    }
                    Console.Write("{0}", percussionNote.Name());
                }
                Console.WriteLine(); 
            }

            public void NoteOn(NoteOnMessage msg)
            {
                lock (this)
                {
                    percussionPressed[msg.ToString] = true;
                    PrintStatus();
                }
            }

            public void NoteOff(NoteOffMessage msg)
            {
                lock (this)
                {
                    percussionPressed.Remove(msg);
                    PrintStatus();
                }
            }

            private InputDevice inputDevice;
            private Dictionary<Percussion, string> percussionPressed;
        }

        public override void Run()
        {
            // Prompt user to choose an input device (or if there is only one, use that one).
            InputDevice inputDevice = ExampleUtil.ChooseInputDeviceFromConsole();
            if (inputDevice == null)
            {
                Console.WriteLine("No input devices, so can't run this example.");
                ExampleUtil.PressAnyKeyToContinue();
                return;
            }
            inputDevice.Open();
            inputDevice.StartReceiving(null);

            Summarizer summarizer = new Summarizer(inputDevice);
            ExampleUtil.PressAnyKeyToContinue();
            inputDevice.StopReceiving();
            inputDevice.Close();
            inputDevice.RemoveAllEventHandlers();
        }
    }
}
使用系统;
使用Midi;
使用系统线程;
使用System.Collections.Generic;
名称空间MIDI示例
{    
公共类Example05:ExampleBase
{
公共示例05()
:base(“Example05.cs”,“在演奏时打印音符和和弦。”)
{ }
公共类摘要器
{
公共摘要器(InputDevice InputDevice)
{
this.inputDevice=inputDevice;
敲击按下=新字典();
inputDevice.NoteOn+=新的inputDevice.NoteOnHandler(this.NoteOn);
inputDevice.NoteOff+=新的inputDevice.NoteOffHandler(this.NoteOff);
}
私有无效打印状态()
{
Console.Clear();
Console.WriteLine(“在MIDI输入设备上播放音符和和弦,并观看”);
控制台。WriteLine(“他们的名字打印在这里。按任何查询键退出。”);
Console.WriteLine();
//打印当前按下的便笺。
列表敲击=新列表(敲击按下.Keys);
打击乐;
控制台。写(“注释:”);
对于(int i=0;i<打击乐器计数;++i)
{
//音高=音高[i];
打击乐打击乐音符=打击乐[i];
如果(i>0)
{
控制台。写(“,”;
}
Write(“{0}”,touctionnote.Name());
}
Console.WriteLine();
}
公共无效便笺(便笺消息消息)
{
锁(这个)
{
敲击按下[msg.ToString]=真;
PrintStatus();
}
}
公共作废注释关闭(注释关闭消息消息)
{
锁(这个)
{
敲击按压。去除(味精);
PrintStatus();
}
}
专用输入设备输入设备;
私人字典打击乐器;
}
公共覆盖无效运行()
{
//提示用户选择输入设备(如果只有一个,则使用该设备)。
InputDevice InputDevice=ExampleUtil.ChooseInputDeviceFromConsole();
如果(inputDevice==null)
{
WriteLine(“没有输入设备,因此无法运行此示例。”);
ExampleUtil.按任意键继续();
返回;
}
inputDevice.Open();
inputDevice.StartReceiving(空);
摘要器摘要器=新摘要器(输入设备);
ExampleUtil.按任意键继续();
inputDevice.StopReceiving();
inputDevice.Close();
inputDevice.RemoveAllEventHandlers();
}
}
}
具体来说,我不确定如何定义NoteOn和NoteOff行为

提前谢谢

菲尔

我本以为可以播放音符,但没有音符值,只有音高值似乎没有意义

不幸的是,这是不可能的。音符的音高值决定要播放的鼓样。MIDI协议没有指定“snare on”或“timpani on”。它只是说C2或E5

也就是说,如果您的控制器和synth使用的是通用MIDI,则通常会有一个映射:


我的意思是你没有代码问题。你的问题是解释那些音高值。

你定义了课堂上的行为说明。谢谢你的回答,罗伯特-这正是我遇到的问题。具体地说,StructionPressed[msg.ToString]给了我一个关于最佳重载版本缺少参数的错误。说到C#和MIDI,我都是新手!你能详细说明一下我是如何得到这个音符值的吗?
strictionpressed[msg.ToString]
是数组语法。更具体地说,它指的是。更具体地说,它似乎是指;请注意,此索引器将
int
值作为其参数,而不是
字符串
,您正试图向其传递
字符串
。如果你没有更多关于C#如何工作的背景知识,恐怕这很难解释。哎呀,我错了!好的,我已经更正了定义,以便传递int,但是我看不出行strictionpressed[msg.ToString]=true中msg可用的任何选项是如何适用的?请参见此处:只有打击按下[msg.Pitch]=true看起来是有效的(根据原始示例),但对于缺少参数的最佳重载选项会出现相同的错误?打击对象从何而来?我在图书馆里看到了
打击乐信息
,但没有看到
打击乐
。谢谢Brad,这很有意义。我只是试过这个例子