Skip to content Skip to navigation Skip to collection information

OpenStax-CNX

You are here: Home » Content » Musical Signal Processing with LabVIEW (All Modules) » Standard MIDI Files

Navigation

Table of Contents

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • IEEE-SPS display tagshide tags

    This collection is included inLens: IEEE Signal Processing Society Lens
    By: IEEE Signal Processing Society

    Comments:

    "A multimedia educational resource for signal processing students and faculty."

    Click the "IEEE-SPS" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • NSF Partnership display tagshide tags

    This collection is included inLens: NSF Partnership in Signal Processing
    By: Sidney Burrus

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • National Instruments display tagshide tags

    This collection is included in aLens by: National Instruments

    Comments:

    "Developed by Rose Hulman Prof Ed Doering, this collection is a multimedia educational resource for students and faculty that augments traditional DSP courses and courses that cover music […]"

    Click the "National Instruments" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • Evowl

    This collection is included inLens: Rice LMS's Lens
    By: Rice LMS

    Comments:

    "Language: en Supporting Agencies: Rose-Hulman Institute of Technology"

    Click the "Evowl" link to see all content selected in this lens.

  • Lens for Engineering

    This module and collection are included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

  • NI Signal Processing display tagshide tags

    This collection is included inLens: Digital Signal Processing with NI LabVIEW and the National Instruments Platform
    By: Sam Shearman

    Comments:

    "This online course covers signal processing concepts using music and audio to keep the subject relevant and interesting. Written by Prof. Ed Doering from the Rose-Hulman Institute of Technology, […]"

    Click the "NI Signal Processing" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Standard MIDI Files

Module by: Ed Doering. E-mail the authorEdited By: Erik Luther, Sam Shearman

Summary: A complete musical performance can be recorded by sequencing software, which saves individual MIDI messages generated by a synthesizer and measures the time interval between them. The messages and timing information is stored in a standard MIDI file, a binary-format file designed to maximize flexibility and minimize file size. In this module you will learn how to understand the structure of a standard MIDI file at the byte level.

Introduction

In the 1970s, analog synthesizers were either played live by a keyboards musician or by a hardware device called a sequencer. The sequencer could be programmed with a pattern of notes and applied continuously as a loop to the keyboard. In 1983 the MIDI (Musical Instrument Digital Interface) standard was released, and MIDI-capable synthesizers could be connected to personal computers; the IBM PC and Apple Macintosh were among the first PCs to be connected to synthesizers. Software applications were developed to emulate the behavior of hardware sequencers, so naturally these programs were called “sequencers.”

A sequencer application records MIDI messages and measures the time intervals between each received message. When the song is played back, the sequencer transmits the MIDI messages to the synthesizer, with suitable delays to match the original measurements. Sequencers store related MIDI messages in tracks. For example, the musician can record a drum track, and then record a bass track while listening to the drum track, and continue in this fashion until a complete song is recorded with full instrumentation. The finished result is stored in a standard MIDI file (SMF), normally named with the .mid suffix. In recent years sequencing software has merged with audio recording software to create a digital audio workstation, or DAW. Audio and MIDI tracks may be easily recorded, edited, and produced in one seamless environment.

Standard MIDI files can be played by a number of non-sequencer applications such as Winamp and Windows Media Player. In the absence of a synthesizer, these applications send the MIDI messages directly to the MIDI synthesizer included on the computer’s soundcard. With a sufficiently high-quality soundcard, a software synthesizer application can be used instead of conventional synthesizer hardware to produce high-quality music; a USB piano-style keyboard is the only additional piece of gear needed.

After completing this module you will understand the structure of a standard MIDI file, including fundamental concepts such as files chunks, delta-times in variable-length format, running status, and meta-events. See MIDI Messages to learn more about the types of messages that can be contained in a standard MIDI file.

High-Level Structure

A standard MIDI file contains two high-level types of information called chunks: a single header chunk, and one or more track chunks. The header chunk defines which of three possible file types is in force, the number of track chunks in the file, and information related to timing. The track chunk contains pairs of delta times and events; the delta time indicates the elapsed time between two Note-On messages, for example.

A chunk begins with the chunk type, a four-byte string whose value is either MThd for a header chunk or MTrk for a track chunk. The chunk length follows this string, and indicates the number of bytes remaining in the chunk. The chunk length is a 32-bit unsigned integer in big-endian format, i.e., the most-significant byte is first.

Header Chunk

The header chunk begins with the string MThd, and is followed by a fixed chunk length value of 6; as a 32-bit unsigned integer this is 0x00_00_00_06.

The file type follows, and is a 16-bit unsigned integer that takes on one of three possible values: 0 (0x00_00) indicates single track data, 1 (0x00_01) indicates multi-track data which is vertically synchronous (i.e., they are parts of the same song), and 2 (0x00_02) indicates multi-track data with no implied timing relationship between tracks. Type 1 is by far the most common, where each track contains MIDI messages on a single channel. In this way tracks can be associated with a single instrument and recorded individually

Next is the number of tracks, a 16-bit unsigned integer. The number of tracks can range from one (0x00_01) to 65,535 (0xFF_FF). In practice the number of tracks is typically about 20 or so.

The last value in the header chunk is the division, a 16-bit unsigned integer that indicates the number of ticks per quarter note. The tick is a dimensionless unit of time, and is the smallest grain of time used to indicate the interval between events. A typical value of division is 120 (0x00_78).

Track Chunk

The track chunk begins with the string MTrk, and is followed by the track length, a 32-bit unsigned integer that indicates the number of bytes remaining in the track. In theory a track could be as long as 4 Gbytes!

The remainder of the track is composed of pairs of delta-times and events. A delta-time is in units of tics, and indicates the time interval between events. An event is either a MIDI message or a meta-event. Meta-events are unique to the standard MIDI file, and indicate information such as track name, tempo, copyright notice, lyric text, and so on.

