MIDI - Notes

Overview

  • MIDI -- Musical Instrument Digital Interface -- is a technical standard that describes a protocol, digital interface and connectors.
  • A single MIDI link can carry up to sixteen channels of information, each of which can be routed to a separate device. (This is Hardware terminology)
  • MIDI carries event messages that specify things like:
    • notation,
    • pitch and velocity,
    • control signals for parameters such as volume, vibrato, audio panning,
    • cues, (Note On, Off, Program Change, etc ? also fading start over period of time ? )
    • and clock signals that set and synchronize tempo between multiple devices.
    • Also ... Filters (adjust timbre) and
    • Envelope Attack (time it takes for the synthesizer to reach max volume)
  • Software or hardware which records multiple midi messages which can be (edited and) used to play the notes later is called "Sequencer".
  • Patch = Set of adjusted parameters or Preset which can be stored in devices internal memory.
  • A patch can be selected/recalled using MIDI event with address (bank-number, program-number-max-128) By using bank number we can work-around the max programs limit of 128 -- hence a device can mimic huge number of various instruments (all kinds of Pianos or all kinds of violins etc).
  • SMF - Standard Midi File Format.
  • SMF organizes MIDI messages into one or more parallel tracks, and timestamp the events.
  • A header contains the arrangement's track count, tempo and which of three SMF formats the file is in.
  • A type 0 file contains the entire performance, merged onto a single track,
  • Type 1 files may contain any number of tracks that are performed in synchrony.
  • Type 2 files are rarely used and store multiple arrangements.
  • Lyrics can be embedded in Midi file.
  • Midi track can represent effects device (e.g. on master output) like reverbs, delays, chorus.
  • A MIDI link can carry sixteen independent channels(0-15) of information.
  • A device can be configured to only listen to specific channels. (e.g. (0 and 1) or (0 and 2)). This is Omni Off.
  • A device can listen/respond to all channels. This is Omni On. (Bit primitive).
  • Device may be mono-phonic. A new Note-On terminates preivousone Or may be poly-phonic.
  • Midi Msg = Status Byte (Msg Type) + upto 2 data bytes containing parameters..
  • There are five types of message:
    • Channel Voice, (e.g. note-on msg with (pitch, velocity, channel-number); Patch/Program Change msg, etc)
    • Channel Mode, (e.g. Turn on/off poly-phonic/Omni msg to channel-number. Note-off for all notes, etc)
    • System Common, (e.g. MIDI time code. Channel independent. All devices must listen to system msgs)
    • System Real-Time, (e.g. MIDI clock and Active Sensing. )
    • System Exclusive. Manufacturer proprietary messages to specific device instrument: Note: SysEx ID setting also allows addressing of specific instance of Yamaha XXX keyboard in the bus.

SMF - Midi File Format

Microsoft Windows bundles SMFs together with Downloadable Sounds (DLS) in a Resource Interchange File Format (RIFF) wrapper, as RMID files with a .rmi extension. RIFF-RMID has been deprecated in favor of Extensible Music Files (XMF).

Controllers include devices such as pedals and levers. Controller numbers 120-127 are reserved as "Channel Mode Messages".

Midi File Format:

Header Chunk | (Tempo) Track Chunk1 | Track Chunk 2-17

Header Chunk:

MThd     - 4 bytes - Midi Track Header Signature 4 bytes. "File Type"
Length   - 4 bytes - 
SMF Type - 2 bytes -  Type 0 means only single Track.
Total Tracks - 2 bytes - 
PPQ Value    - 2 bytes - PPQN (Pulses Per Quarter Note, also known as PPQ). 96 is common.
                         Higher PPQ is more fine control.

Meta Track Chunk 1 :

Track Number - 4 bytes
(PPQ-Offset,  Time Signature Meta Event)
(PPQ-Offset,  Key Signature Meta Event Changes) * 
(PPQ-Offset,  Additional meta events)  ... e.g. "Track name", copyright, "Instrument Name",
              "Lyric", "Marker", "Cue Point" (e.g. He starts dreaming). 

