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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
/*
* libADLMIDI is a free MIDI to WAV conversion library with OPL3 emulation
*
* Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma <bisqwit@iki.fi>
* ADLMIDI Library API: Copyright (c) 2017 Vitaly Novichkov <admin@wohlnet.ru>
*
* Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation:
* http://iki.fi/bisqwit/source/adlmidi.html
*
* This program 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, either version 3 of the License, or
* any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "adlmidi_private.hpp"
#include "adlmidi_mus2mid.h"
#include "adlmidi_xmi2mid.h"
uint64_t MIDIplay::ReadBEint(const void *buffer, size_t nbytes)
{
uint64_t result = 0;
const unsigned char *data = reinterpret_cast<const unsigned char *>(buffer);
for(unsigned n = 0; n < nbytes; ++n)
result = (result << 8) + data[n];
return result;
}
uint64_t MIDIplay::ReadLEint(const void *buffer, size_t nbytes)
{
uint64_t result = 0;
const unsigned char *data = reinterpret_cast<const unsigned char *>(buffer);
for(unsigned n = 0; n < nbytes; ++n)
result = result + static_cast<uint64_t>(data[n] << (n * 8));
return result;
}
bool MIDIplay::LoadBank(const std::string &filename)
{
fileReader file;
file.openFile(filename.c_str());
return LoadBank(file);
}
bool MIDIplay::LoadBank(void *data, unsigned long size)
{
fileReader file;
file.openData(data, size);
return LoadBank(file);
}
/* WOPL-needed misc functions */
static uint16_t toUint16LE(const uint8_t *arr)
{
uint16_t num = arr[0];
num |= ((arr[1] << 8) & 0xFF00);
return num;
}
static uint16_t toUint16BE(const uint8_t *arr)
{
uint16_t num = arr[1];
num |= ((arr[0] << 8) & 0xFF00);
return num;
}
static int16_t toSint16BE(const uint8_t *arr)
{
int16_t num = *reinterpret_cast<const int8_t *>(&arr[0]);
num *= 1 << 8;
num |= arr[1];
return num;
}
static const char *wopl3_magic = "WOPL3-BANK\0";
static const uint16_t wopl_latest_version = 2;
enum WOPL_InstrumentFlags
{
WOPL_Flags_NONE = 0,
WOPL_Flag_Enable4OP = 0x01,
WOPL_Flag_Pseudo4OP = 0x02
};
struct WOPL_Inst
{
bool fourOps;
char padding[7];
struct adlinsdata adlins;
struct adldata op[2];
};
static bool readInstrument(MIDIplay::fileReader &file, WOPL_Inst &ins, bool isPercussion = false)
{
uint8_t idata[62];
if(file.read(idata, 1, 62) != 62)
return false;
//strncpy(ins.name, char_p(idata), 32);
ins.op[0].finetune = (int8_t)toSint16BE(idata + 32);
ins.op[1].finetune = (int8_t)toSint16BE(idata + 34);
//ins.velocity_offset = int8_t(idata[36]);
ins.adlins.voice2_fine_tune = 0.0;
int8_t voice2_fine_tune = int8_t(idata[37]);
if(voice2_fine_tune != 0)
{
if(voice2_fine_tune == 1)
ins.adlins.voice2_fine_tune = 0.000025;
else if(voice2_fine_tune == -1)
ins.adlins.voice2_fine_tune = -0.000025;
else
ins.adlins.voice2_fine_tune = ((voice2_fine_tune * 15.625) / 1000.0);
}
ins.adlins.tone = isPercussion ? idata[38] : 0;
/* TODO: add those fields into next version of WOPL format
* and re-generate those values on file save! */
ins.adlins.ms_sound_kon = 1000;
ins.adlins.ms_sound_koff = 500;
/* ----------------------------------------------------- */
uint8_t flags = idata[39];
ins.adlins.flags = (flags & WOPL_Flag_Enable4OP) && (flags & WOPL_Flag_Pseudo4OP) ? adlinsdata::Flag_Pseudo4op : 0;
ins.fourOps = (flags & WOPL_Flag_Enable4OP) || (flags & WOPL_Flag_Pseudo4OP);
ins.op[0].feedconn = (idata[40]);
ins.op[1].feedconn = (idata[41]);
for(size_t op = 0, slt = 0; op < 4; op++, slt++)
{
size_t off = 42 + size_t(op) * 5;
// ins.setAVEKM(op, idata[off + 0]);//AVEKM
// ins.setAtDec(op, idata[off + 2]);//AtDec
// ins.setSusRel(op, idata[off + 3]);//SusRel
// ins.setWaveForm(op, idata[off + 4]);//WaveForm
// ins.setKSLL(op, idata[off + 1]);//KSLL
ins.op[slt].carrier_E862 =
((static_cast<uint32_t>(idata[off + 4]) << 24) & 0xFF000000) //WaveForm
| ((static_cast<uint32_t>(idata[off + 3]) << 16) & 0x00FF0000) //SusRel
| ((static_cast<uint32_t>(idata[off + 2]) << 8) & 0x0000FF00) //AtDec
| ((static_cast<uint32_t>(idata[off + 0]) << 0) & 0x000000FF); //AVEKM
ins.op[slt].carrier_40 = idata[off + 1];//KSLL
op++;
off = 42 + size_t(op) * 5;
ins.op[slt].modulator_E862 =
((static_cast<uint32_t>(idata[off + 4]) << 24) & 0xFF000000) //WaveForm
| ((static_cast<uint32_t>(idata[off + 3]) << 16) & 0x00FF0000) //SusRel
| ((static_cast<uint32_t>(idata[off + 2]) << 8) & 0x0000FF00) //AtDec
| ((static_cast<uint32_t>(idata[off + 0]) << 0) & 0x000000FF); //AVEKM
ins.op[slt].modulator_40 = idata[off + 1];//KSLL
}
return true;
}
bool MIDIplay::LoadBank(MIDIplay::fileReader &fr)
{
size_t fsize;
ADL_UNUSED(fsize);
if(!fr.isValid())
{
errorStringOut = "Custom bank: Invalid data stream!";
return false;
}
char magic[32];
std::memset(magic, 0, 32);
uint16_t version = 0;
uint16_t count_melodic_banks = 1;
uint16_t count_percusive_banks = 1;
if(fr.read(magic, 1, 11) != 11)
{
errorStringOut = "Custom bank: Can't read magic number!";
return false;
}
if(std::strncmp(magic, wopl3_magic, 11) != 0)
{
errorStringOut = "Custom bank: Invalid magic number!";
return false;
}
uint8_t version_raw[2];
if(fr.read(version_raw, 1, 2) != 2)
{
errorStringOut = "Custom bank: Can't read version!";
return false;
}
version = toUint16LE(version_raw);
if(version > wopl_latest_version)
{
errorStringOut = "Custom bank: Unsupported WOPL version!";
return false;
}
uint8_t head[6];
std::memset(head, 0, 6);
if(fr.read(head, 1, 6) != 6)
{
errorStringOut = "Custom bank: Can't read header!";
return false;
}
count_melodic_banks = toUint16BE(head);
count_percusive_banks = toUint16BE(head + 2);
if((count_melodic_banks < 1) || (count_percusive_banks < 1))
{
errorStringOut = "Custom bank: Too few banks in this file!";
return false;
}
/*UNUSED YET*/
bool default_deep_vibrato = ((head[4]>>0) & 0x01);
bool default_deep_tremolo = ((head[4]>>1) & 0x01);
//5'th byte reserved for Deep-Tremolo and Deep-Vibrato flags
m_setup.HighTremoloMode = default_deep_tremolo;
m_setup.HighVibratoMode = default_deep_vibrato;
//6'th byte reserved for ADLMIDI's default volume model
m_setup.VolumeModel = (int)head[5];
opl.dynamic_melodic_banks.clear();
opl.dynamic_percussion_banks.clear();
opl.setEmbeddedBank(m_setup.AdlBank);
if(version >= 2)//Read bank meta-entries
{
for(uint16_t i = 0; i < count_melodic_banks; i++)
{
uint8_t bank_meta[34];
if(fr.read(bank_meta, 1, 34) != 34)
{
errorStringOut = "Custom bank: Fail to read melodic bank meta-data!";
return false;
}
uint16_t bank = uint16_t(bank_meta[33]) * 256 + uint16_t(bank_meta[32]);
size_t offset = opl.dynamic_melodic_banks.size();
opl.dynamic_melodic_banks[bank] = offset;
//strncpy(bankMeta.name, char_p(bank_meta), 32);
//bankMeta.lsb = bank_meta[32];
//bankMeta.msb = bank_meta[33];
}
for(uint16_t i = 0; i < count_percusive_banks; i++)
{
uint8_t bank_meta[34];
if(fr.read(bank_meta, 1, 34) != 34)
{
errorStringOut = "Custom bank: Fail to read melodic bank meta-data!";
return false;
}
uint16_t bank = uint16_t(bank_meta[33]) * 256 + uint16_t(bank_meta[32]);
size_t offset = opl.dynamic_percussion_banks.size();
opl.dynamic_percussion_banks[bank] = offset;
//strncpy(bankMeta.name, char_p(bank_meta), 32);
//bankMeta.lsb = bank_meta[32];
//bankMeta.msb = bank_meta[33];
}
}
uint16_t total = 128 * count_melodic_banks;
bool readPercussion = false;
tryAgain:
for(uint16_t i = 0; i < total; i++)
{
WOPL_Inst ins;
std::memset(&ins, 0, sizeof(WOPL_Inst));
if(!readInstrument(fr, ins, readPercussion))
{
opl.setEmbeddedBank(m_setup.AdlBank);
errorStringOut = "Custom bank: Fail to read instrument!";
return false;
}
/*
* 0..127 - melodic, 128...255 - percussion.
* TODO: Make separated melodic and drum arrays and make MIDI bank ID support
*/
ins.adlins.adlno1 = static_cast<uint16_t>(opl.dynamic_instruments.size() | opl.DynamicInstrumentTag);
opl.dynamic_instruments.push_back(ins.op[0]);
ins.adlins.adlno2 = ins.adlins.adlno1;
if(ins.fourOps)
{
ins.adlins.adlno2 = static_cast<uint16_t>(opl.dynamic_instruments.size() | opl.DynamicInstrumentTag);
opl.dynamic_instruments.push_back(ins.op[1]);
}
opl.dynamic_metainstruments.push_back(ins.adlins);
}
if(!readPercussion)
{
total = 128 * count_percusive_banks;
readPercussion = true;
goto tryAgain;
}
opl.AdlBank = ~0u; // Use dynamic banks!
//Percussion offset is count of instruments multipled to count of melodic banks
opl.dynamic_percussion_offset = 128 * count_melodic_banks;
return true;
}
bool MIDIplay::LoadMIDI(const std::string &filename)
{
fileReader file;
file.openFile(filename.c_str());
if(!LoadMIDI(file))
return false;
return true;
}
bool MIDIplay::LoadMIDI(void *data, unsigned long size)
{
fileReader file;
file.openData(data, size);
return LoadMIDI(file);
}
bool MIDIplay::LoadMIDI(MIDIplay::fileReader &fr)
{
size_t fsize;
ADL_UNUSED(fsize);
//! Temp buffer for conversion
AdlMIDI_CPtr<uint8_t> cvt_buf;
errorString.clear();
#ifdef DISABLE_EMBEDDED_BANKS
if((opl.AdlBank != ~0u) || (opl.dynamic_metainstruments.size() < 256))
{
ADLMIDI_ErrorString = "Bank is not set! Please load any instruments bank by using of adl_openBankFile() or adl_openBankData() functions!";
return false;
}
#endif
if(!fr.isValid())
{
errorStringOut = "Invalid data stream!\n";
#ifndef _WIN32
errorStringOut += std::strerror(errno);
#endif
return false;
}
/**** Set all properties BEFORE starting of actial file reading! ****/
m_setup.stored_samples = 0;
m_setup.backup_samples_size = 0;
opl.HighTremoloMode = m_setup.HighTremoloMode == -1 ? adlbanksetup[m_setup.AdlBank].deepTremolo : (bool)m_setup.HighTremoloMode;
opl.HighVibratoMode = m_setup.HighVibratoMode == -1 ? adlbanksetup[m_setup.AdlBank].deepVibrato : (bool)m_setup.HighVibratoMode;
opl.AdlPercussionMode = m_setup.AdlPercussionMode == -1 ? adlbanksetup[m_setup.AdlBank].adLibPercussions : (bool)m_setup.AdlPercussionMode;
opl.ScaleModulators = m_setup.ScaleModulators == -1 ? adlbanksetup[m_setup.AdlBank].scaleModulators : (bool)m_setup.ScaleModulators;
opl.LogarithmicVolumes = m_setup.LogarithmicVolumes;
//opl.CartoonersVolumes = false;
opl.m_musicMode = OPL3::MODE_MIDI;
opl.ChangeVolumeRangesModel(static_cast<ADLMIDI_VolumeModels>(m_setup.VolumeModel));
if(m_setup.VolumeModel == ADLMIDI_VolumeModel_AUTO)//Use bank default volume model
opl.m_volumeScale = (OPL3::VolumesScale)adlbanksetup[m_setup.AdlBank].volumeModel;
opl.NumCards = m_setup.NumCards;
opl.NumFourOps = m_setup.NumFourOps;
cmf_percussion_mode = false;
opl.Reset(m_setup.PCM_RATE);
atEnd = false;
loopStart = true;
invalidLoop = false;
bool is_GMF = false; // GMD/MUS files (ScummVM)
//bool is_MUS = false; // MUS/DMX files (Doom)
bool is_IMF = false; // IMF
bool is_CMF = false; // Creative Music format (CMF/CTMF)
bool is_RSXX = false; // RSXX, such as Cartooners
const size_t HeaderSize = 4 + 4 + 2 + 2 + 2; // 14
char HeaderBuf[HeaderSize] = "";
size_t DeltaTicks = 192, TrackCount = 1;
riffskip:
fsize = fr.read(HeaderBuf, 1, HeaderSize);
if(std::memcmp(HeaderBuf, "RIFF", 4) == 0)
{
fr.seek(6l, SEEK_CUR);
goto riffskip;
}
if(std::memcmp(HeaderBuf, "GMF\x1", 4) == 0)
{
// GMD/MUS files (ScummVM)
fr.seek(7 - static_cast<long>(HeaderSize), SEEK_CUR);
is_GMF = true;
}
else if(std::memcmp(HeaderBuf, "MUS\x1A", 4) == 0)
{
// MUS/DMX files (Doom)
//uint64_t start = ReadLEint(HeaderBuf + 6, 2);
//is_MUS = true;
fr.seek(0, SEEK_END);
size_t mus_len = fr.tell();
fr.seek(0, SEEK_SET);
uint8_t *mus = (uint8_t *)malloc(mus_len);
if(!mus)
{
errorStringOut = "Out of memory!";
return false;
}
fr.read(mus, 1, mus_len);
//Close source stream
fr.close();
uint8_t *mid = NULL;
uint32_t mid_len = 0;
int m2mret = AdlMidi_mus2midi(mus, static_cast<uint32_t>(mus_len),
&mid, &mid_len, 0);
if(mus) free(mus);
if(m2mret < 0)
{
errorStringOut = "Invalid MUS/DMX data format!";
return false;
}
cvt_buf.reset(mid);
//Open converted MIDI file
fr.openData(mid, static_cast<size_t>(mid_len));
//Re-Read header again!
goto riffskip;
}
else if(std::memcmp(HeaderBuf, "FORM", 4) == 0)
{
if(std::memcmp(HeaderBuf + 8, "XDIR", 4) != 0)
{
fr.close();
errorStringOut = fr._fileName + ": Invalid format\n";
return false;
}
fr.seek(0, SEEK_END);
size_t mus_len = fr.tell();
fr.seek(0, SEEK_SET);
uint8_t *mus = (uint8_t *)malloc(mus_len);
if(!mus)
{
errorStringOut = "Out of memory!";
return false;
}
fr.read(mus, 1, mus_len);
//Close source stream
fr.close();
uint8_t *mid = NULL;
uint32_t mid_len = 0;
int m2mret = AdlMidi_xmi2midi(mus, static_cast<uint32_t>(mus_len),
&mid, &mid_len, XMIDI_CONVERT_NOCONVERSION);
if(mus) free(mus);
if(m2mret < 0)
{
errorStringOut = "Invalid XMI data format!";
return false;
}
cvt_buf.reset(mid);
//Open converted MIDI file
fr.openData(mid, static_cast<size_t>(mid_len));
//Re-Read header again!
goto riffskip;
}
else if(std::memcmp(HeaderBuf, "CTMF", 4) == 0)
{
opl.dynamic_instruments.clear();
opl.dynamic_metainstruments.clear();
// Creative Music Format (CMF).
// When playing CTMF files, use the following commandline:
// adlmidi song8.ctmf -p -v 1 1 0
// i.e. enable percussion mode, deeper vibrato, and use only 1 card.
is_CMF = true;
//unsigned version = ReadLEint(HeaderBuf+4, 2);
uint64_t ins_start = ReadLEint(HeaderBuf + 6, 2);
uint64_t mus_start = ReadLEint(HeaderBuf + 8, 2);
//unsigned deltas = ReadLEint(HeaderBuf+10, 2);
uint64_t ticks = ReadLEint(HeaderBuf + 12, 2);
// Read title, author, remarks start offsets in file
fr.read(HeaderBuf, 1, 6);
//unsigned long notes_starts[3] = {ReadLEint(HeaderBuf+0,2),ReadLEint(HeaderBuf+0,4),ReadLEint(HeaderBuf+0,6)};
fr.seek(16, SEEK_CUR); // Skip the channels-in-use table
fr.read(HeaderBuf, 1, 4);
uint64_t ins_count = ReadLEint(HeaderBuf + 0, 2); //, basictempo = ReadLEint(HeaderBuf+2, 2);
fr.seek(static_cast<long>(ins_start), SEEK_SET);
//std::printf("%u instruments\n", ins_count);
for(unsigned i = 0; i < ins_count; ++i)
{
unsigned char InsData[16];
fr.read(InsData, 1, 16);
/*std::printf("Ins %3u: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
i, InsData[0],InsData[1],InsData[2],InsData[3], InsData[4],InsData[5],InsData[6],InsData[7],
InsData[8],InsData[9],InsData[10],InsData[11], InsData[12],InsData[13],InsData[14],InsData[15]);*/
struct adldata adl;
struct adlinsdata adlins;
adl.modulator_E862 =
((static_cast<uint32_t>(InsData[8] & 0x07) << 24) & 0xFF000000) //WaveForm
| ((static_cast<uint32_t>(InsData[6]) << 16) & 0x00FF0000) //Sustain/Release
| ((static_cast<uint32_t>(InsData[4]) << 8) & 0x0000FF00) //Attack/Decay
| ((static_cast<uint32_t>(InsData[0]) << 0) & 0x000000FF); //MultKEVA
adl.carrier_E862 =
((static_cast<uint32_t>(InsData[9] & 0x07) << 24) & 0xFF000000) //WaveForm
| ((static_cast<uint32_t>(InsData[7]) << 16) & 0x00FF0000) //Sustain/Release
| ((static_cast<uint32_t>(InsData[5]) << 8) & 0x0000FF00) //Attack/Decay
| ((static_cast<uint32_t>(InsData[1]) << 0) & 0x000000FF); //MultKEVA
adl.modulator_40 = InsData[2];
adl.carrier_40 = InsData[3];
adl.feedconn = InsData[10] & 0x0F;
adl.finetune = 0;
adlins.adlno1 = static_cast<uint16_t>(opl.dynamic_instruments.size() | opl.DynamicInstrumentTag);
adlins.adlno2 = adlins.adlno1;
adlins.ms_sound_kon = 1000;
adlins.ms_sound_koff = 500;
adlins.tone = 0;
adlins.flags = 0;
adlins.voice2_fine_tune = 0.0;
opl.dynamic_metainstruments.push_back(adlins);
opl.dynamic_instruments.push_back(adl);
}
fr.seeku(mus_start, SEEK_SET);
TrackCount = 1;
DeltaTicks = (size_t)ticks;
opl.AdlBank = ~0u; // Ignore AdlBank number, use dynamic banks instead
//std::printf("CMF deltas %u ticks %u, basictempo = %u\n", deltas, ticks, basictempo);
opl.LogarithmicVolumes = true;
opl.AdlPercussionMode = true;
opl.m_musicMode = OPL3::MODE_CMF;
opl.m_volumeScale = OPL3::VOLUME_CMF;
}
else
{
// Try to identify RSXX format
if(HeaderBuf[0] == 0x7D)
{
fr.seek(0x6D, SEEK_SET);
fr.read(HeaderBuf, 6, 1);
if(std::memcmp(HeaderBuf, "rsxx}u", 6) == 0)
{
is_RSXX = true;
fr.seek(0x7D, SEEK_SET);
TrackCount = 1;
DeltaTicks = 60;
opl.LogarithmicVolumes = true;
//opl.CartoonersVolumes = true;
opl.m_musicMode = OPL3::MODE_RSXX;
opl.m_volumeScale = OPL3::VOLUME_CMF;
}
}
// Try parsing as an IMF file
if(!is_RSXX)
{
do
{
uint8_t raw[4];
size_t end = static_cast<uint8_t>(HeaderBuf[0]) + 256 * static_cast<uint8_t>(HeaderBuf[1]);
if(!end || (end & 3))
break;
size_t backup_pos = fr.tell();
int64_t sum1 = 0, sum2 = 0;
fr.seek(2, SEEK_SET);
for(unsigned n = 0; n < 42; ++n)
{
if(fr.read(raw, 1, 4) != 4)
break;
int64_t value1 = raw[0];
value1 += raw[1] << 8;
sum1 += value1;
int64_t value2 = raw[2];
value2 += raw[3] << 8;
sum2 += value2;
}
fr.seek(static_cast<long>(backup_pos), SEEK_SET);
if(sum1 > sum2)
{
is_IMF = true;
DeltaTicks = 1;
}
} while(false);
}
if(!is_IMF && !is_RSXX)
{
if(std::memcmp(HeaderBuf, "MThd\0\0\0\6", 8) != 0)
{
fr.close();
errorStringOut = fr._fileName + ": Invalid format, Header signature is unknown!\n";
return false;
}
/*size_t Fmt = ReadBEint(HeaderBuf + 8, 2);*/
TrackCount = (size_t)ReadBEint(HeaderBuf + 10, 2);
DeltaTicks = (size_t)ReadBEint(HeaderBuf + 12, 2);
}
}
TrackData.clear();
TrackData.resize(TrackCount, std::vector<uint8_t>());
//CurrentPosition.track.clear();
//CurrentPosition.track.resize(TrackCount);
InvDeltaTicks = fraction<uint64_t>(1, 1000000l * static_cast<uint64_t>(DeltaTicks));
//Tempo = 1000000l * InvDeltaTicks;
Tempo = fraction<uint64_t>(1, static_cast<uint64_t>(DeltaTicks));
static const unsigned char EndTag[4] = {0xFF, 0x2F, 0x00, 0x00};
size_t totalGotten = 0;
for(size_t tk = 0; tk < TrackCount; ++tk)
{
// Read track header
size_t TrackLength;
if(is_IMF)
{
//std::fprintf(stderr, "Reading IMF file...\n");
size_t end = static_cast<uint8_t>(HeaderBuf[0]) + 256 * static_cast<uint8_t>(HeaderBuf[1]);
unsigned IMF_tempo = 1428;
static const unsigned char imf_tempo[] = {0x0,//Zero delay!
MidiEvent::T_SPECIAL, MidiEvent::ST_TEMPOCHANGE, 0x4,
static_cast<uint8_t>(IMF_tempo >> 24),
static_cast<uint8_t>(IMF_tempo >> 16),
static_cast<uint8_t>(IMF_tempo >> 8),
static_cast<uint8_t>(IMF_tempo)
};
TrackData[tk].insert(TrackData[tk].end(), imf_tempo, imf_tempo + sizeof(imf_tempo));
TrackData[tk].push_back(0x00);
fr.seek(2, SEEK_SET);
while(fr.tell() < end && !fr.eof())
{
uint8_t special_event_buf[5];
uint8_t raw[4];
special_event_buf[0] = MidiEvent::T_SPECIAL;
special_event_buf[1] = MidiEvent::ST_RAWOPL;
special_event_buf[2] = 0x02;
if(fr.read(raw, 1, 4) != 4)
break;
special_event_buf[3] = raw[0]; // port index
special_event_buf[4] = raw[1]; // port value
uint32_t delay = static_cast<uint32_t>(raw[2]);
delay += 256 * static_cast<uint32_t>(raw[3]);
totalGotten += 4;
//if(special_event_buf[3] <= 8) continue;
//fprintf(stderr, "Put %02X <- %02X, plus %04X delay\n", special_event_buf[3],special_event_buf[4], delay);
TrackData[tk].insert(TrackData[tk].end(), special_event_buf, special_event_buf + 5);
//if(delay>>21) TrackData[tk].push_back( 0x80 | ((delay>>21) & 0x7F ) );
if(delay >> 14)
TrackData[tk].push_back(0x80 | ((delay >> 14) & 0x7F));
if(delay >> 7)
TrackData[tk].push_back(0x80 | ((delay >> 7) & 0x7F));
TrackData[tk].push_back(((delay >> 0) & 0x7F));
}
TrackData[tk].insert(TrackData[tk].end(), EndTag + 0, EndTag + 4);
//CurrentPosition.track[tk].delay = 0;
//CurrentPosition.began = true;
//std::fprintf(stderr, "Done reading IMF file\n");
opl.NumFourOps = 0; //Don't use 4-operator channels for IMF playing!
opl.m_musicMode = OPL3::MODE_IMF;
}
else
{
// Take the rest of the file
if(is_GMF || is_CMF || is_RSXX)
{
size_t pos = fr.tell();
fr.seek(0, SEEK_END);
TrackLength = fr.tell() - pos;
fr.seek(static_cast<long>(pos), SEEK_SET);
}
//else if(is_MUS) // Read TrackLength from file position 4
//{
// size_t pos = fr.tell();
// fr.seek(4, SEEK_SET);
// TrackLength = static_cast<size_t>(fr.getc());
// TrackLength += static_cast<size_t>(fr.getc() << 8);
// fr.seek(static_cast<long>(pos), SEEK_SET);
//}
else
{
fsize = fr.read(HeaderBuf, 1, 8);
if(std::memcmp(HeaderBuf, "MTrk", 4) != 0)
{
fr.close();
errorStringOut = fr._fileName + ": Invalid format, MTrk signature is not found!\n";
return false;
}
TrackLength = (size_t)ReadBEint(HeaderBuf + 4, 4);
}
// Read track data
TrackData[tk].resize(TrackLength);
fsize = fr.read(&TrackData[tk][0], 1, TrackLength);
totalGotten += fsize;
if(is_GMF/*|| is_MUS*/) // Note: CMF does include the track end tag.
TrackData[tk].insert(TrackData[tk].end(), EndTag + 0, EndTag + 4);
if(is_RSXX)//Finalize raw track data with a zero
TrackData[tk].push_back(0);
//bool ok = false;
//// Read next event time
//uint64_t tkDelay = ReadVarLenEx(tk, ok);
//if(ok)
// CurrentPosition.track[tk].delay = tkDelay;
//else
//{
// std::stringstream msg;
// msg << fr._fileName << ": invalid variable length in the track " << tk << "! (error code " << tkDelay << ")";
// ADLMIDI_ErrorString = msg.str();
// return false;
//}
}
}
for(size_t tk = 0; tk < TrackCount; ++tk)
totalGotten += TrackData[tk].size();
if(totalGotten == 0)
{
errorStringOut = fr._fileName + ": Empty track data";
return false;
}
//Build new MIDI events table (ALPHA!!!)
if(!buildTrackData())
{
errorStringOut = fr._fileName + ": MIDI data parsing error has occouped!\n" + errorString;
return false;
}
opl.Reset(m_setup.PCM_RATE); // Reset AdLib
//opl.Reset(); // ...twice (just in case someone misprogrammed OPL3 previously)
ch.clear();
ch.resize(opl.NumChannels);
return true;
}
|