A delta time uses variable-length format (VLF), and can be anywhere from one to four bytes in length. Short delta times require only one byte, and long delta times can require up to four bytes. Since short delta times tend to dominate the standard MIDI file (think of a chord hit that generates a burst of Note-On messages with very little time between events), most of the delta times are only one byte long and the overall file size is thus minimized. However, since very long delta times must be accommodated as well, the variable-length format can use up to four bytes to represent very long time intervals.

Variable-Length Format

Variable-length format (VLF) is used to represent delta times and the length of meta-events (to be described below). A numerical value represented in VLF requires from one to four bytes, depending on the size of the numerical value. Since the standard MIDI file is parsed one byte at a time, some type of scheme is required to let the parser know the length of the VLF number. A naïve approach would be to include an additional byte at the beginning of the VLF to indicate the number of bytes remaining in the value. However, this approach would mean that a delta time always requires a minimum of two bytes. Instead, the VLF uses the most-significant bit (MSB) as a flag to indicate whether more bytes follow.

For example, consider the unsigned integer 0x42 = 0b0100_0010 (66 decimal), which can fit within seven bits. Since the MSB is clear (is zero), the parser will not seek any further bytes, and will conclude that the numerical value is 0x42.

Now consider the slightly larger value 0x80 = 0b1000_0000 (128 in decimal), which requires all eight bits of the byte. Since a VLF byte only has seven usable bits, this value must be encoded in two bytes, like this: 0x81 0x00. Why? If we group the original value into two 7-bit groups, we have the two binary values 0b000_0001 and 0b000_0000. The MSB of the first group must be set to 1 to indicate that more bytes follow, so it becomes 0b1000_0001 = 0x81. The MSB of the second group must be set to 0 to indicate that no bytes follow, so it becomes 0b0000_0000 = 0x00.

Exercise 1

A delta time in VLF appears as two bytes, 0x820C. What is the delta time expressed as a decimal value?

Exercise 2

A delta time in VLF appears as four bytes, 0xF3E8A975. What is the delta time expressed as a hexadecimal value?

Running Status

Running status is another technique developed to minimize the size of a standard MIDI file. Consider a long sequence of Note-On and Note-Off messages (“long” meaning perhaps thousands of events). Eventually you would notice that storing the status byte for each and every note message would seem redundant and wasteful. Instead, it would be more economical to store one complete Note-On message (a status byte followed by the note number byte and the velocity byte), and then from that point onwards store only the note number byte and velocity byte.

Clearly this is a simple solution for the application that creates the MIDI file, but what about the application that must read and parse the file? How can the parser know whether or not the status byte has been omitted from a message? Fortunately, the status byte can easily be distinguished from the data bytes by examination of the MSB (most-significant bit). When the parser expects a new message to start and finds that the byte has its MSB cleared (set to zero), the parser recovers the status information by using the information from the most-recent complete message. That is, the status continues to be the same as whatever was indicated by the most recent status byte received. In this way, a series of Note On messages can be conveyed by only two bytes per message instead of three.

But how do notes get turned off without storing a Note-Off message? Fortunately a Note-On message with zero velocity is equivalent to a Note-Off event! Thus, once the running status is established as “note on,” it is possible to turn notes on and off for as long as you like, with each event requiring only two bytes.

Any time another message type needs to be inserted (such as a Program Change), the running status changes to match the new type of message. Alternatively, when a different channel is required for a note message, a fresh Note-On status byte must be sent. For these reasons, most standard MIDI files are organized as Type 1 (multi-track, vertically synchronous), where each track corresponds to a different voice on its own channel. The Program Change message occur at the beginning of the track to select the desired voice, a complete Note-On message is sent, and running status is used for the duration of the track to send Note-On and Note-Off messages as two-byte values.

Meta-Events

Meta-events provide a mechanism for file-related information to be represented, such as track name, tempo, copyright notice, lyric text, and so on. A meta-event begins with an 8-bit unsigned integer of value 0xFF. Note that the MSB (most-significant bit) is set, so a meta-event begins in the same way as a MIDI message status byte, whose MSB is also set. Next, the meta-event type is indicated by an 8-bit unsigned integer. After this, the number of bytes remaining in the meta-event is indicated by a numerical value in variable-length format (VLF); see an earlier section for full details). Lastly, the remainder of the meta-event information follows.

Some common meta-event types include 0x01 (text event), 0x02 (copyright notice), 0x03 (track name), 0x04 (instrument name), 0x05 (lyric text), and 0x7F (sequencer-specific). All of these meta-events can have an arbitrary length. Sequencer-specific is analogous to the System-Exclusive MIDI message, in that the data contained in the meta-event is arbitrary, and can normally be interpreted only by the sequencer application that created the standard MIDI file.

Tracks normally conclude with the end-of-track meta-event, whose type is 0x2F and whose length is zero. The end-of-track meta-event appears as the byte sequence 0xFF 0x2F 0x00 (meta-event, end-of-track type, zero length, with no data following).

The set-tempo meta-event (type 0x51) provides the value microseconds per quarter note, a 24-bit (3-byte) unsigned integer. This value in conjunction with the division value in the header chunk (division = ticks per quarter note) determines how to translate a delta time in ticks to a time in seconds. If the set-tempo meta-event is not included in the standard MIDI file, the value defaults to 500,000 microseconds per quarter note (or 0.5 seconds per quarter note).

Exercise: Given T (the value of a set-tempo meta-event) and D (the division value in the header chunk), determine an equation that can convert a delta time in ticks to a delta time in seconds.

For Further Study

Collection Navigation

Content actions

Download:

Collection as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Module as:

PDF | More downloads ...

Add:

Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks