| 1 |
/* |
| 2 |
Copyright (c) 2009 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef CORSIX_TH_XMI2MID_H_ |
| 24 |
#define CORSIX_TH_XMI2MID_H_ |
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
// MIDI Events are from the MIDI 1.0 Spec |
| 30 |
// MIDI Meta Events are from the Standard MIDI File Spec 1.0 |
| 31 |
// Both specs are available from the MIDI Association website: |
| 32 |
// https://www.midi.org |
| 33 |
|
| 34 |
constexpr uint8_t midi_event_note_off = 0x80; |
| 35 |
constexpr uint8_t midi_event_note_on = 0x90; |
| 36 |
constexpr uint8_t midi_event_poly_key_pressure = 0xA0; |
| 37 |
constexpr uint8_t midi_event_control_change = 0xB0; |
| 38 |
constexpr uint8_t midi_event_program_change = 0xC0; |
| 39 |
constexpr uint8_t midi_event_channel_pressure = 0xD0; |
| 40 |
constexpr uint8_t midi_event_pitch_bend = 0xE0; |
| 41 |
|
| 42 |
constexpr uint8_t midi_event_sysex = 0xF0; |
| 43 |
constexpr uint8_t midi_event_end_of_sysex = 0xF7; |
| 44 |
constexpr uint8_t midi_event_meta = 0xFF; |
| 45 |
|
| 46 |
constexpr uint8_t midi_meta_event_end_of_track = 0x2F; |
| 47 |
constexpr uint8_t midi_meta_event_set_tempo = 0x51; |
| 48 |
|
| 49 |
constexpr uint8_t midi_control_code_volume = 7; |
| 50 |
|
| 51 |
constexpr uint8_t midi_channel_code_all_notes_off = 123; |
| 52 |
constexpr uint8_t midi_channel_code_all_sound_off = 120; |
| 53 |
constexpr uint8_t midi_channel_code_reset_controllers = 121; |
| 54 |
|
| 55 |
namespace { |
| 56 |
|
| 57 |
// Multiply the time duration of XMI events and the tempo by this factor. |
| 58 |
// The idea is to reduce the rounding errors that occur when converting from |
| 59 |
// XMI's 120Hz timing to MIDI's microseconds per quarter note timing. |
| 60 |
// Larger values reduce rounding errors but also increase the chance of hitting |
| 61 |
// the maximum MIDI division of 0x7FFF (32767). |
| 62 |
// |
| 63 |
// Experimentally 100 results in a division of 10000 for the fastest tempo in |
| 64 |
// Theme Hospital. Many but not all tracks have exact timing with a value of 1. |
| 65 |
constexpr int time_multiplier = 100; |
| 66 |
|
| 67 |
} // namespace |
| 68 |
|
| 69 |
// There are 120 ticks per second in XMI timing (which reduces to 3 / 25000us). |
| 70 |
constexpr double xmi_ticks_per_microsecond = (3.0 * time_multiplier) / 25000.0; |
| 71 |
constexpr double xmi_microseconds_per_tick = 25000.0 / (3.0 * time_multiplier); |
| 72 |
|
| 73 |
struct midi_token { |
| 74 |
int time; |
| 75 |
std::vector<uint8_t> buffer{}; |
| 76 |
uint8_t type; |
| 77 |
uint8_t data{0}; |
| 78 |
|
| 79 |
midi_token(int time, uint8_t type) : time(time), type(type) {} |
| 80 |
}; |
| 81 |
|
| 82 |
bool operator<(const midi_token& oLeft, const midi_token& oRight); |
| 83 |
|
| 84 |
using midi_token_list = std::vector<midi_token>; |
| 85 |
|
| 86 |
midi_token_list xmi_to_midi_token_list(const unsigned char* xmi_data, |
| 87 |
size_t xmi_length, uint32_t& iTempo); |
| 88 |
|
| 89 |
uint8_t* transcode_xmi_to_midi(const unsigned char* xmi_data, size_t xmi_length, |
| 90 |
size_t* midi_length); |
| 91 |
|
| 92 |
#endif // CORSIX_TH_XMI2MID_H_ |