OpenTTD Source: src/music/midifile.hpp Source File

OpenTTD Source 15.0-RC2

midifile.hpp
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
8/* @file midifile.hpp Parser for standard MIDI files */
9
10#ifndef MUSIC_MIDIFILE_HPP
11#define MUSIC_MIDIFILE_HPP
12
13#include "../fileio_type.h"
14#include "midi.h"
15
16struct MusicSongInfo;
17
18 struct MidiFile {
19 struct DataBlock {
20 uint32_t ticktime;
21 int64_t realtime = 0;
22 std::vector<uint8_t> data;
23 DataBlock(uint32_t _ticktime = 0) : ticktime(_ticktime) { }
24 };
25 struct TempoChange {
26 uint32_t ticktime;
27 uint32_t tempo;
28 TempoChange(uint32_t _ticktime, uint32_t _tempo) : ticktime(_ticktime), tempo(_tempo) { }
29 };
30
31 std::vector<DataBlock> blocks{};
32 std::vector<TempoChange> tempos{};
33 uint16_t tickdiv = 0;
34
35 MidiFile();
36 ~MidiFile();
37
38 bool LoadFile(const std::string &filename);
39 bool LoadMpsData(const uint8_t *data, size_t length);
40 bool LoadSong(const MusicSongInfo &song);
41 void MoveFrom(MidiFile &other);
42
43 bool WriteSMF(const std::string &filename);
44
45 static std::string GetSMFFile(const MusicSongInfo &song);
46 static bool ReadSMFHeader(const std::string &filename, SMFHeader &header);
47 static bool ReadSMFHeader(FileHandle &file, SMFHeader &header);
48};
49
50#endif /* MUSIC_MIDIFILE_HPP */
FileHandle
Definition fileio_type.h:132
MidiFile::DataBlock
Definition midifile.hpp:19
MidiFile::DataBlock::data
std::vector< uint8_t > data
raw midi data contained in block
Definition midifile.hpp:22
MidiFile::DataBlock::ticktime
uint32_t ticktime
tick number since start of file this block should be triggered at
Definition midifile.hpp:20
MidiFile::DataBlock::realtime
int64_t realtime
real-time (microseconds) since start of file this block should be triggered at
Definition midifile.hpp:21
MidiFile::TempoChange
Definition midifile.hpp:25
MidiFile::TempoChange::tempo
uint32_t tempo
new tempo in microseconds per tick
Definition midifile.hpp:27
MidiFile::TempoChange::ticktime
uint32_t ticktime
tick number since start of file this tempo change occurs at
Definition midifile.hpp:26
MidiFile
Definition midifile.hpp:18
MidiFile::tempos
std::vector< TempoChange > tempos
list of tempo changes in file
Definition midifile.hpp:32
MidiFile::LoadMpsData
bool LoadMpsData(const uint8_t *data, size_t length)
Create MIDI data from song data for the original Microprose music drivers.
Definition midifile.cpp:830
MidiFile::MoveFrom
void MoveFrom(MidiFile &other)
Move data from other to this, and clears other.
Definition midifile.cpp:862
MidiFile::LoadFile
bool LoadFile(const std::string &filename)
Load a standard MIDI file.
Definition midifile.cpp:452
MidiFile::ReadSMFHeader
static bool ReadSMFHeader(const std::string &filename, SMFHeader &header)
Read the header of a standard MIDI file.
Definition midifile.cpp:411
MidiFile::blocks
std::vector< DataBlock > blocks
sequential time-annotated data of file, merged to a single track
Definition midifile.hpp:31
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition midifile.cpp:1033
MidiFile::tickdiv
uint16_t tickdiv
ticks per quarter note
Definition midifile.hpp:33
MidiFile::WriteSMF
bool WriteSMF(const std::string &filename)
Write a Standard MIDI File containing the decoded music.
Definition midifile.cpp:906
MusicSongInfo
Metadata about a music track.
Definition base_media_music.h:35
SMFHeader
Header of a Standard MIDI File.
Definition midi.h:14