| 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 "fileOpsCommon.h" |
| 21 |
|
| 22 |
// known version numbers: |
| 23 |
// - 27: v1.1.7 |
| 24 |
// - current format version |
| 25 |
// - adds sample start/end points |
| 26 |
// - 1.2 introduces MSX2 (AY+SCC) |
| 27 |
// - 26: v1.1.3 |
| 28 |
// - changes height of FDS wave to 6-bit (it was 4-bit before) |
| 29 |
// - 25: v1.1 |
| 30 |
// - adds pattern names (in a rather odd way) |
| 31 |
// - v1.1.4 fixes these but breaks old songs (yeah) |
| 32 |
// - introduces SMS+OPLL system |
| 33 |
// - 24: v0.12/0.13/1.0 |
| 34 |
// - changes pattern length from char to int, probably to allow for size 256 |
| 35 |
// - 23: ??? |
| 36 |
// - what happened here? |
| 37 |
// - 20: v11.1 (?) |
| 38 |
// - E5xx effect range is now ±1 semitone |
| 39 |
// - 19: v11 |
| 40 |
// - introduces Arcade system |
| 41 |
// - changes to the FM instrument format due to YMU759 being dropped |
| 42 |
// - 18: v10 |
| 43 |
// - radically changes STD instrument for Game Boy |
| 44 |
// - 17: v9 |
| 45 |
// - changes C64 volIsCutoff flag from int to char for unknown reasons |
| 46 |
// - 16: v8 (?) |
| 47 |
// - introduces C64 system |
| 48 |
// - 15: v7 (?) |
| 49 |
// - 14: v6 (?) |
| 50 |
// - introduces NES system |
| 51 |
// - changes macro and wave values from char to int |
| 52 |
// - 13: v5.1 |
| 53 |
// - introduces PC Engine system in later version (how?) |
| 54 |
// - stores highlight in file |
| 55 |
// - 12: v5 (?) |
| 56 |
// - introduces Game Boy system |
| 57 |
// - introduces wavetables |
| 58 |
// - 11: ??? |
| 59 |
// - introduces Sega Master System |
| 60 |
// - custom Hz support |
| 61 |
// - instrument type (FM/STD) present |
| 62 |
// - prior to this version the instrument type depended on the system |
| 63 |
// - 10: ??? |
| 64 |
// - introduces multiple effect columns |
| 65 |
// - 9: v3.9 |
| 66 |
// - introduces Genesis system |
| 67 |
// - introduces system number |
| 68 |
// - patterns now stored in current known format |
| 69 |
// - 8: ??? |
| 70 |
// - only used in the Medivo YMU cover |
| 71 |
// - 7: ??? |
| 72 |
// - only present in a later version of First.dmf |
| 73 |
// - pattern format changes: empty field is 0xFF instead of 0x80 |
| 74 |
// - instrument now stored in pattern |
| 75 |
// - 5: BETA 3 |
| 76 |
// - adds arpeggio tick |
| 77 |
// - 4: BETA 2 |
| 78 |
// - possibly adds instrument number (stored in channel)? |
| 79 |
// - cannot confirm as I don't have any version 4 modules |
| 80 |
// - 3: BETA 1 |
| 81 |
// - possibly the first version that could save |
| 82 |
// - basic format, no system number, 16 instruments, one speed, YMU759-only |
| 83 |
// - patterns were stored in a different format (chars instead of shorts) and no instrument |
| 84 |
// - if somebody manages to find a version 2 or even 1 module, please tell me as it will be worth a lot |
| 85 |
|
| 86 |
static double samplePitches[11]={ |
| 87 |
0.1666666666, 0.2, 0.25, 0.333333333, 0.5, |
| 88 |
1, |
| 89 |
2, 3, 4, 5, 6 |
| 90 |
}; |
| 91 |
|
| 92 |
bool DivEngine::loadDMF(unsigned char* file, size_t len) { |
| 93 |
SafeReader reader=SafeReader(file,len); |
| 94 |
warnings=""; |
| 95 |
try { |
| 96 |
DivSong ds; |
| 97 |
unsigned char historicColIns[DIV_MAX_CHANS]; |
| 98 |
for (int i=0; i<DIV_MAX_CHANS; i++) { |
| 99 |
historicColIns[i]=i; |
| 100 |
} |
| 101 |
|
| 102 |
ds.nullWave.len=32; |
| 103 |
for (int i=0; i<32; i++) { |
| 104 |
ds.nullWave.data[i]=15; |
| 105 |
} |
| 106 |
|
| 107 |
ds.isDMF=true; |
| 108 |
|
| 109 |
if (!reader.seek(16,SEEK_SET)) { |
| 110 |
logE("premature end of file!"); |
| 111 |
lastError="incomplete file"; |
| 112 |
delete[] file; |
| 113 |
return false; |
| 114 |
} |
| 115 |
ds.version=(unsigned char)reader.readC(); |
| 116 |
logI("module version %d (0x%.2x)",ds.version,ds.version); |
| 117 |
if (ds.version>0x1b) { |
| 118 |
logE("this version is not supported by Furnace yet!"); |
| 119 |
lastError="this version is not supported by Furnace yet"; |
| 120 |
delete[] file; |
| 121 |
return false; |
| 122 |
} |
| 123 |
unsigned char sys=0; |
| 124 |
ds.systemLen=1; |
| 125 |
if (ds.version<0x09) { |
| 126 |
// V E R S I O N -> 3 <- |
| 127 |
// AWESOME |
| 128 |
ds.system[0]=DIV_SYSTEM_YMU759; |
| 129 |
} else { |
| 130 |
sys=reader.readC(); |
| 131 |
ds.system[0]=systemFromFileDMF(sys); |
| 132 |
} |
| 133 |
if (ds.system[0]==DIV_SYSTEM_NULL) { |
| 134 |
logE("invalid system 0x%.2x!",sys); |
| 135 |
lastError="system not supported. running old version?"; |
| 136 |
delete[] file; |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
if (ds.system[0]==DIV_SYSTEM_YMU759 && ds.version<0x10) { |
| 141 |
ds.vendor=reader.readString((unsigned char)reader.readC()); |
| 142 |
ds.carrier=reader.readString((unsigned char)reader.readC()); |
| 143 |
ds.category=reader.readString((unsigned char)reader.readC()); |
| 144 |
ds.name=reader.readString((unsigned char)reader.readC()); |
| 145 |
ds.author=reader.readString((unsigned char)reader.readC()); |
| 146 |
ds.writer=reader.readString((unsigned char)reader.readC()); |
| 147 |
ds.composer=reader.readString((unsigned char)reader.readC()); |
| 148 |
ds.arranger=reader.readString((unsigned char)reader.readC()); |
| 149 |
ds.copyright=reader.readString((unsigned char)reader.readC()); |
| 150 |
ds.manGroup=reader.readString((unsigned char)reader.readC()); |
| 151 |
ds.manInfo=reader.readString((unsigned char)reader.readC()); |
| 152 |
ds.createdDate=reader.readString((unsigned char)reader.readC()); |
| 153 |
ds.revisionDate=reader.readString((unsigned char)reader.readC()); |
| 154 |
logI("%s by %s",ds.name.c_str(),ds.author.c_str()); |
| 155 |
logI("has YMU-specific data:"); |
| 156 |
logI("- carrier: %s",ds.carrier.c_str()); |
| 157 |
logI("- category: %s",ds.category.c_str()); |
| 158 |
logI("- vendor: %s",ds.vendor.c_str()); |
| 159 |
logI("- writer: %s",ds.writer.c_str()); |
| 160 |
logI("- composer: %s",ds.composer.c_str()); |
| 161 |
logI("- arranger: %s",ds.arranger.c_str()); |
| 162 |
logI("- copyright: %s",ds.copyright.c_str()); |
| 163 |
logI("- management group: %s",ds.manGroup.c_str()); |
| 164 |
logI("- management info: %s",ds.manInfo.c_str()); |
| 165 |
logI("- created on: %s",ds.createdDate.c_str()); |
| 166 |
logI("- revision date: %s",ds.revisionDate.c_str()); |
| 167 |
} else { |
| 168 |
ds.name=reader.readString((unsigned char)reader.readC()); |
| 169 |
ds.author=reader.readString((unsigned char)reader.readC()); |
| 170 |
logI("%s by %s",ds.name.c_str(),ds.author.c_str()); |
| 171 |
} |
| 172 |
|
| 173 |
// compatibility flags |
| 174 |
if (!getConfInt("noDMFCompat",0)) { |
| 175 |
ds.limitSlides=true; |
| 176 |
ds.linearPitch=1; |
| 177 |
ds.loopModality=0; |
| 178 |
ds.properNoiseLayout=false; |
| 179 |
ds.waveDutyIsVol=false; |
| 180 |
// TODO: WHAT?! geodude.dmf fails when this is true |
| 181 |
// but isn't that how Defle behaves??? |
| 182 |
ds.resetMacroOnPorta=false; |
| 183 |
ds.legacyVolumeSlides=true; |
| 184 |
ds.compatibleArpeggio=true; |
| 185 |
ds.noteOffResetsSlides=true; |
| 186 |
ds.targetResetsSlides=true; |
| 187 |
ds.arpNonPorta=false; |
| 188 |
ds.algMacroBehavior=false; |
| 189 |
ds.brokenShortcutSlides=false; |
| 190 |
ds.ignoreDuplicateSlides=true; |
| 191 |
ds.brokenDACMode=true; |
| 192 |
ds.oneTickCut=false; |
| 193 |
ds.newInsTriggersInPorta=true; |
| 194 |
ds.arp0Reset=true; |
| 195 |
ds.brokenSpeedSel=true; |
| 196 |
ds.noSlidesOnFirstTick=false; |
| 197 |
ds.rowResetsArpPos=false; |
| 198 |
ds.ignoreJumpAtEnd=true; |
| 199 |
ds.buggyPortaAfterSlide=true; |
| 200 |
ds.gbInsAffectsEnvelope=true; |
| 201 |
ds.ignoreDACModeOutsideIntendedChannel=false; |
| 202 |
ds.e1e2AlsoTakePriority=true; |
| 203 |
ds.fbPortaPause=true; |
| 204 |
ds.snDutyReset=true; |
| 205 |
ds.oldOctaveBoundary=false; |
| 206 |
ds.noOPN2Vol=true; |
| 207 |
ds.newVolumeScaling=false; |
| 208 |
ds.volMacroLinger=false; |
| 209 |
ds.brokenOutVol=true; |
| 210 |
ds.brokenOutVol2=true; |
| 211 |
ds.e1e2StopOnSameNote=true; |
| 212 |
ds.brokenPortaArp=false; |
| 213 |
ds.snNoLowPeriods=true; |
| 214 |
ds.disableSampleMacro=true; |
| 215 |
ds.preNoteNoEffect=true; |
| 216 |
ds.oldDPCM=true; |
| 217 |
ds.delayBehavior=0; |
| 218 |
ds.jumpTreatment=2; |
| 219 |
ds.oldAlwaysSetVolume=true; |
| 220 |
|
| 221 |
// 1.1 compat flags |
| 222 |
if (ds.version>24) { |
| 223 |
ds.waveDutyIsVol=true; |
| 224 |
ds.legacyVolumeSlides=false; |
| 225 |
} |
| 226 |
|
| 227 |
// Neo Geo detune is caused by Defle running Neo Geo at the wrong clock. |
| 228 |
/* |
| 229 |
if (ds.system[0]==DIV_SYSTEM_YM2610 || ds.system[0]==DIV_SYSTEM_YM2610_EXT |
| 230 |
|| ds.system[0]==DIV_SYSTEM_YM2610_FULL || ds.system[0]==DIV_SYSTEM_YM2610_FULL_EXT |
| 231 |
|| ds.system[0]==DIV_SYSTEM_YM2610B || ds.system[0]==DIV_SYSTEM_YM2610B_EXT) { |
| 232 |
ds.tuning=443.23; |
| 233 |
} |
| 234 |
*/ |
| 235 |
|
| 236 |
// Genesis detuned on Defle v10 and earlier |
| 237 |
/*if (ds.version<19 && ds.system[0]==DIV_SYSTEM_GENESIS) { |
| 238 |
ds.tuning=443.23; |
| 239 |
}*/ |
| 240 |
// C64 detuned on Defle v11 and earlier |
| 241 |
/*if (ds.version<21 && (ds.system[0]==DIV_SYSTEM_C64_6581 || ds.system[0]==DIV_SYSTEM_C64_8580)) { |
| 242 |
ds.tuning=433.2; |
| 243 |
}*/ |
| 244 |
|
| 245 |
// Game Boy arp+soundLen screwery |
| 246 |
if (ds.system[0]==DIV_SYSTEM_GB) { |
| 247 |
ds.systemFlags[0].set("enoughAlready",true); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
logI("reading module data..."); |
| 252 |
if (ds.version>0x0c) { |
| 253 |
ds.subsong[0]->hilightA=reader.readC(); |
| 254 |
ds.subsong[0]->hilightB=reader.readC(); |
| 255 |
} |
| 256 |
|
| 257 |
bool customTempo=false; |
| 258 |
|
| 259 |
ds.subsong[0]->timeBase=reader.readC(); |
| 260 |
ds.subsong[0]->speeds.len=2; |
| 261 |
ds.subsong[0]->speeds.val[0]=reader.readC(); |
| 262 |
if (ds.version>0x07) { |
| 263 |
ds.subsong[0]->speeds.val[1]=reader.readC(); |
| 264 |
bool pal=reader.readC(); |
| 265 |
ds.subsong[0]->hz=pal?60:50; |
| 266 |
customTempo=reader.readC(); |
| 267 |
} else { |
| 268 |
ds.subsong[0]->speeds.len=1; |
| 269 |
} |
| 270 |
if (ds.version>0x0a) { |
| 271 |
String hz=reader.readString(3); |
| 272 |
if (customTempo) { |
| 273 |
try { |
| 274 |
ds.subsong[0]->hz=std::stoi(hz); |
| 275 |
} catch (std::exception& e) { |
| 276 |
logW("invalid custom Hz!"); |
| 277 |
ds.subsong[0]->hz=60; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
if (ds.version>0x17) { |
| 282 |
ds.subsong[0]->patLen=reader.readI(); |
| 283 |
} else { |
| 284 |
ds.subsong[0]->patLen=(unsigned char)reader.readC(); |
| 285 |
} |
| 286 |
ds.subsong[0]->ordersLen=(unsigned char)reader.readC(); |
| 287 |
|
| 288 |
if (ds.subsong[0]->patLen<0) { |
| 289 |
logE("pattern length is negative!"); |
| 290 |
lastError="pattern lengrh is negative!"; |
| 291 |
delete[] file; |
| 292 |
return false; |
| 293 |
} |
| 294 |
if (ds.subsong[0]->patLen>256) { |
| 295 |
logE("pattern length is too large!"); |
| 296 |
lastError="pattern length is too large!"; |
| 297 |
delete[] file; |
| 298 |
return false; |
| 299 |
} |
| 300 |
if (ds.subsong[0]->ordersLen<0) { |
| 301 |
logE("song length is negative!"); |
| 302 |
lastError="song length is negative!"; |
| 303 |
delete[] file; |
| 304 |
return false; |
| 305 |
} |
| 306 |
if (ds.subsong[0]->ordersLen>127) { |
| 307 |
logE("song is too long!"); |
| 308 |
lastError="song is too long!"; |
| 309 |
delete[] file; |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
if (ds.version<20 && ds.version>3) { |
| 314 |
ds.subsong[0]->arpLen=reader.readC(); |
| 315 |
} else { |
| 316 |
ds.subsong[0]->arpLen=1; |
| 317 |
} |
| 318 |
|
| 319 |
if (ds.system[0]==DIV_SYSTEM_YMU759) { |
| 320 |
switch (ds.subsong[0]->timeBase) { |
| 321 |
case 0: |
| 322 |
ds.subsong[0]->hz=248; |
| 323 |
break; |
| 324 |
case 1: |
| 325 |
ds.subsong[0]->hz=200; |
| 326 |
break; |
| 327 |
case 2: |
| 328 |
ds.subsong[0]->hz=100; |
| 329 |
break; |
| 330 |
case 3: |
| 331 |
ds.subsong[0]->hz=50; |
| 332 |
break; |
| 333 |
case 4: |
| 334 |
ds.subsong[0]->hz=25; |
| 335 |
break; |
| 336 |
case 5: |
| 337 |
ds.subsong[0]->hz=20; |
| 338 |
break; |
| 339 |
default: |
| 340 |
ds.subsong[0]->hz=248; |
| 341 |
break; |
| 342 |
} |
| 343 |
ds.subsong[0]->timeBase=0; |
| 344 |
addWarning("Yamaha YMU759 emulation is incomplete! please migrate your song to the OPL3 system."); |
| 345 |
} |
| 346 |
|
| 347 |
logV("%x",reader.tell()); |
| 348 |
|
| 349 |
logI("reading pattern matrix (%d * %d = %d)...",ds.subsong[0]->ordersLen,getChannelCount(ds.system[0]),ds.subsong[0]->ordersLen*getChannelCount(ds.system[0])); |
| 350 |
for (int i=0; i<getChannelCount(ds.system[0]); i++) { |
| 351 |
for (int j=0; j<ds.subsong[0]->ordersLen; j++) { |
| 352 |
ds.subsong[0]->orders.ord[i][j]=reader.readC(); |
| 353 |
if (ds.subsong[0]->orders.ord[i][j]>0x7f) { |
| 354 |
logE("order at %d, %d out of range! (%d)",i,j,ds.subsong[0]->orders.ord[i][j]); |
| 355 |
lastError=fmt::sprintf("order at %d, %d out of range! (%d)",i,j,ds.subsong[0]->orders.ord[i][j]); |
| 356 |
delete[] file; |
| 357 |
return false; |
| 358 |
} |
| 359 |
if (ds.version>0x18) { // 1.1 pattern names |
| 360 |
ds.subsong[0]->pat[i].getPattern(j,true)->name=reader.readString((unsigned char)reader.readC()); |
| 361 |
} |
| 362 |
} |
| 363 |
if (ds.version>0x03 && ds.version<0x06 && i<16) { |
| 364 |
historicColIns[i]=reader.readC(); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
logV("%x",reader.tell()); |
| 369 |
|
| 370 |
if (ds.version>0x05) { |
| 371 |
ds.insLen=(unsigned char)reader.readC(); |
| 372 |
} else { |
| 373 |
ds.insLen=16; |
| 374 |
} |
| 375 |
logI("reading instruments (%d)...",ds.insLen); |
| 376 |
if (ds.insLen>0) ds.ins.reserve(ds.insLen); |
| 377 |
for (int i=0; i<ds.insLen; i++) { |
| 378 |
DivInstrument* ins=new DivInstrument; |
| 379 |
unsigned char mode=0; |
| 380 |
if (ds.version>0x05) { |
| 381 |
ins->name=reader.readString((unsigned char)reader.readC()); |
| 382 |
} |
| 383 |
logD("%d name: %s",i,ins->name.c_str()); |
| 384 |
if (ds.version<0x0b) { |
| 385 |
// instruments in ancient versions were all FM. |
| 386 |
mode=1; |
| 387 |
} else { |
| 388 |
mode=reader.readC(); |
| 389 |
if (mode>1) logW("%d: invalid instrument mode %d!",i,mode); |
| 390 |
} |
| 391 |
ins->type=mode?DIV_INS_FM:DIV_INS_STD; |
| 392 |
if (ds.system[0]==DIV_SYSTEM_GB) { |
| 393 |
ins->type=DIV_INS_GB; |
| 394 |
} |
| 395 |
if (ds.system[0]==DIV_SYSTEM_C64_8580 || ds.system[0]==DIV_SYSTEM_C64_6581) { |
| 396 |
ins->type=DIV_INS_C64; |
| 397 |
} |
| 398 |
if (ds.system[0]==DIV_SYSTEM_YM2610 || ds.system[0]==DIV_SYSTEM_YM2610_EXT |
| 399 |
|| ds.system[0]==DIV_SYSTEM_YM2610_FULL || ds.system[0]==DIV_SYSTEM_YM2610_FULL_EXT |
| 400 |
|| ds.system[0]==DIV_SYSTEM_YM2610B || ds.system[0]==DIV_SYSTEM_YM2610B_EXT) { |
| 401 |
if (!mode) { |
| 402 |
ins->type=DIV_INS_AY; |
| 403 |
} |
| 404 |
} |
| 405 |
if (ds.system[0]==DIV_SYSTEM_PCE) { |
| 406 |
ins->type=DIV_INS_PCE; |
| 407 |
} |
| 408 |
if ((ds.system[0]==DIV_SYSTEM_SMS_OPLL || ds.system[0]==DIV_SYSTEM_NES_VRC7) && ins->type==DIV_INS_FM) { |
| 409 |
ins->type=DIV_INS_OPLL; |
| 410 |
} |
| 411 |
if (ds.system[0]==DIV_SYSTEM_YMU759) { |
| 412 |
ins->type=DIV_INS_OPL; |
| 413 |
} |
| 414 |
if (ds.system[0]==DIV_SYSTEM_ARCADE) { |
| 415 |
ins->type=DIV_INS_OPM; |
| 416 |
} |
| 417 |
if (ds.system[0]==DIV_SYSTEM_MSX2) { |
| 418 |
ins->type=DIV_INS_AY; |
| 419 |
} |
| 420 |
if ((ds.system[0]==DIV_SYSTEM_NES || ds.system[0]==DIV_SYSTEM_NES_VRC7 || ds.system[0]==DIV_SYSTEM_NES_FDS) && ins->type==DIV_INS_STD) { |
| 421 |
ins->type=DIV_INS_NES; |
| 422 |
} |
| 423 |
|
| 424 |
if (mode) { // FM |
| 425 |
if (ds.version>0x05) { |
| 426 |
ins->fm.alg=reader.readC(); |
| 427 |
if (ds.version<0x13) { |
| 428 |
reader.readC(); |
| 429 |
} |
| 430 |
ins->fm.fb=reader.readC(); |
| 431 |
if (ds.version<0x13) { |
| 432 |
reader.readC(); |
| 433 |
} |
| 434 |
ins->fm.fms=reader.readC(); |
| 435 |
if (ds.version<0x13) { |
| 436 |
reader.readC(); |
| 437 |
ins->fm.ops=2+reader.readC()*2; |
| 438 |
if (ds.system[0]!=DIV_SYSTEM_YMU759) ins->fm.ops=4; |
| 439 |
} else { |
| 440 |
ins->fm.ops=4; |
| 441 |
} |
| 442 |
ins->fm.ams=reader.readC(); |
| 443 |
} else { |
| 444 |
ins->fm.alg=reader.readC(); |
| 445 |
reader.readC(); |
| 446 |
ins->fm.fb=reader.readC(); |
| 447 |
reader.readC(); // apparently an index of sorts starting from 0x59? |
| 448 |
ins->fm.fms=reader.readC(); |
| 449 |
reader.readC(); // 0x59+index? |
| 450 |
ins->fm.ops=2+reader.readC()*2; |
| 451 |
} |
| 452 |
|
| 453 |
logD("ALG %d FB %d FMS %d AMS %d OPS %d",ins->fm.alg,ins->fm.fb,ins->fm.fms,ins->fm.ams,ins->fm.ops); |
| 454 |
if (ins->fm.ops!=2 && ins->fm.ops!=4) { |
| 455 |
logE("invalid op count %d. did we read it wrong?",ins->fm.ops); |
| 456 |
lastError="file is corrupt or unreadable at operators"; |
| 457 |
delete[] file; |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
for (int j=0; j<ins->fm.ops; j++) { |
| 462 |
ins->fm.op[j].am=reader.readC(); |
| 463 |
ins->fm.op[j].ar=reader.readC(); |
| 464 |
if (ds.system[0]==DIV_SYSTEM_SMS_OPLL || ds.system[0]==DIV_SYSTEM_NES_VRC7) { |
| 465 |
ins->fm.op[j].ar&=15; |
| 466 |
} |
| 467 |
if (ds.version<0x13) { |
| 468 |
ins->fm.op[j].dam=reader.readC(); |
| 469 |
} |
| 470 |
ins->fm.op[j].dr=reader.readC(); |
| 471 |
if (ds.system[0]==DIV_SYSTEM_SMS_OPLL || ds.system[0]==DIV_SYSTEM_NES_VRC7) { |
| 472 |
ins->fm.op[j].dr&=15; |
| 473 |
} |
| 474 |
if (ds.version<0x13) { |
| 475 |
ins->fm.op[j].dvb=reader.readC(); |
| 476 |
ins->fm.op[j].egt=reader.readC(); |
| 477 |
ins->fm.op[j].ksl=reader.readC(); |
| 478 |
if (ds.version<0x11) { // don't know when did this change |
| 479 |
ins->fm.op[j].ksr=reader.readC(); |
| 480 |
} |
| 481 |
} |
| 482 |
ins->fm.op[j].mult=reader.readC(); |
| 483 |
ins->fm.op[j].rr=reader.readC(); |
| 484 |
ins->fm.op[j].sl=reader.readC(); |
| 485 |
if (ds.version<0x13) { |
| 486 |
ins->fm.op[j].sus=reader.readC(); |
| 487 |
} |
| 488 |
ins->fm.op[j].tl=reader.readC(); |
| 489 |
if (ds.version<0x13) { |
| 490 |
ins->fm.op[j].vib=reader.readC(); |
| 491 |
ins->fm.op[j].ws=reader.readC(); |
| 492 |
} else { |
| 493 |
if (ds.system[0]==DIV_SYSTEM_SMS_OPLL || ds.system[0]==DIV_SYSTEM_NES_VRC7) { |
| 494 |
if (j==0) { |
| 495 |
ins->fm.opllPreset=reader.readC(); |
| 496 |
} else { |
| 497 |
reader.readC(); |
| 498 |
} |
| 499 |
} else { |
| 500 |
ins->fm.op[j].dt2=reader.readC(); |
| 501 |
} |
| 502 |
} |
| 503 |
if (ds.version>0x05) { |
| 504 |
if (ds.system[0]==DIV_SYSTEM_SMS_OPLL || ds.system[0]==DIV_SYSTEM_NES_VRC7) { |
| 505 |
ins->fm.op[j].ksr=reader.readC(); |
| 506 |
ins->fm.op[j].vib=reader.readC(); |
| 507 |
ins->fm.op[j].ksl=reader.readC(); |
| 508 |
ins->fm.op[j].ssgEnv=reader.readC(); |
| 509 |
} else { |
| 510 |
ins->fm.op[j].rs=reader.readC(); |
| 511 |
ins->fm.op[j].dt=reader.readC(); |
| 512 |
ins->fm.op[j].d2r=reader.readC(); |
| 513 |
ins->fm.op[j].ssgEnv=reader.readC(); |
| 514 |
} |
| 515 |
} |
| 516 |
if (ds.version<0x12) { // before version 10 all ops were responsive to volume |
| 517 |
ins->fm.op[j].kvs=1; |
| 518 |
} |
| 519 |
|
| 520 |
logD("OP%d: AM %d AR %d DAM %d DR %d DVB %d EGT %d KSL %d MULT %d RR %d SL %d SUS %d TL %d VIB %d WS %d RS %d DT %d D2R %d SSG-EG %d",j, |
| 521 |
ins->fm.op[j].am, |
| 522 |
ins->fm.op[j].ar, |
| 523 |
ins->fm.op[j].dam, |
| 524 |
ins->fm.op[j].dr, |
| 525 |
ins->fm.op[j].dvb, |
| 526 |
ins->fm.op[j].egt, |
| 527 |
ins->fm.op[j].ksl, |
| 528 |
ins->fm.op[j].mult, |
| 529 |
ins->fm.op[j].rr, |
| 530 |
ins->fm.op[j].sl, |
| 531 |
ins->fm.op[j].sus, |
| 532 |
ins->fm.op[j].tl, |
| 533 |
ins->fm.op[j].vib, |
| 534 |
ins->fm.op[j].ws, |
| 535 |
ins->fm.op[j].rs, |
| 536 |
ins->fm.op[j].dt, |
| 537 |
ins->fm.op[j].d2r, |
| 538 |
ins->fm.op[j].ssgEnv |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
// swap alg operator 2 and 3 if YMU759 |
| 543 |
if (ds.system[0]==DIV_SYSTEM_YMU759 && ins->fm.ops==4) { |
| 544 |
DivInstrumentFM::Operator oldOp=ins->fm.op[2]; |
| 545 |
ins->fm.op[2]=ins->fm.op[1]; |
| 546 |
ins->fm.op[1]=oldOp; |
| 547 |
|
| 548 |
if (ins->fm.alg==1) { |
| 549 |
ins->fm.alg=2; |
| 550 |
} else if (ins->fm.alg==2) { |
| 551 |
ins->fm.alg=1; |
| 552 |
} |
| 553 |
} |
| 554 |
} else { // STD |
| 555 |
if (ds.system[0]!=DIV_SYSTEM_GB || ds.version<0x12) { |
| 556 |
ins->std.volMacro.len=reader.readC(); |
| 557 |
for (int j=0; j<ins->std.volMacro.len; j++) { |
| 558 |
if (ds.version<0x0e) { |
| 559 |
ins->std.volMacro.val[j]=reader.readC(); |
| 560 |
} else { |
| 561 |
ins->std.volMacro.val[j]=reader.readI(); |
| 562 |
} |
| 563 |
} |
| 564 |
if (ins->std.volMacro.len>0) { |
| 565 |
ins->std.volMacro.open=true; |
| 566 |
ins->std.volMacro.loop=reader.readC(); |
| 567 |
} else { |
| 568 |
ins->std.volMacro.open=false; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
ins->std.arpMacro.len=reader.readC(); |
| 573 |
for (int j=0; j<ins->std.arpMacro.len; j++) { |
| 574 |
if (ds.version<0x0e) { |
| 575 |
ins->std.arpMacro.val[j]=reader.readC(); |
| 576 |
} else { |
| 577 |
ins->std.arpMacro.val[j]=reader.readI(); |
| 578 |
} |
| 579 |
} |
| 580 |
if (ins->std.arpMacro.len>0) { |
| 581 |
ins->std.arpMacro.loop=reader.readC(); |
| 582 |
ins->std.arpMacro.open=true; |
| 583 |
} else { |
| 584 |
ins->std.arpMacro.open=false; |
| 585 |
} |
| 586 |
if (ds.version>0x0f) { |
| 587 |
ins->std.arpMacro.mode=reader.readC(); |
| 588 |
} |
| 589 |
if (!ins->std.arpMacro.mode) { |
| 590 |
for (int j=0; j<ins->std.arpMacro.len; j++) { |
| 591 |
ins->std.arpMacro.val[j]-=12; |
| 592 |
} |
| 593 |
} else { |
| 594 |
ins->std.arpMacro.mode=0; |
| 595 |
for (int j=0; j<ins->std.arpMacro.len; j++) { |
| 596 |
ins->std.arpMacro.val[j]^=0x40000000; |
| 597 |
} |
| 598 |
if (ins->std.arpMacro.loop==255 && ins->std.arpMacro.len<255) { |
| 599 |
ins->std.arpMacro.val[ins->std.arpMacro.len++]=0; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
ins->std.dutyMacro.len=reader.readC(); |
| 604 |
for (int j=0; j<ins->std.dutyMacro.len; j++) { |
| 605 |
if (ds.version<0x0e) { |
| 606 |
ins->std.dutyMacro.val[j]=reader.readC(); |
| 607 |
} else { |
| 608 |
ins->std.dutyMacro.val[j]=reader.readI(); |
| 609 |
} |
| 610 |
/*if ((ds.system[0]==DIV_SYSTEM_C64_8580 || ds.system[0]==DIV_SYSTEM_C64_6581) && ins->std.dutyMacro.val[j]>24) { |
| 611 |
ins->std.dutyMacro.val[j]=24; |
| 612 |
}*/ |
| 613 |
} |
| 614 |
if (ins->std.dutyMacro.len>0) { |
| 615 |
ins->std.dutyMacro.open=true; |
| 616 |
ins->std.dutyMacro.loop=reader.readC(); |
| 617 |
} else { |
| 618 |
ins->std.dutyMacro.open=false; |
| 619 |
} |
| 620 |
|
| 621 |
ins->std.waveMacro.len=reader.readC(); |
| 622 |
for (int j=0; j<ins->std.waveMacro.len; j++) { |
| 623 |
if (ds.version<0x0e) { |
| 624 |
ins->std.waveMacro.val[j]=reader.readC(); |
| 625 |
} else { |
| 626 |
ins->std.waveMacro.val[j]=reader.readI(); |
| 627 |
} |
| 628 |
|
| 629 |
// piece of crap offset by 1 |
| 630 |
if (ds.system[0]==DIV_SYSTEM_YM2610 || ds.system[0]==DIV_SYSTEM_YM2610_EXT) { |
| 631 |
ins->std.waveMacro.val[j]++; |
| 632 |
} |
| 633 |
} |
| 634 |
if (ins->std.waveMacro.len>0) { |
| 635 |
ins->std.waveMacro.open=true; |
| 636 |
ins->std.waveMacro.loop=reader.readC(); |
| 637 |
} else { |
| 638 |
ins->std.waveMacro.open=false; |
| 639 |
} |
| 640 |
|
| 641 |
if (ds.system[0]==DIV_SYSTEM_C64_6581 || ds.system[0]==DIV_SYSTEM_C64_8580) { |
| 642 |
bool volIsCutoff=false; |
| 643 |
|
| 644 |
ins->c64.triOn=reader.readC(); |
| 645 |
ins->c64.sawOn=reader.readC(); |
| 646 |
ins->c64.pulseOn=reader.readC(); |
| 647 |
ins->c64.noiseOn=reader.readC(); |
| 648 |
|
| 649 |
ins->c64.a=reader.readC(); |
| 650 |
ins->c64.d=reader.readC(); |
| 651 |
ins->c64.s=reader.readC(); |
| 652 |
ins->c64.r=reader.readC(); |
| 653 |
|
| 654 |
ins->c64.duty=(reader.readC()*4095)/100; |
| 655 |
|
| 656 |
ins->c64.ringMod=reader.readC(); |
| 657 |
ins->c64.oscSync=reader.readC(); |
| 658 |
ins->c64.toFilter=reader.readC(); |
| 659 |
if (ds.version<0x11) { |
| 660 |
volIsCutoff=reader.readI(); |
| 661 |
} else { |
| 662 |
volIsCutoff=reader.readC(); |
| 663 |
} |
| 664 |
ins->c64.initFilter=reader.readC(); |
| 665 |
|
| 666 |
ins->c64.res=reader.readC(); |
| 667 |
ins->c64.cut=(reader.readC()*2047)/100; |
| 668 |
ins->c64.hp=reader.readC(); |
| 669 |
ins->c64.bp=reader.readC(); |
| 670 |
ins->c64.lp=reader.readC(); |
| 671 |
ins->c64.ch3off=reader.readC(); |
| 672 |
|
| 673 |
// weird storage |
| 674 |
if (volIsCutoff) { |
| 675 |
// move to alg (new cutoff) |
| 676 |
ins->std.algMacro.len=ins->std.volMacro.len; |
| 677 |
ins->std.algMacro.loop=ins->std.volMacro.loop; |
| 678 |
ins->std.algMacro.rel=ins->std.volMacro.rel; |
| 679 |
for (int j=0; j<ins->std.algMacro.len; j++) { |
| 680 |
ins->std.algMacro.val[j]=-(ins->std.volMacro.val[j]-18); |
| 681 |
} |
| 682 |
ins->std.volMacro.len=0; |
| 683 |
memset(ins->std.volMacro.val,0,256*sizeof(int)); |
| 684 |
} |
| 685 |
for (int j=0; j<ins->std.dutyMacro.len; j++) { |
| 686 |
ins->std.dutyMacro.val[j]-=12; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
if (ds.system[0]==DIV_SYSTEM_GB && ds.version>0x11) { |
| 691 |
ins->gb.envVol=reader.readC(); |
| 692 |
ins->gb.envDir=reader.readC(); |
| 693 |
ins->gb.envLen=reader.readC(); |
| 694 |
ins->gb.soundLen=reader.readC(); |
| 695 |
ins->std.volMacro.open=false; |
| 696 |
|
| 697 |
logD("GB data: vol %d dir %d len %d sl %d",ins->gb.envVol,ins->gb.envDir,ins->gb.envLen,ins->gb.soundLen); |
| 698 |
} else if (ds.system[0]==DIV_SYSTEM_GB) { |
| 699 |
// set software envelope flag |
| 700 |
ins->gb.softEnv=true; |
| 701 |
// try to convert macro to envelope in case the user decides to switch to them |
| 702 |
if (ins->std.volMacro.len>0) { |
| 703 |
ins->gb.envVol=ins->std.volMacro.val[0]; |
| 704 |
if (ins->std.volMacro.val[0]<ins->std.volMacro.val[1]) { |
| 705 |
ins->gb.envDir=true; |
| 706 |
} |
| 707 |
if (ins->std.volMacro.val[ins->std.volMacro.len-1]==0) { |
| 708 |
ins->gb.soundLen=ins->std.volMacro.len*2; |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
ds.ins.push_back(ins); |
| 715 |
} |
| 716 |
|
| 717 |
if (ds.version>0x0b) { |
| 718 |
ds.waveLen=(unsigned char)reader.readC(); |
| 719 |
logI("reading wavetables (%d)...",ds.waveLen); |
| 720 |
if (ds.waveLen>0) ds.wave.reserve(ds.waveLen); |
| 721 |
for (int i=0; i<ds.waveLen; i++) { |
| 722 |
DivWavetable* wave=new DivWavetable; |
| 723 |
wave->len=(unsigned char)reader.readI(); |
| 724 |
if (ds.system[0]==DIV_SYSTEM_GB) { |
| 725 |
wave->max=15; |
| 726 |
} |
| 727 |
if (ds.system[0]==DIV_SYSTEM_NES_FDS) { |
| 728 |
wave->max=63; |
| 729 |
} |
| 730 |
if (ds.system[0]==DIV_SYSTEM_MSX2) { |
| 731 |
wave->max=255; |
| 732 |
} |
| 733 |
if (wave->len>65) { |
| 734 |
logE("invalid wave length %d. are we doing something wrong?",wave->len); |
| 735 |
lastError="file is corrupt or unreadable at wavetables"; |
| 736 |
delete[] file; |
| 737 |
return false; |
| 738 |
} |
| 739 |
logD("%d length %d",i,wave->len); |
| 740 |
for (int j=0; j<wave->len; j++) { |
| 741 |
if (ds.version<0x0e) { |
| 742 |
wave->data[j]=reader.readC(); |
| 743 |
} else { |
| 744 |
wave->data[j]=reader.readI(); |
| 745 |
} |
| 746 |
wave->data[j]&=wave->max; |
| 747 |
} |
| 748 |
// #FDS4Bit |
| 749 |
if (ds.system[0]==DIV_SYSTEM_NES_FDS && ds.version<0x1a) { |
| 750 |
for (int j=0; j<wave->len; j++) { |
| 751 |
wave->data[j]*=4; |
| 752 |
} |
| 753 |
} |
| 754 |
ds.wave.push_back(wave); |
| 755 |
} |
| 756 |
|
| 757 |
// sometimes there's a single length 0 wavetable in the file. I don't know why. |
| 758 |
if (ds.waveLen==1) { |
| 759 |
if (ds.wave[0]->len==0) { |
| 760 |
ds.clearWavetables(); |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
logV("%x",reader.tell()); |
| 766 |
|
| 767 |
logI("reading patterns (%d channels, %d orders)...",getChannelCount(ds.system[0]),ds.subsong[0]->ordersLen); |
| 768 |
for (int i=0; i<getChannelCount(ds.system[0]); i++) { |
| 769 |
DivChannelData& chan=ds.subsong[0]->pat[i]; |
| 770 |
if (ds.version<0x0a) { |
| 771 |
chan.effectCols=1; |
| 772 |
} else { |
| 773 |
chan.effectCols=reader.readC(); |
| 774 |
} |
| 775 |
logD("%d fx rows: %d",i,chan.effectCols); |
| 776 |
if (chan.effectCols>4 || chan.effectCols<1) { |
| 777 |
logE("invalid effect column count %d. are you sure everything is ok?",chan.effectCols); |
| 778 |
lastError="file is corrupt or unreadable at effect columns"; |
| 779 |
delete[] file; |
| 780 |
return false; |
| 781 |
} |
| 782 |
for (int j=0; j<ds.subsong[0]->ordersLen; j++) { |
| 783 |
DivPattern* pat=chan.getPattern(ds.subsong[0]->orders.ord[i][j],true); |
| 784 |
if (ds.version>0x08) { // current pattern format |
| 785 |
for (int k=0; k<ds.subsong[0]->patLen; k++) { |
| 786 |
// note |
| 787 |
pat->data[k][0]=reader.readS(); |
| 788 |
// octave |
| 789 |
pat->data[k][1]=reader.readS(); |
| 790 |
if (ds.system[0]==DIV_SYSTEM_SMS && ds.version<0x0e && pat->data[k][1]>0) { |
| 791 |
// apparently it was up one octave before |
| 792 |
pat->data[k][1]--; |
| 793 |
} else if (ds.system[0]==DIV_SYSTEM_GENESIS && ds.version<0x0e && pat->data[k][1]>0 && i>5) { |
| 794 |
// ditto |
| 795 |
pat->data[k][1]--; |
| 796 |
} else if (ds.system[0]==DIV_SYSTEM_MSX2 && pat->data[k][1]>0 && i<3) { |
| 797 |
// why the hell? |
| 798 |
pat->data[k][1]++; |
| 799 |
} |
| 800 |
if (ds.version<0x12) { |
| 801 |
if (ds.system[0]==DIV_SYSTEM_GB && i==3 && pat->data[k][1]>0) { |
| 802 |
// back then noise was 2 octaves lower |
| 803 |
pat->data[k][1]-=2; |
| 804 |
} |
| 805 |
} |
| 806 |
if (ds.system[0]==DIV_SYSTEM_YMU759 && pat->data[k][0]!=0) { |
| 807 |
// apparently YMU759 is stored 2 octaves lower |
| 808 |
pat->data[k][1]+=2; |
| 809 |
} |
| 810 |
if (pat->data[k][0]==0 && pat->data[k][1]!=0) { |
| 811 |
logD("what? %d:%d:%d note %d octave %d",i,j,k,pat->data[k][0],pat->data[k][1]); |
| 812 |
pat->data[k][0]=12; |
| 813 |
pat->data[k][1]--; |
| 814 |
} |
| 815 |
// volume |
| 816 |
pat->data[k][3]=reader.readS(); |
| 817 |
if (ds.version<0x0a) { |
| 818 |
// back then volume was stored as 00-ff instead of 00-7f/0-f |
| 819 |
if (i>5) { |
| 820 |
pat->data[k][3]>>=4; |
| 821 |
} else { |
| 822 |
pat->data[k][3]>>=1; |
| 823 |
} |
| 824 |
} |
| 825 |
if (ds.version<0x12) { |
| 826 |
if (ds.system[0]==DIV_SYSTEM_GB && i==2 && pat->data[k][3]>0) { |
| 827 |
// volume range of GB wave channel was 0-3 rather than 0-F |
| 828 |
pat->data[k][3]=(pat->data[k][3]&3)*5; |
| 829 |
} |
| 830 |
} |
| 831 |
for (int l=0; l<chan.effectCols; l++) { |
| 832 |
// effect |
| 833 |
pat->data[k][4+(l<<1)]=reader.readS(); |
| 834 |
pat->data[k][5+(l<<1)]=reader.readS(); |
| 835 |
|
| 836 |
if (ds.version<0x14) { |
| 837 |
if (pat->data[k][4+(l<<1)]==0xe5 && pat->data[k][5+(l<<1)]!=-1) { |
| 838 |
pat->data[k][5+(l<<1)]=128+((pat->data[k][5+(l<<1)]-128)/4); |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
// instrument |
| 843 |
pat->data[k][2]=reader.readS(); |
| 844 |
|
| 845 |
// this is sad |
| 846 |
if (ds.system[0]==DIV_SYSTEM_NES_FDS) { |
| 847 |
if (i==5 && pat->data[k][2]!=-1) { |
| 848 |
if (pat->data[k][2]>=0 && pat->data[k][2]<ds.insLen) { |
| 849 |
ds.ins[pat->data[k][2]]->type=DIV_INS_FDS; |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
if (ds.system[0]==DIV_SYSTEM_MSX2) { |
| 854 |
if (i>=3 && pat->data[k][2]!=-1) { |
| 855 |
if (pat->data[k][2]>=0 && pat->data[k][2]<ds.insLen) { |
| 856 |
ds.ins[pat->data[k][2]]->type=DIV_INS_SCC; |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
} else { // historic pattern format |
| 862 |
if (i<16) pat->data[0][2]=historicColIns[i]; |
| 863 |
for (int k=0; k<ds.subsong[0]->patLen; k++) { |
| 864 |
// note |
| 865 |
pat->data[k][0]=reader.readC(); |
| 866 |
// octave |
| 867 |
pat->data[k][1]=reader.readC(); |
| 868 |
if (pat->data[k][0]!=0) { |
| 869 |
// YMU759 is stored 2 octaves lower |
| 870 |
pat->data[k][1]+=2; |
| 871 |
} |
| 872 |
if (pat->data[k][0]==0 && pat->data[k][1]!=0) { |
| 873 |
logD("what? %d:%d:%d note %d octave %d",i,j,k,pat->data[k][0],pat->data[k][1]); |
| 874 |
pat->data[k][0]=12; |
| 875 |
pat->data[k][1]--; |
| 876 |
} |
| 877 |
// volume and effect |
| 878 |
unsigned char vol=reader.readC(); |
| 879 |
unsigned char fx=reader.readC(); |
| 880 |
unsigned char fxVal=reader.readC(); |
| 881 |
pat->data[k][3]=(vol==0x80 || vol==0xff)?-1:vol; |
| 882 |
// effect |
| 883 |
pat->data[k][4]=(fx==0x80 || fx==0xff)?-1:fx; |
| 884 |
pat->data[k][5]=(fxVal==0x80 || fx==0xff)?-1:fxVal; |
| 885 |
// instrument |
| 886 |
if (ds.version>0x05) { |
| 887 |
pat->data[k][2]=reader.readC(); |
| 888 |
if (pat->data[k][2]==0x80 || pat->data[k][2]==0xff) pat->data[k][2]=-1; |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
int ymuSampleRate=20; |
| 896 |
|
| 897 |
ds.sampleLen=(unsigned char)reader.readC(); |
| 898 |
logI("reading samples (%d)...",ds.sampleLen); |
| 899 |
if (ds.version<0x0b && ds.sampleLen>0) { |
| 900 |
// it appears this byte stored the YMU759 sample rate |
| 901 |
ymuSampleRate=reader.readC(); |
| 902 |
} |
| 903 |
if (ds.sampleLen>0) ds.sample.reserve(ds.sampleLen); |
| 904 |
for (int i=0; i<ds.sampleLen; i++) { |
| 905 |
DivSample* sample=new DivSample; |
| 906 |
int length=reader.readI(); |
| 907 |
int cutStart=0; |
| 908 |
int cutEnd=length; |
| 909 |
int pitch=5; |
| 910 |
int vol=50; |
| 911 |
short* data; |
| 912 |
unsigned char* adpcmData; |
| 913 |
// I don't think a sample can be that big |
| 914 |
if (length<0 || length>(1<<29L)) { |
| 915 |
logE("invalid sample length %d. are we doing something wrong?",length); |
| 916 |
lastError="file is corrupt or unreadable at samples"; |
| 917 |
delete[] file; |
| 918 |
return false; |
| 919 |
} |
| 920 |
if (ds.version>0x16) { |
| 921 |
sample->name=reader.readString((unsigned char)reader.readC()); |
| 922 |
} else { |
| 923 |
sample->name=""; |
| 924 |
} |
| 925 |
logD("%d name %s (%d)",i,sample->name.c_str(),length); |
| 926 |
sample->rate=22050; |
| 927 |
if (ds.version>=0x0b) { |
| 928 |
sample->rate=fileToDivRate(reader.readC()); |
| 929 |
sample->centerRate=sample->rate; |
| 930 |
pitch=reader.readC(); |
| 931 |
vol=reader.readC(); |
| 932 |
|
| 933 |
if (pitch<0 || pitch>10) { |
| 934 |
logW("%d: sample pitch is wrong! (%d)",i,pitch); |
| 935 |
pitch=5; |
| 936 |
} |
| 937 |
} |
| 938 |
if (ds.version<=0x08) { |
| 939 |
sample->rate=ymuSampleRate*400; |
| 940 |
} |
| 941 |
if (ds.version>0x15) { |
| 942 |
sample->depth=(DivSampleDepth)reader.readC(); |
| 943 |
if (sample->depth!=DIV_SAMPLE_DEPTH_8BIT && sample->depth!=DIV_SAMPLE_DEPTH_16BIT) { |
| 944 |
logW("%d: sample depth is wrong! (%d)",i,(int)sample->depth); |
| 945 |
sample->depth=DIV_SAMPLE_DEPTH_16BIT; |
| 946 |
} |
| 947 |
} else { |
| 948 |
if (ds.version>0x08) { |
| 949 |
sample->depth=DIV_SAMPLE_DEPTH_16BIT; |
| 950 |
} else { |
| 951 |
// it appears samples were stored as ADPCM back then |
| 952 |
sample->depth=DIV_SAMPLE_DEPTH_YMZ_ADPCM; |
| 953 |
} |
| 954 |
} |
| 955 |
if (ds.version>=0x1b) { |
| 956 |
// what the hell man... |
| 957 |
cutStart=reader.readI(); |
| 958 |
cutEnd=reader.readI(); |
| 959 |
logV("cutStart: %d cutEnd: %d",cutStart,cutEnd); |
| 960 |
} |
| 961 |
if (length>0) { |
| 962 |
if (ds.version>0x08) { |
| 963 |
if (ds.version<0x0b) { |
| 964 |
data=new short[1+(length/2)]; |
| 965 |
reader.read(data,length); |
| 966 |
length/=2; |
| 967 |
} else { |
| 968 |
data=new short[length]; |
| 969 |
reader.read(data,length*2); |
| 970 |
} |
| 971 |
|
| 972 |
#ifdef TA_BIG_ENDIAN |
| 973 |
// convert to big-endian |
| 974 |
for (int pos=0; pos<length; pos++) { |
| 975 |
data[pos]=(short)((((unsigned short)data[pos])<<8)|(((unsigned short)data[pos])>>8)); |
| 976 |
} |
| 977 |
#endif |
| 978 |
|
| 979 |
int scaledLen=ceil((double)length/samplePitches[pitch]); |
| 980 |
|
| 981 |
if (scaledLen>0) { |
| 982 |
// resample |
| 983 |
logD("%d: scaling from %d...",i,pitch); |
| 984 |
|
| 985 |
short* newData=new short[scaledLen]; |
| 986 |
memset(newData,0,scaledLen*sizeof(short)); |
| 987 |
int k=0; |
| 988 |
float mult=(float)(vol)/50.0f; |
| 989 |
for (double j=0; j<length; j+=samplePitches[pitch]) { |
| 990 |
if (k>=scaledLen) { |
| 991 |
break; |
| 992 |
} |
| 993 |
if (sample->depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 994 |
float next=(float)(data[(unsigned int)j]-0x80)*mult; |
| 995 |
newData[k++]=fmin(fmax(next,-128),127); |
| 996 |
} else { |
| 997 |
float next=(float)data[(unsigned int)j]*mult; |
| 998 |
newData[k++]=fmin(fmax(next,-32768),32767); |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
delete[] data; |
| 1003 |
data=newData; |
| 1004 |
} |
| 1005 |
|
| 1006 |
logV("length: %d. scaledLen: %d.",length,scaledLen); |
| 1007 |
|
| 1008 |
if (ds.version>=0x1b) { |
| 1009 |
if (cutStart<0 || cutStart>scaledLen) { |
| 1010 |
logE("cutStart is out of range! (%d, scaledLen: %d)",cutStart,scaledLen); |
| 1011 |
lastError="file is corrupt or unreadable at samples"; |
| 1012 |
delete[] file; |
| 1013 |
return false; |
| 1014 |
} |
| 1015 |
if (cutEnd<0 || cutEnd>scaledLen) { |
| 1016 |
logE("cutEnd is out of range! (%d, scaledLen: %d)",cutEnd,scaledLen); |
| 1017 |
lastError="file is corrupt or unreadable at samples"; |
| 1018 |
delete[] file; |
| 1019 |
return false; |
| 1020 |
} |
| 1021 |
if (cutEnd<cutStart) { |
| 1022 |
logE("cutEnd %d is before cutStart %d. what's going on?",cutEnd,cutStart); |
| 1023 |
lastError="file is corrupt or unreadable at samples"; |
| 1024 |
delete[] file; |
| 1025 |
return false; |
| 1026 |
} |
| 1027 |
if (cutStart!=0 || cutEnd!=scaledLen) { |
| 1028 |
// cut data |
| 1029 |
short* newData=new short[cutEnd-cutStart]; |
| 1030 |
memcpy(newData,&data[cutStart],(cutEnd-cutStart)*sizeof(short)); |
| 1031 |
delete[] data; |
| 1032 |
data=newData; |
| 1033 |
scaledLen=cutEnd-cutStart; |
| 1034 |
cutStart=0; |
| 1035 |
cutEnd=scaledLen; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
// copy data |
| 1040 |
if (!sample->init(scaledLen)) { |
| 1041 |
logE("%d: error while initializing sample!",i); |
| 1042 |
} else { |
| 1043 |
for (int i=0; i<scaledLen; i++) { |
| 1044 |
if (sample->depth==DIV_SAMPLE_DEPTH_8BIT) { |
| 1045 |
sample->data8[i]=data[i]; |
| 1046 |
} else { |
| 1047 |
sample->data16[i]=data[i]; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
delete[] data; |
| 1053 |
} else { |
| 1054 |
// YMZ ADPCM |
| 1055 |
adpcmData=new unsigned char[length]; |
| 1056 |
logV("%x",reader.tell()); |
| 1057 |
reader.read(adpcmData,length); |
| 1058 |
for (int i=0; i<length; i++) { |
| 1059 |
adpcmData[i]=(adpcmData[i]<<4)|(adpcmData[i]>>4); |
| 1060 |
} |
| 1061 |
if (!sample->init(length*2)) { |
| 1062 |
logE("%d: error while initializing sample!",i); |
| 1063 |
} |
| 1064 |
|
| 1065 |
memcpy(sample->dataZ,adpcmData,length); |
| 1066 |
delete[] adpcmData; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
ds.sample.push_back(sample); |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (reader.tell()<reader.size()) { |
| 1073 |
if ((reader.tell()+1)!=reader.size()) { |
| 1074 |
logW("premature end of song (we are at %x, but size is %x)",reader.tell(),reader.size()); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
// AY+SCC stuff |
| 1079 |
if (ds.system[0]==DIV_SYSTEM_MSX2) { |
| 1080 |
for (DivInstrument* i: ds.ins) { |
| 1081 |
if (i->type==DIV_INS_AY) { |
| 1082 |
// move wave macro |
| 1083 |
i->std.waveMacro.len=i->std.dutyMacro.len; |
| 1084 |
i->std.waveMacro.loop=i->std.dutyMacro.loop; |
| 1085 |
i->std.waveMacro.mode=i->std.dutyMacro.mode; |
| 1086 |
i->std.waveMacro.open=i->std.dutyMacro.open; |
| 1087 |
memcpy(i->std.waveMacro.val,i->std.dutyMacro.val,sizeof(i->std.waveMacro.val)); |
| 1088 |
for (int j=0; j<i->std.waveMacro.len; j++) { |
| 1089 |
i->std.waveMacro.val[j]++; |
| 1090 |
} |
| 1091 |
i->std.dutyMacro.len=0; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
// handle compound systems |
| 1097 |
if (ds.system[0]==DIV_SYSTEM_GENESIS) { |
| 1098 |
ds.systemLen=2; |
| 1099 |
ds.system[0]=DIV_SYSTEM_YM2612; |
| 1100 |
ds.system[1]=DIV_SYSTEM_SMS; |
| 1101 |
ds.systemVol[1]=0.5f; |
| 1102 |
} |
| 1103 |
if (ds.system[0]==DIV_SYSTEM_GENESIS_EXT) { |
| 1104 |
ds.systemLen=2; |
| 1105 |
ds.system[0]=DIV_SYSTEM_YM2612_EXT; |
| 1106 |
ds.system[1]=DIV_SYSTEM_SMS; |
| 1107 |
ds.systemVol[1]=0.5f; |
| 1108 |
} |
| 1109 |
if (ds.system[0]==DIV_SYSTEM_ARCADE) { |
| 1110 |
ds.systemLen=2; |
| 1111 |
ds.system[0]=DIV_SYSTEM_YM2151; |
| 1112 |
ds.system[1]=DIV_SYSTEM_SEGAPCM_COMPAT; |
| 1113 |
} |
| 1114 |
if (ds.system[0]==DIV_SYSTEM_MSX2) { |
| 1115 |
ds.systemLen=2; |
| 1116 |
ds.system[0]=DIV_SYSTEM_AY8910; |
| 1117 |
ds.system[1]=DIV_SYSTEM_SCC; |
| 1118 |
ds.systemFlags[0].set("chipType",1); |
| 1119 |
} |
| 1120 |
if (ds.system[0]==DIV_SYSTEM_SMS_OPLL) { |
| 1121 |
ds.systemLen=2; |
| 1122 |
ds.system[0]=DIV_SYSTEM_SMS; |
| 1123 |
ds.system[1]=DIV_SYSTEM_OPLL; |
| 1124 |
} |
| 1125 |
if (ds.system[0]==DIV_SYSTEM_NES_VRC7) { |
| 1126 |
ds.systemLen=2; |
| 1127 |
ds.system[0]=DIV_SYSTEM_NES; |
| 1128 |
ds.system[1]=DIV_SYSTEM_VRC7; |
| 1129 |
} |
| 1130 |
if (ds.system[0]==DIV_SYSTEM_NES_FDS) { |
| 1131 |
ds.systemLen=2; |
| 1132 |
ds.system[0]=DIV_SYSTEM_NES; |
| 1133 |
ds.system[1]=DIV_SYSTEM_FDS; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// SMS noise freq |
| 1137 |
if (ds.system[0]==DIV_SYSTEM_SMS) { |
| 1138 |
ds.systemFlags[0].set("noEasyNoise",true); |
| 1139 |
} |
| 1140 |
|
| 1141 |
// NES PCM |
| 1142 |
if (ds.system[0]==DIV_SYSTEM_NES) { |
| 1143 |
ds.systemFlags[0].set("dpcmMode",false); |
| 1144 |
} |
| 1145 |
|
| 1146 |
// C64 no key priority, reset time, multiply relative and macro race |
| 1147 |
if (ds.system[0]==DIV_SYSTEM_C64_8580 || ds.system[0]==DIV_SYSTEM_C64_6581) { |
| 1148 |
ds.systemFlags[0].set("keyPriority",false); |
| 1149 |
ds.systemFlags[0].set("initResetTime",1); |
| 1150 |
ds.systemFlags[0].set("multiplyRel",true); |
| 1151 |
ds.systemFlags[0].set("macroRace",true); |
| 1152 |
} |
| 1153 |
|
| 1154 |
// OPM broken pitch |
| 1155 |
if (ds.system[0]==DIV_SYSTEM_YM2151) { |
| 1156 |
ds.systemFlags[0].set("brokenPitch",true); |
| 1157 |
} |
| 1158 |
|
| 1159 |
ds.systemName=getSongSystemLegacyName(ds,!getConfInt("noMultiSystem",0)); |
| 1160 |
|
| 1161 |
if (active) quitDispatch(); |
| 1162 |
BUSY_BEGIN_SOFT; |
| 1163 |
saveLock.lock(); |
| 1164 |
song.unload(); |
| 1165 |
song=ds; |
| 1166 |
changeSong(0); |
| 1167 |
recalcChans(); |
| 1168 |
saveLock.unlock(); |
| 1169 |
BUSY_END; |
| 1170 |
if (active) { |
| 1171 |
initDispatch(); |
| 1172 |
BUSY_BEGIN; |
| 1173 |
renderSamples(); |
| 1174 |
reset(); |
| 1175 |
BUSY_END; |
| 1176 |
} |
| 1177 |
} catch (EndOfFileException& e) { |
| 1178 |
logE("premature end of file!"); |
| 1179 |
lastError="incomplete file"; |
| 1180 |
delete[] file; |
| 1181 |
return false; |
| 1182 |
} |
| 1183 |
delete[] file; |
| 1184 |
return true; |
| 1185 |
} |
| 1186 |
|
| 1187 |
SafeWriter* DivEngine::saveDMF(unsigned char version) { |
| 1188 |
// fail if version is not supported |
| 1189 |
if (version>26) version=26; |
| 1190 |
if (version<24) { |
| 1191 |
logE("cannot save in this version!"); |
| 1192 |
lastError="invalid version to save in! this is a bug!"; |
| 1193 |
return NULL; |
| 1194 |
} |
| 1195 |
// check whether system is compound |
| 1196 |
bool isFlat=false; |
| 1197 |
if (song.systemLen==2) { |
| 1198 |
if (song.system[0]==DIV_SYSTEM_YM2612 && song.system[1]==DIV_SYSTEM_SMS) { |
| 1199 |
isFlat=true; |
| 1200 |
} |
| 1201 |
if (song.system[0]==DIV_SYSTEM_YM2612_EXT && song.system[1]==DIV_SYSTEM_SMS) { |
| 1202 |
isFlat=true; |
| 1203 |
} |
| 1204 |
if (song.system[0]==DIV_SYSTEM_YM2151 && song.system[1]==DIV_SYSTEM_SEGAPCM_COMPAT) { |
| 1205 |
isFlat=true; |
| 1206 |
} |
| 1207 |
if (song.system[0]==DIV_SYSTEM_SMS && song.system[1]==DIV_SYSTEM_OPLL) { |
| 1208 |
isFlat=true; |
| 1209 |
} |
| 1210 |
if (song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_VRC7) { |
| 1211 |
isFlat=true; |
| 1212 |
} |
| 1213 |
if (song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_FDS) { |
| 1214 |
isFlat=true; |
| 1215 |
} |
| 1216 |
if (song.system[0]==DIV_SYSTEM_AY8910 && song.system[1]==DIV_SYSTEM_SCC) { |
| 1217 |
isFlat=true; |
| 1218 |
addWarning("your song will sound different. I am not going to bother adding further compatibility."); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
// fail if more than one system |
| 1222 |
if (!isFlat && song.systemLen!=1) { |
| 1223 |
logE("cannot save multiple systems in this format!"); |
| 1224 |
lastError="multiple systems not possible on .dmf"; |
| 1225 |
return NULL; |
| 1226 |
} |
| 1227 |
// fail if this is an YMU759 song |
| 1228 |
if (song.system[0]==DIV_SYSTEM_YMU759) { |
| 1229 |
logE("cannot save YMU759 song!"); |
| 1230 |
lastError="YMU759 song saving is not supported"; |
| 1231 |
return NULL; |
| 1232 |
} |
| 1233 |
// fail if the system is SMS+OPLL and version<25 |
| 1234 |
if (version<25 && song.system[0]==DIV_SYSTEM_SMS && song.system[1]==DIV_SYSTEM_OPLL) { |
| 1235 |
logE("Master System FM expansion not supported in 1.0/legacy .dmf!"); |
| 1236 |
lastError="Master System FM expansion not supported in 1.0/legacy .dmf!"; |
| 1237 |
return NULL; |
| 1238 |
} |
| 1239 |
// fail if the system is NES+VRC7 and version<25 |
| 1240 |
if (version<25 && song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_VRC7) { |
| 1241 |
logE("NES + VRC7 not supported in 1.0/legacy .dmf!"); |
| 1242 |
lastError="NES + VRC7 not supported in 1.0/legacy .dmf!"; |
| 1243 |
return NULL; |
| 1244 |
} |
| 1245 |
// fail if the system is FDS and version<25 |
| 1246 |
if (version<25 && song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_FDS) { |
| 1247 |
logE("FDS not supported in 1.0/legacy .dmf!"); |
| 1248 |
lastError="FDS not supported in 1.0/legacy .dmf!"; |
| 1249 |
return NULL; |
| 1250 |
} |
| 1251 |
// fail if the system is SCC and version<25 |
| 1252 |
if (version<25 && song.system[0]==DIV_SYSTEM_AY8910 && song.system[1]==DIV_SYSTEM_SCC) { |
| 1253 |
logE("AY + SCC not supported in 1.0/legacy .dmf!"); |
| 1254 |
lastError="AY + SCC not supported in 1.0/legacy .dmf!"; |
| 1255 |
return NULL; |
| 1256 |
} |
| 1257 |
// fail if the system is Furnace-exclusive |
| 1258 |
if (!isFlat && systemToFileDMF(song.system[0])==0) { |
| 1259 |
logE("cannot save Furnace-exclusive system song!"); |
| 1260 |
lastError="this system is not possible on .dmf"; |
| 1261 |
return NULL; |
| 1262 |
} |
| 1263 |
// fail if values are out of range |
| 1264 |
if (curSubSong->ordersLen>127) { |
| 1265 |
logE("maximum .dmf song length is 127!"); |
| 1266 |
lastError="maximum .dmf song length is 127"; |
| 1267 |
return NULL; |
| 1268 |
} |
| 1269 |
if (song.ins.size()>128) { |
| 1270 |
logE("maximum number of instruments in .dmf is 128!"); |
| 1271 |
lastError="maximum number of instruments in .dmf is 128"; |
| 1272 |
return NULL; |
| 1273 |
} |
| 1274 |
if (song.wave.size()>64) { |
| 1275 |
logE("maximum number of wavetables in .dmf is 64!"); |
| 1276 |
lastError="maximum number of wavetables in .dmf is 64"; |
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
for (int i=0; i<chans; i++) { |
| 1280 |
for (int j=0; j<curSubSong->ordersLen; j++) { |
| 1281 |
if (curOrders->ord[i][j]>0x7f) { |
| 1282 |
logE("order %d, %d is out of range (0-127)!",curOrders->ord[i][j]); |
| 1283 |
lastError=fmt::sprintf("order %d, %d is out of range (0-127)",curOrders->ord[i][j]); |
| 1284 |
return NULL; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
} |
| 1288 |
saveLock.lock(); |
| 1289 |
warnings=""; |
| 1290 |
song.version=version; |
| 1291 |
song.isDMF=true; |
| 1292 |
|
| 1293 |
SafeWriter* w=new SafeWriter; |
| 1294 |
w->init(); |
| 1295 |
// write magic |
| 1296 |
w->write(DIV_DMF_MAGIC,16); |
| 1297 |
// version |
| 1298 |
w->writeC(version); |
| 1299 |
DivSystem sys=DIV_SYSTEM_NULL; |
| 1300 |
if (song.system[0]==DIV_SYSTEM_YM2612 && song.system[1]==DIV_SYSTEM_SMS) { |
| 1301 |
w->writeC(systemToFileDMF(DIV_SYSTEM_GENESIS)); |
| 1302 |
sys=DIV_SYSTEM_GENESIS; |
| 1303 |
} else if (song.system[0]==DIV_SYSTEM_YM2612_EXT && song.system[1]==DIV_SYSTEM_SMS) { |
| 1304 |
w->writeC(systemToFileDMF(DIV_SYSTEM_GENESIS_EXT)); |
| 1305 |
sys=DIV_SYSTEM_GENESIS_EXT; |
| 1306 |
} else if (song.system[0]==DIV_SYSTEM_YM2151 && song.system[1]==DIV_SYSTEM_SEGAPCM_COMPAT) { |
| 1307 |
w->writeC(systemToFileDMF(DIV_SYSTEM_ARCADE)); |
| 1308 |
sys=DIV_SYSTEM_ARCADE; |
| 1309 |
} else if (song.system[0]==DIV_SYSTEM_SMS && song.system[1]==DIV_SYSTEM_OPLL) { |
| 1310 |
w->writeC(systemToFileDMF(DIV_SYSTEM_SMS_OPLL)); |
| 1311 |
sys=DIV_SYSTEM_SMS_OPLL; |
| 1312 |
} else if (song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_VRC7) { |
| 1313 |
w->writeC(systemToFileDMF(DIV_SYSTEM_NES_VRC7)); |
| 1314 |
sys=DIV_SYSTEM_NES_VRC7; |
| 1315 |
} else if (song.system[0]==DIV_SYSTEM_NES && song.system[1]==DIV_SYSTEM_FDS) { |
| 1316 |
w->writeC(systemToFileDMF(DIV_SYSTEM_NES_FDS)); |
| 1317 |
sys=DIV_SYSTEM_NES_FDS; |
| 1318 |
} else if (song.system[0]==DIV_SYSTEM_AY8910 && song.system[1]==DIV_SYSTEM_SCC) { |
| 1319 |
w->writeC(systemToFileDMF(DIV_SYSTEM_MSX2)); |
| 1320 |
sys=DIV_SYSTEM_MSX2; |
| 1321 |
} else { |
| 1322 |
w->writeC(systemToFileDMF(song.system[0])); |
| 1323 |
sys=song.system[0]; |
| 1324 |
} |
| 1325 |
|
| 1326 |
// song info |
| 1327 |
w->writeString(song.name,true); |
| 1328 |
w->writeString(song.author,true); |
| 1329 |
w->writeC(curSubSong->hilightA); |
| 1330 |
w->writeC(curSubSong->hilightB); |
| 1331 |
|
| 1332 |
int intHz=curSubSong->hz; |
| 1333 |
|
| 1334 |
w->writeC(curSubSong->timeBase); |
| 1335 |
w->writeC(curSubSong->speeds.val[0]); |
| 1336 |
w->writeC((curSubSong->speeds.len>=2)?curSubSong->speeds.val[1]:curSubSong->speeds.val[0]); |
| 1337 |
w->writeC((intHz<=53)?0:1); |
| 1338 |
w->writeC((intHz!=60 && intHz!=50)); |
| 1339 |
char customHz[4]; |
| 1340 |
memset(customHz,0,4); |
| 1341 |
snprintf(customHz,4,"%d",(int)curSubSong->hz); |
| 1342 |
w->write(customHz,3); |
| 1343 |
w->writeI(curSubSong->patLen); |
| 1344 |
w->writeC(curSubSong->ordersLen); |
| 1345 |
|
| 1346 |
for (int i=0; i<chans; i++) { |
| 1347 |
for (int j=0; j<curSubSong->ordersLen; j++) { |
| 1348 |
w->writeC(curOrders->ord[i][j]); |
| 1349 |
if (version>=25) { |
| 1350 |
DivPattern* pat=curPat[i].getPattern(j,false); |
| 1351 |
w->writeString(pat->name,true); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
if (song.subsong.size()>1) { |
| 1357 |
addWarning("only the currently selected subsong will be saved"); |
| 1358 |
} |
| 1359 |
|
| 1360 |
if (!song.grooves.empty()) { |
| 1361 |
addWarning("grooves will not be saved"); |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (curSubSong->speeds.len>2) { |
| 1365 |
addWarning("only the first two speeds will be effective"); |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (curSubSong->virtualTempoD!=curSubSong->virtualTempoN) { |
| 1369 |
addWarning(".dmf format does not support virtual tempo"); |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (song.tuning<439.99 && song.tuning>440.01) { |
| 1373 |
addWarning(".dmf format does not support tuning"); |
| 1374 |
} |
| 1375 |
|
| 1376 |
if (sys==DIV_SYSTEM_C64_6581 || sys==DIV_SYSTEM_C64_8580) { |
| 1377 |
addWarning("absolute duty/cutoff macro not available in .dmf!"); |
| 1378 |
addWarning("duty precision will be lost"); |
| 1379 |
} |
| 1380 |
|
| 1381 |
for (DivInstrument* i: song.ins) { |
| 1382 |
if (i->type==DIV_INS_AMIGA) { |
| 1383 |
addWarning(".dmf format does not support arbitrary-pitch sample mode"); |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
for (DivInstrument* i: song.ins) { |
| 1389 |
if (i->type==DIV_INS_FM || i->type==DIV_INS_OPM) { |
| 1390 |
addWarning("no FM macros in .dmf format"); |
| 1391 |
break; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
w->writeC(song.ins.size()); |
| 1396 |
for (DivInstrument* i: song.ins) { |
| 1397 |
w->writeString(i->name,true); |
| 1398 |
|
| 1399 |
// safety check |
| 1400 |
if (!isFMSystem(sys) && i->type!=DIV_INS_STD && i->type!=DIV_INS_NES && i->type!=DIV_INS_FDS) { |
| 1401 |
switch (song.system[0]) { |
| 1402 |
case DIV_SYSTEM_GB: |
| 1403 |
i->type=DIV_INS_GB; |
| 1404 |
break; |
| 1405 |
case DIV_SYSTEM_NES: |
| 1406 |
i->type=DIV_INS_NES; |
| 1407 |
break; |
| 1408 |
case DIV_SYSTEM_C64_6581: |
| 1409 |
case DIV_SYSTEM_C64_8580: |
| 1410 |
i->type=DIV_INS_C64; |
| 1411 |
break; |
| 1412 |
case DIV_SYSTEM_PCE: |
| 1413 |
i->type=DIV_INS_PCE; |
| 1414 |
break; |
| 1415 |
case DIV_SYSTEM_YM2610: |
| 1416 |
case DIV_SYSTEM_YM2610_EXT: |
| 1417 |
i->type=DIV_INS_AY; |
| 1418 |
break; |
| 1419 |
default: |
| 1420 |
i->type=DIV_INS_STD; |
| 1421 |
break; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
if (!isSTDSystem(sys) && i->type!=DIV_INS_FM && i->type!=DIV_INS_OPM) { |
| 1425 |
if (sys==DIV_SYSTEM_ARCADE) { |
| 1426 |
i->type=DIV_INS_OPM; |
| 1427 |
} else { |
| 1428 |
i->type=DIV_INS_FM; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
w->writeC((i->type==DIV_INS_FM || i->type==DIV_INS_OPM || i->type==DIV_INS_OPLL)?1:0); |
| 1433 |
if (i->type==DIV_INS_FM || i->type==DIV_INS_OPM || i->type==DIV_INS_OPLL) { // FM |
| 1434 |
w->writeC(i->fm.alg); |
| 1435 |
w->writeC(i->fm.fb); |
| 1436 |
w->writeC(i->fm.fms); |
| 1437 |
w->writeC(i->fm.ams); |
| 1438 |
|
| 1439 |
for (int j=0; j<4; j++) { |
| 1440 |
DivInstrumentFM::Operator& op=i->fm.op[j]; |
| 1441 |
w->writeC(op.am); |
| 1442 |
w->writeC(op.ar); |
| 1443 |
w->writeC(op.dr); |
| 1444 |
w->writeC(op.mult); |
| 1445 |
w->writeC(op.rr); |
| 1446 |
w->writeC(op.sl); |
| 1447 |
w->writeC(op.tl); |
| 1448 |
if ((sys==DIV_SYSTEM_SMS_OPLL || sys==DIV_SYSTEM_NES_VRC7) && j==0) { |
| 1449 |
w->writeC(i->fm.opllPreset); |
| 1450 |
} else { |
| 1451 |
w->writeC(op.dt2); |
| 1452 |
} |
| 1453 |
if (sys==DIV_SYSTEM_SMS_OPLL || sys==DIV_SYSTEM_NES_VRC7) { |
| 1454 |
w->writeC(op.ksr); |
| 1455 |
w->writeC(op.vib); |
| 1456 |
w->writeC(op.ksl); |
| 1457 |
w->writeC(op.ssgEnv); |
| 1458 |
} else { |
| 1459 |
w->writeC(op.rs); |
| 1460 |
w->writeC(op.dt); |
| 1461 |
w->writeC(op.d2r); |
| 1462 |
w->writeC(op.ssgEnv); |
| 1463 |
} |
| 1464 |
} |
| 1465 |
} else { // STD |
| 1466 |
bool volIsCutoff=false; |
| 1467 |
|
| 1468 |
if (sys!=DIV_SYSTEM_GB) { |
| 1469 |
int realVolMacroLen=i->std.volMacro.len; |
| 1470 |
if (realVolMacroLen>127) realVolMacroLen=127; |
| 1471 |
if (sys==DIV_SYSTEM_C64_6581 || sys==DIV_SYSTEM_C64_8580) { |
| 1472 |
if (i->std.algMacro.len>0) volIsCutoff=true; |
| 1473 |
if (volIsCutoff) { |
| 1474 |
if (i->std.volMacro.len>0) { |
| 1475 |
addWarning(".dmf only supports volume or cutoff macro in C64, but not both. volume macro will be lost."); |
| 1476 |
} |
| 1477 |
realVolMacroLen=i->std.algMacro.len; |
| 1478 |
if (realVolMacroLen>127) realVolMacroLen=127; |
| 1479 |
w->writeC(realVolMacroLen); |
| 1480 |
for (int j=0; j<realVolMacroLen; j++) { |
| 1481 |
w->writeI((-i->std.algMacro.val[j])+18); |
| 1482 |
} |
| 1483 |
} else { |
| 1484 |
w->writeC(realVolMacroLen); |
| 1485 |
for (int j=0; j<realVolMacroLen; j++) { |
| 1486 |
w->writeI(i->std.volMacro.val[j]); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
} else { |
| 1490 |
w->writeC(realVolMacroLen); |
| 1491 |
for (int j=0; j<realVolMacroLen; j++) { |
| 1492 |
w->writeI(i->std.volMacro.val[j]); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
if (realVolMacroLen>0) { |
| 1496 |
if (volIsCutoff) { |
| 1497 |
w->writeC(i->std.algMacro.loop); |
| 1498 |
} else { |
| 1499 |
w->writeC(i->std.volMacro.loop); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
bool arpMacroMode=false; |
| 1505 |
int arpMacroHowManyFixed=0; |
| 1506 |
int realArpMacroLen=i->std.arpMacro.len; |
| 1507 |
for (int j=0; j<i->std.arpMacro.len; j++) { |
| 1508 |
if ((i->std.arpMacro.val[j]&0xc0000000)==0x40000000 || (i->std.arpMacro.val[j]&0xc0000000)==0x80000000) { |
| 1509 |
arpMacroHowManyFixed++; |
| 1510 |
} |
| 1511 |
} |
| 1512 |
if (arpMacroHowManyFixed>=i->std.arpMacro.len-1) { |
| 1513 |
arpMacroMode=true; |
| 1514 |
} |
| 1515 |
if (i->std.arpMacro.len>0) { |
| 1516 |
if (arpMacroMode && i->std.arpMacro.val[i->std.arpMacro.len-1]==0 && i->std.arpMacro.loop>=i->std.arpMacro.len) { |
| 1517 |
realArpMacroLen--; |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
if (realArpMacroLen>127) realArpMacroLen=127; |
| 1522 |
|
| 1523 |
w->writeC(realArpMacroLen); |
| 1524 |
|
| 1525 |
if (arpMacroMode) { |
| 1526 |
for (int j=0; j<realArpMacroLen; j++) { |
| 1527 |
if ((i->std.arpMacro.val[j]&0xc0000000)==0x40000000 || (i->std.arpMacro.val[j]&0xc0000000)==0x80000000) { |
| 1528 |
w->writeI(i->std.arpMacro.val[j]^0x40000000); |
| 1529 |
} else { |
| 1530 |
w->writeI(i->std.arpMacro.val[j]); |
| 1531 |
} |
| 1532 |
} |
| 1533 |
} else { |
| 1534 |
for (int j=0; j<realArpMacroLen; j++) { |
| 1535 |
if ((i->std.arpMacro.val[j]&0xc0000000)==0x40000000 || (i->std.arpMacro.val[j]&0xc0000000)==0x80000000) { |
| 1536 |
w->writeI((i->std.arpMacro.val[j]^0x40000000)+12); |
| 1537 |
} else { |
| 1538 |
w->writeI(i->std.arpMacro.val[j]+12); |
| 1539 |
} |
| 1540 |
} |
| 1541 |
} |
| 1542 |
if (realArpMacroLen>0) { |
| 1543 |
w->writeC(i->std.arpMacro.loop); |
| 1544 |
} |
| 1545 |
w->writeC(arpMacroMode); |
| 1546 |
|
| 1547 |
int realDutyMacroLen=i->std.dutyMacro.len; |
| 1548 |
if (realDutyMacroLen>127) realDutyMacroLen=127; |
| 1549 |
w->writeC(realDutyMacroLen); |
| 1550 |
if (sys==DIV_SYSTEM_C64_6581 || sys==DIV_SYSTEM_C64_8580) { |
| 1551 |
for (int j=0; j<realDutyMacroLen; j++) { |
| 1552 |
w->writeI(i->std.dutyMacro.val[j]+12); |
| 1553 |
} |
| 1554 |
} else { |
| 1555 |
for (int j=0; j<realDutyMacroLen; j++) { |
| 1556 |
w->writeI(i->std.dutyMacro.val[j]); |
| 1557 |
} |
| 1558 |
} |
| 1559 |
if (realDutyMacroLen>0) { |
| 1560 |
w->writeC(i->std.dutyMacro.loop); |
| 1561 |
} |
| 1562 |
|
| 1563 |
int realWaveMacroLen=i->std.waveMacro.len; |
| 1564 |
if (realWaveMacroLen>127) realWaveMacroLen=127; |
| 1565 |
w->writeC(realWaveMacroLen); |
| 1566 |
for (int j=0; j<realWaveMacroLen; j++) { |
| 1567 |
// piece of crap offset by 1 |
| 1568 |
if (song.system[0]==DIV_SYSTEM_YM2610 || song.system[0]==DIV_SYSTEM_YM2610_EXT) { |
| 1569 |
w->writeI(i->std.waveMacro.val[j]-1); |
| 1570 |
} else { |
| 1571 |
w->writeI(i->std.waveMacro.val[j]); |
| 1572 |
} |
| 1573 |
} |
| 1574 |
if (realWaveMacroLen>0) { |
| 1575 |
w->writeC(i->std.waveMacro.loop); |
| 1576 |
} |
| 1577 |
|
| 1578 |
if (sys==DIV_SYSTEM_C64_6581 || sys==DIV_SYSTEM_C64_8580) { |
| 1579 |
w->writeC(i->c64.triOn); |
| 1580 |
w->writeC(i->c64.sawOn); |
| 1581 |
w->writeC(i->c64.pulseOn); |
| 1582 |
w->writeC(i->c64.noiseOn); |
| 1583 |
|
| 1584 |
w->writeC(i->c64.a); |
| 1585 |
w->writeC(i->c64.d); |
| 1586 |
w->writeC(i->c64.s); |
| 1587 |
w->writeC(i->c64.r); |
| 1588 |
|
| 1589 |
logW("duty and cutoff precision will be lost!"); |
| 1590 |
w->writeC((i->c64.duty*100)/4095); |
| 1591 |
|
| 1592 |
w->writeC(i->c64.ringMod); |
| 1593 |
w->writeC(i->c64.oscSync); |
| 1594 |
|
| 1595 |
w->writeC(i->c64.toFilter); |
| 1596 |
w->writeC(volIsCutoff); |
| 1597 |
w->writeC(i->c64.initFilter); |
| 1598 |
|
| 1599 |
w->writeC(i->c64.res); |
| 1600 |
w->writeC((i->c64.cut*100)/2047); |
| 1601 |
w->writeC(i->c64.hp); |
| 1602 |
w->writeC(i->c64.bp); |
| 1603 |
w->writeC(i->c64.lp); |
| 1604 |
w->writeC(i->c64.ch3off); |
| 1605 |
} |
| 1606 |
|
| 1607 |
if (sys==DIV_SYSTEM_GB) { |
| 1608 |
w->writeC(i->gb.envVol); |
| 1609 |
w->writeC(i->gb.envDir); |
| 1610 |
w->writeC(i->gb.envLen); |
| 1611 |
w->writeC(i->gb.soundLen); |
| 1612 |
} |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
w->writeC(song.wave.size()); |
| 1617 |
for (DivWavetable* i: song.wave) { |
| 1618 |
w->writeI(i->len); |
| 1619 |
if (sys==DIV_SYSTEM_NES_FDS && version<26) { |
| 1620 |
for (int j=0; j<i->len; j++) { |
| 1621 |
w->writeI(i->data[j]>>2); |
| 1622 |
} |
| 1623 |
} else { |
| 1624 |
for (int j=0; j<i->len; j++) { |
| 1625 |
w->writeI(i->data[j]); |
| 1626 |
} |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
bool relWarning=false; |
| 1631 |
|
| 1632 |
for (int i=0; i<getChannelCount(sys); i++) { |
| 1633 |
w->writeC(curPat[i].effectCols); |
| 1634 |
|
| 1635 |
for (int j=0; j<curSubSong->ordersLen; j++) { |
| 1636 |
DivPattern* pat=curPat[i].getPattern(curOrders->ord[i][j],false); |
| 1637 |
for (int k=0; k<curSubSong->patLen; k++) { |
| 1638 |
if ((pat->data[k][0]==101 || pat->data[k][0]==102) && pat->data[k][1]==0) { |
| 1639 |
w->writeS(100); |
| 1640 |
w->writeS(0); |
| 1641 |
if (!relWarning) { |
| 1642 |
relWarning=true; |
| 1643 |
addWarning("note/macro release will be converted to note off!"); |
| 1644 |
} |
| 1645 |
} else { |
| 1646 |
w->writeS(pat->data[k][0]); // note |
| 1647 |
w->writeS(pat->data[k][1]); // octave |
| 1648 |
} |
| 1649 |
w->writeS(pat->data[k][3]); // volume |
| 1650 |
#ifdef TA_BIG_ENDIAN |
| 1651 |
for (int l=0; l<curPat[i].effectCols*2; l++) { |
| 1652 |
w->writeS(pat->data[k][4+l]); |
| 1653 |
} |
| 1654 |
#else |
| 1655 |
w->write(&pat->data[k][4],2*curPat[i].effectCols*2); // effects |
| 1656 |
#endif |
| 1657 |
w->writeS(pat->data[k][2]); // instrument |
| 1658 |
} |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
if (song.sample.size()>0) { |
| 1663 |
addWarning("samples' rates will be rounded to nearest compatible value"); |
| 1664 |
} |
| 1665 |
|
| 1666 |
w->writeC(song.sample.size()); |
| 1667 |
for (DivSample* i: song.sample) { |
| 1668 |
w->writeI(i->samples); |
| 1669 |
w->writeString(i->name,true); |
| 1670 |
w->writeC(divToFileRate(i->rate)); |
| 1671 |
w->writeC(5); |
| 1672 |
w->writeC(50); |
| 1673 |
// i'm too lazy to deal with .dmf's weird way of storing 8-bit samples |
| 1674 |
w->writeC(16); |
| 1675 |
// well I can't be lazy if it's on a big-endian system |
| 1676 |
#ifdef TA_BIG_ENDIAN |
| 1677 |
for (unsigned int j=0; j<i->length16; j++) { |
| 1678 |
w->writeC(((unsigned short)i->data16[j])&0xff); |
| 1679 |
w->writeC(((unsigned short)i->data16[j])>>8); |
| 1680 |
} |
| 1681 |
#else |
| 1682 |
w->write(i->data16,i->length16); |
| 1683 |
#endif |
| 1684 |
} |
| 1685 |
|
| 1686 |
saveLock.unlock(); |
| 1687 |
return w; |
| 1688 |
} |