C# 使用NAudio从MIDI文件中读取注释
任务是使用NAudio库从MIDI文件中获取所有音符及其时间。到目前为止,我从文件中得到了所有的笔记,但我无法得到他们的时间C# 使用NAudio从MIDI文件中读取注释,c#,midi,naudio,C#,Midi,Naudio,任务是使用NAudio库从MIDI文件中获取所有音符及其时间。到目前为止,我从文件中得到了所有的笔记,但我无法得到他们的时间 Note noteOn = new Note(); //custom class Note MidiFile midi = new MidiFile(open.FileName); List<TempoEvent> tempo = new List<TempoEvent>();
Note noteOn = new Note(); //custom class Note
MidiFile midi = new MidiFile(open.FileName);
List<TempoEvent> tempo = new List<TempoEvent>();
for (int i = 0; i < midi.Events.Count(); i++)
{
foreach (MidiEvent note in midi.Events[i])
{
TempoEvent tempoE;
try { tempoE = (TempoEvent)note; tempo.Add(tempoE); }
catch { }
if (note.CommandCode == MidiCommandCode.NoteOn)
{
var t_note = ( NoteOnEvent)note;
var noteOffEvent = t_note.OffEvent;
noteOn.NoteName.Add(t_note.NoteName);
noteOn.NoteNumber.Add(t_note.NoteNumber);
noteOn.NoteVelocity.Add(t_note.Velocity);
noteOn.NoteLenght.Add(t_note.NoteLength);
double d = (t_note.AbsoluteTime / midi.DeltaTicksPerQuarterNote) * tempo[tempo.Count() - 1].Tempo;
noteOn.StartTime.Add(TimeSpan.FromSeconds(d));
}
}
}
我不知道参数lastTempoEvent.RealTime
和tempo
。这是最后一个节奏项目的节奏还是
3) 读取MIDI文件非常慢,对于较小的文件可以,但对于较大的文件则不行。这个小文件有~150个NoteOnEvents
,而这个大文件有~1250个NoteOnEvents
,这不是很重。为什么这么慢
tempo
是上一个tempo事件的微秒/四分音符
值。
lastTempoEvent.RealTime
是您为上一个节奏事件计算的时间(以微秒为单位)
最后一个节奏事件是绝对时间最大且仍在该事件绝对时间之前的节奏事件。
该节奏事件可能位于另一个曲目中,因此在处理事件之前,最好合并所有曲目(将midi.Events.MidiFileType
设置为零)您可以看看其他提供MIDI文件解析的.NET库。例如,使用以下代码,您可以获取MIDI文件中包含的所有音符:
MidiFile file = MidiFile.Read("Great Song.mid");
IEnumerable<Note> notes = file.GetNotes();
使用TimeAs
和LengthAs
方法,您无需自己进行任何计算。MetricTimeSpan
的实例可以隐式转换为TimeSpan
MidiFile file = MidiFile.Read("Great Song.mid");
IEnumerable<Note> notes = file.GetNotes();
TempoMap tempoMap = file.GetTempoMap();
MetricTimeSpan metricTime = note.TimeAs<MetricTimeSpan>(tempoMap);
MetricTimeSpan metricLength = note.LengthAs<MetricTimeSpan>(tempoMap);