如何读取MIDI文件

时间:2019-08-13 01:54:04

标签: midi

我正在尝试查找示例C#(表单)代码以打开Midi文件并将注释和持续时间(按通道)转储到列表中。我看到了nAudio和Drywetmidi。

进行了一些挖掘,到目前为止使用nAudio进行了挖掘。我在哪里出错了?

    public void ConvertFile()
    {
        MidiFile midiFile = new MidiFile("test.mid", false);
        for (int track = 0; track < midiFile.Tracks; track++)
        {
            IList<MidiEvent> trackEvents = midiFile.Events[track];


            string noteChannel = "", noteName = "";
            int noteDuration = 0;
            long noteStart = 0;

            foreach (MidiEvent midiEvent in trackEvents)
            {

                if (midiEvent is NoteOnEvent noteOnEvent)
                {
                    noteChannel = noteOnEvent.Channel + " ";
                    noteStart = noteOnEvent.AbsoluteTime;
                    if (noteOnEvent.Velocity > 0)
                    {
                        noteName += noteOnEvent.NoteName + "   ";
                        noteDuration = noteOnEvent.NoteLength;
                    }
                }

            }
            if (noteName != "")
                MessageBox.Show(noteName, noteChannel);
        }
    }

1 个答案:

答案 0 :(得分:1)

使用DryWetMIDI,您可以使用以下代码轻松获取所有音符:

MidiFile file = MidiFile.Read("My MIDI file.mid");
IEnumerable<Note> notes = file.GetNotes();

Note具有属性TimeLengthChannelVelocityOffVelocity