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
|
#include "progs_cache.h"
#include "file_formats/load_ail.h"
#include "file_formats/load_bisqwit.h"
#include "file_formats/load_bnk2.h"
#include "file_formats/load_bnk.h"
#include "file_formats/load_ibk.h"
#include "file_formats/load_jv.h"
#include "file_formats/load_op2.h"
#include "file_formats/load_tmb.h"
#include "file_formats/load_wopl.h"
#include "file_formats/load_ea.h"
InstBuffer MakeNoSoundIns1()
{
InstBuffer nosnd;
uint8_t d[] = {0x00, 0x10, 0x07, 0x07, 0xF7, 0xF7, 0x00, 0x00, 0xFF, 0xFF, 0x00};
std::memcpy(nosnd.data, d, 11);
return nosnd;
}
void BanksDump::toOps(const InstBuffer_t &inData, BanksDump::Operator *outData, size_t begin)
{
outData[begin + 0].d_E862 =
uint_fast32_t(inData.op1_wave << 24)
+ uint_fast32_t(inData.op1_susrel << 16)
+ uint_fast32_t(inData.op1_atdec << 8)
+ uint_fast32_t(inData.op1_amvib << 0);
outData[begin + 1].d_E862 =
uint_fast32_t(inData.op2_wave << 24)
+ uint_fast32_t(inData.op2_susrel << 16)
+ uint_fast32_t(inData.op2_atdec << 8)
+ uint_fast32_t(inData.op2_amvib << 0);
outData[begin + 0].d_40 = inData.op1_ksltl;
outData[begin + 1].d_40 = inData.op2_ksltl;
}
size_t BanksDump::initBank(size_t bankId, const std::string &title, uint_fast16_t bankSetup)
{
for(size_t bID = 0; bID < banks.size(); bID++)
{
BankEntry &be = banks[bID];
if(bankId == be.bankId)
{
be.bankTitle = title;
be.bankSetup = bankSetup;
return bID;
}
}
size_t bankIndex = banks.size();
banks.emplace_back();
BankEntry &b = banks.back();
b.bankId = bankId;
b.bankTitle = title;
b.bankSetup = bankSetup;
return bankIndex;
}
void BanksDump::addMidiBank(size_t bankId, bool percussion, BanksDump::MidiBank b)
{
assert(bankId < banks.size());
BankEntry &be = banks[bankId];
auto it = std::find(midiBanks.begin(), midiBanks.end(), b);
if(it == midiBanks.end())
{
b.midiBankId = midiBanks.size();
midiBanks.push_back(b);
}
else
{
b.midiBankId = it->midiBankId;
}
if(percussion)
be.percussion.push_back(b.midiBankId);
else
be.melodic.push_back(b.midiBankId);
}
void BanksDump::addInstrument(BanksDump::MidiBank &bank, size_t patchId,
BanksDump::InstrumentEntry e,
BanksDump::Operator *ops,
const std::string &meta)
{
assert(patchId < 128);
size_t opsCount = ((e.instFlags & InstrumentEntry::WOPL_Ins_4op) != 0 ||
(e.instFlags & InstrumentEntry::WOPL_Ins_Pseudo4op) != 0) ?
4 : 2;
if((e.instFlags & InstrumentEntry::WOPL_Ins_IsBlank) != 0)
{
bank.instruments[patchId] = -1;
return;
}
for(size_t op = 0; op < opsCount; op++)
{
Operator o = ops[op];
auto it = std::find(operators.begin(), operators.end(), o);
if(it == operators.end())
{
o.opId = operators.size();
e.ops[op] = static_cast<int_fast32_t>(o.opId);
operators.push_back(o);
}
else
{
e.ops[op] = static_cast<int_fast32_t>(it->opId);
}
}
auto it = std::find(instruments.begin(), instruments.end(), e);
if(it == instruments.end())
{
e.instId = instruments.size();
e.instMetas.push_back(meta + "_" + std::to_string(patchId));
instruments.push_back(e);
}
else
{
e.instId = it->instId;
e.instMetas.push_back(meta + "_" + std::to_string(patchId));
it->instMetas.push_back(meta + "_" + std::to_string(patchId));
}
bank.instruments[patchId] = static_cast<int_fast32_t>(e.instId);
}
void BanksDump::exportBanks(const std::string &outPath, bool donntOverride, const std::string &headerName)
{
FILE *out;
// Check if file exist
if(donntOverride)
{
out = std::fopen(outPath.c_str(), "r");
if(out)
{
// File exist, don't override it
std::fclose(out);
return;
}
}
out = std::fopen(outPath.c_str(), "w");
std::fprintf(out, "/**********************************************************\n"
" This file is generated by `gen_adldata` automatically\n"
" Don't edit it directly!\n"
" To modify content of this file, modify banks\n"
" and re-run the `gen_adldata` build step.\n"
"***********************************************************/\n\n"
"#include \"%s\"\n\n\n", headerName.c_str());
std::fprintf(out, "const size_t g_embeddedBanksCount = %zu;\n\n", banks.size());
std::fprintf(out, "const BanksDump::BankEntry g_embeddedBanks[] =\n"
"{\n");
std::vector<size_t> bankNumberLists;
for(const BankEntry &be : banks)
{
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\t{0x%04lX, %zu, %zu, \"%s\", ",
#else
std::fprintf(out, "{0x%04lX,%zu,%zu,\"%s\",",
#endif
be.bankSetup,
be.melodic.size(),
be.percussion.size(),
be.bankTitle.c_str());
#ifndef ADL_GENDATA_MINIFY
fprintf(out, "%zu, ", bankNumberLists.size()); // Use offset to point the common array of bank IDs
#else
fprintf(out, "%zu,", bankNumberLists.size()); // Use offset to point the common array of bank IDs
#endif
for(const size_t &me : be.melodic)
bankNumberLists.push_back(me);
fprintf(out, "%zu", bankNumberLists.size());
for(const size_t &me : be.percussion)
bankNumberLists.push_back(me);
std::fprintf(out, "},");
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\n");
#endif
}
std::fprintf(out, "};\n\n");
#ifdef ADL_GENDATA_MINIFY
std::fprintf(out, "#define q(x) g_embeddedBanks[x].title\n");
#endif
std::fprintf(out, "const char* const g_embeddedBankNames[] =\n"
"{\n\t");
{
bool commaNeeded = false;
#ifndef ADL_GENDATA_MINIFY
size_t operatorEntryCounter = 0;
#endif
for(const BankEntry &be : banks)
{
if(commaNeeded)
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, ", ");
#else
std::fprintf(out, ",");
#endif
else
commaNeeded = true;
#ifndef ADL_GENDATA_MINIFY
operatorEntryCounter++;
if(operatorEntryCounter >= 25)
{
std::fprintf(out, "\n");
operatorEntryCounter = 0;
}
if(operatorEntryCounter == 0)
std::fprintf(out, "\t");
#endif
#ifdef ADL_GENDATA_MINIFY
std::fprintf(out, "q(%zu)", be.bankId);
#else
std::fprintf(out, "g_embeddedBanks[%zu].title", be.bankId);
#endif
}
std::fprintf(out, ",\n\tNULL"); // Make a null entry as finalizer
}
std::fprintf(out, "\n};\n\n");
std::fprintf(out, "const size_t g_embeddedBanksMidiIndex[] =\n"
"{");
{
bool commaNeeded = false;
for(const size_t &me : bankNumberLists)
{
if(commaNeeded)
std::fprintf(out, ",");
else
commaNeeded = true;
std::fprintf(out, "%zu", me);
}
}
std::fprintf(out, "};\n\n");
std::fprintf(out, "const BanksDump::MidiBank g_embeddedBanksMidi[] =\n"
"{\n");
for(const MidiBank &be : midiBanks)
{
bool commaNeeded = true;
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\t");
#endif
std::fprintf(out, "{%u,%u,", be.msb, be.lsb);
std::fprintf(out, "{");
commaNeeded = false;
for(size_t i = 0; i < 128; i++)
{
if(commaNeeded)
std::fprintf(out, ",");
else
commaNeeded = true;
std::fprintf(out, "%ld", be.instruments[i]);
}
std::fprintf(out, "}");
std::fprintf(out, "},");
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\n");
#endif
}
std::fprintf(out, "};\n\n");
std::fprintf(out, "const BanksDump::InstrumentEntry g_embeddedBanksInstruments[] =\n"
"{\n");
for(const InstrumentEntry &be : instruments)
{
size_t opsCount = ((be.instFlags & InstrumentEntry::WOPL_Ins_4op) != 0 ||
(be.instFlags & InstrumentEntry::WOPL_Ins_Pseudo4op) != 0) ? 4 : 2;
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\t");
#endif
std::fprintf(out, "{%d,%d,%d,%u,%s%lX,%d,%s%lX,%s%lX,%s%lX,",
be.noteOffset1,
be.noteOffset2,
be.midiVelocityOffset,
be.percussionKeyNumber,
(be.instFlags == 0 ? "" : "0x"), be.instFlags, // for compactness, don't print "0x" when is zero
be.secondVoiceDetune,
(be.fbConn == 0 ? "" : "0x"), be.fbConn,
(be.delay_on_ms == 0 ? "" : "0x"), be.delay_on_ms,
(be.delay_off_ms == 0 ? "" : "0x"), be.delay_off_ms);
if(opsCount == 4)
std::fprintf(out, "{%ld,%ld,%ld,%ld} ",
be.ops[0], be.ops[1], be.ops[2], be.ops[3]);
else
std::fprintf(out, "{%ld,%ld}",
be.ops[0], be.ops[1]);
std::fprintf(out, "},");
#ifndef ADL_GENDATA_MINIFY
std::fprintf(out, "\n");
#endif
}
std::fprintf(out, "};\n\n");
std::fprintf(out, "const BanksDump::Operator g_embeddedBanksOperators[] =\n"
"{\n");
#ifndef ADL_GENDATA_MINIFY
size_t operatorEntryCounter = 0;
#endif
for(const Operator &be : operators)
{
#ifndef ADL_GENDATA_MINIFY
if(operatorEntryCounter == 0)
std::fprintf(out, "\t");
#endif
std::fprintf(out, "{0x%07lX,%s%02lX},",
be.d_E862,
(be.d_40 == 0 ? "" : "0x"), be.d_40);
#ifndef ADL_GENDATA_MINIFY
operatorEntryCounter++;
if(operatorEntryCounter >= 25)
{
std::fprintf(out, "\n");
operatorEntryCounter = 0;
}
#endif
}
std::fprintf(out, "\n};\n\n");
std::fclose(out);
}
struct OpCheckData
{
uint_fast8_t egEn;
uint_fast8_t attack;
uint_fast8_t decay;
uint_fast8_t sustain;
uint_fast8_t release;
uint_fast8_t level;
void setData(uint_fast32_t d_E862, uint_fast32_t d_40)
{
egEn = static_cast<uint_fast8_t>(((d_E862 & 0xFF) >> 5) & 0x01);
decay = static_cast<uint_fast8_t>((d_E862 >> 8) & 0x0F);
attack = static_cast<uint_fast8_t>((d_E862 >> 12) & 0x0F);
release = static_cast<uint_fast8_t>((d_E862 >> 16) & 0x0F);
sustain = static_cast<uint_fast8_t>((d_E862 >> 20) & 0x0F);
level = static_cast<uint_fast8_t>(d_40 & 0x3F);
}
bool isOpSilent(bool moreInfo)
{
// level=0x3f - silence
// attack=0x00 - silence
// attack=0x0F & sustain=0x0F & decay=0x0F - half-silence
// attack=0x0F & decay=0x0F & release=0x00 & egOff - half-silence
if(level == 0x3F)
{
if(moreInfo)
std::fprintf(stdout, "== volume=0x3F ==\n");
return true;
}
if(attack == 0x00)
{
if(moreInfo)
std::fprintf(stdout, "== attack=0x00 ==\n");
return true;
}
if(attack == 0x0F && sustain == 0x0F && decay == 0x0F)
{
if(moreInfo)
std::fprintf(stdout, "== attack=0x0F, sustain=0x0F, decay=0x0F ==\n");
return true;
}
if(attack == 0x0F && decay == 0x0F && release == 0x00 && !egEn)
{
if(moreInfo)
std::fprintf(stdout, "== attack=0x0F, decay=0x0F, release=0x00, !egEn ==\n");
return true;
}
return false;
}
};
bool BanksDump::isSilent(const BanksDump &db, const BanksDump::InstrumentEntry &ins, bool moreInfo)
{
bool isPseudo4ops = ((ins.instFlags & BanksDump::InstrumentEntry::WOPL_Ins_Pseudo4op) != 0);
bool is4ops = ((ins.instFlags & BanksDump::InstrumentEntry::WOPL_Ins_4op) != 0) && !isPseudo4ops;
size_t opsNum = (is4ops || isPseudo4ops) ? 4 : 2;
BanksDump::Operator ops[4];
assert(ins.ops[0] >= 0);
assert(ins.ops[1] >= 0);
ops[0] = db.operators[ins.ops[0]];
ops[1] = db.operators[ins.ops[1]];
if(opsNum > 2)
{
assert(ins.ops[2] >= 0);
assert(ins.ops[3] >= 0);
ops[2] = db.operators[ins.ops[2]];
ops[3] = db.operators[ins.ops[3]];
}
return isSilent(ops, ins.fbConn, opsNum, isPseudo4ops, moreInfo);
}
bool BanksDump::isSilent(const BanksDump::Operator *ops, uint_fast16_t fbConn, size_t countOps, bool pseudo4op, bool moreInfo)
{
// TODO: Implement this completely!!!
const uint_fast8_t conn1 = (fbConn) & 0x01;
const uint_fast8_t conn2 = (fbConn >> 8) & 0x01;
OpCheckData opd[4];
for(size_t i = 0; i < 4; i++)
opd[i].setData(ops[i].d_E862, ops[i].d_40);
if(countOps == 2)
{
if(conn1 == 0)
{
if(opd[1].isOpSilent(moreInfo))
return true;
}
if(conn1 == 1)
{
if(opd[0].isOpSilent(moreInfo) && opd[1].isOpSilent(moreInfo))
return true;
}
}
else if(countOps == 4 && pseudo4op)
{
bool silent1 = false;
bool silent2 = false;
if(conn1 == 0 && opd[1].isOpSilent(moreInfo))
silent1 = true;
if(conn1 == 1 && opd[0].isOpSilent(moreInfo) && opd[1].isOpSilent(moreInfo))
silent1 = true;
if(conn2 == 0 && opd[3].isOpSilent(moreInfo))
silent2 = true;
if(conn2 == 1 && opd[2].isOpSilent(moreInfo) && opd[3].isOpSilent(moreInfo))
silent2 = true;
if(silent1 && silent2)
return true;
}
else if(countOps == 4 && !pseudo4op)
{
if(conn1 == 0 && conn2 == 0) // FM-FM [0, 0, 0, 1]
{
if(opd[3].isOpSilent(moreInfo))
return true;
}
if(conn1 == 1 && conn2 == 0) // AM-FM [1, 0, 0, 1]
{
if(opd[0].isOpSilent(moreInfo) && opd[3].isOpSilent(moreInfo))
return true;
}
if(conn1 == 0 && conn2 == 1) // FM-AM [0, 1, 0, 1]
{
if(opd[1].isOpSilent(moreInfo) && opd[3].isOpSilent(moreInfo))
return true;
}
if(conn1 == 1 && conn2 == 1) // FM-AM [1, 0, 1, 1]
{
if(opd[0].isOpSilent(moreInfo) && opd[2].isOpSilent(moreInfo) && opd[3].isOpSilent(moreInfo))
return true;
}
}
return false;
}
void BanksDump::InstrumentEntry::setFbConn(uint_fast16_t fbConn1, uint_fast16_t fbConn2)
{
fbConn = (static_cast<uint_fast16_t>(fbConn1 & 0x0F)) |
(static_cast<uint_fast16_t>(fbConn2 & 0x0F) << 8);
}
|