OpenTTD
midifile.hpp
1 /* $Id$ */
2 
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 <http://www.gnu.org/licenses/>.
8 */
9 
10 /* @file midifile.hpp Parser for standard MIDI files */
11 
12 #ifndef MUSIC_MIDIFILE_HPP
13 #define MUSIC_MIDIFILE_HPP
14 
15 #include "../stdafx.h"
16 #include "../core/smallvec_type.hpp"
17 #include "midi.h"
18 #include <vector>
19 #include <string>
20 
21 struct MusicSongInfo;
22 
23 struct MidiFile {
24  struct DataBlock {
25  uint32 ticktime;
26  uint32 realtime;
28  DataBlock(uint32 _ticktime = 0) : ticktime(_ticktime) { }
29  };
30  struct TempoChange {
31  uint32 ticktime;
32  uint32 tempo;
33  TempoChange(uint32 _ticktime, uint32 _tempo) : ticktime(_ticktime), tempo(_tempo) { }
34  };
35 
36  std::vector<DataBlock> blocks;
37  std::vector<TempoChange> tempos;
38  uint16 tickdiv;
39 
40  MidiFile();
41  ~MidiFile();
42 
43  bool LoadFile(const char *filename);
44  bool LoadMpsData(const byte *data, size_t length);
45  bool LoadSong(const MusicSongInfo &song);
46  void MoveFrom(MidiFile &other);
47 
48  bool WriteSMF(const char *filename);
49 
50  static std::string GetSMFFile(const MusicSongInfo &song);
51  static bool ReadSMFHeader(const char *filename, SMFHeader &header);
52  static bool ReadSMFHeader(FILE *file, SMFHeader &header);
53 };
54 
55 #endif /* MUSIC_MIDIFILE_HPP */
Metadata about a music track.
Header of a Stanard MIDI File.
Definition: midi.h:18
bool LoadMpsData(const byte *data, size_t length)
Create MIDI data from song data for the original Microprose music drivers.
Definition: midifile.cpp:833
bool LoadFile(const char *filename)
Load a standard MIDI file.
Definition: midifile.cpp:451
SmallVector< byte, 8 > data
raw midi data contained in block
Definition: midifile.hpp:27
uint16 tickdiv
ticks per quarter note
Definition: midifile.hpp:38
void MoveFrom(MidiFile &other)
Move data from other to this, and clears other.
Definition: midifile.cpp:867
std::vector< DataBlock > blocks
sequential time-annotated data of file, merged to a single track
Definition: midifile.hpp:36
uint32 tempo
new tempo in microseconds per tick
Definition: midifile.hpp:32
std::vector< TempoChange > tempos
list of tempo changes in file
Definition: midifile.hpp:37
uint32 realtime
real-time (microseconds) since start of file this block should be triggered at
Definition: midifile.hpp:26
uint32 ticktime
tick number since start of file this tempo change occurs at
Definition: midifile.hpp:31
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1041
static bool ReadSMFHeader(const char *filename, SMFHeader &header)
Read the header of a standard MIDI file.
Definition: midifile.cpp:409
uint32 ticktime
tick number since start of file this block should be triggered at
Definition: midifile.hpp:25
bool WriteSMF(const char *filename)
Write a Standard MIDI File containing the decoded music.
Definition: midifile.cpp:911