Track 2 :

Track Number - 4 bytes
PPQ, Patch Change Message.
PPQ, Reset All Controllers Message. All values set to default values. (c = 121 command)
PPQ, PAN control
PPQ, Volume control .... (Say, start with High volume)

PPQ, First Midi Message ...  Followed by N messages.
PPQ, End of Track Message.

Note: Channel Mode Messages followed by Channel Voice Messages.

Note: Use smfsh utility to examine the midi file:

$ smfsh
  load file
  tracks
  track 1 
  events
  tempo
  help

MIDI Tracks vs Channels

MIDI Channels represents real/virtual instruments. MIDI standard supports only max 16 channels. To overcome this limitation (channel, bank) virtually represents large number of instruments.

MIDI Tracks are mostly logical representation in software or in MIDI file. In theory single MIDI track can represent entire composition in MIDI file. The software can split this into logical tracks. (e.g. 1 track per channel or (channel,bank) ).

Track:Channel is N:N relation:

- A track can have one or more channels. Possible only in MIDI file. Not possible in Cubase track.
  A track can not change it's instrument/program selection between start to end.
  In practice a track can be associated with only one Channel instance. i.e (channel, Prog, instance-no).
- A channel instance can be associated with multiple tracks:
  e.g. Left Hand and Right Hand separate tracks.  But both hands play the same instrument.
  e.g. Drum Kits may be split into different groove beats tracks mapping into same instrument.

General MIDI - GM

  • Different instruments may have different kinds of sounds at same program location. To standardize a soundbank, GM - General MIDI std was established in 1991. MIDI std was at 1983.
  • GM specifies a bank of 128 sounds arranged into 16 families of eight related instruments, and assigns a specific program number to each instrument.
  • Percussion instruments are placed on channel 10, and a specific MIDI note value is mapped to each percussion sound.
  • GM-compliant devices must offer 24-note polyphony
  • Middle C as note number 60
  • GM-compatible devices are required to respond to velocity, aftertouch, and pitch bend, sustain pedal, etc.
  • Roland's General Standard, or GS, system included additional sounds, drumkits and effects with "bank" support.
  • Yamaha's Extended General MIDI, or XG, followed in 1994. XG not compatible with GS.
  • Both standards feature backward compatibility with the GM specification, but are not compatible with each other.
  • GM2 maintains backward compatibility with GM, but increases polyphony to 32 voices, etc.

MIDI csv Tool

Example MIDI file converted to csv

For complete info see, midicsv tool http://www.fourmilab.ch/webtools/midicsv/

::
0, 0, Header, 1, 2, 480 1, 0, Start_track 1, 0, Title_t, "Close Encounters" 1, 0, Text_t, "Sample for MIDIcsv Distribution" 1, 0, Copyright_t, "This file is in the public domain" 1, 0, Time_signature, 4, 2, 24, 8 1, 0, Tempo, 500000 1, 0, End_track 2, 0, Start_track 2, 0, Instrument_name_t, "Church Organ" 2, 0, Program_c, 1, 19 2, 0, Note_on_c, 1, 79, 81 2, 960, Note_off_c, 1, 79, 0 2, 960, Note_on_c, 1, 81, 81 2, 1920, Note_off_c, 1, 81, 0 ... 0, 0, End_of_file

Synopsis:

Header Records:
-----------------

0, 0, Header, format, nTracks, division                 # The first record of a CSV MIDI file is always the Header record. 
                                                        #  format: the MIDI file type (0, 1, or 2), 
                                                        #  division: the number of clock pulses per quarter note. 
Track, 0, Start_track
Track, Time, End_track
0, 0, End_of_file


Track, Time, Tempo, Number                                # Tempo is Number of microseconds per beat; 500000 means .5 secs; 120 bpm.

