Midifile View on GitHub

MidiFile class

home   »   documentation   »   classes

The MidiFile class is an interface for reading and writing Standard MIDI files. MIDI file tracks are stored as a list of MidiEventList objects, which in turn are lists of MidiEvents The MidiFile class can be considered to be a two-dimensional array of events that are accessible with the [] operator. The first dimension is the track index, and the second dimension is the event index for the given track. Thus, midifile[2][15] would return the 16th event in the third track.

MidiEvents consist of a list of MIDI message bytes along with timing and other variables. For example, the construct midifile[2][15][0] would return the MIDI command byte for the 16th message in the third track, and midifile[2][15].tick would return the timestamp for the event (either in delta or absolute tick values, see the tick-related functions described further below).

Reading/writing functions

The two main member functions for reading and writing Standard MIDI Files are read and write. The argument to these functions can be either a filename or an input/output stream object. The status function can be called after reading or writing to determine if the action was successful.

read — Read a Standard MIDI File in binary or ASCII formats.
write — Write a Standard MIDI file from the MidiFile contents.
status — Returns true if last read/write was successful.


Two additional functions, called writeHex and writeBinasc can be used to print ASCII hex codes that determinstically represent a binary Standard MIDI File. The read function will transparently parse either binary MIDI files, or their ASCII representations in one of these two formats.

writeHex — Print MidiFile as a list of ASCII hex bytes.
writeBinasc — Print ASCII version of MIDI file in binasc format.


Track-related functions

The MidiFile class contains a list tracks, each stored as a MidiEventList object. The [] operator accesses each list, and the getTrackCount function will report the current number of tracks in a MidiFile object.

operatorSQUAREBRACKETS — Returns the list of events for a track.
getTrackCount — Return the number of tracks in the MidiFile.


The tracks in a MidiFile can reversibly be merged into a single event list by calling the joinTracks function. This will cause the getTrackCount function to report that there is one track in the file, and if the file is written in this state, it will be saved as a type-0 MIDI file (from which the multi-track state will not be recoverable). Before tracks are joined, the events in each track must be in correct time sequence, so the sort function may need to be called before joining the tracks.

joinTracks — Merge all tracks into a single stream of events.
splitTracks — Separate events into their original track configuration.


The hasJoinedTracks and hasSplitTracks functions can be used to detect the current state of the tracks in a MidiFile. By default, a MidiFile will be in the split state.

hasJoinedTracks — Return true if MidiFile is in the joined-track state.
hasSplitTracks — Return true if MidiFile is in the split-track state.


When a MidiFile is in the joined state, the original track is stored in the track variable of each MidiEvent. The getSplitTrack function returns the track index for when a MidiFile is in the split state.

getSplitTrack — Get the track number of an event when MidiFile is in split state.


Here are functions which relate to adding, deleting and merging tracks:

addTrack — Added one or more empty tracks to the MidiFile.
deleteTrack — Remove the specified track from the MidiFile.
mergeTracks — Combine two tracks into a single track.


When a MidiFile is in absolute-tick mode (see further below), the tick variable of MidiEvent objects in the MidiFile are the total number of ticks since the start time for the file. In the absolute-tick mode, MidiEvents can be added to tracks in non-sequential order. To re-arrange the events into proper time order (such as before writing the file), use the sortTracks function, which will sort all tracks in the MIDI file, or sortTrack function, which will sort a particular track by its index number.

sortTracks — Sort all tracks in the MidiFile by event timestamps.
sortTrack — Sort a track in terms of timestamps of the events.


Time-related functions

MidiEvents stored in a MidiFile structure contain two public variables related to time:

  1. int MidiEvent::tick — Quanta time-units describing durations in Standard MIDI Files.
  2. double MidiEvent::seconds — Interpreted physical time units in seconds calculated by doTimeInSecondsAnalysis() from .tick data and tempo meta-messages stored in the MidiFile.

Event tick interpretation

Tick values on MidiEvents can be set to two types of states: (1) delta time, which indicate the number of ticks to wait from the last event in the track before performing the current event, and (2) absoute time, where the tick value represents the cumulative tick time since the start of the MidiFile until the performance time of the current event. Standard MIDI Files store tick times as delta ticks, but it is often more useful to manipulate event data with absolute ticks. The absoluteTicks and deltaTicks functions switch the event tick values within the MidiFile between these two modes:

absoluteTicks — Convert event delta tick values into absolute ticks.
deltaTicks — Convert timestamps into delta times.


The isDeltaTime and isAbsoluteTime functions can be used to check which mode in which the event ticks are currently given.

isDeltaTime — Returns true if event ticks are in delta-time mode.
isAbsoluteTime — Returns true if event ticks are in absolute-time mode.


Tick values are symbolic time units, such as rhythms in music notation. For example quarter notes do not have a specific duration in seconds until a tempo is applied to the rhythm. Within Standard MIDI Files, there is a field which specifies how many ticks represent a quarter note. This convserion value can be read or set with the following two MidiFile member functions:

getTicksPerQuarterNote — Return the delta time ticks units which represent a quarter note.
setTicksPerQuarterNote — Set the ticks-per-quarter note value.


Physical time of events

doTimeAnalysis — Calculate the time in seconds for each event in the MidiFile.
getTimeInSeconds — Return the time in seconds for the specified message.
setMillisecondTicks — Set the ticks per quarter note value in the MIDI file header
getAbsoluteTickTime — Convert a time in seconds into an absolute tick time.
sortTrack — Sort a track in terms of timestamps of the events.
sortTracks — Sort all tracks in the MidiFile by event timestamps.


Other functions

addEvent — Add a new MidiEvents to the end of the specified track.
addMetaEvent — Insert a meta message event.
addPitchBend — Insert a pitch bend message into the given track.
allocateEvents — Allocate extra space for a track event list.
clear — Clear all tracks, leaving one track with no data in it.
extractVlvTime — Extract a VLV value from the input stream.
getEvent — Return the event at the given index in the specified track.
getEventCount — Return the number of events in the specified track.
getFilename — Return the filename for the MidiFile.
makeVLV — Convert an integer into a list of variable length value bytes.
writeHex — Print MidiFile as a list of ASCII hex bytes.
setFilename — Set the filename of the MidiFile.


Static functions

readByte — Read one byte from the input stream.
readLittleEndian2Bytes — Read two bytes from the input stream in little-endian format.
readLittleEndian4Bytes — Read four bytes from the input stream in little-endian order.


Private functions

buildTimeMap — Create a time map relating ticks to absolute time.
extractMidiData — Extract a MIDI message from the input stream.
linearTickInterpolationAtSecond — Return the tick value at a given input time.
secondsearch — Comparison function for sorting time.
ticksearch — Compare function for finding a tick entry in the time map.
unpackVLV — Convert VLV byte list into an unsigned long value.
writeVLValue — Convert an integer int a list of VLV bytes.

 

Keyboard shortcuts for this page:

  • ctrl++/= to open all function documentation entries.
  • ctrl+ to close all documentation.
  • ctrl+eto toggle display of all code examples.
  • shft+click will open documentation for a single method and close all other entries.