1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
|
/*
* Load_mdl.cpp
* ------------
* Purpose: Digitrakker (MDL) module loader
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Loaders.h"
#include "ChunkReader.h"
#include <stdexcept>
OPENMPT_NAMESPACE_BEGIN
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(push, 1)
#endif
// MDL file header
struct PACKED MDLFileHeader
{
char id[4]; // "DMDL"
uint8 version;
};
STATIC_ASSERT(sizeof(MDLFileHeader) == 5);
// RIFF-style Chunk
struct PACKED MDLChunk
{
// 16-Bit chunk identifiers
enum ChunkIdentifiers
{
idInfo = MAGIC2LE('I','N'),
idMessage = MAGIC2LE('M','E'),
idPats = MAGIC2LE('P','A'),
idPatNames = MAGIC2LE('P','N'),
idTracks = MAGIC2LE('T','R'),
idInstrs = MAGIC2LE('I','I'),
idVolEnvs = MAGIC2LE('V','E'),
idPanEnvs = MAGIC2LE('P','E'),
idFreqEnvs = MAGIC2LE('F','E'),
idSampleInfo = MAGIC2LE('I','S'),
ifSampleData = MAGIC2LE('S','A'),
};
typedef ChunkIdentifiers id_type;
uint16 id;
uint32 length;
size_t GetLength() const
{
return SwapBytesReturnLE(length);
}
id_type GetID() const
{
return static_cast<id_type>(SwapBytesReturnLE(id));
}
};
STATIC_ASSERT(sizeof(MDLChunk) == 6);
struct PACKED MDLInfoBlock
{
char title[32];
char composer[20];
uint16 numOrders;
uint16 restartPos;
uint8 globalVol; // 1...255
uint8 speed; // 1...255
uint8 tempo; // 4...255
uint8 chnSetup[32];
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(numOrders);
SwapBytesLE(restartPos);
}
};
STATIC_ASSERT(sizeof(MDLInfoBlock) == 91);
// Sample header in II block
struct PACKED MDLSampleHeader
{
uint8 smpNum;
uint8 lastNote;
uint8 volume;
uint8 volEnvFlags; // 6 bits env #, 2 bits flags
uint8 panning;
uint8 panEnvFlags;
uint16 fadeout;
uint8 vibSpeed;
uint8 vibDepth;
uint8 vibSweep;
uint8 vibType;
uint8 reserved; // zero
uint8 freqEnvFlags;
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(fadeout);
}
};
STATIC_ASSERT(sizeof(MDLSampleHeader) == 14);
// Part of the sample header that's common between v0 and v1.
struct PACKED MDLSampleInfoCommon
{
uint8 sampleIndex;
char name[32];
char filename[8];
};
STATIC_ASSERT(sizeof(MDLSampleInfoCommon) == 41);
struct PACKED MDLSampleInfov0
{
enum SampleFlags
{
smp16Bit = 0x01,
smpPingPong = 0x02,
smpNoPack = 0x00,
smpPack8Bit = 0x04,
smpPack16Bit = 0x08,
smpPackMask = 0x0C,
};
uint16 c4speed;
uint32 length;
uint32 loopStart;
uint32 loopLength;
uint8 volume;
uint8 flags;
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(c4speed);
SwapBytesLE(length);
SwapBytesLE(loopStart);
SwapBytesLE(loopLength);
}
// Convert an MDL sample header to OpenMPT's internal sample header.
SampleIO ConvertToMPT(ModSample &mptSmp) const
{
mptSmp.nC5Speed = c4speed * 2;
mptSmp.nLength = length;
mptSmp.nVolume = volume;
mptSmp.nLoopStart = loopStart;
mptSmp.nLoopEnd = loopLength;
if(loopLength != 0)
{
mptSmp.nLoopEnd += mptSmp.nLoopStart;
mptSmp.uFlags.set(CHN_LOOP);
}
if(flags & smp16Bit)
{
mptSmp.uFlags.set(CHN_16BIT);
mptSmp.nLength /= 2;
mptSmp.nLoopStart /= 2;
mptSmp.nLoopEnd /= 2;
}
if(flags & smpPingPong)
{
mptSmp.uFlags.set(CHN_PINGPONGLOOP);
}
return SampleIO(
(flags & smp16Bit) ? SampleIO::_16bit : SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
(flags & smpPackMask) ? SampleIO::MDL : SampleIO::signedPCM);
}
};
STATIC_ASSERT(sizeof(MDLSampleInfov0) == 16);
struct PACKED MDLSampleInfo
{
enum SampleFlags
{
smp16Bit = 0x01,
smpPingPong = 0x02,
smpNoPack = 0x00,
smpPack8Bit = 0x04,
smpPack16Bit = 0x08,
smpPackMask = 0x0C,
};
uint32 c4speed;
uint32 length;
uint32 loopStart;
uint32 loopLength;
uint8 unused; // was volume in v0.0, why it was changed I have no idea
uint8 flags;
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(c4speed);
SwapBytesLE(length);
SwapBytesLE(loopStart);
SwapBytesLE(loopLength);
}
// Convert an MDL sample header to OpenMPT's internal sample header.
SampleIO ConvertToMPT(ModSample &mptSmp) const
{
mptSmp.nC5Speed = c4speed * 2;
mptSmp.nLength = length;
mptSmp.nLoopStart = loopStart;
mptSmp.nLoopEnd = loopLength;
if(loopLength != 0)
{
mptSmp.nLoopEnd += mptSmp.nLoopStart;
mptSmp.uFlags.set(CHN_LOOP);
}
if(flags & smp16Bit)
{
mptSmp.uFlags.set(CHN_16BIT);
mptSmp.nLength /= 2;
mptSmp.nLoopStart /= 2;
mptSmp.nLoopEnd /= 2;
}
if(flags & smpPingPong)
{
mptSmp.uFlags.set(CHN_PINGPONGLOOP);
}
return SampleIO(
(flags & smp16Bit) ? SampleIO::_16bit : SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
(flags & smpPackMask) ? SampleIO::MDL : SampleIO::signedPCM);
}
};
STATIC_ASSERT(sizeof(MDLSampleInfo) == 18);
struct PACKED MDLEnvelope
{
uint8 envNum;
struct
{
uint8 x; // Delta value from last point, 0 means no more points defined
uint8 y; // 0...63
} nodes[15];
uint8 flags;
uint8 loop; // Lower 4 bits = start, upper 4 bits = end
void ConvertToMPT(InstrumentEnvelope &mptEnv) const
{
mptEnv.dwFlags.reset();
mptEnv.clear();
mptEnv.reserve(15);
int16 tick = -nodes[0].x;
for(uint8 n = 0; n < 15; n++)
{
if(!nodes[n].x)
break;
tick += nodes[n].x;
mptEnv.push_back(EnvelopeNode(tick, std::min(nodes[n].y, uint8(64)))); // actually 0-63
}
mptEnv.nLoopStart = (loop & 0x0F);
mptEnv.nLoopEnd = (loop >> 4);
mptEnv.nSustainStart = mptEnv.nSustainEnd = (flags & 0x0F);
if(flags & 0x10) mptEnv.dwFlags.set(ENV_SUSTAIN);
if(flags & 0x20) mptEnv.dwFlags.set(ENV_LOOP);
}
};
STATIC_ASSERT(sizeof(MDLEnvelope) == 33);
struct PACKED MDLPatternHeader
{
uint8 channels;
uint8 lastRow;
char name[16];
};
STATIC_ASSERT(sizeof(MDLPatternHeader) == 18);
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(pop)
#endif
enum
{
MDLNOTE_NOTE = 1 << 0,
MDLNOTE_SAMPLE = 1 << 1,
MDLNOTE_VOLUME = 1 << 2,
MDLNOTE_EFFECTS = 1 << 3,
MDLNOTE_PARAM1 = 1 << 4,
MDLNOTE_PARAM2 = 1 << 5,
};
static const uint8 MDLVibratoType[] = { VIB_SINE, VIB_RAMP_DOWN, VIB_SQUARE, VIB_SINE };
static const ModCommand::COMMAND MDLEffTrans[] =
{
/* 0 */ CMD_NONE,
/* 1st column only */
/* 1 */ CMD_PORTAMENTOUP,
/* 2 */ CMD_PORTAMENTODOWN,
/* 3 */ CMD_TONEPORTAMENTO,
/* 4 */ CMD_VIBRATO,
/* 5 */ CMD_ARPEGGIO,
/* 6 */ CMD_NONE,
/* Either column */
/* 7 */ CMD_TEMPO,
/* 8 */ CMD_PANNING8,
/* 9 */ CMD_SETENVPOSITION,
/* A */ CMD_NONE,
/* B */ CMD_POSITIONJUMP,
/* C */ CMD_GLOBALVOLUME,
/* D */ CMD_PATTERNBREAK,
/* E */ CMD_S3MCMDEX,
/* F */ CMD_SPEED,
/* 2nd column only */
/* G */ CMD_VOLUMESLIDE, // up
/* H */ CMD_VOLUMESLIDE, // down
/* I */ CMD_RETRIG,
/* J */ CMD_TREMOLO,
/* K */ CMD_TREMOR,
/* L */ CMD_NONE,
};
// receive an MDL effect, give back a 'normal' one.
static void ConvertMDLCommand(uint8_t &cmd, uint8_t ¶m)
//---------------------------------------------------------
{
if(cmd >= CountOf(MDLEffTrans))
return;
uint8 origCmd = cmd;
cmd = MDLEffTrans[cmd];
switch(origCmd)
{
#ifdef MODPLUG_TRACKER
case 0x07: // Tempo
// MDL supports any nonzero tempo value, but OpenMPT doesn't
param = std::max(param, uint8(0x20));
break;
#endif // MODPLUG_TRACKER
case 0x08: // Panning
param = (param & 0x7F) * 2u;
break;
case 0x0C: // Global volume
param = (param + 1) / 2u;
break;
case 0x0D: // Pattern Break
// Convert from BCD
param = 10 * (param >> 4) + (param & 0x0F);
break;
case 0x0E: // Special
switch(param >> 4)
{
case 0x0: // unused
case 0x3: // unused
case 0x5: // Set Finetune
case 0x8: // Set Samplestatus (loop type)
cmd = CMD_NONE;
break;
case 0x1: // Pan Slide Left
cmd = CMD_PANNINGSLIDE;
param = (std::min<uint8>(param & 0x0F, 0x0E) << 4) | 0x0F;
break;
case 0x2: // Pan Slide Right
cmd = CMD_PANNINGSLIDE;
param = 0xF0 | std::min<uint8>(param & 0x0F, 0x0E);
break;
case 0x4: // Vibrato Waveform
param = 0x30 | (param & 0x0F);
break;
case 0x6: // Pattern Loop
param = 0xB0 | (param & 0x0F);
break;
case 0x7: // Tremolo Waveform
param = 0x40 | (param & 0x0F);
break;
case 0x9: // Retrig
cmd = CMD_RETRIG;
param &= 0x0F;
break;
case 0xA: // Global vol slide up
cmd = CMD_GLOBALVOLSLIDE;
param = 0xF0 & (((param & 0x0F) + 1) << 3);
break;
case 0xB: // Global vol slide down
cmd = CMD_GLOBALVOLSLIDE;
param = ((param & 0x0F) + 1) >> 1;
break;
case 0xC: // Note cut
case 0xD: // Note delay
case 0xE: // Pattern delay
// Nothing to change here
break;
case 0xF: // Offset -- further mangled later.
cmd = CMD_OFFSET;
break;
}
break;
case 0x10: // Volslide up
if(param < 0xE0)
{
// 00...DF regular slide - four times more precise than in XM
param >>= 2;
if(param > 0x0F)
param = 0x0F;
param <<= 4;
} else if(param < 0xF0)
{
// E0...EF extra fine slide (on first tick, 4 times finer)
param = (((param & 0x0F) << 2) | 0x0F);
} else
{
// F0...FF regular fine slide (on first tick) - like in XM
param = ((param << 4) | 0x0F);
}
break;
case 0x11: // Volslide down
if(param < 0xE0)
{
// 00...DF regular slide - four times more precise than in XM
param >>= 2;
if(param > 0x0F)
param = 0x0F;
} else if(param < 0xF0)
{
// E0...EF extra fine slide (on first tick, 4 times finer)
param = (((param & 0x0F) >> 2) | 0xF0);
} else
{
// F0...FF regular fine slide (on first tick) - like in XM
}
break;
}
}
// Returns true if command was lost
static bool ImportMDLCommands(ModCommand &m, uint8 vol, uint8 e1, uint8 e2, uint8 p1, uint8 p2)
//---------------------------------------------------------------------------------------------
{
// Map second effect values 1-6 to effects G-L
if(e2 >= 1 && e2 <= 6)
e2 += 15;
ConvertMDLCommand(e1, p1);
ConvertMDLCommand(e2, p2);
/* From the Digitrakker documentation:
* EFx -xx - Set Sample Offset
This is a double-command. It starts the
sample at adress xxx*256.
Example: C-5 01 -- EF1 -23 ->starts sample
01 at address 12300 (in hex).
Kind of screwy, but I guess it's better than the mess required to do it with IT (which effectively
requires 3 rows in order to set the offset past 0xff00). If we had access to the entire track, we
*might* be able to shove the high offset SAy into surrounding rows (or 2x MPTM #xx), but it wouldn't
always be possible, it'd make the loader a lot uglier, and generally would be more trouble than
it'd be worth to implement.
What's more is, if there's another effect in the second column, it's ALSO processed in addition to the
offset, and the second data byte is shared between the two effects. */
if(e1 == CMD_OFFSET)
{
// EFy -xx => offset yxx00
p1 = (p1 & 0x0F) ? 0xFF : p2;
if(e2 == CMD_OFFSET)
e2 = CMD_NONE;
} else if (e2 == CMD_OFFSET)
{
// --- EFy => offset y0000 (best we can do without doing a ton of extra work is 0xff00)
p2 = (p2 & 0x0F) ? 0xFF : 0;
}
if(vol)
{
m.volcmd = VOLCMD_VOLUME;
m.vol = (vol + 2) / 4u;
}
// If we have Dxx + G00, or Dxx + H00, combine them into Lxx/Kxx.
ModCommand::CombineEffects(e1, p1, e2, p2);
bool lostCommand = false;
// Try to fit the "best" effect into e2.
if(e1 == CMD_NONE)
{
// Easy
} else if(e2 == CMD_NONE)
{
// Almost as easy
e2 = e1;
p2 = p1;
e1 = CMD_NONE;
} else if(e1 == e2 && e1 != CMD_S3MCMDEX)
{
// Digitrakker processes the effects left-to-right, so if both effects are the same, the
// second essentially overrides the first.
e1 = CMD_NONE;
} else if(!vol)
{
lostCommand |= !ModCommand::TwoRegularCommandsToMPT(e1, p1, e2, p2);
m.volcmd = e1;
m.vol = p1;
} else
{
if(ModCommand::GetEffectWeight((ModCommand::COMMAND)e1) > ModCommand::GetEffectWeight((ModCommand::COMMAND)e2))
{
std::swap(e1, e2);
std::swap(p1, p2);
}
}
m.command = e2;
m.param = p2;
return lostCommand;
}
static void MDLReadEnvelopes(FileReader file, std::vector<MDLEnvelope> &envelopes)
//--------------------------------------------------------------------------------
{
if(!file.CanRead(1))
return;
envelopes.resize(64);
uint8 numEnvs = file.ReadUint8();
while(numEnvs--)
{
MDLEnvelope mdlEnv;
if(!file.ReadStruct(mdlEnv) || mdlEnv.envNum > 63)
continue;
envelopes[mdlEnv.envNum] = mdlEnv;
}
}
static void CopyEnvelope(InstrumentEnvelope &mptEnv, uint8 flags, std::vector<MDLEnvelope> &envelopes)
//----------------------------------------------------------------------------------------------------
{
uint8 envNum = flags & 0x3F;
if(envNum < envelopes.size())
envelopes[envNum].ConvertToMPT(mptEnv);
mptEnv.dwFlags.set(ENV_ENABLED, (flags & 0x80) && !mptEnv.empty());
}
bool CSoundFile::ReadMDL(FileReader &file, ModLoadingFlags loadFlags)
//-------------------------------------------------------------------
{
file.Rewind();
MDLFileHeader fileHeader;
if(!file.ReadStruct(fileHeader)
|| memcmp(fileHeader.id, "DMDL", 4)
|| fileHeader.version >= 0x20)
{
return false;
} else if(loadFlags == onlyVerifyHeader)
{
return true;
}
ChunkReader chunkFile(file);
ChunkReader::ChunkList<MDLChunk> chunks = chunkFile.ReadChunks<MDLChunk>(0);
// Read global info
FileReader chunk = chunks.GetChunk(MDLChunk::idInfo);
MDLInfoBlock info;
if(!chunk.IsValid() || !chunk.ReadConvertEndianness(info))
{
return false;
}
InitializeGlobals(MOD_TYPE_MDL);
m_SongFlags = SONG_ITCOMPATGXX;
m_playBehaviour.set(kPerChannelGlobalVolSlide);
m_playBehaviour.reset(kITVibratoTremoloPanbrello);
m_playBehaviour.reset(kITSCxStopsSample); // Gate effect in underbeat.mdl
m_madeWithTracker = std::string("Digitrakker ") + (
(fileHeader.version == 0x11) ? "3" // really could be 2.99b - close enough
: (fileHeader.version == 0x10) ? "2.3"
: (fileHeader.version == 0x00) ? "2.0 - 2.2b" // there was no 1.x release
: "");
mpt::String::Read<mpt::String::spacePadded>(m_songName, info.title);
{
std::string artist;
mpt::String::Read<mpt::String::spacePadded>(artist, info.composer);
m_songArtist = mpt::ToUnicode(mpt::CharsetCP437, artist);
}
m_nDefaultGlobalVolume = info.globalVol + 1;
m_nDefaultSpeed = Clamp(info.speed, uint8(1), uint8(255));
m_nDefaultTempo.Set(Clamp(info.tempo, uint8(4), uint8(255)));
Order.ReadAsByte(chunk, info.numOrders);
Order.SetRestartPos(info.restartPos);
m_nChannels = 0;
for(CHANNELINDEX c = 0; c < 32; c++)
{
ChnSettings[c].Reset();
ChnSettings[c].nPan = (info.chnSetup[c] & 0x7F) * 2u;
if(ChnSettings[c].nPan == 254)
ChnSettings[c].nPan = 256;
if(info.chnSetup[c] & 0x80)
ChnSettings[c].dwFlags.set(CHN_MUTE);
else
m_nChannels = c + 1;
chunk.ReadString<mpt::String::spacePadded>(ChnSettings[c].szName, 8);
}
// Read song message
chunk = chunks.GetChunk(MDLChunk::idMessage);
m_songMessage.Read(chunk, chunk.GetLength(), SongMessage::leCR);
// Read sample info and data
chunk = chunks.GetChunk(MDLChunk::idSampleInfo);
if(chunk.IsValid())
{
FileReader dataChunk = chunks.GetChunk(MDLChunk::ifSampleData);
uint8 numSamples = chunk.ReadUint8();
for(uint8 smp = 0; smp < numSamples; smp++)
{
MDLSampleInfoCommon header;
if(!chunk.ReadStruct(header) || header.sampleIndex == 0)
continue;
#if 1
STATIC_ASSERT(MPT_MAX_UNSIGNED_VALUE(header.sampleIndex) < MAX_SAMPLES);
#else
MPT_MAYBE_CONSTANT_IF(header.sampleIndex >= MAX_SAMPLES)
continue;
#endif
if(header.sampleIndex > GetNumSamples())
m_nSamples = header.sampleIndex;
ModSample &sample = Samples[header.sampleIndex];
sample.Initialize();
mpt::String::Read<mpt::String::spacePadded>(m_szNames[header.sampleIndex], header.name);
mpt::String::Read<mpt::String::spacePadded>(sample.filename, header.filename);
SampleIO sampleIO;
if(fileHeader.version >= 0x10)
{
MDLSampleInfo sampleHeader;
chunk.ReadConvertEndianness(sampleHeader);
sampleIO = sampleHeader.ConvertToMPT(sample);
} else
{
MDLSampleInfov0 sampleHeader;
chunk.ReadConvertEndianness(sampleHeader);
sampleIO = sampleHeader.ConvertToMPT(sample);
}
if((loadFlags & loadSampleData) && (sample.nLength || sampleIO.GetEncoding() == SampleIO::MDL))
{
sampleIO.ReadSample(sample, dataChunk);
}
}
}
chunk = chunks.GetChunk(MDLChunk::idInstrs);
if(chunk.IsValid())
{
std::vector<MDLEnvelope> volEnvs, panEnvs, pitchEnvs;
MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idVolEnvs), volEnvs);
MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idPanEnvs), panEnvs);
MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idFreqEnvs), pitchEnvs);
uint8 numInstruments = chunk.ReadUint8();
for(uint8 i = 0; i < numInstruments; i++)
{
uint8 ins = chunk.ReadUint8();
uint8 numSamples = chunk.ReadUint8();
uint8 firstNote = 0;
ModInstrument *mptIns = nullptr;
if(ins == 0
|| !chunk.CanRead(32 + sizeof(MDLSampleHeader) * numSamples)
|| (mptIns = AllocateInstrument(ins)) == nullptr)
{
chunk.Skip(32 + sizeof(MDLSampleHeader) * numSamples);
continue;
}
m_nInstruments = std::max<INSTRUMENTINDEX>(m_nInstruments, ins);
chunk.ReadString<mpt::String::spacePadded>(mptIns->name, 32);
while(numSamples--)
{
MDLSampleHeader sampleHeader;
chunk.ReadConvertEndianness(sampleHeader);
if(sampleHeader.smpNum == 0)
continue;
#if 1
STATIC_ASSERT(MPT_MAX_UNSIGNED_VALUE(sampleHeader.smpNum) < MAX_SAMPLES);
#else
MPT_MAYBE_CONSTANT_IF(sampleHeader.smpNum >= MAX_SAMPLES)
continue;
#endif
LimitMax(sampleHeader.lastNote, static_cast<uint8>(CountOf(mptIns->Keyboard)));
for(uint8 n = firstNote; n <= sampleHeader.lastNote; n++)
{
mptIns->Keyboard[n] = sampleHeader.smpNum;
}
firstNote = sampleHeader.lastNote + 1;
CopyEnvelope(mptIns->VolEnv, sampleHeader.volEnvFlags, volEnvs);
CopyEnvelope(mptIns->PanEnv, sampleHeader.panEnvFlags, panEnvs);
CopyEnvelope(mptIns->PitchEnv, sampleHeader.freqEnvFlags, pitchEnvs);
mptIns->nFadeOut = (sampleHeader.fadeout + 1u) / 2u;
#ifdef MODPLUG_TRACKER
if((mptIns->VolEnv.dwFlags & (ENV_ENABLED | ENV_LOOP)) == ENV_ENABLED)
{
// Fade-out is only supposed to happen on key-off, not at the end of a volume envelope.
// Fake it by putting a loop at the end.
mptIns->VolEnv.nLoopStart = mptIns->VolEnv.nLoopEnd = static_cast<uint8>(mptIns->VolEnv.size() - 1);
mptIns->VolEnv.dwFlags.set(ENV_LOOP);
}
for(InstrumentEnvelope::iterator it = mptIns->PitchEnv.begin(); it != mptIns->PitchEnv.end(); it++)
{
// Scale pitch envelope
it->value = (it->value * 6u) / 16u;
}
#endif // MODPLUG_TRACKER
// Samples were already initialized above. Let's hope they are not going to be re-used with different volume / panning / vibrato...
ModSample &mptSmp = Samples[sampleHeader.smpNum];
// Not quite correct - this flag literally enables and disables the default volume of a sample. If you disable this flag,
// the sample volume of a previously sample is re-used, even if you put an instrument number next to the note.
if(sampleHeader.volEnvFlags & 0x40)
mptSmp.nVolume = sampleHeader.volume;
mptSmp.nPan = std::min<uint16>(sampleHeader.panning * 2, 254);
mptSmp.nVibType = MDLVibratoType[sampleHeader.vibType & 3];
mptSmp.nVibSweep = sampleHeader.vibSweep;
mptSmp.nVibDepth = sampleHeader.vibDepth;
mptSmp.nVibRate = sampleHeader.vibSpeed;
if(sampleHeader.panEnvFlags & 0x40)
mptSmp.uFlags.set(CHN_PANNING);
}
}
}
// Read pattern tracks
std::vector<FileReader> tracks;
if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idTracks)).IsValid())
{
uint32 numTracks = chunk.ReadUint16LE();
tracks.resize(numTracks + 1);
for(uint32 i = 1; i <= numTracks; i++)
{
tracks[i] = chunk.ReadChunk(chunk.ReadUint16LE());
}
}
// Read actual patterns
if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idPats)).IsValid())
{
PATTERNINDEX numPats = chunk.ReadUint8();
// In case any muted channels contain data, be sure that we import them as well.
for(PATTERNINDEX pat = 0; pat < numPats; pat++)
{
CHANNELINDEX numChans = 32;
if(fileHeader.version >= 0x10)
{
MDLPatternHeader patHead;
chunk.ReadStruct(patHead);
if(patHead.channels > m_nChannels && patHead.channels <= 32)
m_nChannels = patHead.channels;
numChans = patHead.channels;
}
for(CHANNELINDEX chn = 0; chn < numChans; chn++)
{
if(chunk.ReadUint16LE() > 0 && chn >= m_nChannels)
m_nChannels = chn + 1;
}
}
chunk.Seek(1);
for(PATTERNINDEX pat = 0; pat < numPats; pat++)
{
CHANNELINDEX numChans = 32;
ROWINDEX numRows = 64;
char name[17] = "";
if(fileHeader.version >= 0x10)
{
MDLPatternHeader patHead;
chunk.ReadStruct(patHead);
numChans = patHead.channels;
numRows = patHead.lastRow + 1;
mpt::String::Read<mpt::String::spacePadded>(name, patHead.name);
}
if(!Patterns.Insert(pat, numRows))
{
chunk.Skip(2 * numChans);
continue;
}
Patterns[pat].SetName(name);
for(CHANNELINDEX chn = 0; chn < numChans; chn++)
{
uint16 trkNum = chunk.ReadUint16LE();
if(!trkNum || trkNum >= tracks.size() || chn >= m_nChannels)
continue;
FileReader &track = tracks[trkNum];
track.Rewind();
ROWINDEX row = 0;
while(row < numRows && track.CanRead(1))
{
ModCommand *m = Patterns[pat].GetpModCommand(row, chn);
uint8 b = track.ReadUint8();
uint8 x = (b >> 2), y = (b & 3);
switch(y)
{
case 0:
// (x + 1) empty notes follow
row += x + 1;
break;
case 1:
// Repeat previous note (x + 1) times
if(row > 0)
{
ModCommand &orig = *Patterns[pat].GetpModCommand(row - 1, chn);
do
{
*m = orig;
m += m_nChannels;
row++;
} while (row < numRows && x--);
}
break;
case 2:
// Copy note from row x
if(row > x)
{
*m = *Patterns[pat].GetpModCommand(x, chn);
}
row++;
break;
case 3:
// New note data
if(x & MDLNOTE_NOTE)
{
b = track.ReadUint8();
m->note = (b > 120) ? NOTE_KEYOFF : b;
}
if(x & MDLNOTE_SAMPLE)
{
m->instr = track.ReadUint8();
}
{
uint8 vol = 0, e1 = 0, e2 = 0, p1 = 0, p2 = 0;
if(x & MDLNOTE_VOLUME)
{
vol = track.ReadUint8();
}
if(x & MDLNOTE_EFFECTS)
{
b = track.ReadUint8();
e1 = (b & 0x0F);
e2 = (b >> 4);
}
if(x & MDLNOTE_PARAM1)
p1 = track.ReadUint8();
if(x & MDLNOTE_PARAM2)
p2 = track.ReadUint8();
ImportMDLCommands(*m, vol, e1, e2, p1, p2);
}
row++;
break;
}
}
}
}
}
if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idPatNames)).IsValid())
{
PATTERNINDEX i = 0;
while(i < Patterns.Size() && chunk.CanRead(16))
{
char name[17];
chunk.ReadString<mpt::String::spacePadded>(name, 16);
Patterns[i].SetName(name);
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// MDL Sample Unpacking
// MDL Huffman ReadBits compression
uint8 MDLReadBits(uint32 &bitbuf, int32 &bitnum, const uint8 *(&ibuf), size_t &bytesLeft, int8 n)
//-----------------------------------------------------------------------------------------------
{
if(bitnum < n)
{
if(bytesLeft)
{
bitbuf |= (((uint32)(*ibuf++)) << bitnum);
bitnum += 8;
bytesLeft--;
} else
{
throw std::range_error("Truncated MDL sample block");
}
}
uint8 v = static_cast<uint8>(bitbuf & ((1 << n) - 1));
bitbuf >>= n;
bitnum -= n;
return v;
}
OPENMPT_NAMESPACE_END
|