Skip to content

mgtcardenas/JIDI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The MIDI File Format

Glossary

  • u1 - One byte
  • u2 - Two bytes (big endian)
  • u4 - Four bytes (big endian)
  • varlen - A variable length integer, that can be 1 to 4 bytes. The
                 integer ends when you encounter a byte that does not
                 have the 8th bit set (a byte less than 0x80).
  • len? - The length of the data depends on some code

Basic Structure

Header

The MIDI files begin with the main MIDI Header:

  1. u4 = The four ascii characters 'MThd'
  2. u4 = The length of the MThd header = 6 bytes
  3. u2 = 0 if the file contains a single track
            1 if the file contains one or more simultaneous tracks
            2 if the file contains one or more independent tracks
  4. u2 = number of tracks
  5. u2 = if > 0, the number of pulses per quarter note
            if <= 0, then ???

Tracks

Next come the individual MIDI tracks. The total number of MIDI
tracks was given above, in the MThd header. Each track starts
with a header:

  1. u4 = The four ascii characters 'MTrk'
  2. u4 = Amount of track data, in bytes.

The track data consists of a series of MIDI events. Each MIDI event
has the following format:

  1. varlen - The time between the previous event and this event, measured
                 in "pulses". The number of pulses per quarter note is given
                 in the MThd header.
  2. u1 - The Event code, always between 0x80 and 0xFF
  3. len? - The event data. The length of this data is determined by the
              event code. The first byte of the event data is always < 0x80.

The event code is optional. If the event code is missing, then it
defaults to the previous event code. For example:

varlen, eventcode1, eventdata,
varlen, eventcode2, eventdata,
varlen, eventdata, // eventcode is eventcode2
varlen, eventdata, // eventcode is eventcode2
varlen, eventcode3, eventdata,
....

How do you know if the eventcode is there or missing? Well:

  • All event codes are between 0x80 and 0xFF
  • The first byte of eventdata is always less than 0x80.
    So, after the varlen delta time, if the next byte is between 0x80
    and 0xFF, its an event code. Otherwise, its event data.

Event Codes

Note Off Event

Code:  u1 - 0x80 thru 0x8F  
            0x80 is for channel 1, 0x8F is for channel 16.  
Data:  u1 - The note number, 0-127.  Middle C is 60 (0x3C)
       u1 - The note velocity.  This should be 0

Note On Event

 Code:  u1 - 0x90 thru 0x9F
             0x90 is for channel 1, 0x9F is for channel 16.
 Data:  u1 - The note number, 0-127.  Middle C is 60 (0x3C)
        u1 - The note velocity, from 0 (no sound) to 127 (loud).
             A value of 0 is equivalent to a Note Off.

Key Pressure

 Code:  u1 - 0xA0 thru 0xAF
 Data:  u1 - The note number, 0-127.
        u1 - The pressure.

Control Change

 Code:  u1 - 0xB0 thru 0xBF
 Data:  u1 - The controller number
        u1 - The value

Program Change

 Code:  u1 - 0xC0 thru 0xCF
 Data:  u1 - The program number.

Channel Pressure

 Code:  u1 - 0xD0 thru 0xDF
        u1 - The pressure.

Pitch Bend

 Code:  u1 - 0xE0 thru 0xEF
 Data:  u2 - Some data

Meta Event

 Code:  u1     - 0xFF
 Data:  u1     - Metacode
        varlen - Length of meta event
        u1varlen] - Meta event data.

Meta Event Codes

Sequence Number

Metacode: u1        - 0x0
          varlen    - 0 or 2
          u1varlen] - Sequence number

Text

 Metacode: u1        - 0x1  
           varlen    - Length of text
           u1varlen] - Text

Copyright

 Metacode: u1        - 0x2
           varlen    - Length of text
           u1varlen] - Text

Track Name

 Metacode: u1        - 0x3  
           varlen    - Length of name
           u1varlen] - Track Name

Time Signature

 Metacode: u1         - 0x58
           varlen     - 4 
           u1         - numerator
           u1         - log2(denominator)
           u1         - clocks in metronome click
           u1         - 32nd notes in quarter note (usually 8)

Key Signature

 Metacode: u1         - 0x59  
           varlen     - 2
           u1         - if >= 0, then number of sharps
                        if < 0, then number of flats * -1
           u1         - 0 if major key
                        1 if minor key

Tempo

 Metacode: u1         - 0x51  
           varlen     - 3  
           u3         - quarter note length in microseconds

About

A reupload of my codebase for manipulating MIDI files using Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published