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 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
|
/*
* Load_ams.cpp
* ------------
* Purpose: AMS (Extreme's Tracker / Velvet Studio) module loader
* Notes : Extreme was renamed to Velvet Development at some point,
* and thus they also renamed their tracker from
* "Extreme's Tracker" to "Velvet Studio".
* While the two programs look rather similiar, the structure of both
* programs' "AMS" format is significantly different in some places -
* Velvet Studio is a rather advanced tracker in comparison to Extreme's Tracker.
* The source code of Velvet Studio has been released into the
* public domain in 2013: https://github.com/Patosc/VelvetStudio/commits/master
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Loaders.h"
OPENMPT_NAMESPACE_BEGIN
/////////////////////////////////////////////////////////////////////
// Common code for both AMS formats
// Read variable-length AMS string (we ignore the maximum text length specified by the AMS specs and accept any length).
template<size_t destSize>
static bool ReadAMSString(char (&destBuffer)[destSize], FileReader &file)
//-----------------------------------------------------------------------
{
const size_t length = file.ReadUint8();
return file.ReadString<mpt::String::spacePadded>(destBuffer, length);
}
// Read variable-length AMS string (we ignore the maximum text length specified by the AMS specs and accept any length).
static bool ReadAMSString(std::string &dest, FileReader &file)
//------------------------------------------------------------
{
const size_t length = file.ReadUint8();
return file.ReadString<mpt::String::spacePadded>(dest, length);
}
// Read AMS or AMS2 (newVersion = true) pattern. At least this part of the format is more or less identical between the two trackers...
static void ReadAMSPattern(CPattern &pattern, bool newVersion, FileReader &patternChunk)
//--------------------------------------------------------------------------------------
{
enum
{
emptyRow = 0xFF, // No commands on row
endOfRowMask = 0x80, // If set, no more commands on this row
noteMask = 0x40, // If set, no note+instr in this command
channelMask = 0x1F, // Mask for extracting channel
// Note flags
readNextCmd = 0x80, // One more command follows
noteDataMask = 0x7F, // Extract note
// Command flags
volCommand = 0x40, // Effect is compressed volume command
commandMask = 0x3F, // Command or volume mask
};
// Effect translation table for extended (non-Protracker) effects
static const ModCommand::COMMAND effTrans[] =
{
CMD_S3MCMDEX, // Forward / Backward
CMD_PORTAMENTOUP, // Extra fine slide up
CMD_PORTAMENTODOWN, // Extra fine slide up
CMD_RETRIG, // Retrigger
CMD_NONE,
CMD_TONEPORTAVOL, // Toneporta with fine volume slide
CMD_VIBRATOVOL, // Vibrato with fine volume slide
CMD_NONE,
CMD_PANNINGSLIDE,
CMD_NONE,
CMD_VOLUMESLIDE, // Two times finder volume slide than Axx
CMD_NONE,
CMD_CHANNELVOLUME, // Channel volume (0...127)
CMD_PATTERNBREAK, // Long pattern break (in hex)
CMD_S3MCMDEX, // Fine slide commands
CMD_NONE, // Fractional BPM
CMD_KEYOFF, // Key off at tick xx
CMD_PORTAMENTOUP, // Porta up, but uses all octaves (?)
CMD_PORTAMENTODOWN, // Porta down, but uses all octaves (?)
CMD_NONE,
CMD_NONE,
CMD_NONE,
CMD_NONE,
CMD_NONE,
CMD_NONE,
CMD_NONE,
CMD_GLOBALVOLSLIDE, // Global volume slide
CMD_NONE,
CMD_GLOBALVOLUME, // Global volume (0... 127)
};
ModCommand dummy = ModCommand::Empty();
for(ROWINDEX row = 0; row < pattern.GetNumRows(); row++)
{
PatternRow baseRow = pattern.GetRow(row);
while(patternChunk.CanRead(1))
{
const uint8 flags = patternChunk.ReadUint8();
if(flags == emptyRow)
{
break;
}
const CHANNELINDEX chn = (flags & channelMask);
ModCommand &m = chn < pattern.GetNumChannels() ? baseRow[chn] : dummy;
bool moreCommands = true;
if(!(flags & noteMask))
{
// Read note + instr
uint8 note = patternChunk.ReadUint8();
moreCommands = (note & readNextCmd) != 0;
note &= noteDataMask;
if(note == 1)
{
m.note = NOTE_KEYOFF;
} else if(note >= 2 && note <= 121 && newVersion)
{
m.note = note - 2 + NOTE_MIN;
} else if(note >= 12 && note <= 108 && !newVersion)
{
m.note = note + 12 + NOTE_MIN;
}
m.instr = patternChunk.ReadUint8();
}
while(moreCommands)
{
// Read one more effect command
ModCommand origCmd = m;
const uint8 command = patternChunk.ReadUint8(), effect = (command & commandMask);
moreCommands = (command & readNextCmd) != 0;
if(command & volCommand)
{
m.volcmd = VOLCMD_VOLUME;
m.vol = effect;
} else
{
m.param = patternChunk.ReadUint8();
if(effect < 0x10)
{
// PT commands
m.command = effect;
CSoundFile::ConvertModCommand(m);
// Post-fix some commands
switch(m.command)
{
case CMD_PANNING8:
// 4-Bit panning
m.command = CMD_PANNING8;
m.param = (m.param & 0x0F) * 0x11;
break;
case CMD_VOLUME:
m.command = CMD_NONE;
m.volcmd = VOLCMD_VOLUME;
m.vol = static_cast<ModCommand::VOL>(std::min((m.param + 1) / 2, 64));
break;
case CMD_MODCMDEX:
if(m.param == 0x80)
{
// Break sample loop (cut after loop)
m.command = CMD_NONE;
} else
{
m.ExtendedMODtoS3MEffect();
}
break;
}
} else if(effect - 0x10 < (int)CountOf(effTrans))
{
// Extended commands
m.command = effTrans[effect - 0x10];
// Post-fix some commands
switch(effect)
{
case 0x10:
// Play sample forwards / backwards
if(m.param <= 0x01)
{
m.param |= 0x9E;
} else
{
m.command = CMD_NONE;
}
break;
case 0x11:
case 0x12:
// Extra fine slides
m.param = static_cast<ModCommand::PARAM>(std::min(uint8(0x0F), m.param) | 0xE0);
break;
case 0x15:
case 0x16:
// Fine slides
m.param = static_cast<ModCommand::PARAM>((std::min(0x10, m.param + 1) / 2) | 0xF0);
break;
case 0x1E:
// More fine slides
switch(m.param >> 4)
{
case 0x1:
// Fine porta up
m.command = CMD_PORTAMENTOUP;
m.param |= 0xF0;
break;
case 0x2:
// Fine porta down
m.command = CMD_PORTAMENTODOWN;
m.param |= 0xF0;
break;
case 0xA:
// Extra fine volume slide up
m.command = CMD_VOLUMESLIDE;
m.param = ((((m.param & 0x0F) + 1) / 2) << 4) | 0x0F;
break;
case 0xB:
// Extra fine volume slide down
m.command = CMD_VOLUMESLIDE;
m.param = (((m.param & 0x0F) + 1) / 2) | 0xF0;
break;
default:
m.command = CMD_NONE;
break;
}
break;
case 0x1C:
// Adjust channel volume range
m.param = static_cast<ModCommand::PARAM>(std::min((m.param + 1) / 2, 64));
break;
}
}
// Try merging commands first
ModCommand::CombineEffects(m.command, m.param, origCmd.command, origCmd.param);
if(ModCommand::GetEffectWeight(origCmd.command) > ModCommand::GetEffectWeight(m.command))
{
if(m.volcmd == VOLCMD_NONE && ModCommand::ConvertVolEffect(m.command, m.param, true))
{
// Volume column to the rescue!
m.volcmd = m.command;
m.vol = m.param;
}
m.command = origCmd.command;
m.param = origCmd.param;
}
}
}
if(flags & endOfRowMask)
{
// End of row
break;
}
}
}
}
/////////////////////////////////////////////////////////////////////
// AMS (Extreme's Tracker) 1.x loader
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(push, 1)
#endif
// AMS File Header
struct PACKED AMSFileHeader
{
uint8 versionLow;
uint8 versionHigh;
uint8 channelConfig;
uint8 numSamps;
uint16 numPats;
uint16 numOrds;
uint8 midiChannels;
uint16 extraSize;
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(numPats);
SwapBytesLE(numOrds);
SwapBytesLE(extraSize);
}
};
STATIC_ASSERT(sizeof(AMSFileHeader) == 11);
// AMS Sample Header
struct PACKED AMSSampleHeader
{
enum SampleFlags
{
smp16BitOld = 0x04, // AMS 1.0 (at least according to docs, I yet have to find such a file)
smp16Bit = 0x80, // AMS 1.1+
smpPacked = 0x03,
};
uint32 length;
uint32 loopStart;
uint32 loopEnd;
uint8 panFinetune; // High nibble = pan position, low nibble = finetune value
uint16 sampleRate;
uint8 volume; // 0...127
uint8 flags; // See SampleFlags
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(length);
SwapBytesLE(loopStart);
SwapBytesLE(loopEnd);
SwapBytesLE(sampleRate);
}
// Convert sample header to OpenMPT's internal format.
void ConvertToMPT(ModSample &mptSmp) const
{
mptSmp.Initialize();
mptSmp.nLength = length;
mptSmp.nLoopStart = std::min(loopStart, length);
mptSmp.nLoopEnd = std::min(loopEnd, length);
mptSmp.nVolume = (std::min(uint8(127), volume) * 256 + 64) / 127;
if(panFinetune & 0xF0)
{
mptSmp.nPan = (panFinetune & 0xF0);
mptSmp.uFlags = CHN_PANNING;
}
mptSmp.nC5Speed = 2 * sampleRate;
if(sampleRate == 0)
{
mptSmp.nC5Speed = 2 * 8363;
}
uint32 newC4speed = ModSample::TransposeToFrequency(0, MOD2XMFineTune(panFinetune & 0x0F));
mptSmp.nC5Speed = (mptSmp.nC5Speed * newC4speed) / 8363;
if(mptSmp.nLoopStart < mptSmp.nLoopEnd)
{
mptSmp.uFlags.set(CHN_LOOP);
}
if((flags & smp16Bit) || (flags & smp16BitOld))
{
mptSmp.uFlags.set(CHN_16BIT);
}
}
};
STATIC_ASSERT(sizeof(AMSSampleHeader) == 17);
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(pop)
#endif
bool CSoundFile::ReadAMS(FileReader &file, ModLoadingFlags loadFlags)
//-------------------------------------------------------------------
{
file.Rewind();
AMSFileHeader fileHeader;
if(!file.ReadMagic("Extreme")
|| !file.ReadConvertEndianness(fileHeader)
|| !file.Skip(fileHeader.extraSize)
|| !file.CanRead(3u + fileHeader.numSamps * (1u + sizeof(AMSSampleHeader)) + fileHeader.numOrds * 2u + fileHeader.numPats * 4u)
|| fileHeader.versionHigh != 0x01)
{
return false;
} else if(loadFlags == onlyVerifyHeader)
{
return true;
}
InitializeGlobals(MOD_TYPE_AMS);
m_SongFlags = SONG_ITCOMPATGXX | SONG_ITOLDEFFECTS;
m_nChannels = (fileHeader.channelConfig & 0x1F) + 1;
m_nSamples = fileHeader.numSamps;
SetupMODPanning(true);
m_madeWithTracker = mpt::String::Print("Extreme's Tracker %1.%2", fileHeader.versionHigh, fileHeader.versionLow);
std::vector<bool> packSample(fileHeader.numSamps);
STATIC_ASSERT(MAX_SAMPLES > 255);
for(SAMPLEINDEX smp = 1; smp <= GetNumSamples(); smp++)
{
AMSSampleHeader sampleHeader;
file.ReadConvertEndianness(sampleHeader);
sampleHeader.ConvertToMPT(Samples[smp]);
packSample[smp - 1] = (sampleHeader.flags & AMSSampleHeader::smpPacked) != 0;
}
// Texts
ReadAMSString(m_songName, file);
// Read sample names
for(SAMPLEINDEX smp = 1; smp <= GetNumSamples(); smp++)
{
ReadAMSString(m_szNames[smp], file);
}
// Read channel names
for(CHANNELINDEX chn = 0; chn < GetNumChannels(); chn++)
{
ChnSettings[chn].Reset();
ReadAMSString(ChnSettings[chn].szName, file);
}
// Read pattern names
for(PATTERNINDEX pat = 0; pat < fileHeader.numPats; pat++)
{
char name[11];
ReadAMSString(name, file);
// Create pattern now, so name won't be reset later.
if(Patterns.Insert(pat, 64))
{
Patterns[pat].SetName(name);
}
}
// Read packed song message
const uint16 packedLength = file.ReadUint16LE();
if(packedLength && file.CanRead(packedLength))
{
std::vector<uint8> textIn;
file.ReadVector(textIn, packedLength);
std::string textOut;
textOut.reserve(packedLength);
for(std::vector<uint8>::iterator c = textIn.begin(); c != textIn.end(); c++)
{
if(*c & 0x80)
{
textOut.insert(textOut.end(), (*c & 0x7F), ' ');
} else
{
textOut.push_back(*c);
}
}
textOut = mpt::ToCharset(mpt::CharsetCP437, mpt::CharsetCP437AMS, textOut);
// Packed text doesn't include any line breaks!
m_songMessage.ReadFixedLineLength(mpt::byte_cast<const mpt::byte*>(textOut.c_str()), textOut.length(), 76, 0);
}
// Read Order List
std::vector<uint16> orders;
if(file.ReadVectorLE(orders, fileHeader.numOrds))
{
Order.resize(fileHeader.numOrds);
for(size_t i = 0; i < Order.size(); i++)
{
Order[i] = orders[i];
}
}
// Read patterns
for(PATTERNINDEX pat = 0; pat < fileHeader.numPats; pat++)
{
uint32 patLength = file.ReadUint32LE();
FileReader patternChunk = file.ReadChunk(patLength);
if(loadFlags & loadPatternData)
{
ReadAMSPattern(Patterns[pat], false, patternChunk);
}
}
if(loadFlags & loadSampleData)
{
// Read Samples
for(SAMPLEINDEX smp = 1; smp <= GetNumSamples(); smp++)
{
SampleIO(
Samples[smp].uFlags[CHN_16BIT] ? SampleIO::_16bit : SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
packSample[smp - 1] ? SampleIO::AMS : SampleIO::signedPCM)
.ReadSample(Samples[smp], file);
}
}
return true;
}
/////////////////////////////////////////////////////////////////////
// AMS (Velvet Studio) 2.0 - 2.02 loader
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(push, 1)
#endif
// AMS2 File Header
struct PACKED AMS2FileHeader
{
enum FileFlags
{
linearSlides = 0x40,
};
uint8 versionLow; // Version of format (Hi = MainVer, Low = SubVer e.g. 0202 = 2.02)
uint8 versionHigh; // ditto
uint8 numIns; // Nr of Instruments (0-255)
uint16 numPats; // Nr of Patterns (1-1024)
uint16 numOrds; // Nr of Positions (1-65535)
// Rest of header differs between format revision 2.01 and 2.02
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(numPats);
SwapBytesLE(numOrds);
};
};
STATIC_ASSERT(sizeof(AMS2FileHeader) == 7);
// AMS2 Instument Envelope
struct PACKED AMS2Envelope
{
uint8 speed; // Envelope speed (currently not supported, always the same as current BPM)
uint8 sustainPoint; // Envelope sustain point
uint8 loopStart; // Envelope loop Start
uint8 loopEnd; // Envelope loop End
uint8 numPoints; // Envelope length
// Read envelope and do partial conversion.
void ConvertToMPT(InstrumentEnvelope &mptEnv, FileReader &file)
{
file.ReadStruct(*this);
// Read envelope points
uint8 data[64][3];
file.ReadStructPartial(data, numPoints * 3);
if(numPoints <= 1)
{
// This is not an envelope.
return;
}
STATIC_ASSERT(MAX_ENVPOINTS >= CountOf(data));
mptEnv.resize(std::min(numPoints, uint8(CountOf(data))));
mptEnv.nLoopStart = loopStart;
mptEnv.nLoopEnd = loopEnd;
mptEnv.nSustainStart = mptEnv.nSustainEnd = sustainPoint;
for(uint32 i = 0; i < mptEnv.size(); i++)
{
if(i != 0)
{
mptEnv[i].tick = mptEnv[i - 1].tick + static_cast<uint16>(std::max(1, data[i][0] | ((data[i][1] & 0x01) << 8)));
}
mptEnv[i].value = data[i][2];
}
}
};
STATIC_ASSERT(sizeof(AMS2Envelope) == 5);
// AMS2 Instrument Data
struct PACKED AMS2Instrument
{
enum EnvelopeFlags
{
envLoop = 0x01,
envSustain = 0x02,
envEnabled = 0x04,
// Flag shift amounts
volEnvShift = 0,
panEnvShift = 1,
vibEnvShift = 2,
vibAmpMask = 0x3000,
vibAmpShift = 12,
fadeOutMask = 0xFFF,
};
uint8 shadowInstr; // Shadow Instrument. If non-zero, the value=the shadowed inst.
uint16 vibampFadeout; // Vib.Amplify + Volume fadeout in one variable!
uint16 envFlags; // See EnvelopeFlags
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(vibampFadeout);
SwapBytesLE(envFlags);
}
void ApplyFlags(InstrumentEnvelope &mptEnv, EnvelopeFlags shift) const
{
const int flags = envFlags >> (shift * 3);
mptEnv.dwFlags.set(ENV_ENABLED, (flags & envEnabled) != 0);
mptEnv.dwFlags.set(ENV_LOOP, (flags & envLoop) != 0);
mptEnv.dwFlags.set(ENV_SUSTAIN, (flags & envSustain) != 0);
// "Break envelope" should stop the envelope loop when encountering a note-off... We can only use the sustain loop to emulate this behaviour.
if(!(flags & envSustain) && (flags & envLoop) != 0 && (flags & (1 << (9 - shift * 2))) != 0)
{
mptEnv.nSustainStart = mptEnv.nLoopStart;
mptEnv.nSustainEnd = mptEnv.nLoopEnd;
mptEnv.dwFlags.set(ENV_SUSTAIN);
mptEnv.dwFlags.reset(ENV_LOOP);
}
}
};
STATIC_ASSERT(sizeof(AMS2Instrument) == 5);
// AMS2 Sample Header
struct PACKED AMS2SampleHeader
{
enum SampleFlags
{
smpPacked = 0x03,
smp16Bit = 0x04,
smpLoop = 0x08,
smpBidiLoop = 0x10,
smpReverse = 0x40,
};
uint32 length;
uint32 loopStart;
uint32 loopEnd;
uint16 sampledRate; // Whyyyy?
uint8 panFinetune; // High nibble = pan position, low nibble = finetune value
uint16 c4speed; // Why is all of this so redundant?
int8 relativeTone; // q.e.d.
uint8 volume; // 0...127
uint8 flags; // See SampleFlags
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(length);
SwapBytesLE(loopStart);
SwapBytesLE(loopEnd);
SwapBytesLE(sampledRate);
SwapBytesLE(c4speed);
}
// Convert sample header to OpenMPT's internal format.
void ConvertToMPT(ModSample &mptSmp) const
{
mptSmp.Initialize();
mptSmp.nLength = length;
mptSmp.nLoopStart = std::min(loopStart, length);
mptSmp.nLoopEnd = std::min(loopEnd, length);
mptSmp.nC5Speed = c4speed * 2;
if(c4speed == 0)
{
mptSmp.nC5Speed = 8363 * 2;
}
// Why, oh why, does this format need a c5speed and transpose/finetune at the same time...
uint32 newC4speed = ModSample::TransposeToFrequency(relativeTone, MOD2XMFineTune(panFinetune & 0x0F));
mptSmp.nC5Speed = (mptSmp.nC5Speed * newC4speed) / 8363;
mptSmp.nVolume = (std::min(uint8(127), volume) * 256 + 64) / 127;
if(panFinetune & 0xF0)
{
mptSmp.nPan = (panFinetune & 0xF0);
mptSmp.uFlags = CHN_PANNING;
}
if(flags & smp16Bit) mptSmp.uFlags.set(CHN_16BIT);
if((flags & smpLoop) && mptSmp.nLoopStart < mptSmp.nLoopEnd)
{
mptSmp.uFlags.set(CHN_LOOP);
if(flags & smpBidiLoop) mptSmp.uFlags.set(CHN_PINGPONGLOOP);
if(flags & smpReverse) mptSmp.uFlags.set(CHN_REVERSE);
}
}
};
STATIC_ASSERT(sizeof(AMS2SampleHeader) == 20);
// AMS2 Song Description Header
struct PACKED AMS2Description
{
uint32 packedLen; // Including header
uint32 unpackedLen;
uint8 packRoutine; // 01
uint8 preProcessing; // None!
uint8 packingMethod; // RLE
// Convert all multi-byte numeric values to current platform's endianness or vice versa.
void ConvertEndianness()
{
SwapBytesLE(packedLen);
SwapBytesLE(unpackedLen);
}
};
STATIC_ASSERT(sizeof(AMS2Description) == 11);
#ifdef NEEDS_PRAGMA_PACK
#pragma pack(pop)
#endif
bool CSoundFile::ReadAMS2(FileReader &file, ModLoadingFlags loadFlags)
//--------------------------------------------------------------------
{
file.Rewind();
AMS2FileHeader fileHeader;
if(!file.ReadMagic("AMShdr\x1A"))
{
return false;
}
InitializeGlobals(MOD_TYPE_AMS2);
if(!ReadAMSString(m_songName, file)
|| !file.ReadConvertEndianness(fileHeader)
|| fileHeader.versionHigh != 2 || fileHeader.versionLow > 2
|| !file.CanRead(36u + sizeof(AMS2Description) + fileHeader.numIns * 2u + fileHeader.numOrds * 2u + fileHeader.numPats * 4u))
{
return false;
} else if(loadFlags == onlyVerifyHeader)
{
return true;
}
m_nInstruments = fileHeader.numIns;
m_nChannels = 32;
SetupMODPanning(true);
m_madeWithTracker = mpt::String::Print("Velvet Studio %1.%2", fileHeader.versionHigh, mpt::fmt::dec0<2>(fileHeader.versionLow));
uint16 headerFlags;
if(fileHeader.versionLow >= 2)
{
uint16 tempo = std::max(uint16(32 << 8), file.ReadUint16LE()); // 8.8 tempo
m_nDefaultTempo.SetRaw((tempo * TEMPO::fractFact) >> 8);
m_nDefaultSpeed = std::max(uint8(1), file.ReadUint8());
file.Skip(3); // Default values for pattern editor
headerFlags = file.ReadUint16LE();
} else
{
m_nDefaultTempo.Set(std::max(uint8(32), file.ReadUint8()));
m_nDefaultSpeed = std::max(uint8(1), file.ReadUint8());
headerFlags = file.ReadUint8();
}
m_SongFlags = SONG_ITCOMPATGXX | SONG_ITOLDEFFECTS | ((headerFlags & AMS2FileHeader::linearSlides) ? SONG_LINEARSLIDES : SongFlags(0));
// Instruments
std::vector<SAMPLEINDEX> firstSample; // First sample of instrument
std::vector<uint16> sampleSettings; // Shadow sample map... Lo byte = Instrument, Hi byte, lo nibble = Sample index in instrument, Hi byte, hi nibble = Sample pack status
enum
{
instrIndexMask = 0xFF, // Shadow instrument
sampleIndexMask = 0x7F00, // Sample index in instrument
sampleIndexShift = 8,
packStatusMask = 0x8000, // If bit is set, sample is packed
};
STATIC_ASSERT(MAX_INSTRUMENTS > 255);
for(INSTRUMENTINDEX ins = 1; ins <= m_nInstruments; ins++)
{
ModInstrument *instrument = AllocateInstrument(ins);
if(instrument == nullptr
|| !ReadAMSString(instrument->name, file))
{
break;
}
uint8 numSamples = file.ReadUint8();
uint8 sampleAssignment[120];
MemsetZero(sampleAssignment); // Only really needed for v2.0, where the lowest and highest octave aren't cleared.
if(numSamples == 0
|| (fileHeader.versionLow > 0 && !file.ReadArray(sampleAssignment)) // v2.01+: 120 Notes
|| (fileHeader.versionLow == 0 && !file.ReadRaw(sampleAssignment + 12, 96))) // v2.0: 96 Notes
{
continue;
}
STATIC_ASSERT(CountOf(instrument->Keyboard) >= CountOf(sampleAssignment));
for(size_t i = 0; i < 120; i++)
{
instrument->Keyboard[i] = sampleAssignment[i] + GetNumSamples() + 1;
}
AMS2Envelope volEnv, panEnv, vibratoEnv;
volEnv.ConvertToMPT(instrument->VolEnv, file);
panEnv.ConvertToMPT(instrument->PanEnv, file);
vibratoEnv.ConvertToMPT(instrument->PitchEnv, file);
AMS2Instrument instrHeader;
file.ReadConvertEndianness(instrHeader);
instrument->nFadeOut = (instrHeader.vibampFadeout & AMS2Instrument::fadeOutMask);
const int16 vibAmp = 1 << ((instrHeader.vibampFadeout & AMS2Instrument::vibAmpMask) >> AMS2Instrument::vibAmpShift);
instrHeader.ApplyFlags(instrument->VolEnv, AMS2Instrument::volEnvShift);
instrHeader.ApplyFlags(instrument->PanEnv, AMS2Instrument::panEnvShift);
instrHeader.ApplyFlags(instrument->PitchEnv, AMS2Instrument::vibEnvShift);
// Scale envelopes to correct range
for(InstrumentEnvelope::iterator it = instrument->VolEnv.begin(); it != instrument->VolEnv.end(); it++)
{
it->value = std::min(uint8(ENVELOPE_MAX), static_cast<uint8>((it->value * ENVELOPE_MAX + 64u) / 127u));
}
for(InstrumentEnvelope::iterator it = instrument->PanEnv.begin(); it != instrument->PanEnv.end(); it++)
{
it->value = std::min(uint8(ENVELOPE_MAX), static_cast<uint8>((it->value * ENVELOPE_MAX + 128u) / 255u));
}
for(InstrumentEnvelope::iterator it = instrument->PitchEnv.begin(); it != instrument->PitchEnv.end(); it++)
{
#ifdef MODPLUG_TRACKER
it->value = std::min(uint8(ENVELOPE_MAX), static_cast<uint8>(32 + Util::muldivrfloor(static_cast<int8>(it->value - 128), vibAmp, 255)));
#else
// Try to keep as much precision as possible... divide by 8 since that's the highest possible vibAmp factor.
it->value = static_cast<uint8>(128 + Util::muldivrfloor(static_cast<int8>(it->value - 128), vibAmp, 8));
#endif
}
// Sample headers - we will have to read them even for shadow samples, and we will have to load them several times,
// as it is possible that shadow samples use different sample settings like base frequency or panning.
const SAMPLEINDEX firstSmp = GetNumSamples() + 1;
for(SAMPLEINDEX smp = 0; smp < numSamples; smp++)
{
if(firstSmp + smp >= MAX_SAMPLES)
{
file.Skip(sizeof(AMS2SampleHeader));
break;
}
ReadAMSString(m_szNames[firstSmp + smp], file);
AMS2SampleHeader sampleHeader;
file.ReadConvertEndianness(sampleHeader);
sampleHeader.ConvertToMPT(Samples[firstSmp + smp]);
uint16 settings = (instrHeader.shadowInstr & instrIndexMask)
| ((smp << sampleIndexShift) & sampleIndexMask)
| ((sampleHeader.flags & AMS2SampleHeader::smpPacked) ? packStatusMask : 0);
sampleSettings.push_back(settings);
}
firstSample.push_back(firstSmp);
m_nSamples = static_cast<SAMPLEINDEX>(std::min(MAX_SAMPLES - 1, GetNumSamples() + numSamples));
}
// Text
// Read composer name
uint8 composerLength = file.ReadUint8();
if(composerLength)
{
std::string str;
file.ReadString<mpt::String::spacePadded>(str, composerLength);
m_songArtist = mpt::ToUnicode(mpt::CharsetCP437AMS2, str);
}
// Channel names
for(CHANNELINDEX chn = 0; chn < 32; chn++)
{
ChnSettings[chn].Reset();
ReadAMSString(ChnSettings[chn].szName, file);
}
// RLE-Packed description text
AMS2Description descriptionHeader;
if(!file.ReadConvertEndianness(descriptionHeader))
{
return true;
}
if(descriptionHeader.packedLen > sizeof(descriptionHeader) && file.CanRead(descriptionHeader.packedLen - sizeof(descriptionHeader)))
{
const size_t textLength = descriptionHeader.packedLen - sizeof(descriptionHeader);
std::vector<uint8> textIn;
file.ReadVector(textIn, textLength);
std::string textOut;
textOut.reserve(descriptionHeader.unpackedLen);
size_t readLen = 0;
while(readLen < textLength)
{
uint8 c = textIn[readLen++];
if(c == 0xFF && textLength - readLen >= 2)
{
c = textIn[readLen++];
uint32 count = textIn[readLen++];
textOut.insert(textOut.end(), count, c);
} else
{
textOut.push_back(c);
}
}
textOut = mpt::ToCharset(mpt::CharsetCP437, mpt::CharsetCP437AMS2, textOut);
// Packed text doesn't include any line breaks!
m_songMessage.ReadFixedLineLength(mpt::byte_cast<const mpt::byte*>(textOut.c_str()), textOut.length(), 74, 0);
}
// Read Order List
std::vector<uint16> orders;
if(file.ReadVectorLE(orders, fileHeader.numOrds))
{
Order.resize(fileHeader.numOrds);
for(size_t i = 0; i < Order.size(); i++)
{
Order[i] = orders[i];
}
}
// Read Patterns
for(PATTERNINDEX pat = 0; pat < fileHeader.numPats; pat++)
{
uint32 patLength = file.ReadUint32LE();
FileReader patternChunk = file.ReadChunk(patLength);
if(loadFlags & loadPatternData)
{
const ROWINDEX numRows = patternChunk.ReadUint8() + 1;
// We don't need to know the number of channels or commands.
patternChunk.Skip(1);
if(!Patterns.Insert(pat, numRows))
{
continue;
}
char patternName[11];
ReadAMSString(patternName, patternChunk);
Patterns[pat].SetName(patternName);
ReadAMSPattern(Patterns[pat], true, patternChunk);
}
}
if(!(loadFlags & loadSampleData))
{
return true;
}
// Read Samples
for(SAMPLEINDEX smp = 0; smp < GetNumSamples(); smp++)
{
if((sampleSettings[smp] & instrIndexMask) == 0)
{
// Only load samples that aren't part of a shadow instrument
SampleIO(
(Samples[smp + 1].uFlags & CHN_16BIT) ? SampleIO::_16bit : SampleIO::_8bit,
SampleIO::mono,
SampleIO::littleEndian,
(sampleSettings[smp] & packStatusMask) ? SampleIO::AMS : SampleIO::signedPCM)
.ReadSample(Samples[smp + 1], file);
}
}
// Copy shadow samples
for(SAMPLEINDEX smp = 0; smp < GetNumSamples(); smp++)
{
INSTRUMENTINDEX sourceInstr = (sampleSettings[smp] & instrIndexMask);
if(sourceInstr == 0
|| --sourceInstr >= firstSample.size())
{
continue;
}
SAMPLEINDEX sourceSample = ((sampleSettings[smp] & sampleIndexMask) >> sampleIndexShift) + firstSample[sourceInstr];
if(sourceSample > GetNumSamples() || Samples[sourceSample].pSample == nullptr)
{
continue;
}
// Copy over original sample
ModSample &sample = Samples[smp + 1];
ModSample &source = Samples[sourceSample];
sample.uFlags.set(CHN_16BIT, source.uFlags[CHN_16BIT]);
sample.nLength = source.nLength;
if(sample.AllocateSample())
{
memcpy(sample.pSample, source.pSample, source.GetSampleSizeInBytes());
}
}
return true;
}
/////////////////////////////////////////////////////////////////////
// AMS Sample unpacking
void AMSUnpack(const int8 * const source, size_t sourceSize, void * const dest, const size_t destSize, char packCharacter)
//------------------------------------------------------------------------------------------------------------------------
{
std::vector<int8> tempBuf(destSize, 0);
size_t depackSize = destSize;
// Unpack Loop
{
const int8 *in = source;
int8 *out = &tempBuf[0];
size_t i = sourceSize, j = destSize;
while(i != 0 && j != 0)
{
int8 ch = *(in++);
if(--i != 0 && ch == packCharacter)
{
uint8 repCount = *(in++);
repCount = static_cast<uint8>(std::min(static_cast<size_t>(repCount), j));
if(--i != 0 && repCount)
{
ch = *(in++);
i--;
while(repCount-- != 0)
{
*(out++) = ch;
j--;
}
} else
{
*(out++) = packCharacter;
j--;
}
} else
{
*(out++) = ch;
j--;
}
}
// j should only be non-zero for truncated samples
depackSize -= j;
}
// Bit Unpack Loop
{
int8 *out = &tempBuf[0];
uint16 bitcount = 0x80;
size_t k = 0;
uint8 *dst = static_cast<uint8 *>(dest);
for(size_t i = 0; i < depackSize; i++)
{
uint8 al = *out++;
uint16 dh = 0;
for(uint16 count = 0; count < 8; count++)
{
uint16 bl = al & bitcount;
bl = ((bl | (bl << 8)) >> ((dh + 8 - count) & 7)) & 0xFF;
bitcount = ((bitcount | (bitcount << 8)) >> 1) & 0xFF;
dst[k++] |= bl;
if(k >= destSize)
{
k = 0;
dh++;
}
}
bitcount = ((bitcount | (bitcount << 8)) >> dh) & 0xFF;
}
}
// Delta Unpack
{
int8 old = 0;
int8 *out = static_cast<int8 *>(dest);
for(size_t i = depackSize; i != 0; i--)
{
int pos = *reinterpret_cast<uint8 *>(out);
if(pos != 128 && (pos & 0x80) != 0)
{
pos = -(pos & 0x7F);
}
old -= static_cast<int8>(pos);
*(out++) = old;
}
}
}
OPENMPT_NAMESPACE_END
|