Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用NAudio从MIDI文件中读取注释_C#_Midi_Naudio - Fatal编程技术网

C# 使用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>();

任务是使用NAudio库从MIDI文件中获取所有音符及其时间。到目前为止,我从文件中得到了所有的笔记,但我无法得到他们的时间

            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
,这不是很重。为什么这么慢

  • 在MIDI文件中,注释有单独的注释打开和注释关闭事件。 NAudio已经在搜索相应的备忘事件,并为您计算长度,因此您不需要自己处理备忘事件。 (但是,音符打开和音符关闭事件之间的节奏可能会发生变化,因此必须分别计算这两个时间。)

  • 这些是对值的描述,而不是实际的字段名。
    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);