Is that a program that generates the MIDI info in your file?
No. I was just in a creative mood and made the file manually.
what's the equation to figure out the durations for the notes? In other words, how do you translate 9E 00 into 3840? (Whole note.)
The delta-time values use the
variable-length quantity format.
In the variable-length quantity format, the high bit of a byte is clear to indicate the value ends with the current byte, or the high bit of a byte is set to indicate the value continues in the following byte. The actual value is in the lower seven bits of each byte. The most significant bits are first, so you just keep putting the value bits on the right end as you encounter them. The maximum number of bytes that can be used is 4, so the maximum value that can be stored is 4 sets of 7 bits that are all ones, in other words, the binary value that is 28 one bits.
[Source: In the
Standard MIDI Files Specification, you can see the description of variable-length quantity format on printed page 2 (PDF page 4). By the way, as well as the delta-time values, the other things that use the variable-length quantity format are the length values at the beginning of the following events: FF Meta Events, F0 System Excusive events in a MIDI file, and F7 System Exclusive Packet / Escaped events in a MIDI file.]
Example:
encoded hex : 9 E 0 0
encoded binary : 1001 1110 0000 0000
continue flag : 1 0
decoded binary : 001 1110 000 0000
decoded binary : 00111100000000
decoded binary : (00)00 1111 0000 0000
decoded hex : 0 F 0 0
decoded decimal: 3840
If you are more comfortable with decimal math, the decoding process is like this:
1. Let v be a place to accumulate the decoded value. Start with v as 0.
2. Read a byte from the MIDI file into n.
3. What is the value of n?
A. n is 0 to 127: Calculate v + n and store the result back into v. You are done, so end the process here. v is the decoded value.
B. n is 128 to 255: Continue to step 4.
4. Have you already read four bytes?
A. Yes: This shouldn't have happened. End the process with an error message.
B. No: Calculate (v + (n-128)) × 128 and store the result back into v. Go back to step 2.
Once you know how to decode delta-time variable-length quantity values, you can calculate a note's duration as follows:
In a MIDI file the decoded delta-time values are the number of ticks since the previous event in the track.
In your example MIDI file, each note starting message is followed by a note ending message for the same pitch value, but that won't always be the case. So in more complicated MIDI files, you'll have to keep a running sum of the delta ticks you encounter after the note starting message until you see the corresponding note ending message for the same pitch value.
Once you have the total duration in ticks, you can convert it into an amount of quarter notes by using the "ticks per quarter note" resolution value specified in the MIDI file header. In your example, 3840 ticks / 960 ticks per quarter note = 4 quarter notes.
You don't need to use the tempo value unless you want to convert the durations or positions into seconds. For more information how to do that look at the following post:
How to convert ticks to seconds. That post is about calculating a position from the beginning of the song. To calculate a note duration, you'd have to calculate the position of the beginning of the note, the position of the end of the note, then calculate the difference. That will correctly incorporate any tempo changes in the song.