Track, Time, Time_signature, Num, Denom, Click, NotesQ    #  Click: Metronome click rate; NotesQ: Number of 32nd notes per beat; 
           # Denom specifies the denominator as a negative power of two, for example 2 for a quarter note, 3 for an eighth note, etc.  

Meta Events
------------

All meta-events which take a text argument are identified by a suffix of “_t” (e.g. Title_t, Instrument_name_t, etc).

Track, Time, Marker_t, Text                                 # Support for time markers. e.g.   "Third Movement".
Track, Time, Cue_point_t, Text                              # Info cue support.  for example, "Door slams".
Track, Time, Lyric_t, Text                                  # Karaoke support
Track, Time, Text_t, Text                                   # Generic tagging at specific time. Use as you like.
Track, Time, Key_signature, Key, Major/Minor                # Key is 0 for C, range 7 to -7 sharps/flats. "major" or "minor".

Rarely used / Vendor specific:
--------------------------------

Track, 0, Sequence_number, Number                        # This meta-event specifies a sequence Number between 0 and 65535, 
                                                         # used to pack multiple tracks in type 2 MIDI; should occur at Time zero.

Track, Time, Channel_prefix, Number                      # Change current channel event; Future events apply to this channel. 

Track, Time, Sequencer_specific, Length, Data, …         # To store vendor-proprietary data in a MIDI file. 

Track, Time, Unknown_meta_event, Type, Length, Data, …   # midicsv uses to flag unknown meta-event code encountered. 
Track, Time, System_exclusive, Length, Data, …           # Vendor specific ...

Channel Events:
---------------

Track, Time, Note_on_c, Channel, Note, Velocity         # Names of channel events all have a suffix of “_c”.
                                                        # Middle C is defined as Note number 60;
                                                        # Velocity (0 to 127). A Note_on_c event with Velocity zero is Note_off_c.

Track, Time, Note_off_c, Channel, Note, Velocity        # Velocity must be 0.

Track, Time, Pitch_bend_c, Channel, Value               # Range from 0-16383. The value 8192 is no pitch bend; Effect unspecified.
Track, Time, Control_c, Channel, Control_num, Value     # Range is 0-127 for both control_num and value. 
                                                        # Controllers: 1 (modulation); 7:volume, 10:pan, 11:expression, 64:sustain

Track, Time, Program_c, Channel, Program_num            # Switch channel to program (patch) Program_num; Range: 0-127;  
                                                        # Instruments documentation may refer to 1 to 128 patches (refers to n+1)


Track, Time, Poly_aftertouch_c, Channel, Note, Value    # Polyphonic synths repeats sending the pressures until released. 0-127.  
Track, Time, Channel_aftertouch_c, Channel, Value       # Monophonic synths send aftertouch pressure for lastkey for entire channel.

Track, 0, SMPTE_offset, Hour, Minute, Second, Frame, FracFrame   # Used for video sync start time. Fractional Frame time is 0 to 99. 

