MIDI and XML for a drum machine

As a next project I want to make a simple drum machine. Several tracks that each play a different drum sample and each track has sixteen steps. Just like a Roland 808 or the main step sequencer in FruityLoops. It’s nothing complicated but it made me think about how to store the patterns the drum machine plays.

MIDI

MIDI

I’d like to put in an XML file all the information about how many patterns there are, how many tracks, which sound plays when and so on. But I want to keep the file as flexible as possible. It must be easy to later change the length of a pattern to 32 tracks, to add melodic patterns, notes of different length, filter settings… Whatever I need I want to be able to include in the file.

Then it occurred to me that such a file already exist: MIDI!

MIDI has been around since 1982, and today still is one of the main formats for music. Besides channels, notes and timing it can hold all kinds of extra settings as “Control Change” values or “System Exclusive” messages. Very general and flexible and just what I need. Only, MIDI is no XML. And Flash can’t read MIDI files. So I googled MIDI and XML and found I’m not the first to need MIDI in an XML form.

As far as I’ve been able to find two standards exist to format MIDI as XML:

MIDI XML

The first one is MIDI XML. The good thing is that it’s been developed by the MIDI Manufacturers Organization which is the organization that made the original MIDI specification. But the bad thing is that it seems to be incomplete and their only page about MIDI XML was last updated in July 2003.

A few DTD pages exist to show how the XML should be formatted, but that’s about it. Not a lot of information. The most interesting DTD file is MIDIEvents10.dtd, which shows how to represent MIDI events. This is what the XML should look like according to the file:

<NoteOn Channel="1" Note="60" Velocity="100" />
<NoteOn Channel="1" Note="62" Velocity="127" />
<ControlChange Channel="16" Control="7" Value="50" />
<ProgramChange Channel="1" Number="2" />

Clear enough, but this is only useful for a direct stream of MIDI events. Not to represent a MIDI file with a complete song. If only because there is no time information. You’d never know how much time it takes from one note to the next.

MusicXML

The second one is MusicXML. A much more complete standard made by Recordare, a music and software company I had never heard of before. It seems to build on the MIDI XML standard, but goes much further. This is how an event would be represented in MusicXML:

<Event>
  <Delta>0</Delta>
  <NoteOn Channel="1" Note="60" Velocity="100" />
</Event>

The main new thing is the <Delta> tag. It’s value is the time it takes from this event to the next, measured in “Ticks Per Beat”. How many ticks per beat there are can be found in the first section of the XML file. This is from the XML file I use for my drum machine:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MIDIFile PUBLIC
  "-//Recordare//DTD MusicXML 0.9 MIDI//EN"
  "http://www.musicxml.org/dtds/midixml.dtd">
<MIDIFile>
  <Format>0</Format>
  <TrackCount>1</TrackCount>
  <TicksPerBeat>4</TicksPerBeat>
  <TimestampType>Delta</TimestampType>

This means:

  • <Format> holds the MIDI file type (0 or 1. 2 is not supported by MusicXML)
  • <TrackCount> the number of tracks (which is one because a type zero MIDI file can only have one track)
  • <TicksPerBeat> is the finest timing precision of the file. I used four because my drum machine has sixteen steps for a pattern of one bar, which is four steps for one beat. I don’t need more precision here.
  • <TimeStampType> is the way time is notated. It can be Delta or AbsoluteDeltameans  time is the amount of time between the current event and the next event, Absolute means the total time between the start of the song and the current event. I used Delta because real Standard MIDI files (.MID) only use Delta time.

Much more information is available at the Recordare MusicXML pages, including DTD and XSD files, FAQ, tutorials and alphabetical index.

Another very helpful page is http://staff.dasdeck.de/valentin/midi. It’s a collection of PHP scripts to work with MIDI. Especially useful is Midi2Xml where you can upload a standard MIDI file which it translates into MusicXML format. It helped me a lot to discover how MusicXML works.

2 Comments

Wouter,

Hi Martin,
Thanks for the kind words!

About the trackCount in the example XML: I really don’t remember why I used only one track. Now that I look at it again to use twelve tracks seems much clearer and easier to work with. Maybe I just wanted to keep it as basic as possible and use a MIDI type 0 file that can only have one track.

About indicating the number of bars: I wanted to only use information that is part of the official MIDI standard. So that the XML files could theoretically be translated back to real MIDI files, should that be desirable.
And there is no ‘Bars’ element in MIDI. The only timing information is ‘Ticks Per Beat’ since the start of the song.

Cheers,
Wouter

Martin,

Hi Wouter,
Once again, great work, and wise initiative to find inspiration in MusicXml.

I just wonder why your example uses TrackCount=1 whereas your file contains 12 tracks.

Moreover, your XML header does not indicate number of bars. What about considering a Bars element in the header, and even a BeatPerBar (which would force to have songs made of equal length bar, but that’s not a eavy limitation in my opinion).

Regards,
Martin

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − eleven =