-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlSynthMidsFileParser.cpp
359 lines (347 loc) · 10.3 KB
/
FlSynthMidsFileParser.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// Created by caco345 on 9/4/20.
//
// FluidSynth interface based on the RtMidi17 interface.
//
#include <fluidsynth.h>
#include <string>
#include <cstring>
#include <vector>
#include <chrono>
#include <fstream>
#include <iostream>
extern void ParseMidsFile(std::string filename);
extern void StartMidiPlayback();
fluid_synth_t* player;
fluid_settings_t* settings;
fluid_audio_driver_t* audioDriver;
fluid_sequencer_t* sequencer;
fluid_seq_id_t seqId;
#ifdef __HAIKU__
#include <os/MediaKit.h>
static BSoundPlayer* sndplayer = nullptr;
static void BufProc(void* cookie, void *buffer, size_t size, const media_raw_audio_format &format)
{
fluid_synth_write_float(player, size / 8, buffer, 0, 2, buffer, 1, 2);
}
#endif
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;
std::string curFilename;
//MIDI enums.
enum
{
NOTE_OFF = 0x80,
NOTE_ON = 0x90,
POLY_PRESSURE = 0xA0,
CONTROL_CHANGE = 0xB0,
PROGRAM_CHANGE = 0xC0,
AFTERTOUCH = 0xD0,
PITCH_BEND = 0xE0,
};
std::string& GetCurPlayingFilename()
{
return curFilename;
}
void TerminateMidiPlayback()
{
#ifdef __HAIKU__
sndplayer->SetHasData(false);
sndplayer->Stop();
delete sndplayer;
#else
delete_fluid_audio_driver(audioDriver);
#endif
delete_fluid_synth(player);
delete_fluid_settings(settings);
}
void SelectMidiDevice()
{
settings = new_fluid_settings();
fluid_settings_setint(settings, "audio.period-size", 1024);
fluid_settings_setint(settings, "audio.periods", 1);
fluid_settings_setnum(settings, "synth.sample-rate", 48000);
player = new_fluid_synth(settings);
#ifndef __HAIKU__
audioDriver = new_fluid_audio_driver(settings,player);
#endif
auto res = fluid_synth_sfload(player,"./soundfont.sf2",1); // This one's for the Unixers...
if (res == FLUID_FAILED)
{
printf("Failed to load soundfont");
}
// sequencer = new_fluid_sequencer2(0);
// seqId = fluid_sequencer_register_fluidsynth(sequencer,player);
#ifdef __HAIKU__
media_raw_audio_format format{};
format.frame_rate = 48000;
format.format = media_raw_audio_format::B_AUDIO_FLOAT;
format.buffer_size = 960 * 2 * 4;
format.byte_order = B_MEDIA_LITTLE_ENDIAN;
format.channel_count = 2;
sndplayer = new BSoundPlayer(&format, NULL, BufProc, NULL, nullptr);
sndplayer->Start();
sndplayer->SetHasData(true);
#endif
}
void SelectMidiDevice(int selection)
{
return SelectMidiDevice();
}
void ParseMidsFile(std::string filename)
{
//hdr = (MIDIHDR*)HeapAlloc(GetProcessHeap(), 4 | 8, sizeof(*hdr));
//memset((void*)hdr, 0, sizeof(*hdr));
if (player)
{
fluid_synth_system_reset(player);
}
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 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 * 1000) / 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;
int status = midsevent.dwEvent & 0xF0;
int chan = midsevent.dwEvent & 0x0F;
int parm1 = (midsevent.dwEvent & 0xFF00) >> 8;
int parm2 = (midsevent.dwEvent & 0xFF0000) >> 16;
switch(status)
{
case NOTE_ON:
fluid_synth_noteon(player,chan,parm1,parm2);
break;
case NOTE_OFF:
fluid_synth_noteoff(player,chan,parm1);
break;
case CONTROL_CHANGE:
fluid_synth_cc(player,chan,parm1,parm2);
break;
case PROGRAM_CHANGE:
fluid_synth_program_change(player,chan,parm1);
break;
case AFTERTOUCH:
fluid_synth_channel_pressure(player,chan,parm1);
break;
case PITCH_BEND:
fluid_synth_pitch_bend(player,chan,(parm1 & 0x7f) | ((parm2 & 0x7f) << 7));
break;
}
}
void QueueMidiEvent(MidsStreamIDEvent midsevent)
{
MidsEvent realMidsEvent{ midsevent.dwDeltaTime, midsevent.dwEvent};
return QueueMidiEvent(realMidsEvent);
}
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;
fluid_synth_cc(player,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};
fluid_synth_cc(player,i,0x7B,0);
}
paused = true;
}
void ContinueMidiPlayback()
{
paused = false;
}
#ifdef BTRMID_STANDALONE
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