📄 mid1.htm
字号:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>standard midi file formatdustin caldwellthe standard midi file format is a very
strange beast</title>
<meta name="generator" content="microsoft frontpage 3.0">
</head>
<body background="../jpg/di1.JPG">
<p align="center"><font size="6" color="#0000ff">standard midi file format</font></p>
<div align="center"><center>
<table border="0" width="88%">
<tr>
<td width="100%">dustin caldwell<br>
<br>
the standard midi file format is a very strange beast. when viewed as a<br>
whole, it can be quite overwhelming. of course, no matter how you look at it,<br>
describing a piece of music in enough detail to be able to reproduce it<br>
accurately is no small task. so, while complicated, the structure of the midi<br>
file format is fairly intuitive when understood. <br>
i must insert a disclaimer here that i am by no means an expert with<br>
midi nor midi files. i recently obtained a gravis ultrasound board for my pc,<br>
and upon hearing a few midi files (.mid) thought, "gee, i'd like to be able to<br>
make my own .mid files." well, many aggravating hours later, i discovered that<br>
this was no trivial task. but, i couldn't let a stupid file format stop me.<br>
(besides, i once told my wife that computers aren't really that hard to use,<br>
and i'd hate to be a hypocrite) so if any errors are found in this<br>
information, please let me know and i will fix it. also, this document's scope<br>
does not extend to every type of midi command and every possible file<br>
configuration. it is a basic guide that should enable the reader (with a<br>
moderate investment in time) to generate a quality midi file.<br>
<br>
1. overview<br>
<br>
a midi (.mid) file contains basically 2 things, header chunks and track<br>
chunks. section 2 explains the header chunks, and section 3 explains the track<br>
chunks. a midi file contains one header chunk describing the file format,<br>
etc., and any number of track chunks. a track may be thought of in the same<br>
way as a track on a multi-track tape deck. you may assign one track to each<br>
voice, each staff, each instrument or whatever you want. <br>
<br>
2. header chunk<br>
<br>
the header chunk appears at the beginning of the file, and describes the<br>
file in three ways. the header chunk always looks like:<br>
<br>
4d 54 68 64 00 00 00 06 ff ff nn nn dd dd<br>
<br>
the ascii equivalent of the first 4 bytes is mthd. after mthd comes the 4-byte<br>
size of the header. this will always be 00 00 00 06, because the actual header<br>
information will always be 6 bytes. <br>
<br>
ff ff is the file format. there are 3 formats:<br>
<br>
0 - single-track <br>
1 - multiple tracks, synchronous<br>
2 - multiple tracks, asynchronous<br>
<br>
single track is fairly self-explanatory - one track only. synchronous multiple<br>
tracks means that the tracks will all be vertically synchronous, or in other<br>
words, they all start at the same time, and so can represent different parts<br>
in one song. asynchronous multiple tracks do not necessarily start at the same<br>
time, and can be completely asynchronous. <br>
<br>
nn nn is the number of tracks in the midi file.<br>
<br>
dd dd is the number of delta-time ticks per quarter note. (more about this<br>
later)<br>
<br>
<br>
3. track chunks<br>
<br>
the remainder of the file after the header chunk consists of track chunks.<br>
each track has one header and may contain as many midi commands as you like.<br>
the header for a track is very similar to the one for the file:<br>
<br>
4d 54 72 6b xx xx xx xx<br>
<br>
as with the header, the first 4 bytes has an ascii equivalent. this one is<br>
mtrk. the 4 bytes after mtrk give the length of the track (not including the<br>
track header) in bytes. <br>
following the header are midi events. these events are identical to the<br>
actual data sent and received by midi ports on a synth with one addition. a<br>
midi event is preceded by a delta-time. a delta time is the number of ticks<br>
after which the midi event is to be executed. the number of ticks per quarter<br>
note was defined previously in the file header chunk. this delta-time is a<br>
variable-length encoded value. this format, while confusing, allows large<br>
numbers to use as many bytes as they need, without requiring small numbers to<br>
waste bytes by filling with zeros. the number is converted into 7-bit bytes,<br>
and the most-significant bit of each byte is 1 except for the last byte of the<br>
number, which has a msb of 0. this allows the number to be read one byte at a<br>
time, and when you see a msb of 0, you know that it was the last (least<br>
significant) byte of the number. according to the midi spec, the entire delta-<br>
time should be at most 4 bytes long. <br>
following the delta-time is a midi event. each midi event (except a<br>
running midi event) has a command byte which will always have a msb of 1 (the<br>
value will be >= 128). a list of most of these commands is in appendix a. each<br>
command has different parameters and lengths, but the data that follows the<br>
command will have a msb of 0 (less than 128). the exception to this is a meta-<br>
event, which may contain data with a msb of 1. however, meta-events require a<br>
length parameter which alleviates confusion. <br>
one subtlety which can cause confusion is running mode. this is where<br>
the actual midi command is omitted, and the last midi command issued is<br>
assumed. this means that the midi event will consist of a delta-time and the<br>
parameters that would go to the command if it were included. <br>
<br>
4. conclusion<br>
<br>
if this explanation has only served to confuse the issue more, the<br>
appendices contain examples which may help clarify the issue. also, 2<br>
utilities and a graphic file should have been included with this document: <br>
<br>
dec.exe - this utility converts a binary file (like .mid) to a tab-delimited<br>
text file containing the decimal equivalents of each byte.<br>
<br>
rec.exe - this utility converts a tab-delimited text file of decimal values<br>
into a binary file in which each byte corresponds to one of the decimal<br>
values.<br>
<br>
midinote.ps - this is the postscript form of a page showing note numbers with<br>
a keyboard and with the standard grand staff.<br>
 appendix a<br>
<br>
1. midi event commands<br>
<br>
each command byte has 2 parts. the left nybble (4 bits) contains the actual<br>
command, and the right nybble contains the midi channel number on which the<br>
command will be executed. there are 16 midi channels, and 8 midi commands (the<br>
command nybble must have a msb of 1).<br>
in the following table, x indicates the midi channel number. note that all<br>
data bytes will be <128 (msb set to 0).<br>
<br>
hex binary data description<br>
8x 1000xxxx nn vv note off (key is released)<br>
nn=note number<br>
vv=velocity<br>
<br>
9x 1001xxxx nn vv note on (key is pressed)<br>
nn=note number<br>
vv=velocity<br>
<br>
ax 1010xxxx nn vv key after-touch<br>
nn=note number<br>
vv=velocity<br>
<br>
bx 1011xxxx cc vv control change<br>
cc=controller number<br>
vv=new value<br>
<br>
cx 1100xxxx pp program (patch) change<br>
pp=new program number<br>
<br>
dx 1101xxxx cc channel after-touch<br>
cc=channel number<br>
<br>
ex 1110xxxx bb tt pitch wheel change (2000h is normal or no<br>
change)<br>
bb=bottom (least sig) 7 bits of value<br>
tt=top (most sig) 7 bits of value<br>
the following table lists meta-events which have no midi channel number. they<br>
are of the format:<br>
<br>
ff xx nn dd<br>
<br>
all meta-events start with ff followed by the command (xx), the length, or<br>
number of bytes that will contain data (nn), and the actual data (dd).<br>
<br>
hex binary data description<br>
00 00000000 nn ssss sets the track's sequence number.<br>
nn=02 (length of 2-byte sequence number)<br>
ssss=sequence number<br>
<br>
01 00000001 nn tt .. text event- any text you want.<br>
nn=length in bytes of text<br>
tt=text characters<br>
<br>
02 00000010 nn tt .. same as text event, but used for<br>
copyright info.<br>
nn tt=same as text event<br>
<br>
03 00000011 nn tt .. sequence or track name<br>
nn tt=same as text event<br>
<br>
04 00000100 nn tt .. track instrument name<br>
nn tt=same as text event<br>
<br>
05 00000101 nn tt .. lyric<br>
nn tt=same as text event<br>
<br>
06 00000110 nn tt .. marker<br>
nn tt=same as text event<br>
<br>
07 00000111 nn tt .. cue point<br>
nn tt=same as text event<br>
<br>
2f 00101111 00 this event must come at the end of each<br>
track<br>
<br>
51 01010001 03 tttttt set tempo<br>
tttttt=microseconds/quarter note<br>
<br>
58 01011000 04 nn dd ccbb time signature<br>
nn=numerator of time sig.<br>
dd=denominator of time sig. 2=quarter<br>
3=eighth, etc.<br>
cc=number of ticks in metronome click<br>
bb=number of 32nd notes to the quarter<br>
note<br>
<br>
59 01011001 02 sf mi key signature<br>
sf=sharps/flats (-7=7 flats, 0=key of c,<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -