MidiFile Class Reference
juce_audio_basics » midi

Reads/writes standard midi format files. More...

## Public Member Functions

| MidiFile ()
| Creates an empty MidiFile object. More...
| ~MidiFile ()
| Destructor. More...
| MidiFile (const MidiFile &)
| Creates a copy of another MidiFile. More...
MidiFile & | operator= (const MidiFile &)
| Copies from another MidiFile object. More...
| MidiFile (MidiFile &&)
| Creates a copy of another MidiFile. More...
MidiFile & | operator= (MidiFile &&)
| Copies from another MidiFile object. More...
int | getNumTracks () const noexcept
| Returns the number of tracks in the file. More...
const MidiMessageSequence * | getTrack (int index) const noexcept
| Returns a pointer to one of the tracks in the file. More...
void | addTrack (const MidiMessageSequence &trackSequence)
| Adds a midi track to the file. More...
void | clear ()
| Removes all midi tracks from the file. More...
short | getTimeFormat () const noexcept
| Returns the raw time format code that will be written to a stream. More...
void | setTicksPerQuarterNote (int ticksPerQuarterNote) noexcept
| Sets the time format to use when this file is written to a stream. More...
void | setSmpteTimeFormat (int framesPerSecond, int subframeResolution) noexcept
| Sets the time format to use when this file is written to a stream. More...
void | findAllTempoEvents (MidiMessageSequence &tempoChangeEvents) const
| Makes a list of all the tempo-change meta-events from all tracks in the midi file. More...
void | findAllTimeSigEvents (MidiMessageSequence &timeSigEvents) const
| Makes a list of all the time-signature meta-events from all tracks in the midi file. More...
void | findAllKeySigEvents (MidiMessageSequence &keySigEvents) const
| Makes a list of all the time-signature meta-events from all tracks in the midi file. More...
double | getLastTimestamp () const
| Returns the latest timestamp in any of the tracks. More...
bool | readFrom (InputStream &sourceStream, bool createMatchingNoteOffs=true)
| Reads a midi file format stream. More...
bool | writeTo (OutputStream &destStream, int midiFileType=1) const
| Writes the midi tracks as a standard midi file. More...
void | convertTimestampTicksToSeconds ()
| Converts the timestamp of all the midi events from midi ticks to seconds. More...

## Detailed Description

Reads/writes standard midi format files.

To read a midi file, create a MidiFile object and call its readFrom() method. You can then get the individual midi tracks from it using the getTrack() method.

To write a file, create a MidiFile object, add some MidiMessageSequence objects to it using the addTrack() method, and then call its writeTo() method to stream it out.

See also
  MidiMessageSequence

## Constructor & Destructor Documentation

## MidiFile() [1/3]

MidiFile::MidiFile | ( |  | ) |

Creates an empty MidiFile object.

## ~MidiFile()

MidiFile::~MidiFile | ( |  | ) |

Destructor.

## MidiFile() [2/3]

MidiFile::MidiFile | ( | const MidiFile & |  | ) |

Creates a copy of another MidiFile.

## MidiFile() [3/3]

MidiFile::MidiFile | ( | MidiFile && |  | ) |

Creates a copy of another MidiFile.

## Member Function Documentation

## operator=() [1/2]

MidiFile& MidiFile::operator= | ( | const MidiFile & |  | ) |

Copies from another MidiFile object.

## operator=() [2/2]

MidiFile& MidiFile::operator= | ( | MidiFile && |  | ) |

Copies from another MidiFile object.

## getNumTracks()

int MidiFile::getNumTracks | ( |  | ) | const

noexcept

Returns the number of tracks in the file.

See also
  getTrack, addTrack

## getTrack()

const MidiMessageSequence* MidiFile::getTrack | ( | int | index | ) | const

noexcept

Returns a pointer to one of the tracks in the file.

Returns
  a pointer to the track, or nullptr if the index is out-of-range

See also
  getNumTracks, addTrack

## addTrack()

void MidiFile::addTrack | ( | const MidiMessageSequence & | trackSequence | ) |

Adds a midi track to the file.

This will make its own internal copy of the sequence that is passed-in.

See also
  getNumTracks, getTrack

## clear()

void MidiFile::clear | ( |  | ) |

Removes all midi tracks from the file.

See also
  getNumTracks

