c# - 如何使用 Midi 点网类在 C# 中读取打击乐音符值

标签 c# .net midi

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

我计划使用此处的 MIDI-dot-net 类:https://code.google.com/p/midi-dot-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();
        }
    }
}

具体来说,我不确定如何定义 NoteOn 和 NoteOff 行为......

提前致谢

菲尔

最佳答案

I would have thought that it would be possible to get the note played but there's no Note value, only a Pitch value which doeesn't seem to make sense.

不幸的是,这是不可能的。音符的音高值决定了要播放的鼓样本。 MIDI 协议(protocol)没有指定“开启军鼓”或“开启定音鼓”。它只是说 C2 或 E5。

话虽如此,如果您的 Controller 和合成器使用的是通用 MIDI,通常会遵循一个映射。 From Wikipedia :

enter image description here

我的意思是您没有代码问题。您的问题在于解释这些音调值。

关于c# - 如何使用 Midi 点网类在 C# 中读取打击乐音符值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868653/

相关文章:

c# - Azure 通知中心错误 401

c# - 关于 <FIELDSET> <LABEL> 标签而不是使用 TABLE 的文档?

c# - Python3.6内调用C#代码

mp3 - 将音频转换为MIDI的软件

c# - 字符串数据类型的 EF 核心列表到 postgresql

c# - WCF 单一联系点

c# - 仅在特定时间运行一次 azure 函数?

midi - 从 Lilypond 片段创建 MIDI 文件

language-agnostic - MIDI 文件的结构是什么?

c# - Visual Studio 2017 RC 核心控制台项目无法编译