| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "engine.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
#include "../fileutils.h" |
| 23 |
#include <fmt/printf.h> |
| 24 |
#include <limits.h> |
| 25 |
|
| 26 |
enum DivInsFormats { |
| 27 |
DIV_INSFORMAT_DMP, |
| 28 |
DIV_INSFORMAT_TFI, |
| 29 |
DIV_INSFORMAT_VGI, |
| 30 |
DIV_INSFORMAT_FTI, |
| 31 |
DIV_INSFORMAT_BTI, |
| 32 |
DIV_INSFORMAT_S3I, |
| 33 |
DIV_INSFORMAT_SBI, |
| 34 |
DIV_INSFORMAT_Y12, |
| 35 |
DIV_INSFORMAT_OPLI, |
| 36 |
DIV_INSFORMAT_OPNI, |
| 37 |
DIV_INSFORMAT_BNK, |
| 38 |
DIV_INSFORMAT_GYB, |
| 39 |
DIV_INSFORMAT_OPM, |
| 40 |
DIV_INSFORMAT_WOPL, |
| 41 |
DIV_INSFORMAT_WOPN, |
| 42 |
DIV_INSFORMAT_FF, |
| 43 |
}; |
| 44 |
|
| 45 |
// Reused patch data structures |
| 46 |
|
| 47 |
// SBI and some other OPL containers |
| 48 |
struct sbi_t { |
| 49 |
uint8_t Mcharacteristics, |
| 50 |
Ccharacteristics, |
| 51 |
Mscaling_output, |
| 52 |
Cscaling_output, |
| 53 |
Meg_AD, |
| 54 |
Ceg_AD, |
| 55 |
Meg_SR, |
| 56 |
Ceg_SR, |
| 57 |
Mwave, |
| 58 |
Cwave, |
| 59 |
FeedConnect; |
| 60 |
}; |
| 61 |
|
| 62 |
// MIDI-related |
| 63 |
struct midibank_t { |
| 64 |
String name; |
| 65 |
uint8_t bankMsb, |
| 66 |
bankLsb; |
| 67 |
}; |
| 68 |
|
| 69 |
static void readSbiOpData(sbi_t& sbi, SafeReader& reader) { |
| 70 |
sbi.Mcharacteristics = reader.readC(); |
| 71 |
sbi.Ccharacteristics = reader.readC(); |
| 72 |
sbi.Mscaling_output = reader.readC(); |
| 73 |
sbi.Cscaling_output = reader.readC(); |
| 74 |
sbi.Meg_AD = reader.readC(); |
| 75 |
sbi.Ceg_AD = reader.readC(); |
| 76 |
sbi.Meg_SR = reader.readC(); |
| 77 |
sbi.Ceg_SR = reader.readC(); |
| 78 |
sbi.Mwave = reader.readC(); |
| 79 |
sbi.Cwave = reader.readC(); |
| 80 |
sbi.FeedConnect = reader.readC(); |
| 81 |
} |
| 82 |
|
| 83 |
// detune needs extra translation from register to furnace format |
| 84 |
static inline uint8_t fmDtRegisterToFurnace(uint8_t&& dtNative) { |
| 85 |
return (dtNative>=4) ? (7-dtNative) : (dtNative+3); |
| 86 |
} |
| 87 |
|
| 88 |
static bool stringNotBlank(String& str) { |
| 89 |
return str.size() > 0 && str.find_first_not_of(' ') != String::npos; |
| 90 |
} |
| 91 |
|
| 92 |
void DivEngine::loadDMP(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 93 |
DivInstrument* ins=new DivInstrument; |
| 94 |
// this is a ridiculous mess |
| 95 |
unsigned char version=0; |
| 96 |
unsigned char sys=0; |
| 97 |
try { |
| 98 |
reader.seek(0,SEEK_SET); |
| 99 |
version=reader.readC(); |
| 100 |
logD(".dmp version %d",version); |
| 101 |
} catch (EndOfFileException& e) { |
| 102 |
lastError="premature end of file"; |
| 103 |
logE("premature end of file"); |
| 104 |
delete ins; |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if (version>11) { |
| 109 |
lastError="unknown instrument version!"; |
| 110 |
delete ins; |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
ins->name=stripPath; |
| 115 |
|
| 116 |
if (version>=11) { // 1.0 |
| 117 |
try { |
| 118 |
sys=reader.readC(); |
| 119 |
|
| 120 |
switch (sys) { |
| 121 |
case 1: // YMU759 |
| 122 |
ins->type=DIV_INS_FM; |
| 123 |
logD("instrument type is YMU759"); |
| 124 |
break; |
| 125 |
case 2: // Genesis |
| 126 |
ins->type=DIV_INS_FM; |
| 127 |
logD("instrument type is Genesis"); |
| 128 |
break; |
| 129 |
case 3: // SMS |
| 130 |
ins->type=DIV_INS_STD; |
| 131 |
logD("instrument type is SMS"); |
| 132 |
break; |
| 133 |
case 4: // Game Boy |
| 134 |
ins->type=DIV_INS_GB; |
| 135 |
logD("instrument type is Game Boy"); |
| 136 |
break; |
| 137 |
case 5: // PC Engine |
| 138 |
ins->type=DIV_INS_PCE; |
| 139 |
logD("instrument type is PC Engine"); |
| 140 |
break; |
| 141 |
case 6: // NES |
| 142 |
ins->type=DIV_INS_STD; |
| 143 |
logD("instrument type is NES"); |
| 144 |
break; |
| 145 |
case 7: case 0x17: // C64 |
| 146 |
ins->type=DIV_INS_C64; |
| 147 |
logD("instrument type is C64"); |
| 148 |
break; |
| 149 |
case 8: // Arcade |
| 150 |
ins->type=DIV_INS_OPM; |
| 151 |
logD("instrument type is Arcade"); |
| 152 |
break; |
| 153 |
case 9: // Neo Geo |
| 154 |
ins->type=DIV_INS_FM; |
| 155 |
logD("instrument type is Neo Geo"); |
| 156 |
break; |
| 157 |
default: |
| 158 |
logD("instrument type is unknown"); |
| 159 |
lastError=fmt::sprintf("unknown instrument type %d!",sys); |
| 160 |
delete ins; |
| 161 |
return; |
| 162 |
break; |
| 163 |
} |
| 164 |
} catch (EndOfFileException& e) { |
| 165 |
lastError="premature end of file"; |
| 166 |
logE("premature end of file"); |
| 167 |
delete ins; |
| 168 |
return; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
try { |
| 173 |
bool mode=true; |
| 174 |
if (version>1) { |
| 175 |
mode=reader.readC(); |
| 176 |
logD("instrument mode is %d",mode); |
| 177 |
if (mode==0) { |
| 178 |
if (ins->type==DIV_INS_FM) { |
| 179 |
if (sys==9) { |
| 180 |
ins->type=DIV_INS_AY; |
| 181 |
} else { |
| 182 |
ins->type=DIV_INS_STD; |
| 183 |
} |
| 184 |
} |
| 185 |
} else { |
| 186 |
if (sys==3 || sys==6) { |
| 187 |
ins->type=DIV_INS_OPLL; |
| 188 |
} else if (sys==1) { |
| 189 |
ins->type=DIV_INS_OPL; |
| 190 |
} else if (sys==8) { |
| 191 |
ins->type=DIV_INS_OPM; |
| 192 |
} else { |
| 193 |
ins->type=DIV_INS_FM; |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
ins->type=DIV_INS_FM; |
| 198 |
} |
| 199 |
|
| 200 |
if (mode) { // FM |
| 201 |
logD("reading FM data..."); |
| 202 |
if (version<10) { |
| 203 |
if (version>1) { |
| 204 |
// bullcrap! no way to determine the instrument type other than a vague FM/STD! |
| 205 |
if (reader.size()==51) { |
| 206 |
reader.readC(); |
| 207 |
ins->fm.ops=4; |
| 208 |
} else { |
| 209 |
ins->fm.ops=reader.readC()?4:2; |
| 210 |
} |
| 211 |
} else { |
| 212 |
// HELP |
| 213 |
if (reader.size()==49) { |
| 214 |
ins->fm.ops=4; |
| 215 |
reader.readC(); |
| 216 |
} else { |
| 217 |
ins->fm.ops=reader.readC()?2:4; |
| 218 |
} |
| 219 |
} |
| 220 |
} else { |
| 221 |
ins->fm.ops=4; |
| 222 |
} |
| 223 |
if (version>1) { // HELP! in which version of the format did we start storing FMS! |
| 224 |
ins->fm.fms=reader.readC(); |
| 225 |
} |
| 226 |
ins->fm.fb=reader.readC(); |
| 227 |
ins->fm.alg=reader.readC(); |
| 228 |
// DITTO |
| 229 |
if (sys!=1) ins->fm.ams=reader.readC(); |
| 230 |
|
| 231 |
for (int j=0; j<ins->fm.ops; j++) { |
| 232 |
logD("OP%d is at %d",j,reader.tell()); |
| 233 |
ins->fm.op[j].mult=reader.readC(); |
| 234 |
ins->fm.op[j].tl=reader.readC(); |
| 235 |
ins->fm.op[j].ar=reader.readC(); |
| 236 |
ins->fm.op[j].dr=reader.readC(); |
| 237 |
ins->fm.op[j].sl=reader.readC(); |
| 238 |
ins->fm.op[j].rr=reader.readC(); |
| 239 |
ins->fm.op[j].am=reader.readC(); |
| 240 |
// what the hell how do I tell! |
| 241 |
if (sys==1) { // YMU759 |
| 242 |
ins->fm.op[j].ws=reader.readC(); |
| 243 |
ins->fm.op[j].ksl=reader.readC(); |
| 244 |
ins->fm.op[j].vib=reader.readC(); |
| 245 |
ins->fm.op[j].egt=reader.readC(); |
| 246 |
ins->fm.op[j].sus=reader.readC(); |
| 247 |
ins->fm.op[j].ksr=reader.readC(); |
| 248 |
ins->fm.op[j].dvb=reader.readC(); |
| 249 |
ins->fm.op[j].dam=reader.readC(); |
| 250 |
} else { |
| 251 |
if (sys==3 || sys==6) { // OPLL/VRC7 |
| 252 |
ins->fm.op[j].ksr=reader.readC()?1:0; |
| 253 |
ins->fm.op[j].vib=reader.readC(); |
| 254 |
if (j==0) { |
| 255 |
ins->fm.opllPreset=ins->fm.op[j].vib>>4; |
| 256 |
} |
| 257 |
ins->fm.op[j].vib=ins->fm.op[j].vib?1:0; |
| 258 |
ins->fm.op[j].ksl=reader.readC()?1:0; |
| 259 |
ins->fm.op[j].ssgEnv=reader.readC(); |
| 260 |
} else { |
| 261 |
ins->fm.op[j].rs=reader.readC(); |
| 262 |
ins->fm.op[j].dt=reader.readC(); |
| 263 |
ins->fm.op[j].dt2=ins->fm.op[j].dt>>4; |
| 264 |
ins->fm.op[j].dt&=15; |
| 265 |
ins->fm.op[j].d2r=reader.readC(); |
| 266 |
ins->fm.op[j].ssgEnv=reader.readC(); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} else { // STD |
| 271 |
logD("reading STD data..."); |
| 272 |
if (ins->type!=DIV_INS_GB) { |
| 273 |
ins->std.volMacro.len=reader.readC(); |
| 274 |
if (version>5) { |
| 275 |
for (int i=0; i<ins->std.volMacro.len; i++) { |
| 276 |
ins->std.volMacro.val[i]=reader.readI(); |
| 277 |
if (ins->std.volMacro.val[i]>15 && sys==6) { // FDS |
| 278 |
ins->type=DIV_INS_FDS; |
| 279 |
} |
| 280 |
} |
| 281 |
} else { |
| 282 |
for (int i=0; i<ins->std.volMacro.len; i++) { |
| 283 |
ins->std.volMacro.val[i]=reader.readC(); |
| 284 |
} |
| 285 |
} |
| 286 |
if (version<11) for (int i=0; i<ins->std.volMacro.len; i++) { |
| 287 |
if (ins->std.volMacro.val[i]>15 && ins->type==DIV_INS_STD) ins->type=DIV_INS_PCE; |
| 288 |
} |
| 289 |
if (ins->std.volMacro.len>0) { |
| 290 |
ins->std.volMacro.open=true; |
| 291 |
ins->std.volMacro.loop=reader.readC(); |
| 292 |
} else { |
| 293 |
ins->std.volMacro.open=false; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
ins->std.arpMacro.len=reader.readC(); |
| 298 |
if (version>5) { |
| 299 |
for (int i=0; i<ins->std.arpMacro.len; i++) { |
| 300 |
ins->std.arpMacro.val[i]=reader.readI(); |
| 301 |
} |
| 302 |
} else { |
| 303 |
for (int i=0; i<ins->std.arpMacro.len; i++) { |
| 304 |
ins->std.arpMacro.val[i]=reader.readC(); |
| 305 |
} |
| 306 |
} |
| 307 |
if (ins->std.arpMacro.len>0) { |
| 308 |
ins->std.arpMacro.open=true; |
| 309 |
ins->std.arpMacro.loop=reader.readC(); |
| 310 |
} else { |
| 311 |
ins->std.arpMacro.open=false; |
| 312 |
} |
| 313 |
if (version>8) { // TODO: when? |
| 314 |
ins->std.arpMacro.mode=reader.readC(); |
| 315 |
} |
| 316 |
|
| 317 |
ins->std.dutyMacro.len=reader.readC(); |
| 318 |
if (version>5) { |
| 319 |
for (int i=0; i<ins->std.dutyMacro.len; i++) { |
| 320 |
ins->std.dutyMacro.val[i]=reader.readI(); |
| 321 |
} |
| 322 |
} else { |
| 323 |
for (int i=0; i<ins->std.dutyMacro.len; i++) { |
| 324 |
ins->std.dutyMacro.val[i]=reader.readC(); |
| 325 |
} |
| 326 |
} |
| 327 |
if (ins->std.dutyMacro.len>0) { |
| 328 |
ins->std.dutyMacro.open=true; |
| 329 |
ins->std.dutyMacro.loop=reader.readC(); |
| 330 |
} else { |
| 331 |
ins->std.dutyMacro.open=false; |
| 332 |
} |
| 333 |
|
| 334 |
ins->std.waveMacro.len=reader.readC(); |
| 335 |
if (version>5) { |
| 336 |
for (int i=0; i<ins->std.waveMacro.len; i++) { |
| 337 |
ins->std.waveMacro.val[i]=reader.readI(); |
| 338 |
// piece of crap offset by 1 |
| 339 |
if (ins->type==DIV_INS_AY) { |
| 340 |
ins->std.waveMacro.val[i]++; |
| 341 |
} |
| 342 |
} |
| 343 |
} else { |
| 344 |
for (int i=0; i<ins->std.waveMacro.len; i++) { |
| 345 |
ins->std.waveMacro.val[i]=reader.readC(); |
| 346 |
} |
| 347 |
} |
| 348 |
if (ins->std.waveMacro.len>0) { |
| 349 |
ins->std.waveMacro.open=true; |
| 350 |
ins->std.waveMacro.loop=reader.readC(); |
| 351 |
} else { |
| 352 |
ins->std.waveMacro.open=false; |
| 353 |
} |
| 354 |
|
| 355 |
if (ins->type==DIV_INS_C64) { |
| 356 |
bool volIsCutoff=false; |
| 357 |
|
| 358 |
ins->c64.triOn=reader.readC(); |
| 359 |
ins->c64.sawOn=reader.readC(); |
| 360 |
ins->c64.pulseOn=reader.readC(); |
| 361 |
ins->c64.noiseOn=reader.readC(); |
| 362 |
|
| 363 |
ins->c64.a=reader.readC(); |
| 364 |
ins->c64.d=reader.readC(); |
| 365 |
ins->c64.s=reader.readC(); |
| 366 |
ins->c64.r=reader.readC(); |
| 367 |
|
| 368 |
ins->c64.duty=(reader.readC()*4095)/100; |
| 369 |
|
| 370 |
ins->c64.ringMod=reader.readC(); |
| 371 |
ins->c64.oscSync=reader.readC(); |
| 372 |
ins->c64.toFilter=reader.readC(); |
| 373 |
if (version<0x07) { // TODO: UNSURE |
| 374 |
volIsCutoff=reader.readI(); |
| 375 |
} else { |
| 376 |
volIsCutoff=reader.readC(); |
| 377 |
} |
| 378 |
ins->c64.initFilter=reader.readC(); |
| 379 |
|
| 380 |
ins->c64.res=reader.readC(); |
| 381 |
ins->c64.cut=(reader.readC()*2047)/100; |
| 382 |
ins->c64.hp=reader.readC(); |
| 383 |
ins->c64.bp=reader.readC(); |
| 384 |
ins->c64.lp=reader.readC(); |
| 385 |
ins->c64.ch3off=reader.readC(); |
| 386 |
|
| 387 |
// weird storage |
| 388 |
if (volIsCutoff) { |
| 389 |
// move to alg (new cutoff) |
| 390 |
ins->std.algMacro.len=ins->std.volMacro.len; |
| 391 |
ins->std.algMacro.loop=ins->std.volMacro.loop; |
| 392 |
ins->std.algMacro.rel=ins->std.volMacro.rel; |
| 393 |
for (int j=0; j<ins->std.algMacro.len; j++) { |
| 394 |
ins->std.algMacro.val[j]=-(ins->std.volMacro.val[j]-18); |
| 395 |
} |
| 396 |
ins->std.volMacro.len=0; |
| 397 |
memset(ins->std.volMacro.val,0,256*sizeof(int)); |
| 398 |
} |
| 399 |
for (int j=0; j<ins->std.dutyMacro.len; j++) { |
| 400 |
ins->std.dutyMacro.val[j]-=12; |
| 401 |
} |
| 402 |
} |
| 403 |
if (ins->type==DIV_INS_GB) { |
| 404 |
ins->gb.envVol=reader.readC(); |
| 405 |
ins->gb.envDir=reader.readC(); |
| 406 |
ins->gb.envLen=reader.readC(); |
| 407 |
ins->gb.soundLen=reader.readC(); |
| 408 |
} |
| 409 |
} |
| 410 |
} catch (EndOfFileException& e) { |
| 411 |
lastError="premature end of file"; |
| 412 |
logE("premature end of file"); |
| 413 |
delete ins; |
| 414 |
return; |
| 415 |
} |
| 416 |
|
| 417 |
ret.push_back(ins); |
| 418 |
} |
| 419 |
|
| 420 |
void DivEngine::loadTFI(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 421 |
DivInstrument* ins=new DivInstrument; |
| 422 |
try { |
| 423 |
reader.seek(0,SEEK_SET); |
| 424 |
|
| 425 |
ins->type=DIV_INS_FM; |
| 426 |
ins->name=stripPath; |
| 427 |
|
| 428 |
ins->fm.alg=reader.readC(); |
| 429 |
ins->fm.fb=reader.readC(); |
| 430 |
|
| 431 |
for (int i=0; i<4; i++) { |
| 432 |
DivInstrumentFM::Operator& op=ins->fm.op[i]; |
| 433 |
|
| 434 |
op.mult=reader.readC(); |
| 435 |
op.dt=reader.readC(); |
| 436 |
op.tl=reader.readC(); |
| 437 |
op.rs=reader.readC(); |
| 438 |
op.ar=reader.readC(); |
| 439 |
op.dr=reader.readC(); |
| 440 |
op.d2r=reader.readC(); |
| 441 |
op.rr=reader.readC(); |
| 442 |
op.sl=reader.readC(); |
| 443 |
op.ssgEnv=reader.readC(); |
| 444 |
} |
| 445 |
} catch (EndOfFileException& e) { |
| 446 |
lastError="premature end of file"; |
| 447 |
logE("premature end of file"); |
| 448 |
delete ins; |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
ret.push_back(ins); |
| 453 |
} |
| 454 |
|
| 455 |
void DivEngine::loadVGI(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 456 |
DivInstrument* ins=new DivInstrument; |
| 457 |
try { |
| 458 |
reader.seek(0,SEEK_SET); |
| 459 |
|
| 460 |
ins->type=DIV_INS_FM; |
| 461 |
ins->name=stripPath; |
| 462 |
|
| 463 |
ins->fm.alg=reader.readC(); |
| 464 |
ins->fm.fb=reader.readC(); |
| 465 |
unsigned char fmsams=reader.readC(); |
| 466 |
ins->fm.fms=fmsams&7; |
| 467 |
ins->fm.ams=fmsams>>4; |
| 468 |
|
| 469 |
for (int i=0; i<4; i++) { |
| 470 |
DivInstrumentFM::Operator& op=ins->fm.op[i]; |
| 471 |
|
| 472 |
op.mult=reader.readC(); |
| 473 |
op.dt=reader.readC(); |
| 474 |
op.tl=reader.readC(); |
| 475 |
op.rs=reader.readC(); |
| 476 |
op.ar=reader.readC(); |
| 477 |
op.dr=reader.readC(); |
| 478 |
if (op.dr&0x80) { |
| 479 |
op.am=1; |
| 480 |
op.dr&=0x7f; |
| 481 |
} |
| 482 |
op.d2r=reader.readC(); |
| 483 |
op.rr=reader.readC(); |
| 484 |
op.sl=reader.readC(); |
| 485 |
op.ssgEnv=reader.readC(); |
| 486 |
} |
| 487 |
} catch (EndOfFileException& e) { |
| 488 |
lastError="premature end of file"; |
| 489 |
logE("premature end of file"); |
| 490 |
delete ins; |
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
ret.push_back(ins); |
| 495 |
} |
| 496 |
|
| 497 |
void DivEngine::loadS3I(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 498 |
DivInstrument* ins=new DivInstrument; |
| 499 |
try { |
| 500 |
reader.seek(0, SEEK_SET); |
| 501 |
|
| 502 |
uint8_t s3i_type = reader.readC(); |
| 503 |
|
| 504 |
if (s3i_type >= 2) { |
| 505 |
ins->type = DIV_INS_OPL; |
| 506 |
if (s3i_type > 2 && s3i_type <= 7) { |
| 507 |
ins->fm.opllPreset = (uint8_t)(1<<4); // Flag as Drum preset. |
| 508 |
} |
| 509 |
// skip internal filename - we'll use the long name description |
| 510 |
reader.seek(12, SEEK_CUR); |
| 511 |
|
| 512 |
// skip reserved bytes |
| 513 |
reader.seek(3, SEEK_CUR); |
| 514 |
|
| 515 |
// 12-byte opl value - identical to SBI format |
| 516 |
sbi_t s3i; |
| 517 |
readSbiOpData(s3i, reader); |
| 518 |
|
| 519 |
DivInstrumentFM::Operator& opM = ins->fm.op[0]; |
| 520 |
DivInstrumentFM::Operator& opC = ins->fm.op[1]; |
| 521 |
ins->fm.ops = 2; |
| 522 |
opM.mult = s3i.Mcharacteristics & 0xF; |
| 523 |
opM.ksr = ((s3i.Mcharacteristics >> 4) & 0x1); |
| 524 |
opM.sus = ((s3i.Mcharacteristics >> 5) & 0x1); |
| 525 |
opM.vib = ((s3i.Mcharacteristics >> 6) & 0x1); |
| 526 |
opM.am = ((s3i.Mcharacteristics >> 7) & 0x1); |
| 527 |
opM.tl = s3i.Mscaling_output & 0x3F; |
| 528 |
opM.ksl = ((s3i.Mscaling_output >> 6) & 0x3); |
| 529 |
opM.ar = ((s3i.Meg_AD >> 4) & 0xF); |
| 530 |
opM.dr = (s3i.Meg_AD & 0xF); |
| 531 |
opM.rr = (s3i.Meg_SR & 0xF); |
| 532 |
opM.sl = ((s3i.Meg_SR >> 4) & 0xF); |
| 533 |
opM.ws = s3i.Mwave; |
| 534 |
|
| 535 |
ins->fm.alg = (s3i.FeedConnect & 0x1); |
| 536 |
ins->fm.fb = ((s3i.FeedConnect >> 1) & 0x7); |
| 537 |
|
| 538 |
opC.mult = s3i.Ccharacteristics & 0xF; |
| 539 |
opC.ksr = ((s3i.Ccharacteristics >> 4) & 0x1); |
| 540 |
opC.sus = ((s3i.Ccharacteristics >> 5) & 0x1); |
| 541 |
opC.vib = ((s3i.Ccharacteristics >> 6) & 0x1); |
| 542 |
opC.am = ((s3i.Ccharacteristics >> 7) & 0x1); |
| 543 |
opC.tl = s3i.Cscaling_output & 0x3F; |
| 544 |
opC.ksl = ((s3i.Cscaling_output >> 6) & 0x3); |
| 545 |
opC.ar = ((s3i.Ceg_AD >> 4) & 0xF); |
| 546 |
opC.dr = (s3i.Ceg_AD & 0xF); |
| 547 |
opC.rr = (s3i.Ceg_SR & 0xF); |
| 548 |
opC.sl = ((s3i.Ceg_SR >> 4) & 0xF); |
| 549 |
opC.ws = s3i.Cwave; |
| 550 |
|
| 551 |
// Skip more stuff we don't need |
| 552 |
reader.seek(21, SEEK_CUR); |
| 553 |
} else { |
| 554 |
lastError="S3I PCM samples currently not supported."; |
| 555 |
logE("S3I PCM samples currently not supported."); |
| 556 |
} |
| 557 |
String insName = reader.readString(28); |
| 558 |
ins->name = stringNotBlank(insName) ? insName : stripPath; |
| 559 |
|
| 560 |
int s3i_signature = reader.readI(); |
| 561 |
|
| 562 |
if (s3i_signature != 0x49524353) { |
| 563 |
addWarning("S3I signature invalid."); |
| 564 |
logW("S3I signature invalid."); |
| 565 |
}; |
| 566 |
} catch (EndOfFileException& e) { |
| 567 |
lastError="premature end of file"; |
| 568 |
logE("premature end of file"); |
| 569 |
delete ins; |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
ret.push_back(ins); |
| 574 |
} |
| 575 |
|
| 576 |
void DivEngine::loadSBI(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 577 |
std::vector<DivInstrument*> insList; // in case 2x2op |
| 578 |
DivInstrument* ins=new DivInstrument; |
| 579 |
try { |
| 580 |
reader.seek(0, SEEK_SET); |
| 581 |
ins->type = DIV_INS_OPL; |
| 582 |
|
| 583 |
int sbi_header = reader.readI(); |
| 584 |
// SBI header determines format |
| 585 |
bool is_2op = (sbi_header == 0x1A494253 || sbi_header == 0x1A504F32); // SBI\x1A or 2OP\x1A |
| 586 |
bool is_4op = (sbi_header == 0x1A504F34); // 4OP\x1A |
| 587 |
bool is_6op = (sbi_header == 0x1A504F36); // 6OP\x1A - Freq Monster 801-specific |
| 588 |
|
| 589 |
// 32-byte null terminated instrument name |
| 590 |
String insName = reader.readString(32); |
| 591 |
insName = stringNotBlank(insName) ? insName : stripPath; |
| 592 |
|
| 593 |
auto writeOp = [](sbi_t& sbi, DivInstrumentFM::Operator& opM, DivInstrumentFM::Operator& opC) { |
| 594 |
opM.mult = sbi.Mcharacteristics & 0xF; |
| 595 |
opM.ksr = ((sbi.Mcharacteristics >> 4) & 0x1); |
| 596 |
opM.sus = ((sbi.Mcharacteristics >> 5) & 0x1); |
| 597 |
opM.vib = ((sbi.Mcharacteristics >> 6) & 0x1); |
| 598 |
opM.am = ((sbi.Mcharacteristics >> 7) & 0x1); |
| 599 |
opM.tl = sbi.Mscaling_output & 0x3F; |
| 600 |
opM.ksl = ((sbi.Mscaling_output >> 6) & 0x3); |
| 601 |
opM.ar = ((sbi.Meg_AD >> 4) & 0xF); |
| 602 |
opM.dr = (sbi.Meg_AD & 0xF); |
| 603 |
opM.rr = (sbi.Meg_SR & 0xF); |
| 604 |
opM.sl = ((sbi.Meg_SR >> 4) & 0xF); |
| 605 |
opM.ws = sbi.Mwave; |
| 606 |
|
| 607 |
opC.mult = sbi.Ccharacteristics & 0xF; |
| 608 |
opC.ksr = ((sbi.Ccharacteristics >> 4) & 0x1); |
| 609 |
opC.sus = ((sbi.Ccharacteristics >> 5) & 0x1); |
| 610 |
opC.vib = ((sbi.Ccharacteristics >> 6) & 0x1); |
| 611 |
opC.am = ((sbi.Ccharacteristics >> 7) & 0x1); |
| 612 |
opC.tl = sbi.Cscaling_output & 0x3F; |
| 613 |
opC.ksl = ((sbi.Cscaling_output >> 6) & 0x3); |
| 614 |
opC.ar = ((sbi.Ceg_AD >> 4) & 0xF); |
| 615 |
opC.dr = (sbi.Ceg_AD & 0xF); |
| 616 |
opC.rr = (sbi.Ceg_SR & 0xF); |
| 617 |
opC.sl = ((sbi.Ceg_SR >> 4) & 0xF); |
| 618 |
opC.ws = sbi.Cwave; |
| 619 |
}; |
| 620 |
|
| 621 |
sbi_t sbi_op12; // 2op (+6op portion) |
| 622 |
sbi_t sbi_op34; // 4op |
| 623 |
|
| 624 |
readSbiOpData(sbi_op12, reader); |
| 625 |
|
| 626 |
if (is_2op) { |
| 627 |
DivInstrumentFM::Operator& opM = ins->fm.op[0]; |
| 628 |
DivInstrumentFM::Operator& opC = ins->fm.op[1]; |
| 629 |
ins->fm.ops = 2; |
| 630 |
ins->name = insName; |
| 631 |
writeOp(sbi_op12, opM, opC); |
| 632 |
ins->fm.alg = (sbi_op12.FeedConnect & 0x1); |
| 633 |
ins->fm.fb = ((sbi_op12.FeedConnect >> 1) & 0x7); |
| 634 |
|
| 635 |
// SBTimbre extensions |
| 636 |
uint8_t perc_voc = reader.readC(); |
| 637 |
if (perc_voc >= 6) { |
| 638 |
ins->fm.opllPreset = (uint8_t)(1 << 4); |
| 639 |
} |
| 640 |
|
| 641 |
// Ignore rest of file - rest is 'reserved padding'. |
| 642 |
reader.seek(4, SEEK_CUR); |
| 643 |
insList.push_back(ins); |
| 644 |
|
| 645 |
} else if (is_4op || is_6op) { |
| 646 |
readSbiOpData(sbi_op34, reader); |
| 647 |
|
| 648 |
// Operator placement is different so need to place in correct registers. |
| 649 |
// Note: 6op is an unofficial extension of 4op SBIs by Darron Broad (Freq Monster 801). |
| 650 |
// We'll only use the 4op portion here for pure OPL3. |
| 651 |
DivInstrumentFM::Operator& opM = ins->fm.op[0]; |
| 652 |
DivInstrumentFM::Operator& opC = ins->fm.op[2]; |
| 653 |
DivInstrumentFM::Operator& opM4 = ins->fm.op[1]; |
| 654 |
DivInstrumentFM::Operator& opC4 = ins->fm.op[3]; |
| 655 |
ins->fm.ops = 4; |
| 656 |
ins->name = insName; |
| 657 |
ins->fm.alg = (sbi_op12.FeedConnect & 0x1) | ((sbi_op34.FeedConnect & 0x1) << 1); |
| 658 |
ins->fm.fb = ((sbi_op34.FeedConnect >> 1) & 0x7); |
| 659 |
writeOp(sbi_op12, opM, opC); |
| 660 |
writeOp(sbi_op34, opM4, opC4); |
| 661 |
|
| 662 |
if (is_6op) { |
| 663 |
// Freq Monster 801 6op SBIs use a 4+2op layout |
| 664 |
// Save the 4op portion before reading the 2op part |
| 665 |
ins->name = fmt::sprintf("%s (4op)", ins->name); |
| 666 |
insList.push_back(ins); |
| 667 |
|
| 668 |
readSbiOpData(sbi_op12, reader); |
| 669 |
|
| 670 |
ins = new DivInstrument; |
| 671 |
DivInstrumentFM::Operator& opM6 = ins->fm.op[0]; |
| 672 |
DivInstrumentFM::Operator& opC6 = ins->fm.op[1]; |
| 673 |
ins->type = DIV_INS_OPL; |
| 674 |
ins->fm.ops = 2; |
| 675 |
ins->name = fmt::sprintf("%s (2op)", insName); |
| 676 |
writeOp(sbi_op12, opM6, opC6); |
| 677 |
ins->fm.alg = (sbi_op12.FeedConnect & 0x1); |
| 678 |
ins->fm.fb = ((sbi_op12.FeedConnect >> 1) & 0x7); |
| 679 |
} |
| 680 |
|
| 681 |
// Ignore rest of file once we've read in all we need. |
| 682 |
// Note: Freq Monster 801 adds a ton of other additional fields irrelevant to chip registers. |
| 683 |
// If instrument transpose is ever supported, we can read it in maybe? |
| 684 |
reader.seek(0, SEEK_END); |
| 685 |
insList.push_back(ins); |
| 686 |
} |
| 687 |
|
| 688 |
} catch (EndOfFileException& e) { |
| 689 |
lastError="premature end of file"; |
| 690 |
logE("premature end of file"); |
| 691 |
if (ins != NULL) { |
| 692 |
delete ins; |
| 693 |
} |
| 694 |
for (DivInstrument* p : insList) { |
| 695 |
delete p; |
| 696 |
} |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
for (DivInstrument* p : insList) { |
| 701 |
ret.push_back(p); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
void DivEngine::loadOPLI(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 706 |
std::vector<DivInstrument*> insList; // in case 2x2op |
| 707 |
DivInstrument* ins = new DivInstrument; |
| 708 |
|
| 709 |
auto readOpliOp = [](SafeReader& reader, DivInstrumentFM::Operator& op) { |
| 710 |
uint8_t characteristics = reader.readC(); |
| 711 |
uint8_t keyScaleLevel = reader.readC(); |
| 712 |
uint8_t attackDecay = reader.readC(); |
| 713 |
uint8_t sustainRelease = reader.readC(); |
| 714 |
uint8_t waveSelect = reader.readC(); |
| 715 |
|
| 716 |
op.mult = characteristics & 0xF; |
| 717 |
op.ksr = ((characteristics >> 4) & 0x1); |
| 718 |
op.sus = ((characteristics >> 5) & 0x1); |
| 719 |
op.vib = ((characteristics >> 6) & 0x1); |
| 720 |
op.am = ((characteristics >> 7) & 0x1); |
| 721 |
op.tl = keyScaleLevel & 0x3F; |
| 722 |
op.ksl = ((keyScaleLevel >> 6) & 0x3); |
| 723 |
op.ar = ((attackDecay >> 4) & 0xF); |
| 724 |
op.dr = attackDecay & 0xF; |
| 725 |
op.rr = sustainRelease & 0xF; |
| 726 |
op.sl = ((sustainRelease >> 4) & 0xF); |
| 727 |
op.ws = waveSelect; |
| 728 |
}; |
| 729 |
|
| 730 |
try { |
| 731 |
reader.seek(0, SEEK_SET); |
| 732 |
String header = reader.readString(11); |
| 733 |
|
| 734 |
if (header == "WOPL3-INST") { |
| 735 |
reader.readS(); // skip version (presently no difference here) |
| 736 |
reader.readC(); // skip isPerc field |
| 737 |
|
| 738 |
ins->type = DIV_INS_OPL; |
| 739 |
String insName = reader.readString(32); |
| 740 |
insName = stringNotBlank(insName) ? insName : stripPath; |
| 741 |
ins->name = insName; |
| 742 |
// TODO adapt MIDI key offset to transpose? |
| 743 |
reader.seek(7, SEEK_CUR); // skip MIDI params |
| 744 |
uint8_t instTypeFlags = reader.readC(); // [0EEEDCBA] - see WOPL/OPLI spec |
| 745 |
|
| 746 |
bool is_4op = ((instTypeFlags & 0x1) == 1); |
| 747 |
bool is_2x2op = (((instTypeFlags>>1) & 0x1) == 1); |
| 748 |
bool is_rhythm = (((instTypeFlags>>4) & 0x7) > 0); |
| 749 |
|
| 750 |
uint8_t feedConnect = reader.readC(); |
| 751 |
uint8_t feedConnect2nd = reader.readC(); |
| 752 |
|
| 753 |
ins->fm.alg = (feedConnect & 0x1); |
| 754 |
ins->fm.fb = ((feedConnect >> 1) & 0xF); |
| 755 |
|
| 756 |
if (is_4op && !is_2x2op) { |
| 757 |
ins->fm.ops = 4; |
| 758 |
ins->fm.alg = (feedConnect & 0x1) | ((feedConnect2nd & 0x1) << 1); |
| 759 |
for (int i : {2,0,3,1}) { // omfg >_< |
| 760 |
readOpliOp(reader, ins->fm.op[i]); |
| 761 |
} |
| 762 |
} else { |
| 763 |
ins->fm.ops = 2; |
| 764 |
for (int i : {1,0}) { |
| 765 |
readOpliOp(reader, ins->fm.op[i]); |
| 766 |
} |
| 767 |
if (is_rhythm) { |
| 768 |
ins->fm.opllPreset = (uint8_t)(1<<4); |
| 769 |
|
| 770 |
} else if (is_2x2op) { |
| 771 |
// Note: Pair detuning offset not mappable. Use E5xx effect :P |
| 772 |
ins->name = fmt::sprintf("%s (1)", insName); |
| 773 |
insList.push_back(ins); |
| 774 |
|
| 775 |
ins = new DivInstrument; |
| 776 |
ins->type = DIV_INS_OPL; |
| 777 |
ins->name = fmt::sprintf("%s (2)", insName); |
| 778 |
ins->fm.alg = (feedConnect2nd & 0x1); |
| 779 |
ins->fm.fb = ((feedConnect2nd >> 1) & 0xF); |
| 780 |
for (int i : {1,0}) { |
| 781 |
readOpliOp(reader, ins->fm.op[i]); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
if (!is_2x2op) { |
| 786 |
reader.seek(10, SEEK_CUR); // skip unused operator pair |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
insList.push_back(ins); |
| 791 |
} |
| 792 |
} catch (EndOfFileException& e) { |
| 793 |
lastError="premature end of file"; |
| 794 |
logE("premature end of file"); |
| 795 |
if (ins != NULL) { |
| 796 |
delete ins; |
| 797 |
} |
| 798 |
for (DivInstrument* p : insList) { |
| 799 |
delete p; |
| 800 |
} |
| 801 |
return; |
| 802 |
} |
| 803 |
|
| 804 |
for (DivInstrument* p : insList) { |
| 805 |
ret.push_back(p); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
void DivEngine::loadOPNI(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 810 |
DivInstrument* ins = new DivInstrument; |
| 811 |
|
| 812 |
try { |
| 813 |
reader.seek(0, SEEK_SET); |
| 814 |
|
| 815 |
String header = reader.readString(11); |
| 816 |
if (header == "WOPN2-INST" || header == "WOPN2-IN2T") { // omfg >_< |
| 817 |
uint16_t version = reader.readS(); |
| 818 |
if (!(version >= 2) || version > 0xF) { |
| 819 |
// version 1 doesn't have a version field........ |
| 820 |
reader.seek(-2, SEEK_CUR); |
| 821 |
version = 1; |
| 822 |
} |
| 823 |
|
| 824 |
reader.readC(); // skip isPerc |
| 825 |
ins->type = DIV_INS_FM; |
| 826 |
ins->fm.ops = 4; |
| 827 |
|
| 828 |
String insName = reader.readString(32); |
| 829 |
ins->name = stringNotBlank(insName) ? insName : stripPath; |
| 830 |
// TODO adapt MIDI key offset to transpose? |
| 831 |
if (!reader.seek(3, SEEK_CUR)) { // skip MIDI params |
| 832 |
throw EndOfFileException(&reader, reader.tell() + 3); |
| 833 |
} |
| 834 |
uint8_t feedAlgo = reader.readC(); |
| 835 |
ins->fm.alg = (feedAlgo & 0x7); |
| 836 |
ins->fm.fb = ((feedAlgo>>3) & 0x7); |
| 837 |
reader.readC(); // Skip global bank flags - see WOPN/OPNI spec |
| 838 |
|
| 839 |
auto readOpniOp = [](SafeReader& reader, DivInstrumentFM::Operator& op) { |
| 840 |
uint8_t dtMul = reader.readC(); |
| 841 |
uint8_t totalLevel = reader.readC(); |
| 842 |
uint8_t arRateScale = reader.readC(); |
| 843 |
uint8_t drAmpEnable = reader.readC(); |
| 844 |
uint8_t d2r = reader.readC(); |
| 845 |
uint8_t susRelease = reader.readC(); |
| 846 |
uint8_t ssgEg = reader.readC(); |
| 847 |
|
| 848 |
op.mult = dtMul & 0xF; |
| 849 |
op.dt = ((dtMul >> 4) & 0x7); |
| 850 |
op.tl = totalLevel & 0x7F; |
| 851 |
op.rs = ((arRateScale >> 6) & 0x3); |
| 852 |
op.ar = arRateScale & 0x1F; |
| 853 |
op.dr = drAmpEnable & 0x1F; |
| 854 |
op.am = ((drAmpEnable >> 7) & 0x1); |
| 855 |
op.d2r = d2r & 0x1F; |
| 856 |
op.rr = susRelease & 0xF; |
| 857 |
op.sl = ((susRelease >> 4) & 0xF); |
| 858 |
op.ssgEnv = ssgEg; |
| 859 |
}; |
| 860 |
|
| 861 |
for (int i = 0; i < 4; ++i) { |
| 862 |
readOpniOp(reader, ins->fm.op[i]); |
| 863 |
} |
| 864 |
|
| 865 |
ret.push_back(ins); |
| 866 |
} |
| 867 |
} catch (EndOfFileException& e) { |
| 868 |
lastError="premature end of file"; |
| 869 |
logE("premature end of file"); |
| 870 |
if (ins != NULL) { |
| 871 |
delete ins; |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
void DivEngine::loadY12(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 877 |
DivInstrument *ins = new DivInstrument; |
| 878 |
|
| 879 |
try { |
| 880 |
reader.seek(0, SEEK_SET); |
| 881 |
ins->type = DIV_INS_FM; |
| 882 |
ins->fm.ops = 4; |
| 883 |
ins->name = stripPath; |
| 884 |
|
| 885 |
for (int i = 0; i < 4; ++i) { |
| 886 |
DivInstrumentFM::Operator& insOp = ins->fm.op[i]; |
| 887 |
uint8_t tmp = reader.readC(); |
| 888 |
insOp.mult = tmp & 0xF; |
| 889 |
// ??? |
| 890 |
insOp.dt = ((3 + (tmp >> 4)) & 0x7); |
| 891 |
insOp.tl = (reader.readC() & 0x7F); |
| 892 |
tmp = reader.readC(); |
| 893 |
insOp.rs = ((tmp >> 6) & 0x3); |
| 894 |
insOp.ar = tmp & 0x1F; |
| 895 |
tmp = reader.readC(); |
| 896 |
insOp.dr = tmp & 0x1F; |
| 897 |
insOp.am = ((tmp >> 7) & 0x1); |
| 898 |
insOp.d2r = (reader.readC() & 0x1F); |
| 899 |
tmp = reader.readC(); |
| 900 |
insOp.rr = tmp & 0xF; |
| 901 |
insOp.sl = ((tmp >> 4) & 0xF); |
| 902 |
insOp.ssgEnv = reader.readC(); |
| 903 |
if (!reader.seek(9, SEEK_CUR)) { |
| 904 |
throw EndOfFileException(&reader, reader.tell() + 9); |
| 905 |
} |
| 906 |
} |
| 907 |
ins->fm.alg = reader.readC(); |
| 908 |
ins->fm.fb = reader.readC(); |
| 909 |
if (!reader.seek(62, SEEK_CUR)) { |
| 910 |
throw EndOfFileException(&reader, reader.tell() + 62); |
| 911 |
} |
| 912 |
ret.push_back(ins); |
| 913 |
} catch (EndOfFileException& e) { |
| 914 |
lastError="premature end of file"; |
| 915 |
logE("premature end of file"); |
| 916 |
if (ins != NULL) { |
| 917 |
delete ins; |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
void DivEngine::loadBNK(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 923 |
std::vector<DivInstrument*> insList; |
| 924 |
std::vector<String*> instNames; |
| 925 |
reader.seek(0, SEEK_SET); |
| 926 |
|
| 927 |
// First distinguish between GEMS BNK and Adlib BNK |
| 928 |
uint64_t header = reader.readL(); |
| 929 |
bool is_adlib = ((header>>8) == 0x2d42494c444100L); |
| 930 |
bool is_failed = false; |
| 931 |
int readCount = 0; |
| 932 |
int insCount = 0; |
| 933 |
|
| 934 |
if (is_adlib) { |
| 935 |
try { |
| 936 |
reader.seek(0x0c, SEEK_SET); |
| 937 |
uint32_t name_offset = reader.readI(); |
| 938 |
reader.seek(0x10, SEEK_SET); |
| 939 |
uint32_t data_offset = reader.readI(); |
| 940 |
|
| 941 |
// Seek to BNK patch names |
| 942 |
reader.seek(name_offset, SEEK_SET); |
| 943 |
while (reader.tell() < data_offset) { |
| 944 |
reader.seek(3, SEEK_CUR); |
| 945 |
instNames.push_back(new String(reader.readString(9))); |
| 946 |
++insCount; |
| 947 |
} |
| 948 |
|
| 949 |
// Seek to BNK data |
| 950 |
if (!reader.seek(data_offset, SEEK_SET)) { |
| 951 |
throw EndOfFileException(&reader, data_offset); |
| 952 |
}; |
| 953 |
|
| 954 |
// Read until all patches have been accounted for. |
| 955 |
for (int i = 0; i < insCount; ++i) { |
| 956 |
DivInstrument *ins = new DivInstrument; |
| 957 |
|
| 958 |
ins->type = DIV_INS_OPL; |
| 959 |
ins->fm.ops = 2; |
| 960 |
|
| 961 |
uint8_t timbreMode = reader.readC(); |
| 962 |
reader.readC(); // skip timbre perc voice |
| 963 |
if (timbreMode == 1) { |
| 964 |
ins->fm.opllPreset = (uint8_t)(1<<4); |
| 965 |
} |
| 966 |
|
| 967 |
for (int i = 0; i < 2; ++i) { |
| 968 |
ins->fm.op[i].ksl = reader.readC(); |
| 969 |
ins->fm.op[i].mult = reader.readC(); |
| 970 |
uint8_t fb = reader.readC(); |
| 971 |
if (i==0) { |
| 972 |
ins->fm.fb = fb; |
| 973 |
} |
| 974 |
ins->fm.op[i].ar = reader.readC(); |
| 975 |
ins->fm.op[i].sl = reader.readC(); |
| 976 |
ins->fm.op[i].sus = (reader.readC() != 0) ? 1 : 0; |
| 977 |
ins->fm.op[i].dr = reader.readC(); |
| 978 |
ins->fm.op[i].rr = reader.readC(); |
| 979 |
ins->fm.op[i].tl = reader.readC(); |
| 980 |
ins->fm.op[i].am = reader.readC(); |
| 981 |
ins->fm.op[i].vib = reader.readC(); |
| 982 |
ins->fm.op[i].ksr = reader.readC(); |
| 983 |
uint8_t alg = (reader.readC() == 0) ? 1 : 0; |
| 984 |
if (i==0) { |
| 985 |
ins->fm.alg = alg; |
| 986 |
} |
| 987 |
} |
| 988 |
ins->fm.op[0].ws = reader.readC(); |
| 989 |
ins->fm.op[1].ws = reader.readC(); |
| 990 |
ins->name = stringNotBlank(*instNames[i]) ? (*instNames[i]) : fmt::sprintf("%s[%d]", stripPath, i); |
| 991 |
|
| 992 |
insList.push_back(ins); |
| 993 |
++readCount; |
| 994 |
} |
| 995 |
// All data read, don't care about the rest. |
| 996 |
reader.seek(0, SEEK_END); |
| 997 |
|
| 998 |
} catch (EndOfFileException& e) { |
| 999 |
lastError="premature end of file"; |
| 1000 |
logE("premature end of file"); |
| 1001 |
for (int i = 0; i < readCount; ++i) { |
| 1002 |
delete insList[i]; |
| 1003 |
} |
| 1004 |
is_failed = true; |
| 1005 |
} |
| 1006 |
|
| 1007 |
} else { |
| 1008 |
// assume GEMS BNK for now. |
| 1009 |
lastError="GEMS BNK currently not supported."; |
| 1010 |
logE("GEMS BNK currently not supported."); |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (!is_failed) { |
| 1014 |
for (int i = 0; i < readCount; ++i) { |
| 1015 |
ret.push_back(insList[i]); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
for (String* name : instNames) { |
| 1020 |
delete name; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
void DivEngine::loadFF(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 1025 |
DivInstrument* insList[256]; |
| 1026 |
memset(insList,0,256*sizeof(void*)); |
| 1027 |
int readCount = 0; |
| 1028 |
size_t insCount = reader.size(); |
| 1029 |
insCount = (insCount >> 5) + (((insCount % 0x20) > 0) ? 1 : 0); |
| 1030 |
if (insCount > 256) insCount = 256; |
| 1031 |
uint8_t buf; |
| 1032 |
try { |
| 1033 |
reader.seek(0, SEEK_SET); |
| 1034 |
for (unsigned int i = 0; i < insCount; ++i) { |
| 1035 |
insList[i] = new DivInstrument; |
| 1036 |
DivInstrument* ins = insList[i]; |
| 1037 |
|
| 1038 |
ins->type = DIV_INS_FM; |
| 1039 |
DivInstrumentFM::Operator op; |
| 1040 |
|
| 1041 |
for (unsigned int j = 0; j < 4; j++) { |
| 1042 |
buf = reader.readC(); |
| 1043 |
ins->fm.op[j].mult = buf & 0xf; |
| 1044 |
ins->fm.op[j].dt = fmDtRegisterToFurnace((buf >> 4) & 0x7); |
| 1045 |
ins->fm.op[j].ssgEnv = (buf >> 4) & 0x8; |
| 1046 |
} |
| 1047 |
for (unsigned int j = 0; j < 4; j++) { |
| 1048 |
buf = reader.readC(); |
| 1049 |
ins->fm.op[j].tl = buf & 0x7f; |
| 1050 |
ins->fm.op[j].ssgEnv |= (buf >> 5) & 0x4; |
| 1051 |
} |
| 1052 |
for (unsigned int j = 0; j < 4; j++) { |
| 1053 |
buf = reader.readC(); |
| 1054 |
ins->fm.op[j].ar = buf & 0x1f; |
| 1055 |
ins->fm.op[j].rs = buf >> 6; |
| 1056 |
} |
| 1057 |
for (unsigned int j = 0; j < 4; j++) { |
| 1058 |
buf = reader.readC(); |
| 1059 |
ins->fm.op[j].dr = buf & 0x1f; |
| 1060 |
ins->fm.op[j].ssgEnv |= (buf >> 5) & 0x3; |
| 1061 |
ins->fm.op[j].am = buf >> 7; |
| 1062 |
} |
| 1063 |
for (unsigned int j = 0; j < 4; j++) { |
| 1064 |
buf = reader.readC(); |
| 1065 |
ins->fm.op[j].d2r = buf & 0x1f; |
| 1066 |
} |
| 1067 |
for (unsigned int j = 0; j < 4; j++) { |
| 1068 |
buf = reader.readC(); |
| 1069 |
ins->fm.op[j].rr = buf & 0xf; |
| 1070 |
ins->fm.op[j].sl = buf >> 4; |
| 1071 |
} |
| 1072 |
|
| 1073 |
buf = reader.readC(); |
| 1074 |
ins->fm.alg = buf & 0x7; |
| 1075 |
ins->fm.fb = (buf >> 3) & 0x7; |
| 1076 |
|
| 1077 |
// FIXME This is encoded in Shift-JIS |
| 1078 |
ins->name = reader.readString(7); |
| 1079 |
++readCount; |
| 1080 |
} |
| 1081 |
} catch (EndOfFileException& e) { |
| 1082 |
lastError="premature end of file"; |
| 1083 |
logE("premature end of file"); |
| 1084 |
// Include incomplete entry in deletion. |
| 1085 |
for (int i = readCount; i >= 0; --i) { |
| 1086 |
delete insList[i]; |
| 1087 |
} |
| 1088 |
return; |
| 1089 |
} |
| 1090 |
|
| 1091 |
for (unsigned int i = 0; i < insCount; ++i) { |
| 1092 |
ret.push_back(insList[i]); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
void DivEngine::loadGYB(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 1097 |
std::vector<DivInstrument*> insList; |
| 1098 |
int readCount = 0; |
| 1099 |
bool is_failed = false; |
| 1100 |
|
| 1101 |
auto readInstrument = [&](SafeReader& reader, bool readRegB4) -> DivInstrument* { |
| 1102 |
const int opOrder[] = { 0,1,2,3 }; |
| 1103 |
DivInstrument* ins = new DivInstrument; |
| 1104 |
ins->type = DIV_INS_FM; |
| 1105 |
ins->fm.ops = 4; |
| 1106 |
|
| 1107 |
// see https://plutiedev.com/ym2612-registers |
| 1108 |
// and https://github.com/Wohlstand/OPN2BankEditor/blob/master/Specifications/GYB-file-specification.txt |
| 1109 |
|
| 1110 |
try { |
| 1111 |
uint8_t reg; |
| 1112 |
for (int i : opOrder) { |
| 1113 |
reg = reader.readC(); // MUL/DT |
| 1114 |
ins->fm.op[i].mult = reg & 0xF; |
| 1115 |
ins->fm.op[i].dt = fmDtRegisterToFurnace((reg >> 4) & 0x7); |
| 1116 |
} |
| 1117 |
for (int i : opOrder) { |
| 1118 |
reg = reader.readC(); // TL |
| 1119 |
ins->fm.op[i].tl = reg & 0x7F; |
| 1120 |
} |
| 1121 |
for (int i : opOrder) { |
| 1122 |
reg = reader.readC(); // AR/RS |
| 1123 |
ins->fm.op[i].ar = reg & 0x1F; |
| 1124 |
ins->fm.op[i].rs = ((reg >> 6) & 0x3); |
| 1125 |
} |
| 1126 |
for (int i : opOrder) { |
| 1127 |
reg = reader.readC(); // DR/AM-ENA |
| 1128 |
ins->fm.op[i].dr = reg & 0x1F; |
| 1129 |
ins->fm.op[i].am = ((reg >> 7) & 0x1); |
| 1130 |
} |
| 1131 |
for (int i : opOrder) { |
| 1132 |
reg = reader.readC(); // SR (D2R) |
| 1133 |
ins->fm.op[i].d2r = reg & 0x1F; |
| 1134 |
} |
| 1135 |
for (int i : opOrder) { |
| 1136 |
reg = reader.readC(); // RR/SL |
| 1137 |
ins->fm.op[i].rr = reg & 0xF; |
| 1138 |
ins->fm.op[i].sl = ((reg >> 4) & 0xF); |
| 1139 |
} |
| 1140 |
for (int i : opOrder) { |
| 1141 |
reg = reader.readC(); // SSG-EG |
| 1142 |
ins->fm.op[i].ssgEnv = reg & 0xF; |
| 1143 |
} |
| 1144 |
// ALG/FB |
| 1145 |
reg = reader.readC(); |
| 1146 |
ins->fm.alg = reg & 0x7; |
| 1147 |
ins->fm.fb = ((reg >> 3) & 0x7); |
| 1148 |
|
| 1149 |
if (readRegB4) { // PAN / PMS / AMS |
| 1150 |
reg = reader.readC(); |
| 1151 |
ins->fm.fms = reg & 0x7; |
| 1152 |
ins->fm.ams = ((reg >> 4) & 0x3); |
| 1153 |
} |
| 1154 |
insList.push_back(ins); |
| 1155 |
++readCount; |
| 1156 |
return ins; |
| 1157 |
|
| 1158 |
} catch (...) { |
| 1159 |
// Deallocate and rethrow to outer handler |
| 1160 |
delete ins; |
| 1161 |
throw; |
| 1162 |
} |
| 1163 |
}; |
| 1164 |
auto readInstrumentName = [&](SafeReader& reader, DivInstrument* ins) { |
| 1165 |
uint8_t nameLen = reader.readC(); |
| 1166 |
String insName = (nameLen>0) ? reader.readString(nameLen) : ""; |
| 1167 |
ins->name = stringNotBlank(insName) |
| 1168 |
? insName |
| 1169 |
: fmt::sprintf("%s [%d]", stripPath, readCount - 1); |
| 1170 |
}; |
| 1171 |
|
| 1172 |
try { |
| 1173 |
reader.seek(0, SEEK_SET); |
| 1174 |
uint16_t header = reader.readS(); |
| 1175 |
uint8_t insMelodyCount, insDrumCount; |
| 1176 |
|
| 1177 |
if (header == 0x0C1A) { // 26 12 in decimal bytes |
| 1178 |
uint8_t version = reader.readC(); |
| 1179 |
|
| 1180 |
if ((version ^ 3) > 0) { |
| 1181 |
// GYBv1/2 |
| 1182 |
insMelodyCount = reader.readC(); |
| 1183 |
insDrumCount = reader.readC(); |
| 1184 |
|
| 1185 |
if (insMelodyCount > 128 || insDrumCount > 128) { |
| 1186 |
throw std::invalid_argument("GYBv1/2 patch count is out of bounds."); |
| 1187 |
} |
| 1188 |
|
| 1189 |
if (!reader.seek(0x100, SEEK_CUR)) { // skip MIDI instrument mapping |
| 1190 |
throw EndOfFileException(&reader, reader.tell() + 0x100); |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (version == 2) { |
| 1194 |
reader.readC(); // skip LFO speed (chip-global) |
| 1195 |
} |
| 1196 |
|
| 1197 |
// Instrument data |
| 1198 |
for (int i = 0; i < (insMelodyCount+insDrumCount); ++i) { |
| 1199 |
readInstrument(reader, (version == 2)); |
| 1200 |
|
| 1201 |
// Additional data |
| 1202 |
reader.readC(); // skip transpose |
| 1203 |
if (version == 2) { |
| 1204 |
reader.readC(); // skip padding |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
// Instrument name |
| 1209 |
for (int i = 0; i < (insMelodyCount+insDrumCount); ++i) { |
| 1210 |
readInstrumentName(reader, insList[i]); |
| 1211 |
} |
| 1212 |
|
| 1213 |
// Map to note assignment currently not supported. |
| 1214 |
|
| 1215 |
} else { |
| 1216 |
// GYBv3+ |
| 1217 |
reader.readC(); // skip LFO speed (chip-global) |
| 1218 |
uint32_t fileSize = reader.readI(); |
| 1219 |
uint32_t bankOffset = reader.readI(); |
| 1220 |
uint32_t mapOffset = reader.readI(); |
| 1221 |
|
| 1222 |
if (bankOffset > fileSize || mapOffset > fileSize) { |
| 1223 |
lastError = "GYBv3 file appears to have invalid data offsets."; |
| 1224 |
logE("GYBv3 file appears to have invalid data offsets."); |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (!reader.seek(bankOffset, SEEK_SET)) { |
| 1228 |
throw EndOfFileException(&reader, bankOffset); |
| 1229 |
} |
| 1230 |
uint16_t insCount = reader.readS(); |
| 1231 |
|
| 1232 |
size_t patchPosOffset = reader.tell(); |
| 1233 |
for (int i = 0; i < insCount; ++i) { |
| 1234 |
uint16_t patchSize = reader.readS(); |
| 1235 |
readInstrument(reader, true); |
| 1236 |
|
| 1237 |
// Additional data |
| 1238 |
reader.readC(); // skip transpose |
| 1239 |
uint8_t additionalDataFlags = reader.readC() & 0x1; // skip additional data bitfield |
| 1240 |
|
| 1241 |
// if chord notes attached, skip this |
| 1242 |
if ((additionalDataFlags&1) > 0) { |
| 1243 |
uint8_t notes = reader.readC(); |
| 1244 |
for (int j = 0; j < notes; ++j) { |
| 1245 |
reader.readC(); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
// Instrument Name |
| 1250 |
readInstrumentName(reader, insList[i]); |
| 1251 |
|
| 1252 |
// Retrieve next patch |
| 1253 |
if (!reader.seek(patchPosOffset + patchSize, SEEK_SET)) { |
| 1254 |
throw EndOfFileException(&reader, patchPosOffset + patchSize); |
| 1255 |
} |
| 1256 |
patchPosOffset = reader.tell(); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
reader.seek(0, SEEK_END); |
| 1260 |
} |
| 1261 |
|
| 1262 |
} catch (EndOfFileException& e) { |
| 1263 |
lastError = "premature end of file"; |
| 1264 |
logE("premature end of file"); |
| 1265 |
is_failed = true; |
| 1266 |
|
| 1267 |
} catch (std::invalid_argument& e) { |
| 1268 |
lastError = fmt::sprintf("Invalid value found in patch file. %s", e.what()); |
| 1269 |
logE("Invalid value found in patch file."); |
| 1270 |
logE(e.what()); |
| 1271 |
is_failed = true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (!is_failed) { |
| 1275 |
for (int i = 0; i < readCount; ++i) { |
| 1276 |
if (insList[i] != NULL) { |
| 1277 |
ret.push_back(insList[i]); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
} else { |
| 1281 |
for (int i = 0; i < readCount; ++i) { |
| 1282 |
delete insList[i]; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
void DivEngine::loadOPM(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 1288 |
std::vector<DivInstrument*> insList; |
| 1289 |
|
| 1290 |
int readCount = 0; |
| 1291 |
bool is_failed = false; |
| 1292 |
|
| 1293 |
bool patchNameRead = false, |
| 1294 |
lfoRead = false, |
| 1295 |
characteristicRead = false, |
| 1296 |
m1Read = false, |
| 1297 |
c1Read = false, |
| 1298 |
m2Read = false, |
| 1299 |
c2Read = false; |
| 1300 |
|
| 1301 |
DivInstrument* newPatch = NULL; |
| 1302 |
|
| 1303 |
auto completePatchRead = [&]() -> bool { |
| 1304 |
return patchNameRead && lfoRead && characteristicRead && m1Read && c1Read && m2Read && c2Read; |
| 1305 |
}; |
| 1306 |
auto resetPatchRead = [&]() { |
| 1307 |
patchNameRead = lfoRead = characteristicRead = m1Read = c1Read = m2Read = c2Read = false; |
| 1308 |
newPatch = NULL; |
| 1309 |
}; |
| 1310 |
auto readIntStrWithinRange = [](String&& input, int limitLow = INT_MIN, int limitHigh = INT_MAX) -> int { |
| 1311 |
int x = std::stoi(input.c_str()); |
| 1312 |
if (x > limitHigh || x < limitLow) { |
| 1313 |
throw std::invalid_argument(fmt::sprintf("%s is out of bounds of range [%d..%d]", input, limitLow, limitHigh)); |
| 1314 |
} |
| 1315 |
return x; |
| 1316 |
}; |
| 1317 |
auto readOpmOperator = [&](SafeReader& reader, DivInstrumentFM::Operator& op) { |
| 1318 |
op.ar = readIntStrWithinRange(reader.readStringToken(), 0, 31); |
| 1319 |
op.dr = readIntStrWithinRange(reader.readStringToken(), 0, 31); |
| 1320 |
op.d2r = readIntStrWithinRange(reader.readStringToken(), 0, 31); |
| 1321 |
op.rr = readIntStrWithinRange(reader.readStringToken(), 0, 31); |
| 1322 |
op.sl = readIntStrWithinRange(reader.readStringToken(), 0, 15); |
| 1323 |
op.tl = readIntStrWithinRange(reader.readStringToken(), 0, 127); |
| 1324 |
op.rs = readIntStrWithinRange(reader.readStringToken(), 0, 3);; |
| 1325 |
op.mult = readIntStrWithinRange(reader.readStringToken(), 0, 15); |
| 1326 |
op.dt = fmDtRegisterToFurnace(readIntStrWithinRange(reader.readStringToken(), 0, 7)); |
| 1327 |
op.dt2 = readIntStrWithinRange(reader.readStringToken(), 0, 3); |
| 1328 |
op.am = readIntStrWithinRange(reader.readStringToken(), 0) > 0 ? 1 : 0; |
| 1329 |
}; |
| 1330 |
auto seekGroupValStart = [](SafeReader& reader, int pos) { |
| 1331 |
// Seek to position then move to next ':' character |
| 1332 |
if (!reader.seek(pos, SEEK_SET)) { |
| 1333 |
throw EndOfFileException(&reader, pos); |
| 1334 |
} |
| 1335 |
reader.readStringToken(':', false); |
| 1336 |
}; |
| 1337 |
|
| 1338 |
try { |
| 1339 |
reader.seek(0, SEEK_SET); |
| 1340 |
while (!reader.isEOF()) { |
| 1341 |
// Checking line prefixes since they sometimes may not have a space after the ':' |
| 1342 |
size_t linePos = reader.tell(); |
| 1343 |
String token = reader.readStringToken(); |
| 1344 |
if (token.size() == 0) { |
| 1345 |
continue; |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (token.compare(0,2,"//") == 0) { |
| 1349 |
if (!reader.isEOF()) { |
| 1350 |
reader.readStringLine(); |
| 1351 |
} |
| 1352 |
continue; |
| 1353 |
} |
| 1354 |
|
| 1355 |
// At this point we know any other line would be associated with patch params |
| 1356 |
if (newPatch == NULL) { |
| 1357 |
newPatch = new DivInstrument; |
| 1358 |
newPatch->type = DIV_INS_OPM; |
| 1359 |
newPatch->fm.ops = 4; |
| 1360 |
} |
| 1361 |
|
| 1362 |
// Read each line for their respective params. They may not be written in the same LINE order but they |
| 1363 |
// must absolutely be properly grouped per patch! See inline comments indicating line structure examples. |
| 1364 |
|
| 1365 |
if (token.size() >= 2) { |
| 1366 |
if (token[0] == '@') { |
| 1367 |
// @:123 Name of patch |
| 1368 |
seekGroupValStart(reader, linePos); |
| 1369 |
// Note: Fallback to bank filename and current patch number specified by @n |
| 1370 |
String opmPatchNum = reader.readStringToken(); |
| 1371 |
String insName = reader.readStringLine(); |
| 1372 |
newPatch->name = stringNotBlank(insName) |
| 1373 |
? insName |
| 1374 |
: fmt::sprintf("%s @%s", stripPath, opmPatchNum); |
| 1375 |
patchNameRead = true; |
| 1376 |
|
| 1377 |
} else if (token.compare(0,3,"CH:") == 0) { |
| 1378 |
// CH: PAN FL CON AMS PMS SLOT NE |
| 1379 |
seekGroupValStart(reader, linePos); |
| 1380 |
reader.readStringToken(); // skip PAN |
| 1381 |
newPatch->fm.fb = readIntStrWithinRange(reader.readStringToken(), 0, 7); |
| 1382 |
newPatch->fm.alg = readIntStrWithinRange(reader.readStringToken(), 0, 7); |
| 1383 |
newPatch->fm.ams = readIntStrWithinRange(reader.readStringToken(), 0, 4); |
| 1384 |
newPatch->fm.fms = readIntStrWithinRange(reader.readStringToken(), 0, 7); |
| 1385 |
reader.readStringToken(); // skip SLOT (no furnace equivalent...yet?) |
| 1386 |
reader.readStringToken(); // skip NE (^^^) |
| 1387 |
characteristicRead = true; |
| 1388 |
|
| 1389 |
} else if (token.compare(0,3,"C1:") == 0) { |
| 1390 |
// C1: AR D1R D2R RR D1L TL KS MUL DT1 DT2 AMS-EN |
| 1391 |
seekGroupValStart(reader, linePos); |
| 1392 |
readOpmOperator(reader, newPatch->fm.op[2]); |
| 1393 |
c1Read = true; |
| 1394 |
|
| 1395 |
} else if (token.compare(0,3,"C2:") == 0) { |
| 1396 |
// C2: AR D1R D2R RR D1L TL KS MUL DT1 DT2 AMS-EN |
| 1397 |
seekGroupValStart(reader, linePos); |
| 1398 |
readOpmOperator(reader, newPatch->fm.op[3]); |
| 1399 |
c2Read = true; |
| 1400 |
|
| 1401 |
} else if (token.compare(0,3,"M1:") == 0) { |
| 1402 |
// M1: AR D1R D2R RR D1L TL KS MUL DT1 DT2 AMS-EN |
| 1403 |
seekGroupValStart(reader, linePos); |
| 1404 |
readOpmOperator(reader, newPatch->fm.op[0]); |
| 1405 |
m1Read = true; |
| 1406 |
|
| 1407 |
} else if (token.compare(0,3,"M2:") == 0) { |
| 1408 |
// M2: AR D1R D2R RR D1L TL KS MUL DT1 DT2 AMS-EN |
| 1409 |
seekGroupValStart(reader, linePos); |
| 1410 |
readOpmOperator(reader, newPatch->fm.op[1]); |
| 1411 |
m2Read = true; |
| 1412 |
|
| 1413 |
} else if (token.compare(0,4,"LFO:") == 0) { |
| 1414 |
// LFO:LFRQ AMD PMD WF NFRQ |
| 1415 |
seekGroupValStart(reader, linePos); |
| 1416 |
// Furnace patches do not store these as they are chip-global. |
| 1417 |
reader.readStringLine(); |
| 1418 |
lfoRead = true; |
| 1419 |
} else { |
| 1420 |
// other unsupported lines ignored. |
| 1421 |
reader.readStringLine(); |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (completePatchRead()) { |
| 1426 |
insList.push_back(newPatch); |
| 1427 |
resetPatchRead(); |
| 1428 |
++readCount; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
if (newPatch != NULL) { |
| 1433 |
addWarning("Last OPM patch read was incomplete and therefore not imported."); |
| 1434 |
logW("Last OPM patch read was incomplete and therefore not imported."); |
| 1435 |
delete newPatch; |
| 1436 |
newPatch = NULL; |
| 1437 |
} |
| 1438 |
|
| 1439 |
for (int i = 0; i < readCount; ++i) { |
| 1440 |
ret.push_back(insList[i]); |
| 1441 |
} |
| 1442 |
} catch (EndOfFileException& e) { |
| 1443 |
lastError="premature end of file"; |
| 1444 |
logE("premature end of file"); |
| 1445 |
is_failed = true; |
| 1446 |
} catch (std::invalid_argument& e) { |
| 1447 |
lastError=fmt::sprintf("Invalid value found in patch file. %s", e.what()); |
| 1448 |
logE("Invalid value found in patch file."); |
| 1449 |
logE(e.what()); |
| 1450 |
is_failed = true; |
| 1451 |
} |
| 1452 |
|
| 1453 |
if (is_failed) { |
| 1454 |
for (int i = readCount - 1; i >= 0; --i) { |
| 1455 |
delete insList[i]; |
| 1456 |
} |
| 1457 |
if (newPatch != NULL) { |
| 1458 |
delete newPatch; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
void DivEngine::loadWOPL(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 1465 |
std::vector<DivInstrument*> insList; |
| 1466 |
bool is_failed = false; |
| 1467 |
|
| 1468 |
uint16_t version; |
| 1469 |
uint16_t meloBankCount; |
| 1470 |
uint16_t percBankCount; |
| 1471 |
std::vector<midibank_t*> meloMetadata; |
| 1472 |
std::vector<midibank_t*> percMetadata; |
| 1473 |
|
| 1474 |
auto readWoplOp = [](SafeReader& reader, DivInstrumentFM::Operator& op) { |
| 1475 |
uint8_t characteristics = reader.readC(); |
| 1476 |
uint8_t keyScaleLevel = reader.readC(); |
| 1477 |
uint8_t attackDecay = reader.readC(); |
| 1478 |
uint8_t sustainRelease = reader.readC(); |
| 1479 |
uint8_t waveSelect = reader.readC(); |
| 1480 |
int total = 0; |
| 1481 |
|
| 1482 |
total += (op.mult = characteristics & 0xF); |
| 1483 |
total += (op.ksr = ((characteristics >> 4) & 0x1)); |
| 1484 |
total += (op.sus = ((characteristics >> 5) & 0x1)); |
| 1485 |
total += (op.vib = ((characteristics >> 6) & 0x1)); |
| 1486 |
total += (op.am = ((characteristics >> 7) & 0x1)); |
| 1487 |
total += (op.tl = keyScaleLevel & 0x3F); |
| 1488 |
total += (op.ksl = ((keyScaleLevel >> 6) & 0x3)); |
| 1489 |
total += (op.ar = ((attackDecay >> 4) & 0xF)); |
| 1490 |
total += (op.dr = attackDecay & 0xF); |
| 1491 |
total += (op.rr = sustainRelease & 0xF); |
| 1492 |
total += (op.sl = ((sustainRelease >> 4) & 0xF)); |
| 1493 |
total += (op.ws = waveSelect); |
| 1494 |
return total; |
| 1495 |
}; |
| 1496 |
|
| 1497 |
auto doParseWoplInstrument = [&](bool isPerc, midibank_t*& metadata, int patchNum) { |
| 1498 |
DivInstrument* ins = new DivInstrument; |
| 1499 |
try { |
| 1500 |
long patchSum = 0; |
| 1501 |
ins->type = DIV_INS_OPL; |
| 1502 |
|
| 1503 |
// Establish if it is a blank instrument. |
| 1504 |
String insName = reader.readString(32); |
| 1505 |
patchSum += insName.size(); |
| 1506 |
|
| 1507 |
// TODO adapt MIDI key offset to transpose? |
| 1508 |
reader.seek(7, SEEK_CUR); // skip MIDI params |
| 1509 |
uint8_t instTypeFlags = reader.readC(); // [0EEEDCBA] - see WOPL/OPLI spec |
| 1510 |
|
| 1511 |
bool is_4op = ((instTypeFlags & 0x1) == 1); |
| 1512 |
bool is_2x2op = (((instTypeFlags>>1) & 0x1) == 1); |
| 1513 |
bool is_rhythm = (((instTypeFlags>>4) & 0x7) > 0); |
| 1514 |
|
| 1515 |
uint8_t feedConnect = reader.readC(); |
| 1516 |
uint8_t feedConnect2nd = reader.readC(); |
| 1517 |
|
| 1518 |
ins->fm.alg = (feedConnect & 0x1); |
| 1519 |
ins->fm.fb = ((feedConnect>>1) & 0xF); |
| 1520 |
|
| 1521 |
if (is_4op && !is_2x2op) { |
| 1522 |
ins->fm.ops = 4; |
| 1523 |
ins->fm.alg = (feedConnect & 0x1) | ((feedConnect2nd & 0x1) << 1); |
| 1524 |
for (int i : {2,0,3,1}) { // omfg >_< |
| 1525 |
patchSum += readWoplOp(reader, ins->fm.op[i]); |
| 1526 |
} |
| 1527 |
} else { |
| 1528 |
ins->fm.ops = 2; |
| 1529 |
for (int i : {1,0}) { |
| 1530 |
patchSum += readWoplOp(reader, ins->fm.op[i]); |
| 1531 |
} |
| 1532 |
if (is_rhythm) { |
| 1533 |
ins->fm.opllPreset = (uint8_t)(1<<4); |
| 1534 |
} else if (is_2x2op) { |
| 1535 |
// Note: Pair detuning offset not mappable. Use E5xx effect :P |
| 1536 |
ins->name = stringNotBlank(insName) |
| 1537 |
? fmt::sprintf("%s (1)", insName) |
| 1538 |
: fmt::sprintf("%s[%s] %s Patch %d (1)", |
| 1539 |
stripPath, metadata->name, (isPerc) ? "Drum" : "Melodic", patchNum); |
| 1540 |
insList.push_back(ins); |
| 1541 |
patchSum = 0; |
| 1542 |
ins = new DivInstrument; |
| 1543 |
ins->type = DIV_INS_OPL; |
| 1544 |
ins->name = fmt::sprintf("%s (2)", insName); |
| 1545 |
ins->fm.alg = (feedConnect2nd & 0x1); |
| 1546 |
ins->fm.fb = ((feedConnect2nd >> 1) & 0xF); |
| 1547 |
for (int i : {1,0}) { |
| 1548 |
patchSum += readWoplOp(reader, ins->fm.op[i]); |
| 1549 |
} |
| 1550 |
} |
| 1551 |
|
| 1552 |
if (!is_2x2op) { |
| 1553 |
reader.seek(10, SEEK_CUR); // skip unused operator pair |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (version >= 3) { |
| 1558 |
reader.readS_BE(); // skip keyon delay |
| 1559 |
reader.readS_BE(); // skip keyoff delay |
| 1560 |
} |
| 1561 |
|
| 1562 |
if (patchSum > 0) { |
| 1563 |
// Write instrument |
| 1564 |
// TODO: OPL3BankEditor hardcodes GM1 Melodic patch names which are not included in the bank file...... |
| 1565 |
if (is_2x2op) { |
| 1566 |
ins->name = stringNotBlank(insName) |
| 1567 |
? fmt::sprintf("%s (2)", insName) |
| 1568 |
: fmt::sprintf("%s[%s] %s Patch %d (2)", |
| 1569 |
stripPath, metadata->name, (isPerc) ? "Drum" : "Melodic", patchNum); |
| 1570 |
} else { |
| 1571 |
ins->name = stringNotBlank(insName) |
| 1572 |
? insName |
| 1573 |
: fmt::sprintf("%s[%s] %s Patch %d", |
| 1574 |
stripPath, metadata->name, (isPerc) ? "Drum" : "Melodic", patchNum); |
| 1575 |
} |
| 1576 |
insList.push_back(ins); |
| 1577 |
} else { |
| 1578 |
// Empty instrument |
| 1579 |
delete ins; |
| 1580 |
} |
| 1581 |
} catch (...) { |
| 1582 |
// Deallocate and allow outer handler to do the rest. |
| 1583 |
delete ins; |
| 1584 |
throw; |
| 1585 |
} |
| 1586 |
}; |
| 1587 |
|
| 1588 |
try { |
| 1589 |
reader.seek(0, SEEK_SET); |
| 1590 |
|
| 1591 |
String header = reader.readString(11); |
| 1592 |
if (header == "WOPL3-BANK") { |
| 1593 |
version = reader.readS(); |
| 1594 |
meloBankCount = reader.readS_BE(); |
| 1595 |
percBankCount = reader.readS_BE(); |
| 1596 |
reader.readC(); // skip chip-global LFO |
| 1597 |
reader.readC(); // skip additional flags |
| 1598 |
|
| 1599 |
if (version >= 2) { |
| 1600 |
for (int i = 0; i < meloBankCount; ++i) { |
| 1601 |
meloMetadata.push_back(new midibank_t); |
| 1602 |
String bankName = reader.readString(32); |
| 1603 |
meloMetadata[i]->bankLsb = reader.readC(); |
| 1604 |
meloMetadata[i]->bankMsb = reader.readC(); |
| 1605 |
meloMetadata[i]->name = stringNotBlank(bankName) |
| 1606 |
? bankName |
| 1607 |
: fmt::sprintf("%d/%d", meloMetadata[i]->bankMsb, meloMetadata[i]->bankLsb); |
| 1608 |
} |
| 1609 |
|
| 1610 |
for (int i = 0; i < percBankCount; ++i) { |
| 1611 |
percMetadata.push_back(new midibank_t); |
| 1612 |
String bankName = reader.readString(32); |
| 1613 |
percMetadata[i]->bankLsb = reader.readC(); |
| 1614 |
percMetadata[i]->bankMsb = reader.readC(); |
| 1615 |
percMetadata[i]->name = stringNotBlank(bankName) |
| 1616 |
? bankName |
| 1617 |
: fmt::sprintf("%d/%d", percMetadata[i]->bankMsb, percMetadata[i]->bankLsb); |
| 1618 |
} |
| 1619 |
} else { |
| 1620 |
// TODO do version 1 multibank sets even exist? |
| 1621 |
meloMetadata.push_back(new midibank_t); |
| 1622 |
meloMetadata[0]->bankLsb = 0; |
| 1623 |
meloMetadata[0]->bankMsb = 0; |
| 1624 |
meloMetadata[0]->name = "0/0"; |
| 1625 |
percMetadata.push_back(new midibank_t); |
| 1626 |
percMetadata[0]->bankLsb = 0; |
| 1627 |
percMetadata[0]->bankMsb = 0; |
| 1628 |
percMetadata[0]->name = "0/0"; |
| 1629 |
} |
| 1630 |
|
| 1631 |
for (int i = 0; i < meloBankCount; ++i) { |
| 1632 |
for (int j = 0; j < 128; ++j) { |
| 1633 |
doParseWoplInstrument(false, meloMetadata[i], j); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
for (int i = 0; i < percBankCount; ++i) { |
| 1637 |
for (int j = 0; j < 128; ++j) { |
| 1638 |
doParseWoplInstrument(true, percMetadata[i], j); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
} |
| 1642 |
} catch (EndOfFileException& e) { |
| 1643 |
lastError = "premature end of file"; |
| 1644 |
logE("premature end of file"); |
| 1645 |
is_failed = true; |
| 1646 |
} |
| 1647 |
|
| 1648 |
for (midibank_t* m : meloMetadata) { |
| 1649 |
delete m; |
| 1650 |
} |
| 1651 |
for (midibank_t* m : percMetadata) { |
| 1652 |
delete m; |
| 1653 |
} |
| 1654 |
|
| 1655 |
if (is_failed) { |
| 1656 |
for (DivInstrument* p : insList) { |
| 1657 |
delete p; |
| 1658 |
} |
| 1659 |
} else { |
| 1660 |
for (DivInstrument* p : insList) { |
| 1661 |
ret.push_back(p); |
| 1662 |
} |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
void DivEngine::loadWOPN(SafeReader& reader, std::vector<DivInstrument*>& ret, String& stripPath) { |
| 1667 |
std::vector<DivInstrument*> insList; |
| 1668 |
bool is_failed = false; |
| 1669 |
|
| 1670 |
uint16_t version; |
| 1671 |
uint16_t meloBankCount; |
| 1672 |
uint16_t percBankCount; |
| 1673 |
std::vector<midibank_t*> meloMetadata; |
| 1674 |
std::vector<midibank_t*> percMetadata; |
| 1675 |
|
| 1676 |
auto readWopnOp = [](SafeReader& reader, DivInstrumentFM::Operator& op) { |
| 1677 |
uint8_t dtMul = reader.readC(); |
| 1678 |
uint8_t totalLevel = reader.readC(); |
| 1679 |
uint8_t arRateScale = reader.readC(); |
| 1680 |
uint8_t drAmpEnable = reader.readC(); |
| 1681 |
uint8_t d2r = reader.readC(); |
| 1682 |
uint8_t susRelease = reader.readC(); |
| 1683 |
uint8_t ssgEg = reader.readC(); |
| 1684 |
int total = 0; |
| 1685 |
|
| 1686 |
total += (op.mult = dtMul & 0xF); |
| 1687 |
total += (op.dt = ((dtMul >> 4) & 0x7)); |
| 1688 |
total += (op.tl = totalLevel & 0x7F); |
| 1689 |
total += (op.rs = ((arRateScale >> 6) & 0x3)); |
| 1690 |
total += (op.ar = arRateScale & 0x1F); |
| 1691 |
total += (op.dr = drAmpEnable & 0x1F); |
| 1692 |
total += (op.am = ((drAmpEnable >> 7) & 0x1)); |
| 1693 |
total += (op.d2r = d2r & 0x1F); |
| 1694 |
total += (op.rr = susRelease & 0xF); |
| 1695 |
total += (op.sl = ((susRelease >> 4) & 0xF)); |
| 1696 |
total += (op.ssgEnv = ssgEg); |
| 1697 |
return total; |
| 1698 |
}; |
| 1699 |
auto doParseWopnInstrument = [&](bool isPerc, midibank_t*& metadata, int patchNum) { |
| 1700 |
DivInstrument* ins = new DivInstrument; |
| 1701 |
try { |
| 1702 |
long patchSum = 0; |
| 1703 |
ins->type = DIV_INS_FM; |
| 1704 |
ins->fm.ops = 4; |
| 1705 |
|
| 1706 |
// Establish if it is a blank instrument. |
| 1707 |
String insName = reader.readString(32); |
| 1708 |
patchSum += insName.size(); |
| 1709 |
|
| 1710 |
// TODO adapt MIDI key offset to transpose? |
| 1711 |
if (!reader.seek(3, SEEK_CUR)) { // skip MIDI params |
| 1712 |
throw EndOfFileException(&reader, reader.tell() + 3); |
| 1713 |
} |
| 1714 |
uint8_t feedAlgo = reader.readC(); |
| 1715 |
patchSum += feedAlgo; |
| 1716 |
ins->fm.alg = (feedAlgo & 0x7); |
| 1717 |
ins->fm.fb = ((feedAlgo >> 3) & 0x7); |
| 1718 |
patchSum += reader.readC(); // Skip global bank flags - see WOPN/OPNI spec |
| 1719 |
|
| 1720 |
for (int i = 0; i < 4; ++i) { |
| 1721 |
patchSum += readWopnOp(reader, ins->fm.op[i]); |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (version >= 2) { |
| 1725 |
reader.readS_BE(); // skip keyon delay |
| 1726 |
reader.readS_BE(); // skip keyoff delay |
| 1727 |
} |
| 1728 |
|
| 1729 |
if (patchSum > 0) { |
| 1730 |
// Write instrument |
| 1731 |
// TODO: OPN2BankEditor hardcodes GM1 Melodic patch names which are not included in the bank file...... |
| 1732 |
ins->name = stringNotBlank(insName) |
| 1733 |
? insName |
| 1734 |
: fmt::sprintf("%s[%s] %s Patch %d", |
| 1735 |
stripPath, metadata->name, (isPerc) ? "Drum" : "Melodic", patchNum); |
| 1736 |
insList.push_back(ins); |
| 1737 |
} else { |
| 1738 |
// Empty instrument |
| 1739 |
delete ins; |
| 1740 |
} |
| 1741 |
} catch (...) { |
| 1742 |
// Deallocate and allow outer handler to do the rest. |
| 1743 |
delete ins; |
| 1744 |
throw; |
| 1745 |
} |
| 1746 |
}; |
| 1747 |
|
| 1748 |
try { |
| 1749 |
reader.seek(0, SEEK_SET); |
| 1750 |
|
| 1751 |
String header = reader.readString(11); |
| 1752 |
if (header == "WOPN2-BANK" || header == "WOPN2-B2NK") { // omfg >_< |
| 1753 |
version = reader.readS(); |
| 1754 |
if (!(version >= 2) || version > 0xF) { |
| 1755 |
// version 1 doesn't have a version field........ |
| 1756 |
reader.seek(-2, SEEK_CUR); |
| 1757 |
version = 1; |
| 1758 |
} |
| 1759 |
|
| 1760 |
meloBankCount = reader.readS_BE(); |
| 1761 |
percBankCount = reader.readS_BE(); |
| 1762 |
reader.readC(); // skip chip-global LFO |
| 1763 |
|
| 1764 |
if (version >= 2) { |
| 1765 |
for (int i = 0; i < meloBankCount; ++i) { |
| 1766 |
meloMetadata.push_back(new midibank_t); |
| 1767 |
String bankName = reader.readString(32); |
| 1768 |
meloMetadata[i]->bankLsb = reader.readC(); |
| 1769 |
meloMetadata[i]->bankMsb = reader.readC(); |
| 1770 |
meloMetadata[i]->name = stringNotBlank(bankName) |
| 1771 |
? bankName |
| 1772 |
: fmt::sprintf("%d/%d", meloMetadata[i]->bankMsb, meloMetadata[i]->bankLsb); |
| 1773 |
} |
| 1774 |
|
| 1775 |
for (int i = 0; i < percBankCount; ++i) { |
| 1776 |
percMetadata.push_back(new midibank_t); |
| 1777 |
String bankName = reader.readString(32); |
| 1778 |
percMetadata[i]->bankLsb = reader.readC(); |
| 1779 |
percMetadata[i]->bankMsb = reader.readC(); |
| 1780 |
percMetadata[i]->name = stringNotBlank(bankName) |
| 1781 |
? bankName |
| 1782 |
: fmt::sprintf("%d/%d", percMetadata[i]->bankMsb, percMetadata[i]->bankLsb); |
| 1783 |
} |
| 1784 |
} else { |
| 1785 |
// TODO do version 1 multibank sets even exist? |
| 1786 |
meloMetadata.push_back(new midibank_t); |
| 1787 |
meloMetadata[0]->bankLsb = 0; |
| 1788 |
meloMetadata[0]->bankMsb = 0; |
| 1789 |
meloMetadata[0]->name = "0/0"; |
| 1790 |
percMetadata.push_back(new midibank_t); |
| 1791 |
percMetadata[0]->bankLsb = 0; |
| 1792 |
percMetadata[0]->bankMsb = 0; |
| 1793 |
percMetadata[0]->name = "0/0"; |
| 1794 |
} |
| 1795 |
|
| 1796 |
for (int i = 0; i < meloBankCount; ++i) { |
| 1797 |
for (int j = 0; j < 128; ++j) { |
| 1798 |
doParseWopnInstrument(false, meloMetadata[i], j); |
| 1799 |
} |
| 1800 |
} |
| 1801 |
for (int i = 0; i < percBankCount; ++i) { |
| 1802 |
for (int j = 0; j < 128; ++j) { |
| 1803 |
doParseWopnInstrument(true, percMetadata[i], j); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
} |
| 1807 |
} catch (EndOfFileException& e) { |
| 1808 |
lastError = "premature end of file"; |
| 1809 |
logE("premature end of file"); |
| 1810 |
is_failed = true; |
| 1811 |
} |
| 1812 |
|
| 1813 |
for (midibank_t* m : meloMetadata) { |
| 1814 |
delete m; |
| 1815 |
} |
| 1816 |
for (midibank_t* m : percMetadata) { |
| 1817 |
delete m; |
| 1818 |
} |
| 1819 |
|
| 1820 |
if (is_failed) { |
| 1821 |
for (DivInstrument* p : insList) { |
| 1822 |
delete p; |
| 1823 |
} |
| 1824 |
} else { |
| 1825 |
for (DivInstrument* p : insList) { |
| 1826 |
ret.push_back(p); |
| 1827 |
} |
| 1828 |
} |
| 1829 |
} |
| 1830 |
|
| 1831 |
std::vector<DivInstrument*> DivEngine::instrumentFromFile(const char* path, bool loadAssets, bool readInsName) { |
| 1832 |
std::vector<DivInstrument*> ret; |
| 1833 |
warnings=""; |
| 1834 |
|
| 1835 |
const char* pathRedux=strrchr(path,DIR_SEPARATOR); |
| 1836 |
if (pathRedux==NULL) { |
| 1837 |
pathRedux=path; |
| 1838 |
} else { |
| 1839 |
pathRedux++; |
| 1840 |
} |
| 1841 |
String stripPath; |
| 1842 |
const char* pathReduxEnd=strrchr(pathRedux,'.'); |
| 1843 |
if (pathReduxEnd==NULL) { |
| 1844 |
stripPath=pathRedux; |
| 1845 |
} else { |
| 1846 |
for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) { |
| 1847 |
stripPath+=*i; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
FILE* f=ps_fopen(path,"rb"); |
| 1852 |
if (f==NULL) { |
| 1853 |
lastError=strerror(errno); |
| 1854 |
return ret; |
| 1855 |
} |
| 1856 |
unsigned char* buf; |
| 1857 |
ssize_t len; |
| 1858 |
if (fseek(f,0,SEEK_END)!=0) { |
| 1859 |
lastError=strerror(errno); |
| 1860 |
fclose(f); |
| 1861 |
return ret; |
| 1862 |
} |
| 1863 |
len=ftell(f); |
| 1864 |
if (len<0) { |
| 1865 |
lastError=strerror(errno); |
| 1866 |
fclose(f); |
| 1867 |
return ret; |
| 1868 |
} |
| 1869 |
if (len==(SIZE_MAX>>1)) { |
| 1870 |
lastError=strerror(errno); |
| 1871 |
fclose(f); |
| 1872 |
return ret; |
| 1873 |
} |
| 1874 |
if (len==0) { |
| 1875 |
lastError=strerror(errno); |
| 1876 |
fclose(f); |
| 1877 |
return ret; |
| 1878 |
} |
| 1879 |
if (fseek(f,0,SEEK_SET)!=0) { |
| 1880 |
lastError=strerror(errno); |
| 1881 |
fclose(f); |
| 1882 |
return ret; |
| 1883 |
} |
| 1884 |
buf=new unsigned char[len]; |
| 1885 |
if (fread(buf,1,len,f)!=(size_t)len) { |
| 1886 |
logW("did not read entire instrument file buffer!"); |
| 1887 |
lastError="did not read entire instrument file!"; |
| 1888 |
delete[] buf; |
| 1889 |
return ret; |
| 1890 |
} |
| 1891 |
fclose(f); |
| 1892 |
|
| 1893 |
SafeReader reader=SafeReader(buf,len); |
| 1894 |
|
| 1895 |
unsigned char magic[16]; |
| 1896 |
bool isFurnaceInstr=false; |
| 1897 |
bool isOldFurnaceIns=false; |
| 1898 |
try { |
| 1899 |
reader.read(magic,4); |
| 1900 |
if (memcmp("FINS",magic,4)==0 || memcmp("FINB",magic,4)==0) { |
| 1901 |
isFurnaceInstr=true; |
| 1902 |
logV("found a new Furnace ins"); |
| 1903 |
} else { |
| 1904 |
reader.read(&magic[4],12); |
| 1905 |
if (memcmp("-Furnace instr.-",magic,16)==0) { |
| 1906 |
logV("found an old Furnace ins"); |
| 1907 |
isFurnaceInstr=true; |
| 1908 |
isOldFurnaceIns=true; |
| 1909 |
} |
| 1910 |
} |
| 1911 |
} catch (EndOfFileException& e) { |
| 1912 |
reader.seek(0,SEEK_SET); |
| 1913 |
} |
| 1914 |
|
| 1915 |
if (isFurnaceInstr) { |
| 1916 |
DivInstrument* ins=new DivInstrument; |
| 1917 |
try { |
| 1918 |
short version=0; |
| 1919 |
if (isOldFurnaceIns) { |
| 1920 |
version=reader.readS(); |
| 1921 |
reader.readS(); // reserved |
| 1922 |
} else { |
| 1923 |
version=reader.readS(); |
| 1924 |
reader.seek(0,SEEK_SET); |
| 1925 |
} |
| 1926 |
|
| 1927 |
if (version>DIV_ENGINE_VERSION) { |
| 1928 |
warnings="this instrument is made with a more recent version of Furnace!"; |
| 1929 |
} |
| 1930 |
|
| 1931 |
if (isOldFurnaceIns) { |
| 1932 |
unsigned int dataPtr=reader.readI(); |
| 1933 |
reader.seek(dataPtr,SEEK_SET); |
| 1934 |
} |
| 1935 |
|
| 1936 |
ins->name=stripPath; |
| 1937 |
|
| 1938 |
if (ins->readInsData(reader,version,loadAssets?(&song):NULL)!=DIV_DATA_SUCCESS) { |
| 1939 |
lastError="invalid instrument header/data!"; |
| 1940 |
delete ins; |
| 1941 |
delete[] buf; |
| 1942 |
return ret; |
| 1943 |
} else { |
| 1944 |
if (!readInsName) { |
| 1945 |
ins->name=stripPath; |
| 1946 |
} |
| 1947 |
ret.push_back(ins); |
| 1948 |
} |
| 1949 |
} catch (EndOfFileException& e) { |
| 1950 |
lastError="premature end of file"; |
| 1951 |
logE("premature end of file"); |
| 1952 |
delete ins; |
| 1953 |
delete[] buf; |
| 1954 |
return ret; |
| 1955 |
} |
| 1956 |
} else { // read as a different format |
| 1957 |
const char* ext=strrchr(path,'.'); |
| 1958 |
DivInsFormats format=DIV_INSFORMAT_DMP; |
| 1959 |
if (ext!=NULL) { |
| 1960 |
String extS; |
| 1961 |
for (; *ext; ext++) { |
| 1962 |
char i=*ext; |
| 1963 |
if (i>='A' && i<='Z') { |
| 1964 |
i+='a'-'A'; |
| 1965 |
} |
| 1966 |
extS+=i; |
| 1967 |
} |
| 1968 |
if (extS==".dmp") { |
| 1969 |
format=DIV_INSFORMAT_DMP; |
| 1970 |
} else if (extS==".tfi") { |
| 1971 |
format=DIV_INSFORMAT_TFI; |
| 1972 |
} else if (extS==".vgi") { |
| 1973 |
format=DIV_INSFORMAT_VGI; |
| 1974 |
} else if (extS==".fti") { |
| 1975 |
format=DIV_INSFORMAT_FTI; |
| 1976 |
} else if (extS==".bti") { |
| 1977 |
format=DIV_INSFORMAT_BTI; |
| 1978 |
} else if (extS==".s3i") { |
| 1979 |
format=DIV_INSFORMAT_S3I; |
| 1980 |
} else if (extS==".sbi") { |
| 1981 |
format=DIV_INSFORMAT_SBI; |
| 1982 |
} else if (extS==".opli") { |
| 1983 |
format=DIV_INSFORMAT_OPLI; |
| 1984 |
} else if (extS==".opni") { |
| 1985 |
format=DIV_INSFORMAT_OPNI; |
| 1986 |
} else if (extS==".y12") { |
| 1987 |
format=DIV_INSFORMAT_Y12; |
| 1988 |
} else if (extS==".bnk") { |
| 1989 |
format=DIV_INSFORMAT_BNK; |
| 1990 |
} else if (extS==".gyb") { |
| 1991 |
format=DIV_INSFORMAT_GYB; |
| 1992 |
} else if (extS==".opm") { |
| 1993 |
format=DIV_INSFORMAT_OPM; |
| 1994 |
} else if (extS==".ff") { |
| 1995 |
format=DIV_INSFORMAT_FF; |
| 1996 |
} else if (extS==".wopl") { |
| 1997 |
format=DIV_INSFORMAT_WOPL; |
| 1998 |
} else if (extS==".wopn") { |
| 1999 |
format=DIV_INSFORMAT_WOPN; |
| 2000 |
} else { |
| 2001 |
// unknown format |
| 2002 |
lastError="unknown instrument format"; |
| 2003 |
delete[] buf; |
| 2004 |
return ret; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
switch (format) { |
| 2009 |
case DIV_INSFORMAT_DMP: |
| 2010 |
loadDMP(reader,ret,stripPath); |
| 2011 |
break; |
| 2012 |
case DIV_INSFORMAT_TFI: |
| 2013 |
loadTFI(reader,ret,stripPath); |
| 2014 |
break; |
| 2015 |
case DIV_INSFORMAT_VGI: |
| 2016 |
loadVGI(reader,ret,stripPath); |
| 2017 |
break; |
| 2018 |
case DIV_INSFORMAT_FTI: // TODO |
| 2019 |
break; |
| 2020 |
case DIV_INSFORMAT_BTI: // TODO |
| 2021 |
break; |
| 2022 |
case DIV_INSFORMAT_S3I: |
| 2023 |
loadS3I(reader,ret,stripPath); |
| 2024 |
break; |
| 2025 |
case DIV_INSFORMAT_SBI: |
| 2026 |
loadSBI(reader,ret,stripPath); |
| 2027 |
break; |
| 2028 |
case DIV_INSFORMAT_OPLI: |
| 2029 |
loadOPLI(reader,ret,stripPath); |
| 2030 |
break; |
| 2031 |
case DIV_INSFORMAT_OPNI: |
| 2032 |
loadOPNI(reader,ret,stripPath); |
| 2033 |
break; |
| 2034 |
case DIV_INSFORMAT_Y12: |
| 2035 |
loadY12(reader,ret,stripPath); |
| 2036 |
break; |
| 2037 |
case DIV_INSFORMAT_BNK: |
| 2038 |
loadBNK(reader,ret,stripPath); |
| 2039 |
break; |
| 2040 |
case DIV_INSFORMAT_FF: |
| 2041 |
loadFF(reader,ret,stripPath); |
| 2042 |
break; |
| 2043 |
case DIV_INSFORMAT_GYB: |
| 2044 |
loadGYB(reader,ret,stripPath); |
| 2045 |
break; |
| 2046 |
case DIV_INSFORMAT_OPM: |
| 2047 |
loadOPM(reader,ret,stripPath); |
| 2048 |
break; |
| 2049 |
case DIV_INSFORMAT_WOPL: |
| 2050 |
loadWOPL(reader,ret,stripPath); |
| 2051 |
break; |
| 2052 |
case DIV_INSFORMAT_WOPN: |
| 2053 |
loadWOPN(reader,ret,stripPath); |
| 2054 |
break; |
| 2055 |
} |
| 2056 |
|
| 2057 |
if (reader.tell()<reader.size()) { |
| 2058 |
addWarning("https://github.com/tildearrow/furnace/issues/84"); |
| 2059 |
addWarning("there is more data at the end of the file! what happened here!"); |
| 2060 |
addWarning(fmt::sprintf("exactly %d bytes, if you are curious",reader.size()-reader.tell())); |
| 2061 |
} |
| 2062 |
} |
| 2063 |
|
| 2064 |
delete[] buf; // since we're done with this buffer |
| 2065 |
return ret; |
| 2066 |
} |