-
Notifications
You must be signed in to change notification settings - Fork 1
/
AltMidsFileParser.cpp
296 lines (289 loc) · 7.62 KB
/
AltMidsFileParser.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
//#include <windows.h>
//#include <mmsystem.h>
#include <thread>
#include <chrono>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <fstream>
//#include <tchar.h>
#if __has_include("RtMidi17/include/libremidi/libremidi.hpp")
#define LIBREMIDI_HEADER_ONLY
#include "RtMidi17/include/libremidi/libremidi.hpp"
namespace rtmidi = libremidi;
#else
#include "rtmidi17/rtmidi17.hpp"
#endif
//HMIDIOUT midiDev;
extern void ParseMidsFile(std::string filename);
extern void StartMidiPlayback();
rtmidi::midi_out midiOut(libremidi::API::HAIKU_BMIDI2, "");
extern bool eot;
extern unsigned int devID;
#ifdef BTRMID_STANDALONE
unsigned int devID = 0;
#endif
bool paused = 0;
bool oneshotplay = false;
#pragma pack(push,1)
struct MidsDataHeader
{
char FourCC[4];
uint32_t chunkSize;
uint32_t blocks;
};
struct MidsEvent
{
int dwDeltaTime;
unsigned int dwEvent;
};
struct MidsStreamIDEvent
{
int dwDeltaTime;
int dwStreamID;
unsigned int dwEvent;
};
std::vector<MidsEvent> MidsEvents;
std::vector<MidsStreamIDEvent> MidsStreamIDEvents;
struct MidsHeader
{
char RIFFFourCC[4];
uint32_t RIFFFileSize;
char FourCC[4];
char fmtFourCC[4];
uint32_t headerSize;
uint32_t dwTimeFormat;
uint32_t cbMaxBuffer;
uint32_t dwFlags;
};
#pragma pack(pop)
MidsHeader header;
MidsDataHeader dataHeader;
bool shouldContinue = false;
bool eot = false;
int tempoPerBeat = 6000000;
int timeDiv = 96;
std::chrono::steady_clock curClock;
void SelectMidiDevice(int selection)
{
if (selection >= midiOut.get_port_count())
{
selection = 0;
}
midiOut.open_port(selection);
devID = selection;
}
void SelectMidiDevice()
{
auto cnt = midiOut.get_port_count();
for (uint32_t i = 0; i < cnt; i++)
{
std::cout << i << ". " << midiOut.get_port_name(i) << std::endl;
}
std::cout << "Select MIDI device: " << std::endl;
int selection;
std::cin >> selection;
devID = selection;
midiOut.open_port(selection);
}
#ifdef BTRMID_STANDALONE
void TerminateMidiPlayback()
{
}
int main(int argc, char *argv[])
{
if (argc > 1)
{
if (strncmp(argv[1],"exportmid",9))
{
}
}
std::cout << "Select MIDS file: " << std::endl;
std::string str;
std::cin >> str;
SelectMidiDevice();
ParseMidsFile(str);
StartMidiPlayback();
}
#endif
void QueueMidiEvent(MidsEvent midsevent)
{
//std::cout << "Delta time: " << midsevent.dwDeltaTime << std::endl;
while(paused) {}
//usleep(tempoPerBeat / timeDiv * midsevent.dwDeltaTime);
auto time = curClock.now().time_since_epoch().count();
while ((curClock.now().time_since_epoch().count() - time ) <= (tempoPerBeat * 1000ll) / (long long)timeDiv * midsevent.dwDeltaTime)
{
if (eot) return; // Cancel counting immediatly.
}
//std::this_thread::sleep_for(std::chrono::duration<unsigned long long,std::micro>(tempoPerBeat / timeDiv * midsevent.dwDeltaTime));
if (midsevent.dwEvent & (1 << 24))
{
tempoPerBeat = midsevent.dwEvent & 0x00FFFFFF;
std::cout << "New tempo/beat in microseconds: " << tempoPerBeat << std::endl;
return;
}
uint8_t bytes = 3;
if ((midsevent.dwEvent & 0xC0) == 0xC0
|| (midsevent.dwEvent & 0xD0) == 0xD0) bytes = 2;
midiOut.send_message((unsigned char*)&midsevent.dwEvent,bytes);
}
void QueueMidiEvent(MidsStreamIDEvent midsevent)
{
MidsEvent realMidsEvent{ midsevent.dwDeltaTime, midsevent.dwEvent};
return QueueMidiEvent(realMidsEvent);
}
std::string curFilename;
std::string& GetCurPlayingFilename()
{
return curFilename;
}
void ParseMidsFile(std::string filename)
{
//hdr = (MIDIHDR*)HeapAlloc(GetProcessHeap(), 4 | 8, sizeof(*hdr));
//memset((void*)hdr, 0, sizeof(*hdr));
for (unsigned char i = 0; i < 16; i++)
{
unsigned char firstByte = 0xB0;
firstByte |= i;
std::vector<unsigned char> msgVec = {firstByte,0x7B,0};
midiOut.send_message(msgVec);
}
curFilename = filename;
paused = false;
eot = false;
/*for (int i = 0; i < 16; i++)
{
for (int ii = 0; ii < 128; ii++)
midiOut.send_message(rtmidi::message::note_off(i,ii,0));
}*/
MidsEvents.clear();
MidsStreamIDEvents.clear();
std::fstream file = std::fstream(filename, std::ios::in | std::ios::out | std::ios::binary | std::ios::ate);
if (file.is_open())
{
auto size = file.tellg();
file.seekg(0, file.beg);
file.read((char*)&header, sizeof(header));
if (strncmp(header.RIFFFourCC, "RIFF", 4) == 0 && strncmp(header.FourCC, "MIDS", 4) == 0
&& strncmp(header.fmtFourCC, "fmt ", 4) == 0 && header.headerSize == 12)
{
timeDiv = header.dwTimeFormat;
std::cout << "MDS Time Division: " << header.dwTimeFormat << std::endl;
}
else
{
std::cout << "Bad MIDS file header" << std::endl;
#ifdef BTRMID_STANDALONE
exit(-1);
#endif
return;
}
file.read((char*)&dataHeader, sizeof(dataHeader));
while (file.tellg() != EOF)
{
uint32_t tickOffset = 0;
uint32_t blockSize = 2728;
for (int i = 0; i < dataHeader.blocks; i++)
{
file.read((char*)&tickOffset, 4);
file.read((char*)&blockSize, 4);
if (file.eof()) return; // End of parsing here.
std::cout << "Current Block Size: " << blockSize << std::endl;
std::cout << "Found at offset: " << file.tellg() << std::endl;
std::cout << "Current tick offset: " << tickOffset << std::endl;
if (header.dwFlags == 0) // 0 means Stream ID exists in the MDS File.
{
std::cout << "Total number of delta tick, StreamID and MIDI message pair in this block: " << blockSize / sizeof(MidsStreamIDEvent) << std::endl;
MidsStreamIDEvent* events = (MidsStreamIDEvent*)calloc(blockSize / sizeof(MidsStreamIDEvent), sizeof(MidsStreamIDEvent));
file.read((char*)events, blockSize);
for (int i = 0; i < blockSize / sizeof(MidsStreamIDEvent); i++)
{
MidsStreamIDEvents.push_back(events[i]);
}
//free(events);
}
else
{
std::cout << "Total number of delta tick and MIDI message pair in this block: " << blockSize / sizeof(MidsEvent) << std::endl;
MidsEvent* events = (MidsEvent*)calloc(blockSize / sizeof(MidsEvent), sizeof(MidsEvent));
file.read((char*)events, blockSize);
if (blockSize % 4)
{
std::cout << "Warning: Misaligned block" << std::endl;
}
for (int i = 0; i < blockSize / sizeof(MidsEvent); i++)
{
MidsEvents.push_back(events[i]);
}
//free(events);
}
}
}
}
else
{
std::cout << "Error: Failed to open file!" << std::endl;
#ifdef BTRMID_STANDALONE
exit(-1);
#endif
}
}
void StartMidiPlayback()
{
//midiOut.send_message(rtmidi::message::control_change(0,0x79,0));
//midiOut.send_message(rtmidi::message::control_change(0,0x7B,0));
unsigned int devID = 0;
#ifndef BTRMID_STANDALONE
while(1)
#endif
{
if (header.dwFlags == 0)
{
for (int i = 0; i < MidsStreamIDEvents.size(); i++)
{
while(paused)
{
if (eot) return;
}
QueueMidiEvent(MidsStreamIDEvents[i]);
if (eot) return;
}
}
else for (int i = 0; i < MidsEvents.size(); i++)
{
while(paused)
{
if (eot) return;
}
QueueMidiEvent(MidsEvents[i]);
if (eot) return;
}
if (oneshotplay) return;
}
}
void StopMidiPlayback()
{
paused = false;
eot = true;
//midiOut.send_message(rtmidi::message::control_change(0,0x79,0));
}
void PauseMidiPlayback()
{
/*for (int i = 0; i < 16; i++)
{
for (int ii = 0; ii < 128; ii++){}
//midiOut.send_message(rtmidi::message::note_off(i,ii,0));
}*/
for (unsigned char i = 0; i < 16; i++)
{
std::vector<unsigned char> midiMsg = {static_cast<unsigned char>(0xB0 | i),0x7B,0};
midiOut.send_message(midiMsg);
}
paused = true;
}
void ContinueMidiPlayback()
{
paused = false;
}