| 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 |
class TFMRLEReader; |
| 23 |
|
| 24 |
struct TFMEndOfFileException { |
| 25 |
TFMRLEReader* reader; |
| 26 |
size_t finalSize; |
| 27 |
TFMEndOfFileException(TFMRLEReader* r, size_t fs): |
| 28 |
reader(r), |
| 29 |
finalSize(fs) {} |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
class TFMRLEReader { |
| 34 |
const unsigned char* buf; |
| 35 |
size_t len; |
| 36 |
size_t curSeek; |
| 37 |
bool inTag; |
| 38 |
int tagLenLeft; |
| 39 |
signed char tagChar; |
| 40 |
|
| 41 |
void decodeRLE(unsigned char prevChar) { |
| 42 |
int lenShift=0; |
| 43 |
tagLenLeft=0; |
| 44 |
unsigned char rleTag=0; |
| 45 |
|
| 46 |
do { |
| 47 |
rleTag=readCNoRLE(); |
| 48 |
tagLenLeft|=(rleTag&0x7F)<<lenShift; |
| 49 |
lenShift+=7; |
| 50 |
//logD("offset: %x, RLE tag: %X, len shift: %d, len left: %d",curSeek,rleTag,lenShift,tagLenLeft); |
| 51 |
} while (!(rleTag&0x80)); |
| 52 |
|
| 53 |
if (tagLenLeft) { |
| 54 |
// sync back since we've already read one character |
| 55 |
inTag=true; |
| 56 |
tagLenLeft--; |
| 57 |
tagChar=prevChar; |
| 58 |
} else { |
| 59 |
tagChar=0x80; |
| 60 |
} |
| 61 |
//logD("tag finished: len left: %d, char: %X",tagLenLeft,tagChar); |
| 62 |
} |
| 63 |
|
| 64 |
public: |
| 65 |
TFMRLEReader(const void* b, size_t l) : |
| 66 |
buf((const unsigned char*)b), |
| 67 |
len(l), |
| 68 |
curSeek(0), |
| 69 |
inTag(false), |
| 70 |
tagLenLeft(0), |
| 71 |
tagChar(0) {} |
| 72 |
|
| 73 |
// these functions may throw TFMEndOfFileException |
| 74 |
unsigned char readC() { |
| 75 |
if (inTag) { |
| 76 |
if (tagLenLeft<=0) { |
| 77 |
inTag=false; |
| 78 |
return readC(); |
| 79 |
} |
| 80 |
tagLenLeft--; |
| 81 |
//logD("one char RLE decompressed, tag left: %d, char: %d",tagLenLeft,tagChar); |
| 82 |
return tagChar; |
| 83 |
} |
| 84 |
if (curSeek>len) throw TFMEndOfFileException(this,len); |
| 85 |
|
| 86 |
unsigned char ret=buf[curSeek++]; |
| 87 |
|
| 88 |
// MISLEADING DOCUMENTATION: while TFM music maker's documentation says if the next byte |
| 89 |
// is zero, then it's not a tag but just 0x80 (for example: 0x00 0x80 0x00 = 0x00 0x80) |
| 90 |
// this is actually wrong |
| 91 |
// through research and experimentation, there are times that TFM music maker |
| 92 |
// will use 0x80 0x00 for actual tags (for example: 0x00 0x80 0x00 0x84 = 512 times 0x00 |
| 93 |
// in certain parts of the header and footer) |
| 94 |
// TFM music maker actually uses double 0x80 to escape the 0x80 |
| 95 |
// for example: 0xDA 0x80 0x80 0x00 0x23 = 0xDA 0x80 0x00 0x23) |
| 96 |
if (ret==0x80) { |
| 97 |
decodeRLE(buf[curSeek-2]); |
| 98 |
tagLenLeft--; |
| 99 |
return tagChar; |
| 100 |
} |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
signed char readCNoRLE() { |
| 105 |
if (curSeek>len) throw TFMEndOfFileException(this,len); |
| 106 |
return buf[curSeek++]; |
| 107 |
} |
| 108 |
|
| 109 |
void read(unsigned char* b, size_t l) { |
| 110 |
int i=0; |
| 111 |
while(l--) { |
| 112 |
unsigned char nextChar=readC(); |
| 113 |
b[i++]=nextChar; |
| 114 |
//logD("read next char: %x, index: %d",nextChar,i); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void readNoRLE(unsigned char *b, size_t l) { |
| 119 |
int i=0; |
| 120 |
while (l--) { |
| 121 |
b[i++]=buf[curSeek++]; |
| 122 |
if (curSeek>len) throw TFMEndOfFileException(this,len); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
short readS() { |
| 127 |
return readC()|readC()<<8; |
| 128 |
} |
| 129 |
|
| 130 |
short readSNoRLE() { |
| 131 |
if (curSeek+2>len) throw TFMEndOfFileException(this,len); |
| 132 |
short ret=buf[curSeek]|buf[curSeek+1]<<8; |
| 133 |
curSeek+=2; |
| 134 |
return ret; |
| 135 |
} |
| 136 |
|
| 137 |
String readString(size_t l) { |
| 138 |
String ret; |
| 139 |
ret.reserve(l); |
| 140 |
while (l--) { |
| 141 |
unsigned char byte=readC(); |
| 142 |
if (!byte) { |
| 143 |
skip(l); |
| 144 |
break; |
| 145 |
} |
| 146 |
ret += byte; |
| 147 |
} |
| 148 |
return ret; |
| 149 |
} |
| 150 |
void skip(size_t l) { |
| 151 |
// quick and dirty |
| 152 |
while (l--) { |
| 153 |
//logD("skipping l %d",l); |
| 154 |
readC(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
}; |
| 159 |
|
| 160 |
String TFMparseDate(short date) { |
| 161 |
return fmt::sprintf("%02d.%02d.%02d",date>>11,(date>>7)&0xF,date&0x7F); |
| 162 |
} |
| 163 |
|
| 164 |
struct TFMSpeed { |
| 165 |
unsigned char speedEven; |
| 166 |
unsigned char speedOdd; |
| 167 |
unsigned char interleaveFactor; |
| 168 |
|
| 169 |
bool operator==(const TFMSpeed &s) const { |
| 170 |
return speedEven==s.speedEven && speedOdd==s.speedOdd && interleaveFactor==s.interleaveFactor; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
// to make it work with map |
| 175 |
namespace std { |
| 176 |
template<> struct hash<TFMSpeed> |
| 177 |
{ |
| 178 |
size_t operator()(const TFMSpeed& s) const noexcept { |
| 179 |
return s.speedEven<<16|s.speedOdd<<8|s.interleaveFactor; |
| 180 |
} |
| 181 |
}; |
| 182 |
} |
| 183 |
|
| 184 |
struct TFMParsePatternInfo { |
| 185 |
TFMRLEReader* reader; |
| 186 |
unsigned char maxPat; |
| 187 |
unsigned char* patLens; |
| 188 |
unsigned char* orderList; |
| 189 |
unsigned char speedEven; |
| 190 |
unsigned char speedOdd; |
| 191 |
unsigned char interleaveFactor; |
| 192 |
bool* patExists; |
| 193 |
DivSong* ds; |
| 194 |
int* insNumMaps; |
| 195 |
bool v2; |
| 196 |
unsigned char loopPos; |
| 197 |
}; |
| 198 |
|
| 199 |
void TFMParsePattern(struct TFMParsePatternInfo info) { |
| 200 |
// PATTERN DATA FORMAT (not described properly in the documentation) |
| 201 |
// for each channel in a pattern: |
| 202 |
// - note data (256 bytes) |
| 203 |
// - volume data (256 bytes, values always 0x00-0x1F) |
| 204 |
// - instrument number data (256 bytes) |
| 205 |
// - effect number (256 bytes, values 0x0-0x23 (to represent 0-F and G-Z)) |
| 206 |
// - effect value (256 bytes) |
| 207 |
// - extra 3 effects (1536 bytes 256x3x2) (ONLY ON V2) |
| 208 |
// notes are stored as an inverted value of note+octave*12 |
| 209 |
// key-offs are stored in the note data as 0x01 |
| 210 |
unsigned char patDataBuf[256]; |
| 211 |
unsigned short lastSlide=0; |
| 212 |
unsigned short lastVibrato=0; |
| 213 |
|
| 214 |
struct TFMSpeed speed; |
| 215 |
DivGroovePattern groove; |
| 216 |
speed.speedEven=info.speedEven; |
| 217 |
speed.speedOdd=info.speedOdd; |
| 218 |
speed.interleaveFactor=info.interleaveFactor; |
| 219 |
int speedGrooveIndex=1; |
| 220 |
|
| 221 |
int usedEffectsCol=0; |
| 222 |
std::unordered_map<TFMSpeed, int> speeds({{speed, 0}}); |
| 223 |
|
| 224 |
// initialize the global groove pattern first |
| 225 |
if (speed.interleaveFactor>8) { |
| 226 |
logW("speed interleave factor is bigger than 8, speed information may be inaccurate"); |
| 227 |
speed.interleaveFactor=8; |
| 228 |
} |
| 229 |
for (int i=0; i<speed.interleaveFactor; i++) { |
| 230 |
groove.val[i]=speed.speedEven; |
| 231 |
groove.val[i+speed.interleaveFactor]=speed.speedOdd; |
| 232 |
} |
| 233 |
groove.len=speed.interleaveFactor*2; |
| 234 |
|
| 235 |
info.ds->grooves.push_back(groove); |
| 236 |
|
| 237 |
for (int i=0; i<256; i++) { |
| 238 |
if (i>info.maxPat) break; |
| 239 |
else if (!info.patExists[i]) { |
| 240 |
logD("skipping pattern %d",i); |
| 241 |
info.reader->skip((info.v2) ? 16896 : 7680); |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
logD("parsing pattern %d",i); |
| 246 |
for (int j=0; j<6; j++) { |
| 247 |
DivPattern* pat = info.ds->subsong[0]->pat[j].data[i]; |
| 248 |
|
| 249 |
// notes |
| 250 |
info.reader->read(patDataBuf,256); |
| 251 |
|
| 252 |
logD("parsing notes of pattern %d channel %d",i,j); |
| 253 |
for (int k=0; k<256; k++) { |
| 254 |
if (patDataBuf[k]==0) continue; |
| 255 |
else if (patDataBuf[k]==1) { |
| 256 |
// note off |
| 257 |
pat->data[k][0]=100; |
| 258 |
} else { |
| 259 |
unsigned char invertedNote=~patDataBuf[k]; |
| 260 |
pat->data[k][0]=invertedNote%12; |
| 261 |
pat->data[k][1]=(invertedNote/12)-1; |
| 262 |
|
| 263 |
if (pat->data[k][0]==0) { |
| 264 |
pat->data[k][0]=12; |
| 265 |
pat->data[k][1]--; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// volume |
| 271 |
info.reader->read(patDataBuf,256); |
| 272 |
|
| 273 |
logD("parsing volumes of pattern %d channel %d",i,j); |
| 274 |
for (int k=0; k<256; k++) { |
| 275 |
if (patDataBuf[k]==0) continue; |
| 276 |
else pat->data[k][3]=0x60+patDataBuf[k]; |
| 277 |
} |
| 278 |
|
| 279 |
// instrument |
| 280 |
info.reader->read(patDataBuf,256); |
| 281 |
|
| 282 |
logD("parsing instruments of pattern %d channel %d",i,j); |
| 283 |
for (int k=0; k<256; k++) { |
| 284 |
if (patDataBuf[k]==0) continue; |
| 285 |
pat->data[k][2]=info.insNumMaps[patDataBuf[k]-1]; |
| 286 |
} |
| 287 |
|
| 288 |
// effects |
| 289 |
|
| 290 |
int numEffectsCol=(info.v2) ? 4 : 1; |
| 291 |
for (int l=0; l<numEffectsCol; l++) { |
| 292 |
unsigned char effectNum[256]; |
| 293 |
unsigned char effectVal[256]; |
| 294 |
info.reader->read(effectNum,256); |
| 295 |
info.reader->read(effectVal,256); |
| 296 |
|
| 297 |
for (int k=0; k<256; k++) { |
| 298 |
if (effectNum[k] || effectVal[k]) usedEffectsCol=l+1; |
| 299 |
switch (effectNum[k]) { |
| 300 |
case 0: |
| 301 |
// arpeggio or no effect (if effect val is 0) |
| 302 |
if (effectVal[k]==0) break; |
| 303 |
pat->data[k][4+(l*2)]=effectNum[k]; |
| 304 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 305 |
break; |
| 306 |
case 1: |
| 307 |
// pitch slide up |
| 308 |
case 2: |
| 309 |
// pitch slide down |
| 310 |
pat->data[k][4+(l*2)]=effectNum[k]; |
| 311 |
if (effectVal[k]) { |
| 312 |
lastSlide=effectVal[k]; |
| 313 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 314 |
} else { |
| 315 |
pat->data[k][5+(l*2)]=lastSlide; |
| 316 |
} |
| 317 |
break; |
| 318 |
case 3: |
| 319 |
// portamento |
| 320 |
case 4: |
| 321 |
// vibrato |
| 322 |
pat->data[k][5+(l*2)]=0; |
| 323 |
if (effectVal[k]&0xF0) { |
| 324 |
pat->data[k][5+(l*2)]|=effectVal[k]&0xF0; |
| 325 |
} else { |
| 326 |
pat->data[k][5+(l*2)]|=lastVibrato&0xF0; |
| 327 |
} |
| 328 |
if (effectVal[k]&0x0F) { |
| 329 |
pat->data[k][5+(l*2)]|=effectVal[k]&0x0F; |
| 330 |
} else { |
| 331 |
pat->data[k][5+(l*2)]|=lastVibrato&0x0F; |
| 332 |
} |
| 333 |
pat->data[k][4+(l*2)]=effectNum[k]; |
| 334 |
lastVibrato=pat->data[k][5+(l*2)]; |
| 335 |
break; |
| 336 |
case 5: |
| 337 |
// poramento + volume slide |
| 338 |
pat->data[k][4+(l*2)]=0x06; |
| 339 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 340 |
break; |
| 341 |
case 6: |
| 342 |
// vibrato + volume slide |
| 343 |
pat->data[k][4+(l*2)]=0x05; |
| 344 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 345 |
break; |
| 346 |
case 8: |
| 347 |
// modify TL of operator 1 |
| 348 |
pat->data[k][4+(l*2)]=0x12; |
| 349 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 350 |
break; |
| 351 |
case 9: |
| 352 |
// modify TL of operator 2 |
| 353 |
pat->data[k][4+(l*2)]=0x13; |
| 354 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 355 |
break; |
| 356 |
case 10: |
| 357 |
// volume slide |
| 358 |
pat->data[k][4+(l*2)]=0xA; |
| 359 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 360 |
break; |
| 361 |
case 11: |
| 362 |
// multi-frequency mode of CH3 control |
| 363 |
// TODO |
| 364 |
case 12: |
| 365 |
// modify TL of operator 3 |
| 366 |
pat->data[k][4+(l*2)]=0x14; |
| 367 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 368 |
break; |
| 369 |
case 13: |
| 370 |
// modify TL of operator 4 |
| 371 |
pat->data[k][4+(l*2)]=0x15; |
| 372 |
pat->data[k][5+(l*2)]=effectVal[k]; |
| 373 |
break; |
| 374 |
case 14: |
| 375 |
switch (effectVal[k]>>4) { |
| 376 |
case 0: |
| 377 |
case 1: |
| 378 |
case 2: |
| 379 |
case 3: |
| 380 |
// modify multiplier of operators |
| 381 |
pat->data[k][4+(l*2)]=0x16; |
| 382 |
pat->data[k][5+(l*2)]=((effectVal[k]&0xF0)+0x100)|(effectVal[k]&0xF); |
| 383 |
break; |
| 384 |
case 8: |
| 385 |
// pan |
| 386 |
pat->data[k][4+(l*2)]=0x80; |
| 387 |
if ((effectVal[k]&0xF)==1) { |
| 388 |
pat->data[k][5+(l*2)]=0; |
| 389 |
} else if ((effectVal[k]&0xF)==2) { |
| 390 |
pat->data[k][5+(l*2)]=0xFF; |
| 391 |
} else { |
| 392 |
pat->data[k][5+(l*2)]=0x80; |
| 393 |
} |
| 394 |
break; |
| 395 |
} |
| 396 |
break; |
| 397 |
case 15: |
| 398 |
// speed |
| 399 |
|
| 400 |
if (effectVal[k]==0) { |
| 401 |
// if speed is set to zero (reset to global values) |
| 402 |
speed.speedEven=info.speedEven; |
| 403 |
speed.speedOdd=info.speedOdd; |
| 404 |
speed.interleaveFactor=info.interleaveFactor; |
| 405 |
} else if (effectVal[k]>>4==0) { |
| 406 |
// if the top nibble is set to zero (set interleave factor) |
| 407 |
speed.interleaveFactor=effectVal[k]&0xF; |
| 408 |
} else if ((effectVal[k]>>4)==(effectVal[k]&0xF)) { |
| 409 |
// if both speeds are equal |
| 410 |
pat->data[k][4+(l*2)]=0x0F; |
| 411 |
unsigned char speedSet=effectVal[k]>>4; |
| 412 |
pat->data[k][5+(l*2)]=speedSet; |
| 413 |
break; |
| 414 |
} else { |
| 415 |
speed.speedEven=effectVal[k]>>4; |
| 416 |
speed.speedOdd=effectVal[k]&0xF; |
| 417 |
} |
| 418 |
|
| 419 |
auto speedIndex = speeds.find(speed); |
| 420 |
if (speedIndex != speeds.end()) { |
| 421 |
pat->data[k][4+(l*2)]=0x09; |
| 422 |
pat->data[k][5+(l*2)]=speedIndex->second; |
| 423 |
break; |
| 424 |
} |
| 425 |
if (speed.interleaveFactor>8) { |
| 426 |
logW("speed interleave factor is bigger than 8, speed information may be inaccurate"); |
| 427 |
speed.interleaveFactor=8; |
| 428 |
} |
| 429 |
for (int i=0; i<speed.interleaveFactor; i++) { |
| 430 |
groove.val[i]=speed.speedEven; |
| 431 |
if (i+speed.interleaveFactor<16) { |
| 432 |
groove.val[i+speed.interleaveFactor]=speed.speedOdd; |
| 433 |
} |
| 434 |
} |
| 435 |
groove.len=speed.interleaveFactor*2; |
| 436 |
|
| 437 |
info.ds->grooves.push_back(groove); |
| 438 |
speeds[speed]=speedGrooveIndex; |
| 439 |
|
| 440 |
pat->data[k][4+(l*2)]=0x09; |
| 441 |
pat->data[k][5+(l*2)]=speedGrooveIndex; |
| 442 |
speedGrooveIndex++; |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
info.ds->subsong[0]->pat[j].effectCols=(usedEffectsCol*2)+1; |
| 447 |
|
| 448 |
// put a "jump to next pattern" effect if the pattern is smaller than the maximum pattern length |
| 449 |
if (info.patLens[i]!=0 && info.patLens[i]<info.ds->subsong[0]->patLen) { |
| 450 |
pat->data[info.patLens[i]-1][4+(usedEffectsCol*4)]=0x0D; |
| 451 |
pat->data[info.patLens[i]-1][5+(usedEffectsCol*4)]=0x00; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
// 2nd pass: fixing pitch slides, arpeggios, etc. so the result doesn't sound weird. |
| 458 |
|
| 459 |
bool chArpeggio[6]={false}; |
| 460 |
bool chVibrato[6]={false}; |
| 461 |
bool chPorta[6]={false}; |
| 462 |
bool chVolumeSlide[6]={false}; |
| 463 |
int lastPatSeen=0; |
| 464 |
|
| 465 |
for (int i=0; i<info.ds->subsong[0]->ordersLen; i++) { |
| 466 |
// this is if the last pattern is used more than once |
| 467 |
if (info.orderList[i] == info.orderList[info.ds->subsong[0]->ordersLen - 1]) { |
| 468 |
lastPatSeen++; |
| 469 |
} |
| 470 |
for (int j=0; j<6; j++) { |
| 471 |
for (int l=0; l<usedEffectsCol; l++) { |
| 472 |
DivPattern* pat = info.ds->subsong[0]->pat[j].data[info.orderList[i]]; |
| 473 |
unsigned char truePatLen=(info.patLens[info.orderList[i]]<info.ds->subsong[0]->patLen) ? info.patLens[info.orderList[i]] : info.ds->subsong[0]->patLen; |
| 474 |
|
| 475 |
// default instrument |
| 476 |
if (i==0 && pat->data[0][2]==-1) pat->data[0][2]=0; |
| 477 |
|
| 478 |
for (int k=0; k<truePatLen; k++) { |
| 479 |
if (chArpeggio[j] && pat->data[k][4+(l*2)]!=0x00 && pat->data[k][0]!=-1) { |
| 480 |
pat->data[k][4+usedEffectsCol*2+(l*2)]=0x00; |
| 481 |
pat->data[k][5+usedEffectsCol*2+(l*2)]=0; |
| 482 |
chArpeggio[j]=false; |
| 483 |
} else if (chPorta[j] && pat->data[k][4+(l*2)]!=0x03 && pat->data[k][4+(l*2)]!=0x01 && pat->data[k][4+(l*2)]!=0x02) { |
| 484 |
pat->data[k][4+usedEffectsCol*2+(l*2)]=0x03; |
| 485 |
pat->data[k][5+usedEffectsCol*2+(l*2)]=0; |
| 486 |
chPorta[j]=false; |
| 487 |
} else if (chVibrato[j] && pat->data[k][4+(l*2)]!=0x04 && pat->data[k][0]!=-1) { |
| 488 |
pat->data[k][4+usedEffectsCol*2+(l*2)]=0x04; |
| 489 |
pat->data[k][5+usedEffectsCol*2+(l*2)]=0; |
| 490 |
chVibrato[j]=false; |
| 491 |
} else if (chVolumeSlide[j] && pat->data[k][4+(l*2)]!=0x0A) { |
| 492 |
pat->data[k][4+usedEffectsCol*2+(l*2)]=0x0A; |
| 493 |
pat->data[k][5+usedEffectsCol*2+(l*2)]=0; |
| 494 |
chVolumeSlide[j]=false; |
| 495 |
} |
| 496 |
|
| 497 |
switch (pat->data[k][4+l]) { |
| 498 |
case 0: |
| 499 |
chArpeggio[j]=true; |
| 500 |
break; |
| 501 |
case 1: |
| 502 |
case 2: |
| 503 |
case 3: |
| 504 |
chPorta[j]=true; |
| 505 |
break; |
| 506 |
case 4: |
| 507 |
chVibrato[j]=true; |
| 508 |
break; |
| 509 |
case 0xA: |
| 510 |
chVolumeSlide[j]=true; |
| 511 |
break; |
| 512 |
default: |
| 513 |
break; |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if (lastPatSeen>1) { |
| 521 |
// clone the last pattern |
| 522 |
info.maxPat++; |
| 523 |
for (int i=0;i<6;i++) { |
| 524 |
int lastPatNum=info.ds->subsong[0]->orders.ord[i][info.ds->subsong[0]->ordersLen - 1]; |
| 525 |
DivPattern* newPat=info.ds->subsong[0]->pat[i].getPattern(info.maxPat,true); |
| 526 |
DivPattern* lastPat=info.ds->subsong[0]->pat[i].getPattern(lastPatNum, false); |
| 527 |
lastPat->copyOn(newPat); |
| 528 |
|
| 529 |
info.ds->subsong[0]->orders.ord[i][info.ds->subsong[0]->ordersLen - 1] = info.maxPat; |
| 530 |
newPat->data[info.patLens[lastPatNum]-1][4+(usedEffectsCol*4)] = 0x0B; |
| 531 |
newPat->data[info.patLens[lastPatNum]-1][5+(usedEffectsCol*4)] = info.loopPos; |
| 532 |
info.ds->subsong[0]->pat[i].data[info.maxPat] = newPat; |
| 533 |
} |
| 534 |
} else { |
| 535 |
for (int i=0;i<6;i++) { |
| 536 |
int lastPatNum=info.ds->subsong[0]->orders.ord[i][info.ds->subsong[0]->ordersLen - 1]; |
| 537 |
DivPattern* lastPat=info.ds->subsong[0]->pat[i].getPattern(lastPatNum, false); |
| 538 |
lastPat->data[info.patLens[lastPatNum]-1][4+(usedEffectsCol*4)] = 0x0B; |
| 539 |
lastPat->data[info.patLens[lastPatNum]-1][5+(usedEffectsCol*4)] = info.loopPos; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
bool DivEngine::loadTFMv1(unsigned char* file, size_t len) { |
| 545 |
// the documentation for this version is in russian only |
| 546 |
struct InvalidHeaderException {}; |
| 547 |
bool success=false; |
| 548 |
TFMRLEReader reader=TFMRLEReader(file,len); |
| 549 |
|
| 550 |
try { |
| 551 |
DivSong ds; |
| 552 |
ds.version=DIV_VERSION_TFE; |
| 553 |
ds.systemName="Sega Genesis/Mega Drive or TurboSound FM"; |
| 554 |
ds.subsong[0]->hz=50; |
| 555 |
ds.systemLen=1; |
| 556 |
|
| 557 |
ds.system[0]=DIV_SYSTEM_YM2612; |
| 558 |
ds.loopModality=1; |
| 559 |
|
| 560 |
unsigned char speed=reader.readCNoRLE(); |
| 561 |
unsigned char interleaveFactor=reader.readCNoRLE(); |
| 562 |
|
| 563 |
// TODO: due to limitations with the groove pattern, only interleave factors up to 8 |
| 564 |
// are allowed in furnace |
| 565 |
if (interleaveFactor>8) { |
| 566 |
logW("interleave factor is bigger than 8, speed information may be inaccurate"); |
| 567 |
interleaveFactor=8; |
| 568 |
} |
| 569 |
if ((speed>>4)==(speed&0xF)) { |
| 570 |
ds.subsong[0]->speeds.val[0]=speed&0xF; |
| 571 |
ds.subsong[0]->speeds.len=1; |
| 572 |
} else { |
| 573 |
for (int i=0; i<interleaveFactor; i++) { |
| 574 |
ds.subsong[0]->speeds.val[i]=speed>>4; |
| 575 |
ds.subsong[0]->speeds.val[i+interleaveFactor]=speed&0xF; |
| 576 |
} |
| 577 |
ds.subsong[0]->speeds.len=interleaveFactor*2; |
| 578 |
} |
| 579 |
ds.subsong[0]->ordersLen=reader.readCNoRLE(); |
| 580 |
|
| 581 |
// order loop position |
| 582 |
unsigned char loopPos = reader.readCNoRLE(); |
| 583 |
|
| 584 |
ds.createdDate=TFMparseDate(reader.readSNoRLE()); |
| 585 |
ds.revisionDate=TFMparseDate(reader.readSNoRLE()); |
| 586 |
|
| 587 |
// TODO: use this for something, number of saves |
| 588 |
(void)reader.readSNoRLE(); |
| 589 |
|
| 590 |
// author |
| 591 |
logD("parsing author"); |
| 592 |
ds.author=reader.readString(64); |
| 593 |
|
| 594 |
// name |
| 595 |
logD("parsing name"); |
| 596 |
ds.name=reader.readString(64); |
| 597 |
|
| 598 |
// notes |
| 599 |
logD("parsing notes"); |
| 600 |
String notes=reader.readString(384); |
| 601 |
|
| 602 |
// fix \r\n to \n |
| 603 |
for (auto& c : notes) { |
| 604 |
if (c=='\r') { |
| 605 |
notes.erase(c,1); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
// order list |
| 610 |
logD("parsing order list"); |
| 611 |
unsigned char orderList[256]; |
| 612 |
reader.read(orderList,256); |
| 613 |
|
| 614 |
bool patExists[256]; |
| 615 |
unsigned char maxPat=0; |
| 616 |
for (int i=0; i<ds.subsong[0]->ordersLen; i++) { |
| 617 |
patExists[orderList[i]]=true; |
| 618 |
if (maxPat<orderList[i]) maxPat=orderList[i]; |
| 619 |
|
| 620 |
for (int j=0; j<6; j++) { |
| 621 |
ds.subsong[0]->orders.ord[j][i]=orderList[i]; |
| 622 |
ds.subsong[0]->pat[j].data[orderList[i]]=new DivPattern; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
DivInstrument* insMaps[256]; |
| 627 |
int insNumMaps[256]; |
| 628 |
|
| 629 |
// instrument names |
| 630 |
logD("parsing instruments"); |
| 631 |
unsigned char insName[16]; |
| 632 |
int insCount=0; |
| 633 |
for (int i=0; i<255; i++) { |
| 634 |
reader.read(insName,16); |
| 635 |
|
| 636 |
if (memcmp(insName,"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",16)==0) { |
| 637 |
logD("instrument unused"); |
| 638 |
insNumMaps[i]=i; |
| 639 |
insMaps[i]=NULL; |
| 640 |
continue; |
| 641 |
} |
| 642 |
|
| 643 |
DivInstrument* ins=new DivInstrument; |
| 644 |
ins->type=DIV_INS_FM; |
| 645 |
ins->name=String((const char*)insName,strnlen((const char*)insName,16)); |
| 646 |
ds.ins.push_back(ins); |
| 647 |
|
| 648 |
insNumMaps[i]=insCount; |
| 649 |
insCount++; |
| 650 |
|
| 651 |
insMaps[i]=ins; |
| 652 |
} |
| 653 |
|
| 654 |
ds.insLen=insCount; |
| 655 |
|
| 656 |
// instrument data |
| 657 |
for (int i=0; i<255; i++) { |
| 658 |
if (!insMaps[i]) { |
| 659 |
reader.skip(42); |
| 660 |
continue; |
| 661 |
} |
| 662 |
|
| 663 |
insMaps[i]->fm.alg=reader.readC(); |
| 664 |
insMaps[i]->fm.fb=reader.readC(); |
| 665 |
|
| 666 |
for (int j=0; j<4; j++) { |
| 667 |
insMaps[i]->fm.op[j].mult=reader.readC(); |
| 668 |
insMaps[i]->fm.op[j].dt=reader.readC(); |
| 669 |
insMaps[i]->fm.op[j].tl=reader.readC()^0x7F; |
| 670 |
insMaps[i]->fm.op[j].rs=reader.readC(); |
| 671 |
insMaps[i]->fm.op[j].ar=reader.readC(); |
| 672 |
insMaps[i]->fm.op[j].dr=reader.readC(); |
| 673 |
insMaps[i]->fm.op[j].d2r=reader.readC(); |
| 674 |
insMaps[i]->fm.op[j].rr=reader.readC(); |
| 675 |
insMaps[i]->fm.op[j].sl=reader.readC(); |
| 676 |
insMaps[i]->fm.op[j].ssgEnv=reader.readC(); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
ds.notes=notes; |
| 681 |
|
| 682 |
unsigned char patLens[256]; |
| 683 |
int maxPatLen=0; |
| 684 |
reader.read(patLens, 256); |
| 685 |
for (int i=0; i<256; i++) { |
| 686 |
if (patLens[i]==0) { |
| 687 |
maxPatLen=256; |
| 688 |
break; |
| 689 |
} else if (patLens[i]>maxPatLen) { |
| 690 |
maxPatLen=patLens[i]; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
ds.subsong[0]->patLen=maxPatLen; |
| 695 |
|
| 696 |
struct TFMParsePatternInfo info; |
| 697 |
info.ds=&ds; |
| 698 |
info.insNumMaps=insNumMaps; |
| 699 |
info.maxPat=maxPat; |
| 700 |
info.patExists=patExists; |
| 701 |
info.orderList=orderList; |
| 702 |
info.speedEven=speed>>4; |
| 703 |
info.speedOdd=speed&0xF; |
| 704 |
info.interleaveFactor=interleaveFactor; |
| 705 |
info.patLens=patLens; |
| 706 |
info.reader=&reader; |
| 707 |
info.v2=false; |
| 708 |
info.loopPos=loopPos; |
| 709 |
TFMParsePattern(info); |
| 710 |
|
| 711 |
if (active) quitDispatch(); |
| 712 |
BUSY_BEGIN_SOFT; |
| 713 |
saveLock.lock(); |
| 714 |
song.unload(); |
| 715 |
song=ds; |
| 716 |
changeSong(0); |
| 717 |
recalcChans(); |
| 718 |
saveLock.unlock(); |
| 719 |
BUSY_END; |
| 720 |
if (active) { |
| 721 |
initDispatch(); |
| 722 |
BUSY_BEGIN; |
| 723 |
renderSamples(); |
| 724 |
reset(); |
| 725 |
BUSY_END; |
| 726 |
} |
| 727 |
success=true; |
| 728 |
} catch(TFMEndOfFileException& e) { |
| 729 |
lastError="incomplete file!"; |
| 730 |
} catch(InvalidHeaderException& e) { |
| 731 |
lastError="invalid info header!"; |
| 732 |
} |
| 733 |
|
| 734 |
delete[] file; |
| 735 |
return success; |
| 736 |
} |
| 737 |
|
| 738 |
bool DivEngine::loadTFMv2(unsigned char* file, size_t len) { |
| 739 |
struct InvalidHeaderException {}; |
| 740 |
bool success=false; |
| 741 |
TFMRLEReader reader=TFMRLEReader(file,len); |
| 742 |
|
| 743 |
try { |
| 744 |
DivSong ds; |
| 745 |
ds.version=DIV_VERSION_TFE; |
| 746 |
ds.systemName="Sega Genesis/Mega Drive or TurboSound FM"; |
| 747 |
ds.subsong[0]->hz=50; |
| 748 |
ds.systemLen=1; |
| 749 |
|
| 750 |
ds.system[0]=DIV_SYSTEM_YM2612; |
| 751 |
ds.loopModality=1; |
| 752 |
|
| 753 |
unsigned char magic[8]={0}; |
| 754 |
|
| 755 |
reader.readNoRLE(magic,8); |
| 756 |
if (memcmp(magic,DIV_TFM_MAGIC,8)!=0) throw InvalidHeaderException(); |
| 757 |
|
| 758 |
unsigned char speedEven=reader.readCNoRLE(); |
| 759 |
unsigned char speedOdd=reader.readCNoRLE(); |
| 760 |
unsigned char interleaveFactor=reader.readCNoRLE(); |
| 761 |
|
| 762 |
// TODO: due to limitations with the groove pattern, only interleave factors up to 8 |
| 763 |
// are allowed in furnace |
| 764 |
if (interleaveFactor>8) { |
| 765 |
addWarning("interleave factor is bigger than 8, speed information may be inaccurate"); |
| 766 |
interleaveFactor=8; |
| 767 |
} |
| 768 |
|
| 769 |
if (speedEven==speedOdd) { |
| 770 |
ds.subsong[0]->speeds.val[0]=speedEven; |
| 771 |
ds.subsong[0]->speeds.len=1; |
| 772 |
} else { |
| 773 |
for (int i=0; i<interleaveFactor; i++) { |
| 774 |
ds.subsong[0]->speeds.val[i]=speedEven; |
| 775 |
ds.subsong[0]->speeds.val[i+interleaveFactor]=speedOdd; |
| 776 |
} |
| 777 |
ds.subsong[0]->speeds.len=interleaveFactor*2; |
| 778 |
} |
| 779 |
ds.subsong[0]->ordersLen=reader.readCNoRLE(); |
| 780 |
|
| 781 |
// order loop position |
| 782 |
unsigned char loopPos = reader.readCNoRLE(); |
| 783 |
|
| 784 |
ds.createdDate=TFMparseDate(reader.readSNoRLE()); |
| 785 |
ds.revisionDate=TFMparseDate(reader.readSNoRLE()); |
| 786 |
|
| 787 |
// TODO: use this for something, number of saves |
| 788 |
(void)reader.readSNoRLE(); |
| 789 |
|
| 790 |
// author |
| 791 |
logD("parsing author"); |
| 792 |
ds.author=reader.readString(64); |
| 793 |
|
| 794 |
// name |
| 795 |
logD("parsing name"); |
| 796 |
ds.name=reader.readString(64); |
| 797 |
|
| 798 |
// notes |
| 799 |
logD("parsing notes"); |
| 800 |
String notes=reader.readString(384); |
| 801 |
|
| 802 |
// fix \r\n to \n |
| 803 |
for (auto& c : notes) { |
| 804 |
if (c=='\r') { |
| 805 |
notes.erase(c,1); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
// order list |
| 810 |
logD("parsing order list"); |
| 811 |
unsigned char orderList[256]; |
| 812 |
reader.read(orderList,256); |
| 813 |
|
| 814 |
bool patExists[256]; |
| 815 |
unsigned char maxPat=0; |
| 816 |
for (int i=0; i<ds.subsong[0]->ordersLen; i++) { |
| 817 |
patExists[orderList[i]]=true; |
| 818 |
if (maxPat<orderList[i]) maxPat=orderList[i]; |
| 819 |
|
| 820 |
for (int j=0; j<6; j++) { |
| 821 |
ds.subsong[0]->orders.ord[j][i]=orderList[i]; |
| 822 |
ds.subsong[0]->pat[j].data[orderList[i]]=new DivPattern; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
DivInstrument* insMaps[256]; |
| 827 |
int insNumMaps[256]; |
| 828 |
|
| 829 |
// instrument names |
| 830 |
logD("parsing instruments"); |
| 831 |
unsigned char insName[16]; |
| 832 |
int insCount=0; |
| 833 |
for (int i=0; i<255; i++) { |
| 834 |
reader.read(insName,16); |
| 835 |
|
| 836 |
if (memcmp(insName,"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",16)==0) { |
| 837 |
logD("instrument unused"); |
| 838 |
insNumMaps[i]=i; |
| 839 |
insMaps[i]=NULL; |
| 840 |
continue; |
| 841 |
} |
| 842 |
|
| 843 |
DivInstrument* ins=new DivInstrument; |
| 844 |
ins->type=DIV_INS_FM; |
| 845 |
ins->name=String((const char*)insName,strnlen((const char*)insName,16)); |
| 846 |
ds.ins.push_back(ins); |
| 847 |
|
| 848 |
insNumMaps[i]=insCount; |
| 849 |
insCount++; |
| 850 |
|
| 851 |
insMaps[i]=ins; |
| 852 |
} |
| 853 |
|
| 854 |
ds.insLen=insCount; |
| 855 |
|
| 856 |
// instrument data |
| 857 |
for (int i=0; i<255; i++) { |
| 858 |
if (!insMaps[i]) { |
| 859 |
reader.skip(42); |
| 860 |
continue; |
| 861 |
} |
| 862 |
|
| 863 |
insMaps[i]->fm.alg=reader.readC(); |
| 864 |
insMaps[i]->fm.fb=reader.readC(); |
| 865 |
|
| 866 |
for (int j=0; j<4; j++) { |
| 867 |
insMaps[i]->fm.op[j].mult=reader.readC(); |
| 868 |
insMaps[i]->fm.op[j].dt=reader.readC(); |
| 869 |
insMaps[i]->fm.op[j].tl=reader.readC()^0x7F; |
| 870 |
insMaps[i]->fm.op[j].rs=reader.readC(); |
| 871 |
insMaps[i]->fm.op[j].ar=reader.readC()^0x1F; |
| 872 |
insMaps[i]->fm.op[j].dr=reader.readC()^0x1F; |
| 873 |
insMaps[i]->fm.op[j].d2r=reader.readC()^0x1F; |
| 874 |
insMaps[i]->fm.op[j].rr=reader.readC()^0xF; |
| 875 |
insMaps[i]->fm.op[j].sl=reader.readC(); |
| 876 |
insMaps[i]->fm.op[j].ssgEnv=reader.readC(); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
ds.notes=notes; |
| 881 |
|
| 882 |
unsigned char patLens[256]; |
| 883 |
int maxPatLen=0; |
| 884 |
reader.read(patLens, 256); |
| 885 |
for (int i=0; i<256; i++) { |
| 886 |
if (patLens[i]==0) { |
| 887 |
maxPatLen=256; |
| 888 |
break; |
| 889 |
} else if (patLens[i]>maxPatLen) { |
| 890 |
maxPatLen=patLens[i]; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
ds.subsong[0]->patLen=maxPatLen; |
| 895 |
|
| 896 |
struct TFMParsePatternInfo info; |
| 897 |
info.ds=&ds; |
| 898 |
info.insNumMaps=insNumMaps; |
| 899 |
info.maxPat=maxPat; |
| 900 |
info.patExists=patExists; |
| 901 |
info.orderList=orderList; |
| 902 |
info.speedEven=speedEven; |
| 903 |
info.speedOdd=speedOdd; |
| 904 |
info.interleaveFactor=interleaveFactor; |
| 905 |
info.patLens=patLens; |
| 906 |
info.reader=&reader; |
| 907 |
info.v2=true; |
| 908 |
info.loopPos=loopPos; |
| 909 |
TFMParsePattern(info); |
| 910 |
|
| 911 |
if (active) quitDispatch(); |
| 912 |
BUSY_BEGIN_SOFT; |
| 913 |
saveLock.lock(); |
| 914 |
song.unload(); |
| 915 |
song=ds; |
| 916 |
changeSong(0); |
| 917 |
recalcChans(); |
| 918 |
saveLock.unlock(); |
| 919 |
BUSY_END; |
| 920 |
if (active) { |
| 921 |
initDispatch(); |
| 922 |
BUSY_BEGIN; |
| 923 |
renderSamples(); |
| 924 |
reset(); |
| 925 |
BUSY_END; |
| 926 |
} |
| 927 |
success=true; |
| 928 |
} catch(TFMEndOfFileException& e) { |
| 929 |
lastError="incomplete file!"; |
| 930 |
} catch(InvalidHeaderException& e) { |
| 931 |
lastError="invalid info header!"; |
| 932 |
} |
| 933 |
|
| 934 |
delete[] file; |
| 935 |
return success; |
| 936 |
} |