Re: Hexadecimal and whatever...
re: reading MIDI files
Before going into details, let me suggest that you use the freeware program
T2MF and MF2T (text to midi file/midi file to text) that's posted somewhere
on the internet. If you can't find it, I could probably e-mail it to you.
You are partly correct. You need to read the numbers as bytes. However, MIDI
files store *some* it it's numbers in an irritating number compression
scheme, where the high bit of the number indicates if the next byte is part
of the number. That means that only 7 of the 8 bits of that number are
"real", and the 8th bit is a "continuation" bit. This is done because MIDI
was designed to flow over slow (but cheap) serial lines.
So if you read the byte:
01h = 00000001
you would use the low bits as the number data:
0000001
and the high bit as a flag indicating if the number continues on to the next
byte (in this case, it does not). The number in this case is 01h.
Of course, it's typically more complex than this. If you read the byte:
ADh = 10101101
then you would have the 7 low bits of the number:
0101101
and note that the 8th bit is set, meaning you need to append the following
byte to the number. If the next byte were:
1Fh = 10001111
then you would take the 7 low bits:
0001111
as the high part (bits 8-14) of your accumulated number:
00011110101101
But since the 8th but of the byte is on, you need to append the *next* byte,
also:
01h = 00000001
Again, bits 1-7 are data:
0000001
These are appended to the high part (15-21) of your number:
000000100011110101101
and bit 8 if the byte is finally 0, so you've completely read the byte.
Breaking it up into bytes, we get:
00000 01000111 10101101
00h 87h ADh
87ADh
34733d
If you had simply glommed the bytes together, you would have gotten
something like:
011FADh = 73645
which is *way* wrong. Besides which, if you didn't pay attention to the
"continuation" bit, you probably would have read in *four* bytes, and gotten
an even *more* bogus number, and set your pointer to the wrong file
position.
Remember that not all the numbers in a MIDI file are compressed - you'll
need to consult the specs on which is which.
Trying to create a MIDI file is even more irritating, because you have to
create the entire track before you can write it, since (1) the size of the
track in included in the header, and (2) you can't calculate the size of the
track until you create it, because the data is compressed.
I didn't have time to look any of the above up, but you can read all about
it in the MIDI File specs, posted all over the internet. Again, I'd
recommend MF2T/T2MF, especially since it makes it easier to debug your
program's output - you can edit your input/output as plain text. That's what
I use to generate my computer-composed music with.
If you want, I think I can dig up one of my buggy programs at home in QBASIC
or Euphoria that tries to read MIDI files. Once you are past the number
compression, though, everything else is pretty simple.
-- David Cuny
|
Not Categorized, Please Help
|
|