I'm trying to read a MIDI file and I want to determine the exact time of a NoteOn event from it in C#.
I tried to use absolute time, but the output was something like 256632.
What is this number ?
This is the line of my code that returns the time :
(note as NoteOnEvent).AbsoluteTime
Timing in MIDI files is centered around ticks and beats. A beat is the same as a quarter note. Beats are divided into ticks, the smallest unit of time in MIDI. Each message in a MIDI file has a delta time, which tells how many ticks have passed since the last message. The length of a tick is defined in ticks per beat.
All MIDI Files should specify tempo and time signature. If they don't, the time signature is assumed to be 4/4, and the tempo 120 beats per minute. In format 0, these meta-events should occur at least at the beginning of the single multi-channel track.
MIDI events contain a MIDI message and a corresponding time-stamp expressed in ticks, and can represent the MIDI event information stored in a MIDI file or a Sequence object. The duration of a tick is specified by the timing information contained in the MIDI file or Sequence object.
A MIDI file only contains incremental times. Included as a variable length value between 1 and 4 bytes before each MIDI event. The library you are using is being helpful in providing you with the AbsoluteTime property. Simply calculated by summing the incremental times for each event.
The unit is "delta ticks". The length of a delta tick is not a fixed value, it is specified in the MIDI file header. NAudio exposes it as the MidiFile.DeltaTicksPerQuarterNote property. So you'll need to divide the value you get from AbsoluteTime by this value to get the note position from the start of the song.
This is of course still a relative value, it depends on the tempo of the song. The rate at which you play quarter notes. The recommended tempo is included in the file as well, it is the TempoEvent in NAudio. Its MicrosecondsPerQuarterNote property tells you how long a quarter note should be for subsequent events. Beware that there can be more than one tempo event in a song.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With