1

I am reading a midi file with this parser. But I cannot read the real time.

MidiFile midiFile = new MidiFile("/Jenkins.mid");
var ticksPerQuarterNote = _midiFile.TicksPerQuarterNote;
foreach (MidiTrack track in midiFile.Tracks)
{
    foreach (MidiEvent midiEvent in track.MidiEvents)
    {
        if (midiEvent.MidiEventType != MidiEventType.NoteOn)
           continue;
        int note = midiEvent.Note;
        int time = midiEvent.Time;
    }
}

All the formulas I have seen on the internet use the tempo, but I can't find it.

Pascal
  • 11
  • 1

1 Answers1

0

You can use my DryWetMIDI library which does all these calculations for you:

var midiFile = MidiFile.Read("Jenkins.mid");
var tempoMap = midiFile.GetTempoMap();
    
foreach (var trackChunk in midiFile.GetTrackChunks())
{
    foreach (var timedEvent in trackChunk.GetTimedEvents())
    {
        if (timedEvent.Event.MidiEventType != MidiEventType.NoteOn)
           continue;
    
        MetricTimeSpan metricTime = timedEvent.TimeAs<MetricTimeSpan>(tempoMap);
    }
}

Here we get metric time (hours/minutes/seconds/ms), but the library provides several other formats you can convert MIDI time to. Please read the article of the library docs to learn more: Time and length.

More than that if you actually want to get notes instead of events, it's super easy with DryWetMIDI:

var notes = midiFile.GetNotes();

You can also use TimeAs method on notes.

Maxim
  • 1,995
  • 1
  • 19
  • 24