📄 midiformat.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0120)http://thns.tsinghua.edu.cn/thnsebooks/Program%20Technology/File%20Format%20Specification/Etc/file_format/sound/mid1.htm -->
<HTML><HEAD><TITLE>standard midi file formatdustin caldwellthe standard midi file format is a very strange beast</TITLE>
<META http-equiv=content-type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY background="">
<P align=center><FONT color=#0000ff size=6>standard midi file format</FONT></P>
<DIV align=center>
<CENTER>
<TABLE width="88%" border=0>
<TBODY>
<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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -