OpenTTD Source: MidiFile Struct Reference

OpenTTD Source 15.0-RC2

Data Structures | Public Member Functions | Static Public Member Functions | Data Fields
MidiFile Struct Reference

## Data Structures
struct | DataBlock
struct | TempoChange

## Public Member Functions
bool | LoadFile (const std::string &filename)
| Load a standard MIDI file.
bool | LoadMpsData (const uint8_t *data, size_t length)
| Create MIDI data from song data for the original Microprose music drivers.
bool | LoadSong (const MusicSongInfo &song)
void | MoveFrom (MidiFile &other)
| Move data from other to this, and clears other.
bool | WriteSMF (const std::string &filename)
| Write a Standard MIDI File containing the decoded music.

## Static Public Member Functions
static std::string | GetSMFFile (const MusicSongInfo &song)
| Get the name of a Standard MIDI File for a given song.
static bool | ReadSMFHeader (const std::string &filename, SMFHeader &header)
| Read the header of a standard MIDI file.
static bool | ReadSMFHeader (FileHandle &file, SMFHeader &header)
| Read the header of a standard MIDI file.

## Data Fields
std::vector< DataBlock > | blocks {}
| sequential time-annotated data of file, merged to a single track
std::vector< TempoChange > | tempos {}
| list of tempo changes in file
uint16_t | tickdiv = 0
| ticks per quarter note

## Detailed Description

Definition at line 18 of file midifile.hpp.

## Constructor & Destructor Documentation

## ◆ MidiFile()

MidiFile::MidiFile | ( |  | ) |

Definition at line 1122 of file midifile.cpp.

## ◆ ~MidiFile()

MidiFile::~MidiFile | ( |  | ) |

Definition at line 1127 of file midifile.cpp.

## Member Function Documentation

## ◆ GetSMFFile()

std::string MidiFile::GetSMFFile | ( | const MusicSongInfo & | song | ) |
| static

Get the name of a Standard MIDI File for a given song.

For songs already in SMF format, just returns the original. Otherwise the song is converted, written to a temporary-ish file, and the written filename is returned.

Parameters

  song | Song definition to query

Returns
  Full filename string, empty string if failed

Definition at line 1033 of file midifile.cpp.

References AppendPathSeparator(), BASESET_DIR, MusicSongInfo::cat_index, FileExists(), MusicSongInfo::filename, MusicSongInfo::filetype, FioCreateDirectory(), FioFindFullPath(), GetMusicCatEntryData(), LoadMpsData(), MTT_MPSMIDI, MTT_STANDARDMIDI, OLD_GM_DIR, SP_AUTODOWNLOAD_DIR, and WriteSMF().

Referenced by MusicDriver_BeMidi::PlaySong(), MusicDriver_ExtMidi::PlaySong(), and MusicDriver_FluidSynth::PlaySong().

## ◆ LoadFile()

bool MidiFile::LoadFile | ( | const std::string & | filename | ) |

Load a standard MIDI file.

Parameters

  filename | name of the file to load

Returns
  true if loaded was successful

Definition at line 452 of file midifile.cpp.

References BASESET_DIR, blocks, FioFOpenFile(), ReadSMFHeader(), tempos, and tickdiv.

## ◆ LoadMpsData()

bool MidiFile::LoadMpsData | ( | const uint8_t * | data,
|  | size_t | length
| ) |  |

Create MIDI data from song data for the original Microprose music drivers.

Parameters

  data | pointer to block of data
  length | size of data in bytes

Returns
  true if the data could be loaded

Definition at line 830 of file midifile.cpp.

References MpsMachine::PlayInto().

Referenced by GetSMFFile().

## ◆ LoadSong()

bool MidiFile::LoadSong | ( | const MusicSongInfo & | song | ) |

Definition at line 838 of file midifile.cpp.

## ◆ MoveFrom()

void MidiFile::MoveFrom | ( | MidiFile & | other | ) |

Move data from other to this, and clears other.

Parameters

  other | object containing loaded data to take over

Definition at line 862 of file midifile.cpp.

References blocks, tempos, and tickdiv.

## ◆ ReadSMFHeader() [1/2]

bool MidiFile::ReadSMFHeader | ( | const std::string & | filename,
|  | SMFHeader & | header
| ) |  |
| static

Read the header of a standard MIDI file.

Parameters

  [in] | filename | name of file to read from
  [out] | header | filled with data read

Returns
  true if the file could be opened and contained a header with correct format

Definition at line 411 of file midifile.cpp.

References BASESET_DIR, FioFOpenFile(), and ReadSMFHeader().

Referenced by LoadFile(), and ReadSMFHeader().

## ◆ ReadSMFHeader() [2/2]

bool MidiFile::ReadSMFHeader | ( | FileHandle & | file,
|  | SMFHeader & | header
| ) |  |
| static

Read the header of a standard MIDI file.

The function will consume 14 bytes from the current file pointer position.

Parameters

  [in] | file | open file to read from (should be in binary mode)
  [out] | header | filled with data read

Returns
  true if a header in correct format could be read from the file

Definition at line 426 of file midifile.cpp.

## ◆ WriteSMF()

bool MidiFile::WriteSMF | ( | const std::string & | filename | ) |

Write a Standard MIDI File containing the decoded music.

Parameters

  filename | Name of file to write to

Returns
  True if the file was written to completion

Definition at line 906 of file midifile.cpp.

References blocks, MidiFile::DataBlock::data, FioFOpenFile(), NO_DIRECTORY, MidiFile::TempoChange::tempo, tempos, tickdiv, MidiFile::DataBlock::ticktime, and MidiFile::TempoChange::ticktime.

Referenced by GetSMFFile().

## Field Documentation

## ◆ blocks

std::vector<DataBlock> MidiFile::blocks {}

sequential time-annotated data of file, merged to a single track

Definition at line 31 of file midifile.hpp.

Referenced by LoadFile(), MoveFrom(), MpsMachine::PlayInto(), and WriteSMF().

## ◆ tempos

std::vector<TempoChange> MidiFile::tempos {}

list of tempo changes in file

Definition at line 32 of file midifile.hpp.

Referenced by LoadFile(), MoveFrom(), MpsMachine::PlayInto(), and WriteSMF().

## ◆ tickdiv

uint16_t MidiFile::tickdiv = 0

ticks per quarter note

Definition at line 33 of file midifile.hpp.

Referenced by LoadFile(), MoveFrom(), MpsMachine::PlayInto(), and WriteSMF().

The documentation for this struct was generated from the following files:

- src/music/midifile.hpp
- src/music/midifile.cpp