使用 DryWetMidi 从 MIDI 文件中读取每个音符(音符、力度、长度)

Min*_*one 2 c# midi file

我正在制作一个 C# 程序,它应该从 MIDI 文件中读取每个音符并获取音符的长度和速度,但我不知道该怎么做...

我正在使用DryWetMidi库,但还有其他库吗?

Max*_*xim 5

DryWetMidi 1.1.0开始,获取 MIDI 文件的音符所需要的只是以下代码:

IEnumerable<Note> notes = midiFile.GetNotes();
Run Code Online (Sandbox Code Playgroud)

Note类包含所有你需要的属性:NoteNumberNoteNameOctaveLengthTimeVelocity,和OffVelocity

要获得Time<小时,分钟,秒><酒吧,节拍>,您可以使用TimeAs扩展方法:

TempoMap tempoMap = midiFile.GetTempoMap();
MetricTimeSpan metricTime = note.TimeAs<MetricTimeSpan>(tempoMap);
BarBeatTicksTimeSpan musicalTime = note.TimeAs<BarBeatTicksTimeSpan>(tempoMap);
Run Code Online (Sandbox Code Playgroud)

对于Length,您可以使用LengthAs扩展方法:

TempoMap tempoMap = midiFile.GetTempoMap();
MetricTimeSpan metricLength = note.LengthAs<MetricTimeSpan>(tempoMap);
BarBeatTimeSpan metricLength = note.LengthAs<BarBeatTimeSpan>(tempoMap);
Run Code Online (Sandbox Code Playgroud)

您可以在库文档的时间和长度文章中阅读更多内容。