## getTimeFormat()

short MidiFile::getTimeFormat | ( |  | ) | const

noexcept

Returns the raw time format code that will be written to a stream.

After reading a midi file, this method will return the time-format that was read from the file's header. It can be changed using the setTicksPerQuarterNote() or setSmpteTimeFormat() methods.

If the value returned is positive, it indicates the number of midi ticks per quarter-note - see setTicksPerQuarterNote().

It it's negative, the upper byte indicates the frames-per-second (but negative), and the lower byte is the number of ticks per frame - see setSmpteTimeFormat().

## setTicksPerQuarterNote()

void MidiFile::setTicksPerQuarterNote | ( | int | ticksPerQuarterNote | ) |

noexcept

Sets the time format to use when this file is written to a stream.

If this is called, the file will be written as bars/beats using the specified resolution, rather than SMPTE absolute times, as would be used if setSmpteTimeFormat() had been called instead.

Parameters

  ticksPerQuarterNote | e.g. 96, 960

See also
  setSmpteTimeFormat

## setSmpteTimeFormat()

void MidiFile::setSmpteTimeFormat | ( | int | framesPerSecond,
|  | int | subframeResolution
| ) |  |

noexcept

Sets the time format to use when this file is written to a stream.

If this is called, the file will be written using absolute times, rather than bars/beats as would be the case if setTicksPerBeat() had been called instead.

Parameters

  framesPerSecond | must be 24, 25, 29 or 30
  subframeResolution | the sub-second resolution, e.g. 4 (midi time code), 8, 10, 80 (SMPTE bit resolution), or 100. For millisecond timing, setSmpteTimeFormat (25, 40)

See also
  setTicksPerBeat

## findAllTempoEvents()

void MidiFile::findAllTempoEvents | ( | MidiMessageSequence & | tempoChangeEvents | ) | const

Makes a list of all the tempo-change meta-events from all tracks in the midi file.

Useful for finding the positions of all the tempo changes in a file.

Parameters

  tempoChangeEvents | a list to which all the events will be added

## findAllTimeSigEvents()

void MidiFile::findAllTimeSigEvents | ( | MidiMessageSequence & | timeSigEvents | ) | const

Makes a list of all the time-signature meta-events from all tracks in the midi file.

Useful for finding the positions of all the tempo changes in a file.

Parameters

  timeSigEvents | a list to which all the events will be added

## findAllKeySigEvents()

void MidiFile::findAllKeySigEvents | ( | MidiMessageSequence & | keySigEvents | ) | const

Makes a list of all the time-signature meta-events from all tracks in the midi file.

Parameters

  keySigEvents | a list to which all the events will be added

## getLastTimestamp()

double MidiFile::getLastTimestamp | ( |  | ) | const

Returns the latest timestamp in any of the tracks.

(Useful for finding the length of the file).

## readFrom()

bool MidiFile::readFrom | ( | InputStream & | sourceStream,
|  | bool | createMatchingNoteOffs = `true`
| ) |  |

Reads a midi file format stream.

After calling this, you can get the tracks that were read from the file by using the getNumTracks() and getTrack() methods.

The timestamps of the midi events in the tracks will represent their positions in terms of midi ticks. To convert them to seconds, use the convertTimestampTicksToSeconds() method.

Parameters

  sourceStream | the source stream
  createMatchingNoteOffs | if true, any missing note-offs for previous note-ons will be automatically added at the end of the file by calling MidiMessageSequence::updateMatchedPairs on each track.

Returns
  true if the stream was read successfully

## writeTo()

bool MidiFile::writeTo | ( | OutputStream & | destStream,
|  | int | midiFileType = `1`
| ) |  | const

Writes the midi tracks as a standard midi file.

The midiFileType value is written as the file's format type, which can be 0, 1 or 2 - see the midi file spec for more info about that.

Parameters

  destStream | the destination stream
  midiFileType | the type of midi file

Returns
  true if the operation succeeded.

## convertTimestampTicksToSeconds()

void MidiFile::convertTimestampTicksToSeconds | ( |  | ) |

Converts the timestamp of all the midi events from midi ticks to seconds.

This will use the midi time format and tempo/time signature info in the tracks to convert all the timestamps to absolute values in seconds.

The documentation for this class was generated from the following file:

- juce_MidiFile.h