title: Reading notes from MIDI file using NAudio
body: <p>Task is to get all notes and their time from MIDI file using NAudio library. So far I get all notes from file but I can't get their time.</p>
  
  <pre><code>            Note noteOn = new Note(); //custom class Note
              MidiFile midi = new MidiFile(open.FileName);
              List&lt;TempoEvent&gt; tempo = new List&lt;TempoEvent&gt;();
  
              for (int i = 0; i &lt; 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));
                      }
  
                  }
              }
  </code></pre>
  
  <p>Questions:</p>
  
  <p>1) To get just list of notes I just look in <code>NoteOnEvents</code> or not? If I understand this correctly, each note has 'start' and 'end', start is defined by <code>NoteOnEvent</code> and 'end' is defined by <code>NoteOffEvent</code>. If I look in both events (<code>NoteOn</code> and <code>NoteOff</code>) I would get duplicate notes. Am I right?</p>
  
  <p>2) How to get note's time? According to <a href="https://stackoverflow.com/a/23071105">this post</a> , I get some values but it seems that the first note's time is correct, but others don't. Also in this post, there is a comment which says the formula for calculating time must be:</p>
  
  <pre><code>((note.AbsTime - lastTempoEvent.AbsTime) / midi.ticksPerQuarterNote) * tempo + lastTempoEvent.RealTime.
  </code></pre>
  
  <p>I don't know parameters <code>lastTempoEvent.RealTime</code> and <code>tempo</code>. It's it tempo of last tempo event or?</p>
  
  <p>3) Reading MIDI file it's very slow, for a smaller files it's ok, but for a big files don't. This small files have ~150 <code>NoteOnEvents</code> and this bigger files have ~1250 <code>NoteOnEvents</code>, which isn't so 'heavy'. Why is so slow?</p>