-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmidi_container.h
More file actions
282 lines (213 loc) · 8.14 KB
/
midi_container.h
File metadata and controls
282 lines (213 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef _MIDI_CONTAINER_H_
#define _MIDI_CONTAINER_H_
#include <stdint.h>
#include <string>
#include <vector>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define snprintf sprintf_s
#endif
struct midi_event
{
enum
{
max_static_data_count = 16
};
enum event_type
{
note_off = 0,
note_on,
polyphonic_aftertouch,
control_change,
program_change,
channel_aftertouch,
pitch_wheel,
extended
};
unsigned long m_timestamp;
event_type m_type;
unsigned m_channel;
unsigned long m_data_count;
uint8_t m_data[max_static_data_count];
std::vector<uint8_t> m_ext_data;
midi_event() : m_timestamp(0), m_type(note_off), m_channel(0), m_data_count(0) { }
midi_event( const midi_event & p_in );
midi_event( unsigned long p_timestamp, event_type p_type, unsigned p_channel, const uint8_t * p_data, std::size_t p_data_count );
unsigned long get_data_count() const;
void copy_data( uint8_t * p_out, unsigned long p_offset, unsigned long p_count ) const;
};
class midi_track
{
std::vector<midi_event> m_events;
public:
midi_track() { }
midi_track(const midi_track & p_in);
void add_event( const midi_event & p_event );
std::size_t get_count() const;
const midi_event & operator [] ( std::size_t p_index ) const;
midi_event & operator [] ( std::size_t p_index );
void remove_event( unsigned long index );
};
struct tempo_entry
{
unsigned long m_timestamp;
unsigned m_tempo;
tempo_entry() : m_timestamp(0), m_tempo(0) { }
tempo_entry(unsigned long p_timestamp, unsigned p_tempo);
};
class tempo_map
{
std::vector<tempo_entry> m_entries;
public:
void add_tempo( unsigned p_tempo, unsigned long p_timestamp );
double timestamp_to_seconds( unsigned long p_timestamp, unsigned p_dtx ) const;
std::size_t get_count() const;
const tempo_entry & operator [] ( std::size_t p_index ) const;
tempo_entry & operator [] ( std::size_t p_index );
};
struct system_exclusive_entry
{
std::size_t m_port;
std::size_t m_offset;
std::size_t m_length;
system_exclusive_entry() : m_port(0), m_offset(0), m_length(0) { }
system_exclusive_entry(const system_exclusive_entry & p_in);
system_exclusive_entry(std::size_t p_port, std::size_t p_offset, std::size_t p_length);
};
class system_exclusive_table
{
std::vector<uint8_t> m_data;
std::vector<system_exclusive_entry> m_entries;
public:
unsigned add_entry( const uint8_t * p_data, std::size_t p_size, std::size_t p_port );
void get_entry( unsigned p_index, const uint8_t * & p_data, std::size_t & p_size, std::size_t & p_port ) const;
};
struct midi_stream_event
{
double m_timestamp;
uint32_t m_event;
midi_stream_event() : m_timestamp(0), m_event(0) { }
midi_stream_event(double p_timestamp, uint32_t p_event);
};
struct midi_meta_data_item
{
double m_timestamp;
std::string m_name;
std::string m_value;
midi_meta_data_item() : m_timestamp(0) { }
midi_meta_data_item(const midi_meta_data_item & p_in);
midi_meta_data_item(double p_timestamp, const char * p_name, const char * p_value);
};
class midi_meta_data
{
std::vector<midi_meta_data_item> m_data;
std::vector<uint8_t> m_bitmap;
public:
midi_meta_data() { }
void add_item( const midi_meta_data_item & p_item );
void append( const midi_meta_data & p_data );
bool get_item( const char * p_name, midi_meta_data_item & p_out ) const;
bool get_bitmap( std::vector<uint8_t> & p_out );
void assign_bitmap( std::vector<uint8_t>::const_iterator const& begin, std::vector<uint8_t>::const_iterator const& end );
std::size_t get_count() const;
const midi_meta_data_item & operator [] ( std::size_t p_index ) const;
};
class midi_container
{
public:
enum
{
clean_flag_emidi = 1 << 0,
clean_flag_instruments = 1 << 1,
clean_flag_banks = 1 << 2,
};
private:
unsigned m_form;
unsigned m_dtx;
std::vector<uint64_t> m_channel_mask;
std::vector<tempo_map> m_tempo_map;
std::vector<midi_track> m_tracks;
std::vector<uint8_t> m_port_numbers;
std::vector< std::vector< std::string > > m_device_names;
midi_meta_data m_extra_meta_data;
uint16_t bank_offset;
std::vector<uint8_t> m_embedded_bank;
std::vector<unsigned long> m_timestamp_end;
std::vector<unsigned long> m_timestamp_loop_start;
std::vector<unsigned long> m_timestamp_loop_end;
double timestamp_to_seconds( unsigned long p_timestamp, unsigned long p_subsong ) const;
/*
* Normalize port numbers properly
*/
template <typename T> void limit_port_number(T & number)
{
for ( unsigned i = 0; i < m_port_numbers.size(); i++ )
{
if ( m_port_numbers[ i ] == number )
{
number = i;
return;
}
}
m_port_numbers.push_back( (const uint8_t) number );
number = m_port_numbers.size() - 1;
}
template <typename T> void limit_port_number(T & number) const
{
for ( unsigned i = 0; i < m_port_numbers.size(); i++ )
{
if ( m_port_numbers[ i ] == number )
{
number = i;
return;
}
}
}
public:
midi_container() { m_device_names.resize( 16 ); }
void initialize( unsigned p_form, unsigned p_dtx );
void add_track( const midi_track & p_track );
void add_track_event( std::size_t p_track_index, const midi_event & p_event );
/*
* These functions are really only designed to merge and later remove System Exclusive message dumps
*/
void merge_tracks( const midi_container & p_source );
void set_track_count( unsigned count );
void set_extra_meta_data( const midi_meta_data & p_data );
/*
* Blah.
* Hack 0: Remove channel 16
* Hack 1: Remove channels 11-16
*/
void apply_hackfix( unsigned hack );
void serialize_as_stream( unsigned long subsong, std::vector<midi_stream_event> & p_stream, system_exclusive_table & p_system_exclusive, unsigned long & loop_start, unsigned long & loop_end, unsigned clean_flags ) const;
void serialize_as_standard_midi_file( std::vector<uint8_t> & p_midi_file ) const;
void promote_to_type1();
void trim_start();
private:
void trim_range_of_tracks(unsigned long start, unsigned long end);
void trim_tempo_map(unsigned long p_index, unsigned long base_timestamp);
public:
typedef std::string(*split_callback)(uint8_t bank_msb, uint8_t bank_lsb, uint8_t instrument);
void split_by_instrument_changes(split_callback cb = NULL);
void sort_tracks_by_name();
void remove_tracks_with_no_notes();
unsigned long get_subsong_count() const;
unsigned long get_subsong( unsigned long p_index ) const;
double get_timestamp_end(unsigned long subsong, bool seconds = false) const;
unsigned get_format() const;
unsigned get_track_count() const;
unsigned get_channel_count(unsigned long subsong) const;
unsigned get_port_mask(unsigned long subsong) const;
static unsigned get_port_mask(const std::vector<midi_stream_event> & p_stream, const system_exclusive_table & p_sysex);
double get_timestamp_loop_start(unsigned long subsong, bool seconds = false) const;
double get_timestamp_loop_end(unsigned long subsong, bool seconds = false) const;
void get_meta_data( unsigned long subsong, midi_meta_data & p_out );
void scan_for_loops( bool p_xmi_loops, bool p_marker_loops, bool p_rpgmaker_loops, bool p_touhou_loops );
static void encode_delta( std::vector<uint8_t> & p_out, unsigned long delta );
bool get_embedded_bank( const uint8_t ** out, size_t * size, uint16_t *bank_offset ) const;
void assign_embedded_bank( const uint8_t *bank, size_t size, uint16_t bank_offset );
uint16_t scan_for_bank_offset( void ) const;
};
#endif