
               Moore's Guide to Programming MIDI
                         Version 1.0
                        by Peter Moore

                (C) Copyright 1994 Peter Moore
                      All Rights Reserved

     This document is provided 'as-is' with no warrenties
     whatsoever, neither expressed, nor implied.  The author
     disclaimes all responsibility from the use or inability
     to use this information by you.  The author, although he
     believes this information to be techincally accurate,
     in no way guarentees its accuracy and will not be held
     liable for any techical inaccuracy.  

     This document is copyrighted.  You may freely distribute
     it in this unmodified form provided that no copies are sold
     beyond a reasonable shipping/transfering fee.  You may
     also use this information free of charge.  
     
     However, if you do find the information useful and use it 
     to learn how to program MIDI, I ask for a donation of $10.00 
     although any donation will be accepted.  

     You can send them to:
		Peter Moore
		1336 Cantigney Court
		Naperville, IL 60565     
	
     I wish you luck in your MIDI programming careers.

                                             Peter Moore
                                          
Contents:

  Part 1 - Learning MIDI
     I. Introduction
     II. Starting MIDI- What's needed?
     III. Programming
  Part 2 - MIDI Reference
     I. MIDI Messages
     II. Control Change Messages
     
Part 1 - Learning MIDI

     I. Introduction

          MIDI seems to be the new buzz word these days,
     and indeed, it has the right to be.  The Multimedia
     council has included a General MIDI interface as a
     standard on all MPC compliant sound cards.  However,
     using MIDI is one thing - programming it is quite
     different.  
          There are several sound cards which come with
     MIDI Interfaces, few of which (if any) come with
     detailed information on programming it.  That's where
     this document comes in.  In it, you will learn all
     about the concept and principles of MIDI, and how
     to use it in your applications without the aid of
     a sound card development kit (although we don't
     discuss detailed information pertaining to individual
     sound cards, so a knowledge of your sound card will
     be helpful, but in most cases, it's easy to obtain
     the information you will need just through bulletin
     boards and such).  
          Part 1 deals with the concepts and principles
     of MIDI.  We go beyond just a musician-level discussion
     to talk about the details of how MIDI data is sent from
     your computer to your instrument.  Part 2 is a reference
     section.  Here, we will talk about each MIDI message
     individually.
     
     II. Starting MIDI - What's Needed

          If you were only a musician, you would need simply
     a MIDI-compatable electronic instrument.  That instrument
     could communicate data to and from another MIDI instrument.
     If you were a musician/computerphile, you might use a
     MIDI sound card such as the Creative Lab's Sound Blaster.
     You would also need a program called a sequencer.  A 
     sequencer is a program that allows you to enter MIDI data
     via your terminal or instrument, and then play it back
     on your instrument.  You would also need an interface
     to connect your sound card to your instrument.  But you
     are a programmer, and programmers always need more tools
     than laymen.  For MIDI programming, you could use any
     programming language, though I prefer C because it is
     so easy to create new and reusable functions in it.  All
     you need to use is the out port command/function.  That
     function is listed in each language here:

                    C/C++ : outp
                    BASIC : OUT
                    Assembly : OUT

     If you wish to receive input, use the in port commands:

                    C/C++ : inp
                    BASIC : INP
                    Assembly : IN

          Refer to your information on your sound card for
     which ports to write to and read from.  It would be impossible
     to include information for all the sound cards here, being
     how I don't have access to it all.  And because they are all
     different, it would be senseless to include any individual.
          
          MIDI receives and sends data via 16 channels.  On
     a MIDI instrument, those channels can each be programmed
     for a different sound, called a program.  In this regard,
     you can only control up to 16 sounds at once on a polyphonic
     keyboard with many instrument sounds.  However, the number
     of simultaneous notes playing depends on the capability of
     keyboard.  

     III. Programming
          All MIDI data and commands are sent via the I/O ports
     in the computer (for IBM PC's and up).  There are two
     types of things you can send to the MIDI device: commands
     and data and always in that order.  The command is always
     one byte.  4 of the bits in the byte represent the 16
     channels.  The other four represent the command number-
     16 in all.  This is illustrated as follows:

              +-----------+------------+
              |7  6  5  4 | 3  2  1  0 |
              |Command No.| Channel No.|
              +-----------+------------+

          The next thing sent to the MIDI device is the
     data.  This can be any number of bytes long, depending
     on the type of command.  There are basically two types
     of commands: normal commands such as note on/off and control
     change messages.  Control change messages basically affect
     how the keyboard works such as sustain and volume and
     things like that whereas normal commands tell the
     keyboard to do things.  A control change is a normal
     command except that one byte of the data is the control
     change message.  This process of commands and data
     is illustrated as follows:

          Normal commands:
               Command#/Channel#  Data1...
          Control changes:
               ControlChange/Channel# Control# Data1

          You get the MIDI device to do things by sending
     it a command.  If the command is not supported, it should
     ignore you.  Refer to the information on your sound
     card for sending MIDI commands and data.  You always
     send the data straeam right after the command.  Remember,
     the command is always 1 byte long and the data is at least
     one byte long.

Part II - Reference

     I.  MIDI Messages

          This is a reference for all MIDI messages.  It
     is in the following format:

          [Message Name] : [Message# in decimal [Message# in binary + XXXX
               representing the channel number]
          Description: [description]


     Note Off : 128 1000xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the note number from 0 to 127.
          0 is a very low C and 127 is the highest G.  Middle C
          is 60.
     Data 2 : 0xxxxxxx where xxxxxxx is the velocity from 0 to 127, with
          0 being the softest and 127 being the loudest.  For note off,
          this should always be 0.
     Description : This turns the specified note off on the specified
          channel.  Before the same note can be turned on, note off
          must be first called or the note must be off to begin with.

     Note On : 154 1001xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the note number from 0 to 127.
     Data 2 : 0xxxxxxx where xxxxxxx is the velocity from 0 to 127.
     Description : This turns on the specified note on the specified
          channel.  This function cannot be used on a note unless
          the note is off to begin with or Note Off has been called.

     Polyphonic Aftertouch : 162 1010xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the note from 0 to 127
     Data 2 : 0xxxxxxx where xxxxxxx is the aftertouch value from 0 to
          127 with 0 being the least and 127, the most.
     Description : This causes an aftertouch affect on the specified
          note on the specified channel.

     Control Change : 188 1011xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the control change from 0 to 127
     Data 2 : 0xxxxxxx where xxxxxxx is the control value
     Description : Puts the value of data 2 to the controler with the
          number Data 1.  See the next chapter for details

     Program Change : 192 1100xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the program number to change to
     Description : This will make the specified channel use the program
          number specified in Data 1.  For instance, if you wanted the
          channel 1 to play notes using program 35, you would send:
               11000001 00100011 or
               193 35

     Aftertouch : 208 1101xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the aftertouch value from 0 to
          127.
     Description : This sets aftertouch to all sounds on the specified
          channel to Data 1

     Pitch Bend : 224 1110xxxx
     Data 1 : 0xxxxxxx where xxxxxxx is the least significant byte of
          the pitch bend value
     Data 2 : 0xxxxxxx where xxxxxxx is the most significant byte
     Description : This changes all pitches in the channel of ranges
          from -8192 to +8192 Hz.  Many consecutive pitch bend
          messages at a constant increase give the illusion of
          the pitch gradually increasing.

     There are other messages, but they are beyond the scope
     of this book because they are not necessary to program
     MIDI effectively.  If you do want more information on these,
     you can contact the General MIDI Association whose address 
     is listed at the end.

     II. Control Change Messages

          Each control change message will be identified by its
     number which is passed to Data 2 in the control change command.

     Number  Description       Values
     1 -     Modulation Wheel  0 - 127
     2 -     Breath Controller 0 - 127
     4 -     Foot Controller   0 - 127
     5 -     Portamento Time   0 - 127
     6 -     Data Entry MSB    0 - 127
     7 -     Master Volume     0 - 127
     64 -    Sustain Level     0 - 127
     65 -    Portamento        0 - 127
     66 -    Sostenuto Pedal   0 - 127
     67 -    Soft Pedal        0 - 127
     68 -    Data Increment    127 Only
     69 -    Data Decrement    127 Only

          Most of these are self explanatory.  Please note that there
     are other control change messages, but they are not defined by
     the General MIDI specifications therefore are not discussed here.
     You should contact your instrument manufacturer for inmformation
     on those messages.  Of the above messages that are not
     self-explanatory, if your instrument supports them, the manual
     should discuss them.  


For More Information:

     For more information on the standard MIDI messages, contact
the following organization:

     International MIDI Association
     5316 West 57th Street
     Los Angeles, CA 90056
     United States
     Telephone: (213)-649-6434

     Ask or write for the MIDI 1.0 Detailed Specification
     and Standard MIDI Files 1.0.


     