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
|
#ifndef LOAD_OP2_H
#define LOAD_OP2_H
#include "../progs_cache.h"
#ifndef _MSC_VER
#define PACKED_STRUCT __attribute__((packed))
#else
#define PACKED_STRUCT
#endif
#ifdef _MSC_VER
#pragma pack(push,1)
#endif
struct Doom_OPL2instrument
{
unsigned char trem_vibr_1; /* OP 1: tremolo/vibrato/sustain/KSR/multi */
unsigned char att_dec_1; /* OP 1: attack rate/decay rate */
unsigned char sust_rel_1; /* OP 1: sustain level/release rate */
unsigned char wave_1; /* OP 1: waveform select */
unsigned char scale_1; /* OP 1: key scale level */
unsigned char level_1; /* OP 1: output level */
unsigned char feedback; /* feedback/AM-FM (both operators) */
unsigned char trem_vibr_2; /* OP 2: tremolo/vibrato/sustain/KSR/multi */
unsigned char att_dec_2; /* OP 2: attack rate/decay rate */
unsigned char sust_rel_2; /* OP 2: sustain level/release rate */
unsigned char wave_2; /* OP 2: waveform select */
unsigned char scale_2; /* OP 2: key scale level */
unsigned char level_2; /* OP 2: output level */
unsigned char unused;
short basenote; /* base note offset */
} PACKED_STRUCT;
struct Doom_opl_instr
{
unsigned short flags;
#define FL_FIXED_PITCH 0x0001 // note has fixed pitch (drum note)
#define FL_UNKNOWN 0x0002 // ??? (used in instrument #65 only)
#define FL_DOUBLE_VOICE 0x0004 // use two voices instead of one
unsigned char finetune;
unsigned char note;
struct Doom_OPL2instrument patchdata[2];
} PACKED_STRUCT;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
bool BankFormats::LoadDoom(BanksDump &db, const char *fn, unsigned bank, const std::string &bankTitle, const char *prefix)
{
#ifdef HARD_BANKS
writeIni("OP2", fn, prefix, bank, INI_Both);
#endif
FILE *fp = std::fopen(fn, "rb");
if(!fp)
return false;
std::fseek(fp, 0, SEEK_END);
std::vector<unsigned char> data(size_t(std::ftell(fp)));
std::rewind(fp);
if(std::fread(&data[0], 1, data.size(), fp) != data.size())
{
std::fclose(fp);
return false;
}
std::fclose(fp);
size_t bankDb = db.initBank(bank, bankTitle, BanksDump::BankEntry::SETUP_DMX);
BanksDump::MidiBank bnkMelodique;
BanksDump::MidiBank bnkPercussion;
for(unsigned a = 0; a < 175; ++a)
{
const size_t offset1 = 0x18A4 + a * 32;
const size_t offset2 = 8 + a * 36;
BanksDump::MidiBank &bnk = a < 128 ? bnkMelodique : bnkPercussion;
BanksDump::InstrumentEntry inst;
BanksDump::Operator ops[5];
std::string name;
for(unsigned p = 0; p < 32; ++p)
if(data[offset1] != '\0')
name += char(data[offset1 + p]);
//printf("%3d %3d %3d %8s: ", a,b,c, name.c_str());
int gmno = int(a < 128 ? a : ((a | 128) + 35));
size_t patchId = a < 128 ? a : ((a - 128) + 35);
char name2[512];
snprintf(name2, 512, "%s%c%u", prefix, (gmno < 128 ? 'M' : 'P'), gmno & 127);
Doom_opl_instr &ins = *(Doom_opl_instr *) &data[offset2];
insdata tmp[2] = {MakeNoSoundIns(), MakeNoSoundIns()};
tmp[0].diff = false;
tmp[1].diff = false;
for(size_t index = 0; index < 2; ++index)
{
const Doom_OPL2instrument &src = ins.patchdata[index];
tmp[index].data[0] = src.trem_vibr_1;
tmp[index].data[1] = src.trem_vibr_2;
tmp[index].data[2] = src.att_dec_1;
tmp[index].data[3] = src.att_dec_2;
tmp[index].data[4] = src.sust_rel_1;
tmp[index].data[5] = src.sust_rel_2;
tmp[index].data[6] = src.wave_1;
tmp[index].data[7] = src.wave_2;
tmp[index].data[8] = src.scale_1 | src.level_1;
tmp[index].data[9] = src.scale_2 | src.level_2;
tmp[index].data[10] = src.feedback;
tmp[index].finetune = int8_t(src.basenote + 12);
inst.fbConn |= (uint_fast16_t(src.feedback) << (a == 1 ? 8 : 0));
db.toOps(tmp[index], ops, index * 2);
}
struct ins tmp2;
tmp2.notenum = ins.note;
tmp2.pseudo4op = false;
tmp2.real4op = false;
tmp2.voice2_fine_tune = 0.0;
tmp2.midi_velocity_offset = 0;
tmp2.rhythmModeDrum = 0;
while(tmp2.notenum && tmp2.notenum < 20)
{
tmp2.notenum += 12;
tmp[0].finetune -= 12;
tmp[1].finetune -= 12;
}
inst.noteOffset1 = int8_t(tmp[0].finetune);
inst.noteOffset2 = int8_t(tmp[1].finetune);
if((ins.flags & FL_DOUBLE_VOICE) != 0)
inst.instFlags |= BanksDump::InstrumentEntry::WOPL_Ins_4op | BanksDump::InstrumentEntry::WOPL_Ins_Pseudo4op;
inst.percussionKeyNumber = tmp2.notenum;
inst.secondVoiceDetune = static_cast<char>(static_cast<int>(ins.finetune) - 128);
if(!(ins.flags & FL_DOUBLE_VOICE))
{
size_t resno = InsertIns(tmp[0], tmp2, std::string(1, '\377') + name, name2);
SetBank(bank, (unsigned int)gmno, resno);
}
else // Double instrument
{
tmp2.pseudo4op = true;
tmp2.voice2_fine_tune = (((double)ins.finetune - 128.0) * 15.625) / 1000.0;
if(ins.finetune == 129)
tmp2.voice2_fine_tune = 0.000025;
else if(ins.finetune == 127)
tmp2.voice2_fine_tune = -0.000025;
//printf("/*DOOM FINE TUNE (flags %000X instrument is %d) IS %d -> %lf*/\n", ins.flags, a, ins.finetune, tmp2.fine_tune);
size_t resno = InsertIns(tmp[0], tmp[1], tmp2, std::string(1, '\377') + name, name2);
SetBank(bank, (unsigned int)gmno, resno);
}
db.addInstrument(bnk, patchId, inst, ops);
/*const Doom_OPL2instrument& A = ins.patchdata[0];
const Doom_OPL2instrument& B = ins.patchdata[1];
printf(
"flags:%04X fine:%02X note:%02X\n"
"{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n"
" t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d}\n"
"{t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X f:%02X\n"
" t:%02X a:%02X s:%02X w:%02X s:%02X l:%02X ?:%02X base:%d} ",
ins.flags, ins.finetune, ins.note,
A.trem_vibr_1, A.att_dec_1, A.sust_rel_1,
A.wave_1, A.scale_1, A.level_1, A.feedback,
A.trem_vibr_2, A.att_dec_2, A.sust_rel_2,
A.wave_2, A.scale_2, A.level_2, A.unused, A.basenote,
B.trem_vibr_1, B.att_dec_1, B.sust_rel_1,
B.wave_1, B.scale_1, B.level_1, B.feedback,
B.trem_vibr_2, B.att_dec_2, B.sust_rel_2,
B.wave_2, B.scale_2, B.level_2, B.unused, B.basenote);
printf(" %s VS %s\n", name.c_str(), MidiInsName[a]);
printf("------------------------------------------------------------\n");*/
}
db.addMidiBank(bankDb, false, bnkMelodique);
db.addMidiBank(bankDb, true, bnkPercussion);
AdlBankSetup setup;
setup.volumeModel = VOLUME_DMX;
setup.deepTremolo = false;
setup.deepVibrato = false;
setup.scaleModulators = false;
SetBankSetup(bank, setup);
return true;
}
#endif // LOAD_OP2_H
|