MIDI, Vst, Daw - Some Notes

  • Each external midi device instance is referred as "Port" in FL Studio (and other terminologies for other DAWs ??)
  • Single MIDI device (ie. external MIDI controller) can use multiple MIDI channels ... e.g. Drum PAD can use channel "10" ... A MIDI controller has some proprietory control set "MIDI Out Channel" for the keyboard. If you configure your DAW to map "My Nektar MIDI controller Channel 1 => Piano, Channel 2 => Violin" etc. Then with single key stroke in MIDI controller, you can change current instrument in DAW. This is super useful in Live play.
  • Note: Each "Voice" change in MIDI keyboard device ... does it change MIDI "Channel" from that device (or Port) ?
  • One VSTi could be multi-timbral or single. e.g. One "Grand Piano" vst is single timbral. Then you usually lock single device single channel (like channel 1) with that VST instance. But for Nexus, you typically enable "Channel through" in your DAW and map different MIDI channels to different program number in Nexus. A MIDI track consists of "current program change" events, the piano roll colors different 'program numbers' in different colors.
  • MIDI file contains meta messages about midi sequence that are not to be sent over port. e.g. "Set Tempo" and "End of Track".
  • MIDI channels range from 1 to 16 which can be dynamically mapped to any 'Program' number during performance (whose range is 1-128 or 256 ?).
  • Note On, Note Off, Program Change, Control Change - These are Common MIDI Messages - Each msg embeds Channel Number. Hence MIDI file can record a performance which uses 100 instruments, but at any time, not more than 16 channels active, but number of simultaneous keys are very high since each channel can play all notes simultaneously over 5 or 6 octaves.
  • A third standard, "General MIDI", was introduced in 1991. General MIDI gives specific mappings between MIDI programs instrumental sounds, and also between MIDI key numbers and percussion sounds. i.e. It defines 128 program names and that channel 10 must be associated with Drumkit and assigns the names of the specific keys for the standard drumkit.

Pros and Cons of Midi

Pros:

  • Lot of old compositions exist in Midi, it is easier to explore them if you have strong MIDI support built-in.
  • It is easiest way to share a melody idea with others, with super portable format compatible with all DAWs. It is just the rough blueprint for the song, not the final output.
  • Space efficient way of storing music -- when you have millions of songs / patterns, it is always better to have less size for faster search and management. The midi2wav tools can change it to wav anytime using GM sound fonts.

Cons:

  • Midi is just a standard to capture basic sounds including basic drum kits. Now, you think of modern drum kits with much better new sounds which do not have any closest approximation to GS set. The GM compliance mindset would kill your instinct to find and use the coolest drumkit grooves and sounds. All non-pitched instruments (like some mystic bells and wind chimes) will fall under the category of drum kit. There is no way to represent all of them using current MIDI GS 2 standards.
  • MIDI also discourages you to find / sculpt your own mystic sounds by creating an impression, there were enough number of instruments which already exist. It is subtle, not an inherent flaw. You can always use some 14 instruments at a time, each of which can be anything.
  • If exchanging MIDI becomes a common pattern in elite circles, people will lose interest in using exotic music effects (Reverbs, Compression, Quality of mixing, etc). The melody will get more importance (probably a good thing), but some real factors involved in creating net final result will be unfairly ignored (will be very bad).

MIDI and FL Studio

Using Midi, GM for FL Studio - Fruity LSD

The best compatible sound font players use the fluidsynth library: http://www.fluidsynth.org/ The vstsynth and many opensource projects use this library. Fortunately, we don't have to use any sf font player synthesizer with FL Studio.

We use Fruity LSD as an effect plugin on (first) mixer channel. It is a software synthesizer which uses your soundcard (efficient implementation) and allows you to access 16 instruments.

In step sequencer, you load MIDI out plugin.

Fruity LSD Plugin configuration:

  • Configure Bank - It means the samples collection (.dls file - downloadable soundset). By default it is System32/GM.DLS, but you can change it if you want to.
  • Configure Port - Leave it as 0 for omni -- Meaning it will accept all MIDI live players at same time.

MIDI Out Channel Plugin Configuration for each channel :

  • Port - Leave as 0 to accept any midi controller attached live.

    When multiple instruments have set port:0, the one with the lowest channel number 'traps' midi signal and does not propagate to others. If you want to assign different ports to different devices, then filter it here accordingly.

  • Channel: Choose unique slot number from 1 to 16.

    The patch number (aka Program number or instrument number) chooses the real instrument (like piano, recorder, etc). The midi controller does not send "patch number" over the wire. It just sends the 'channel number' as part of protocol. The midi controller hardware connection (usb slot) defines the "port number".

    The channel number 10 is reserved for drumkit. So patch number is ignored when channel = 10.

Audio to Midi Conversion