Common Events and Function

This page lists some of the more common things that a user is likely to do with the MIDI file. It is not exhaustive; see the class reference for a more complete list of public functions.

Adding Notes

As the MIDI standard is all about music, creating notes will probably be the lion’s share of what you’re doing. This is done with the addNote() function.

MIDIFile.addNote(track, channel, pitch, time, duration, volume, annotation=None)

Add notes to the MIDIFile object

Parameters:
  • track – The track to which the note is added.
  • channel – the MIDI channel to assign to the note. [Integer, 0-15]
  • pitch – the MIDI pitch number [Integer, 0-127].
  • time – the time at which the note sounds. The value can be either quarter notes [Float], or ticks [Integer]. Ticks may be specified by passing eventtime_is_ticks=True to the MIDIFile constructor. The default is quarter notes.
  • duration – the duration of the note. Like the time argument, the value can be either quarter notes [Float], or ticks [Integer].
  • volume – the volume (velocity) of the note. [Integer, 0-127].
  • annotation – Arbitrary data to attach to the note.

The annotation parameter attaches arbitrary data to the note. This is not used in the code, but can be useful anyway. As an example, I have created a project that uses MIDIFile to write csound orchestra files directly from the class EventList.

As an example, the following code-fragment adds two notes to an (already created) MIDIFile object:

track    = 0   # Track numbers are zero-origined
channel  = 0   # MIDI channel number
pitch    = 60  # MIDI note number
time     = 0   # In beats
duration = 1   # In beats
volume   = 100 # 0-127, 127 being full volume

MyMIDI.addNote(track,channel,pitch,time,duration,volume)
time  = 1
pitch = 61
MyMIDI.addNote(track,channel,pitch,time,duration,volume)

Add a Tempo

Every track can have tempos specified (the unit of which is beats per minute).

MIDIFile.addTempo(track, time, tempo)

Add notes to the MIDIFile object

Parameters:
  • track – The track to which the tempo event is added. Note that in a format 1 file this parameter is ignored and the tempo is written to the tempo track
  • time – The time (in beats) at which tempo event is placed
  • tempo – The tempo, in Beats per Minute. [Integer]

Example:

track = 0
time  = 0   # beats, beginning of track
tempo = 120 # BPM
MyMIDI.addTempo(track, time, tempo)

Assign a Name to a Track

MIDIFile.addTrackName(track, time, trackName)

Name a track.

Parameters:
  • track – The track to which the name is assigned.
  • time – The time (in beats) at which the track name event is placed. In general this should probably be time 0 (the beginning of the track).
  • trackName – The name to assign to the track [String]

In general, the time should probably be t=0

Example:

track      = 0
time       = 0
track_name = "Bassline 1"
MyMIDI.addTrackName(track, time, track_name)

Adding a Program Change Event

The program change event tells the the instrument what voice a certain track should sound. As an example, if the instrument you’re using supports General MIDI, you can use the GM numbers to specify the instrument.

Important Note: Within this library program numbers are zero-origined (as they are on a byte-level within the MIDI standard), but most of the documentation you will see is musician-centric, so they are usually given as one-origined. So, for example, if you want to sound a Cello, you would use a program number of 42, not the 43 which is given in the link above.

MIDIFile.addProgramChange(tracknum, channel, time, program)

Add a MIDI program change event.

Parameters:
  • tracknum – The zero-based track number to which program change event is added.
  • channel – the MIDI channel to assign to the event. [Integer, 0-15]
  • time – The time (in beats) at which the program change event is placed [Float].
  • program – the program number. [Integer, 0-127].

Example:

track   = 0
channel = 0
time    = 8 # Eight beats into the composition
program = 42 # A Cello

MyMIDI.addProgramChange(track, channel, time, program)

Writing the File to Disk

Ultimately, you’ll need to write your data to disk to use it.

MIDIFile.writeFile(fileHandle)

Write the MIDI File.

Parameters:fileHandle – A file handle that has been opened for binary writing.

Example:

with open("mymidifile.midi", 'wb') as output_file:
    MyMIDI.writeFile(output_file)

Additional Public Function

The above list is not exhaustive. For example, the library includes methods to create arbitrary channel control events, SysEx and Universal SysEx events, Registered Parameter calls and Non-Registered Parameter calls, etc. Please see the Class Reference for a more complete list of public functions.