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
|
/*
* SampleIO.cpp
* ------------
* Purpose: Central code for reading and writing samples. Create your SampleIO object and have a go at the ReadSample and WriteSample functions!
* Notes : Not all combinations of possible sample format combinations are implemented, especially for WriteSample.
* Using the existing generic functions, it should be quite easy to extend the code, though.
* Authors: Olivier Lapicque
* OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "SampleIO.h"
#include "BitReader.h"
#include "ITCompression.h"
#include "Loaders.h"
#include "ModSampleCopy.h"
#include "SampleCopy.h"
#include "SampleNormalize.h"
#ifndef MODPLUG_NO_FILESAVE
#include "../common/mptFileIO.h"
#include "mpt/io/base.hpp"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
#include "mpt/io_write/buffer.hpp"
#endif
#include "openmpt/soundbase/SampleDecode.hpp"
OPENMPT_NAMESPACE_BEGIN
// Read a sample from memory
size_t SampleIO::ReadSample(ModSample &sample, FileReader &file) const
{
if(!file.IsValid())
{
return 0;
}
LimitMax(sample.nLength, MAX_SAMPLE_LENGTH);
FileReader::pos_type bytesRead = 0; // Amount of memory that has been read from file
FileReader::pos_type filePosition = file.GetPosition();
const std::byte * sourceBuf = nullptr;
FileReader::PinnedView restrictedSampleDataView;
FileReader::pos_type fileSize = 0;
if(UsesFileReaderForDecoding())
{
sourceBuf = nullptr;
fileSize = file.BytesLeft();
} else if(!IsVariableLengthEncoded())
{
restrictedSampleDataView = file.GetPinnedView(CalculateEncodedSize(sample.nLength));
sourceBuf = restrictedSampleDataView.data();
fileSize = restrictedSampleDataView.size();
if(fileSize < 1)
return 0;
} else
{
MPT_ASSERT_NOTREACHED();
}
if(!IsVariableLengthEncoded() && sample.nLength > 0x40000)
{
// Limit sample length to available bytes in file to avoid excessive memory allocation.
// However, for ProTracker MODs we need to support samples exceeding the end of file
// (see the comment about MOD.shorttune2 in Load_mod.cpp), so as a semi-arbitrary threshold,
// we do not apply this limit to samples shorter than 256K.
std::size_t maxLength = static_cast<std::size_t>(fileSize) - std::min(GetEncodedHeaderSize(), static_cast<std::size_t>(fileSize));
uint8 bps = GetEncodedBitsPerSample();
if(bps % 8u != 0)
{
MPT_ASSERT(GetEncoding() == ADPCM && bps == 4);
if(Util::MaxValueOfType(maxLength) / 2u >= maxLength)
maxLength *= 2;
else
maxLength = Util::MaxValueOfType(maxLength);
} else
{
size_t encodedBytesPerSample = GetNumChannels() * GetEncodedBitsPerSample() / 8u;
// Check if we can round up without overflowing
if(Util::MaxValueOfType(maxLength) - maxLength >= (encodedBytesPerSample - 1u))
maxLength += encodedBytesPerSample - 1u;
else
maxLength = Util::MaxValueOfType(maxLength);
maxLength /= encodedBytesPerSample;
}
LimitMax(sample.nLength, mpt::saturate_cast<SmpLength>(maxLength));
} else if(GetEncoding() == IT214 || GetEncoding() == IT215 || GetEncoding() == MDL || GetEncoding() == DMF)
{
// In the best case, IT compression represents each sample point as a single bit.
// In practice, there is of course the two-byte header per compressed block and the initial bit width change.
// As a result, if we have a file length of n, we know that the sample can be at most n*8 sample points long.
// For DMF, there are at least two bits per sample, and for MDL at least 5 (so both are worse than IT).
size_t maxLength = fileSize;
uint8 maxSamplesPerByte = 8 / GetNumChannels();
if(Util::MaxValueOfType(maxLength) / maxSamplesPerByte >= maxLength)
maxLength *= maxSamplesPerByte;
else
maxLength = Util::MaxValueOfType(maxLength);
LimitMax(sample.nLength, mpt::saturate_cast<SmpLength>(maxLength));
} else if(GetEncoding() == AMS)
{
if(fileSize <= 9)
return 0;
file.Skip(4); // Target sample size (we already know this)
SmpLength maxLength = std::min(file.ReadUint32LE(), mpt::saturate_cast<uint32>(fileSize));
file.SkipBack(8);
// In the best case, every byte triplet can decode to 255 bytes, which is a ratio of exactly 1:85
if(Util::MaxValueOfType(maxLength) / 85 >= maxLength)
maxLength *= 85;
else
maxLength = Util::MaxValueOfType(maxLength);
LimitMax(sample.nLength, maxLength / (m_bitdepth / 8u));
}
if(sample.nLength < 1)
{
return 0;
}
sample.uFlags.set(CHN_16BIT, GetBitDepth() >= 16);
sample.uFlags.set(CHN_STEREO, GetChannelFormat() != mono);
size_t sampleSize = sample.AllocateSample(); // Target sample size in bytes
if(sampleSize == 0)
{
sample.nLength = 0;
return 0;
}
MPT_ASSERT(sampleSize >= sample.GetSampleSizeInBytes());
//////////////////////////////////////////////////////
// Compressed samples
if(*this == SampleIO(_8bit, mono, littleEndian, ADPCM))
{
// 4-Bit ADPCM data
int8 compressionTable[16]; // ADPCM Compression LUT
if(file.ReadArray(compressionTable))
{
size_t readLength = (sample.nLength + 1) / 2;
LimitMax(readLength, mpt::saturate_cast<std::size_t>(file.BytesLeft()));
const uint8 *inBuf = mpt::byte_cast<const uint8*>(sourceBuf) + sizeof(compressionTable);
int8 *outBuf = sample.sample8();
int8 delta = 0;
for(size_t i = readLength; i != 0; i--)
{
delta += compressionTable[*inBuf & 0x0F];
*(outBuf++) = delta;
delta += compressionTable[(*inBuf >> 4) & 0x0F];
*(outBuf++) = delta;
inBuf++;
}
bytesRead = sizeof(compressionTable) + readLength;
}
} else if(GetEncoding() == IT214 || GetEncoding() == IT215)
{
// IT 2.14 / 2.15 compressed samples
ITDecompression(file, sample, GetEncoding() == IT215);
bytesRead = file.GetPosition() - filePosition;
} else if(GetEncoding() == AMS && GetChannelFormat() == mono)
{
// AMS compressed samples
file.Skip(4); // Target sample size (we already know this)
uint32 sourceSize = file.ReadUint32LE();
int8 packCharacter = file.ReadUint8();
bytesRead += 9;
FileReader::PinnedView packedDataView = file.ReadPinnedView(sourceSize);
LimitMax(sourceSize, mpt::saturate_cast<uint32>(packedDataView.size()));
bytesRead += sourceSize;
AMSUnpack(packedDataView.span(), mpt::as_span(sample.sampleb(), sample.GetSampleSizeInBytes()), packCharacter);
if(sample.uFlags[CHN_16BIT] && !mpt::endian_is_little())
{
auto p = sample.sample16();
for(SmpLength length = sample.nLength; length != 0; length--, p++)
{
*p = mpt::bit_cast<int16le>(*p);
}
}
} else if(GetEncoding() == PTM8Dto16 && GetChannelFormat() == mono && GetBitDepth() == 16)
{
// PTM 8-Bit delta to 16-Bit sample
bytesRead = CopyMonoSample<SC::DecodeInt16Delta8>(sample, sourceBuf, fileSize);
} else if(GetEncoding() == MDL && GetChannelFormat() == mono && GetBitDepth() <= 16)
{
// Huffman MDL compressed samples
if(file.CanRead(8) && (fileSize = file.ReadUint32LE()) >= 4)
{
BitReader chunk = file.ReadChunk(fileSize);
bytesRead = chunk.GetLength() + 4;
uint8 dlt = 0, lowbyte = 0;
const bool is16bit = GetBitDepth() == 16;
try
{
for(SmpLength j = 0; j < sample.nLength; j++)
{
uint8 hibyte;
if(is16bit)
{
lowbyte = static_cast<uint8>(chunk.ReadBits(8));
}
bool sign = chunk.ReadBits(1) != 0;
if(chunk.ReadBits(1))
{
hibyte = static_cast<uint8>(chunk.ReadBits(3));
} else
{
hibyte = 8;
while(!chunk.ReadBits(1))
{
hibyte += 0x10;
}
hibyte += static_cast<uint8>(chunk.ReadBits(4));
}
if(sign)
{
hibyte = ~hibyte;
}
dlt += hibyte;
if(!is16bit)
{
sample.sample8()[j] = dlt;
} else
{
sample.sample16()[j] = lowbyte | (dlt << 8);
}
}
} catch(const BitReader::eof &)
{
// Data is not sufficient to decode the whole sample
//AddToLog(LogWarning, "Truncated MDL sample block");
}
}
} else if(GetEncoding() == DMF && GetChannelFormat() == mono && GetBitDepth() <= 16)
{
// DMF Huffman compression
if(fileSize > 4)
{
bytesRead = DMFUnpack(file, mpt::byte_cast<uint8 *>(sample.sampleb()), sample.GetSampleSizeInBytes());
}
} else if((GetEncoding() == uLaw || GetEncoding() == aLaw) && GetBitDepth() == 16 && (GetChannelFormat() == mono || GetChannelFormat() == stereoInterleaved))
{
SmpLength readLength = sample.nLength * GetNumChannels();
LimitMax(readLength, mpt::saturate_cast<SmpLength>(fileSize));
bytesRead = readLength;
const std::byte *inBuf = sourceBuf;
int16 *outBuf = sample.sample16();
if(GetEncoding() == uLaw)
{
SC::DecodeInt16uLaw conv;
while(readLength--)
{
*(outBuf++) = conv(inBuf++);
}
} else
{
SC::DecodeInt16ALaw conv;
while(readLength--)
{
*(outBuf++) = conv(inBuf++);
}
}
}
/////////////////////////
// Uncompressed samples
//////////////////////////////////////////////////////
// 8-Bit / Mono / PCM
else if(GetBitDepth() == 8 && GetChannelFormat() == mono)
{
switch(GetEncoding())
{
case signedPCM: // 8-Bit / Mono / Signed / PCM
bytesRead = CopyMonoSample<SC::DecodeInt8>(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 8-Bit / Mono / Unsigned / PCM
bytesRead = CopyMonoSample<SC::DecodeUint8>(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 8-Bit / Mono / Delta / PCM
case MT2:
bytesRead = CopyMonoSample<SC::DecodeInt8Delta>(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 8-Bit / Stereo Split / PCM
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoSplit)
{
switch(GetEncoding())
{
case signedPCM: // 8-Bit / Stereo Split / Signed / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt8>(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 8-Bit / Stereo Split / Unsigned / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeUint8>(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 8-Bit / Stereo Split / Delta / PCM
case MT2: // same as deltaPCM, but right channel is stored as a difference from the left channel
bytesRead = CopyStereoSplitSample<SC::DecodeInt8Delta>(sample, sourceBuf, fileSize);
if(GetEncoding() == MT2)
{
for(int8 *p = sample.sample8(), *pEnd = p + sample.nLength * 2; p < pEnd; p += 2)
{
p[1] = static_cast<int8>(static_cast<uint8>(p[0]) + static_cast<uint8>(p[1]));
}
}
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 8-Bit / Stereo Interleaved / PCM
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoInterleaved)
{
switch(GetEncoding())
{
case signedPCM: // 8-Bit / Stereo Interleaved / Signed / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt8>(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 8-Bit / Stereo Interleaved / Unsigned / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeUint8>(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 8-Bit / Stereo Interleaved / Delta / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt8Delta>(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Mono / Little Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == mono && GetEndianness() == littleEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Stereo Interleaved / Signed / PCM
bytesRead = CopyMonoSample<SC::DecodeInt16<0, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Stereo Interleaved / Unsigned / PCM
bytesRead = CopyMonoSample<SC::DecodeInt16<0x8000u, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Stereo Interleaved / Delta / PCM
case MT2:
bytesRead = CopyMonoSample<SC::DecodeInt16Delta<littleEndian16> >(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Mono / Big Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == mono && GetEndianness() == bigEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Mono / Signed / PCM
bytesRead = CopyMonoSample<SC::DecodeInt16<0, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Mono / Unsigned / PCM
bytesRead = CopyMonoSample<SC::DecodeInt16<0x8000u, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Mono / Delta / PCM
bytesRead = CopyMonoSample<SC::DecodeInt16Delta<bigEndian16> >(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Stereo Split / Little Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoSplit && GetEndianness() == littleEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Stereo Split / Signed / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt16<0, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Stereo Split / Unsigned / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt16<0x8000u, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Stereo Split / Delta / PCM
case MT2: // same as deltaPCM, but right channel is stored as a difference from the left channel
bytesRead = CopyStereoSplitSample<SC::DecodeInt16Delta<littleEndian16> >(sample, sourceBuf, fileSize);
if(GetEncoding() == MT2)
{
for(int16 *p = sample.sample16(), *pEnd = p + sample.nLength * 2; p < pEnd; p += 2)
{
p[1] = static_cast<int16>(static_cast<uint16>(p[0]) + static_cast<uint16>(p[1]));
}
}
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Stereo Split / Big Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoSplit && GetEndianness() == bigEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Stereo Split / Signed / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt16<0, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Stereo Split / Unsigned / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt16<0x8000u, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Stereo Split / Delta / PCM
bytesRead = CopyStereoSplitSample<SC::DecodeInt16Delta<bigEndian16> >(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Stereo Interleaved / Little Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoInterleaved && GetEndianness() == littleEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Stereo Interleaved / Signed / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16<0, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Stereo Interleaved / Unsigned / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16<0x8000u, littleEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Stereo Interleaved / Delta / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16Delta<littleEndian16> >(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 16-Bit / Stereo Interleaved / Big Endian / PCM
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoInterleaved && GetEndianness() == bigEndian)
{
switch(GetEncoding())
{
case signedPCM: // 16-Bit / Stereo Interleaved / Signed / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16<0, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case unsignedPCM: // 16-Bit / Stereo Interleaved / Unsigned / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16<0x8000u, bigEndian16> >(sample, sourceBuf, fileSize);
break;
case deltaPCM: // 16-Bit / Stereo Interleaved / Delta / PCM
bytesRead = CopyStereoInterleavedSample<SC::DecodeInt16Delta<bigEndian16> >(sample, sourceBuf, fileSize);
break;
default:
MPT_ASSERT_NOTREACHED();
break;
}
}
//////////////////////////////////////////////////////
// 24-Bit / Signed / Mono / PCM
else if(GetBitDepth() == 24 && GetChannelFormat() == mono && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, littleEndian24> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, bigEndian24> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 24-Bit / Signed / Stereo Interleaved / PCM
else if(GetBitDepth() == 24 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, littleEndian24> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, bigEndian24> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Signed / Mono / PCM
else if(GetBitDepth() == 32 && GetChannelFormat() == mono && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, littleEndian32> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, bigEndian32> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Signed / Stereo Interleaved / PCM
else if(GetBitDepth() == 32 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, littleEndian32> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, bigEndian32> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 64-Bit / Signed / Mono / PCM
else if(GetBitDepth() == 64 && GetChannelFormat() == mono && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int64>, SC::DecodeInt64<0, littleEndian64> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, int64>, SC::DecodeInt64<0, bigEndian64> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 64-Bit / Signed / Stereo Interleaved / PCM
else if(GetBitDepth() == 64 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int64>, SC::DecodeInt64<0, littleEndian64> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, int64>, SC::DecodeInt64<0, bigEndian64> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Mono / PCM
else if(GetBitDepth() == 32 && GetChannelFormat() == mono && GetEncoding() == floatPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<littleEndian32> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<bigEndian32> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Stereo Interleaved / PCM
else if(GetBitDepth() == 32 && GetChannelFormat() == stereoInterleaved && GetEncoding() == floatPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<littleEndian32> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<bigEndian32> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 64-Bit / Float / Mono / PCM
else if(GetBitDepth() == 64 && GetChannelFormat() == mono && GetEncoding() == floatPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<littleEndian64> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyMonoSample<SC::ConversionChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<bigEndian64> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 64-Bit / Float / Stereo Interleaved / PCM
else if(GetBitDepth() == 64 && GetChannelFormat() == stereoInterleaved && GetEncoding() == floatPCM)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<littleEndian64> > >(sample, sourceBuf, fileSize);
} else
{
bytesRead = CopyStereoInterleavedSample<SC::ConversionChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<bigEndian64> > >(sample, sourceBuf, fileSize);
}
}
//////////////////////////////////////////////////////
// 24-Bit / Signed / Mono, Stereo Interleaved / PCM
else if(GetBitDepth() == 24 && (GetChannelFormat() == mono || GetChannelFormat() == stereoInterleaved) && GetEncoding() == signedPCMnormalize)
{
// Normalize to 16-Bit
uint32 srcPeak = uint32(1)<<31;
if(GetEndianness() == littleEndian)
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, littleEndian24> > >(sample, sourceBuf, fileSize, &srcPeak);
} else
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, bigEndian24> > >(sample, sourceBuf, fileSize, &srcPeak);
}
if(bytesRead && srcPeak != uint32(1)<<31)
{
// Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision.
sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(1), uint32(64)));
sample.uFlags.set(SMP_MODIFIED);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Signed / Mono, Stereo Interleaved / PCM
else if(GetBitDepth() == 32 && (GetChannelFormat() == mono || GetChannelFormat() == stereoInterleaved) && GetEncoding() == signedPCMnormalize)
{
// Normalize to 16-Bit
uint32 srcPeak = uint32(1)<<31;
if(GetEndianness() == littleEndian)
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, littleEndian32> > >(sample, sourceBuf, fileSize, &srcPeak);
} else
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, int32>, SC::DecodeInt32<0, bigEndian32> > >(sample, sourceBuf, fileSize, &srcPeak);
}
if(bytesRead && srcPeak != uint32(1)<<31)
{
// Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision.
sample.nGlobalVol = static_cast<uint16>(Clamp(Util::muldivr_unsigned(sample.nGlobalVol, srcPeak, uint32(1)<<31), uint32(1), uint32(64)));
sample.uFlags.set(SMP_MODIFIED);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Mono, Stereo Interleaved / PCM
else if(GetBitDepth() == 32 && (GetChannelFormat() == mono || GetChannelFormat() == stereoInterleaved) && GetEncoding() == floatPCMnormalize)
{
// Normalize to 16-Bit
somefloat32 srcPeak = 1.0f;
if(GetEndianness() == littleEndian)
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<littleEndian32> > >(sample, sourceBuf, fileSize, &srcPeak);
} else
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, somefloat32>, SC::DecodeFloat32<bigEndian32> > >(sample, sourceBuf, fileSize, &srcPeak);
}
if(bytesRead && srcPeak != 1.0f)
{
// Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision.
sample.nGlobalVol = mpt::saturate_round<uint16>(Clamp(sample.nGlobalVol * srcPeak, 1.0f, 64.0f));
sample.uFlags.set(SMP_MODIFIED);
}
}
//////////////////////////////////////////////////////
// 64-Bit / Float / Mono, Stereo Interleaved / PCM
else if(GetBitDepth() == 64 && (GetChannelFormat() == mono || GetChannelFormat() == stereoInterleaved) && GetEncoding() == floatPCMnormalize)
{
// Normalize to 16-Bit
somefloat64 srcPeak = 1.0;
if(GetEndianness() == littleEndian)
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<littleEndian64> > >(sample, sourceBuf, fileSize, &srcPeak);
} else
{
bytesRead = CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, somefloat64>, SC::DecodeFloat64<bigEndian64> > >(sample, sourceBuf, fileSize, &srcPeak);
}
if(bytesRead && srcPeak != 1.0)
{
// Adjust sample volume so we do not affect relative volume of the sample. Normalizing is only done to increase precision.
sample.nGlobalVol = mpt::saturate_round<uint16>(Clamp(sample.nGlobalVol * srcPeak, 1.0, 64.0));
sample.uFlags.set(SMP_MODIFIED);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Mono / PCM / full scale 2^15
else if(GetBitDepth() == 32 && GetChannelFormat() == mono && GetEncoding() == floatPCM15)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<littleEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<littleEndian32>(1.0f / static_cast<float>(1<<15)))
);
} else
{
bytesRead = CopyMonoSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<bigEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<bigEndian32>(1.0f / static_cast<float>(1<<15)))
);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Stereo Interleaved / PCM / full scale 2^15
else if(GetBitDepth() == 32 && GetChannelFormat() == stereoInterleaved && GetEncoding() == floatPCM15)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<littleEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<littleEndian32>(1.0f / static_cast<float>(1<<15)))
);
} else
{
bytesRead = CopyStereoInterleavedSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<bigEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<bigEndian32>(1.0f / static_cast<float>(1<<15)))
);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Stereo Interleaved / PCM / full scale 2^23
else if(GetBitDepth() == 32 && GetChannelFormat() == mono && GetEncoding() == floatPCM23)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyMonoSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<littleEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<littleEndian32>(1.0f / static_cast<float>(1<<23)))
);
} else
{
bytesRead = CopyMonoSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<bigEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<bigEndian32>(1.0f / static_cast<float>(1<<23)))
);
}
}
//////////////////////////////////////////////////////
// 32-Bit / Float / Stereo Interleaved / PCM / full scale 2^23
else if(GetBitDepth() == 32 && GetChannelFormat() == stereoInterleaved && GetEncoding() == floatPCM23)
{
if(GetEndianness() == littleEndian)
{
bytesRead = CopyStereoInterleavedSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<littleEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<littleEndian32>(1.0f / static_cast<float>(1<<23)))
);
} else
{
bytesRead = CopyStereoInterleavedSample
(sample, sourceBuf, fileSize,
SC::ConversionChain<SC::Convert<int16, somefloat32>, SC::DecodeScaledFloat32<bigEndian32> >
(SC::Convert<int16, somefloat32>(), SC::DecodeScaledFloat32<bigEndian32>(1.0f / static_cast<float>(1<<23)))
);
}
}
////////////////
// Unsupported
else
{
MPT_ASSERT_NOTREACHED();
}
MPT_ASSERT(filePosition + bytesRead <= file.GetLength());
file.Seek(filePosition + bytesRead);
return bytesRead;
}
#ifndef MODPLUG_NO_FILESAVE
// Write a sample to file
size_t SampleIO::WriteSample(std::ostream &f, const ModSample &sample, SmpLength maxSamples) const
{
if(sample.uFlags[CHN_ADLIB])
{
mpt::IO::Write(f, sample.adlib);
return sizeof(sample.adlib);
}
if(!sample.HasSampleData())
{
return 0;
}
std::array<std::byte, mpt::IO::BUFFERSIZE_TINY> writeBuffer;
mpt::IO::WriteBuffer<std::ostream> fb{f, mpt::as_span(writeBuffer)};
SmpLength numSamples = sample.nLength;
if(maxSamples && numSamples > maxSamples)
{
numSamples = maxSamples;
}
std::size_t len = CalculateEncodedSize(numSamples);
if(GetBitDepth() == 16 && GetChannelFormat() == mono && GetEndianness() == littleEndian &&
(GetEncoding() == signedPCM || GetEncoding() == unsignedPCM || GetEncoding() == deltaPCM))
{
// 16-bit little-endian mono samples
MPT_ASSERT(len == numSamples * 2);
const int16 *const pSample16 = sample.sample16();
const int16 *p = pSample16;
int s_old = 0;
const int s_ofs = (GetEncoding() == unsignedPCM) ? 0x8000 : 0;
for(SmpLength j = 0; j < numSamples; j++)
{
int s_new = *p;
p++;
if(sample.uFlags[CHN_STEREO])
{
// Downmix stereo
s_new = (s_new + (*p) + 1) / 2;
p++;
}
if(GetEncoding() == deltaPCM)
{
mpt::IO::Write(fb, mpt::as_le(static_cast<int16>(s_new - s_old)));
s_old = s_new;
} else
{
mpt::IO::Write(fb, mpt::as_le(static_cast<int16>(s_new + s_ofs)));
}
}
}
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoSplit &&
(GetEncoding() == signedPCM || GetEncoding() == unsignedPCM || GetEncoding() == deltaPCM))
{
// 8-bit Stereo samples (not interleaved)
MPT_ASSERT(len == numSamples * 2);
const int8 *const pSample8 = sample.sample8();
const int s_ofs = (GetEncoding() == unsignedPCM) ? 0x80 : 0;
for (uint32 iCh=0; iCh<2; iCh++)
{
const int8 *p = pSample8 + iCh;
int s_old = 0;
for (SmpLength j = 0; j < numSamples; j++)
{
int s_new = *p;
p += 2;
if (GetEncoding() == deltaPCM)
{
mpt::IO::Write(fb, static_cast<int8>(s_new - s_old));
s_old = s_new;
} else
{
mpt::IO::Write(fb, static_cast<int8>(s_new + s_ofs));
}
}
}
}
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoSplit && GetEndianness() == littleEndian &&
(GetEncoding() == signedPCM || GetEncoding() == unsignedPCM || GetEncoding() == deltaPCM))
{
// 16-bit little-endian Stereo samples (not interleaved)
MPT_ASSERT(len == numSamples * 4);
const int16 *const pSample16 = sample.sample16();
const int s_ofs = (GetEncoding() == unsignedPCM) ? 0x8000 : 0;
for (uint32 iCh=0; iCh<2; iCh++)
{
const int16 *p = pSample16 + iCh;
int s_old = 0;
for (SmpLength j = 0; j < numSamples; j++)
{
int s_new = *p;
p += 2;
if (GetEncoding() == deltaPCM)
{
mpt::IO::Write(fb, mpt::as_le(static_cast<int16>(s_new - s_old)));
s_old = s_new;
} else
{
mpt::IO::Write(fb, mpt::as_le(static_cast<int16>(s_new + s_ofs)));
}
}
}
}
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM)
{
// Stereo signed interleaved
MPT_ASSERT(len == numSamples * 2);
const int8 *const pSample8 = sample.sample8();
mpt::IO::Write(f, mpt::as_span(pSample8, len));
}
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM && GetEndianness() == littleEndian)
{
// Stereo signed interleaved, little-endian
MPT_ASSERT(len == numSamples * 4);
const int16 *const pSample16 = sample.sample16();
const int16 *p = pSample16;
for(SmpLength j = 0; j < numSamples; j++)
{
mpt::IO::Write(fb, mpt::as_le(p[0]));
mpt::IO::Write(fb, mpt::as_le(p[1]));
p += 2;
}
}
else if(GetBitDepth() == 16 && GetChannelFormat() == stereoInterleaved && GetEncoding() == signedPCM && GetEndianness() == bigEndian)
{
// Stereo signed interleaved, big-endian
MPT_ASSERT(len == numSamples * 4);
const int16 *const pSample16 = sample.sample16();
const int16 *p = pSample16;
for(SmpLength j = 0; j < numSamples; j++)
{
mpt::IO::Write(fb, mpt::as_be(p[0]));
mpt::IO::Write(fb, mpt::as_be(p[1]));
p += 2;
}
}
else if(GetBitDepth() == 16 && (GetChannelFormat() == stereoSplit || GetChannelFormat() == mono)
&& (GetEncoding() == signedPCM || GetEncoding() == unsignedPCM) && GetEndianness() == bigEndian)
{
// Stereo split / mono signed 16-bit, big-endian
const uint8 numChannels = GetNumChannels();
const uint16 offset = (GetEncoding() == unsignedPCM) ? 0x8000 : 0;
MPT_ASSERT(len == numSamples * numChannels * 2);
for(uint8 chn = 0; chn < numChannels; chn++)
{
const int16 *p = sample.sample16() + chn;
for(SmpLength j = 0; j < numSamples; j++)
{
mpt::IO::Write(fb, mpt::as_be(static_cast<int16>(static_cast<uint16>(*p) + offset)));
p += numChannels;
}
}
}
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoInterleaved && GetEncoding() == unsignedPCM)
{
// Stereo unsigned interleaved
MPT_ASSERT(len == numSamples * 2);
const int8 *const pSample8 = sample.sample8();
for(SmpLength j = 0; j < numSamples * 2; j++)
{
mpt::IO::Write(fb, static_cast<int8>(static_cast<uint8>(pSample8[j]) + 0x80));
}
}
else if(GetEncoding() == IT214 || GetEncoding() == IT215)
{
// IT2.14-encoded samples
ITCompression its(sample, GetEncoding() == IT215, &f, numSamples);
len = its.GetCompressedSize();
}
// Default: assume 8-bit PCM data
else
{
MPT_ASSERT(GetBitDepth() == 8);
MPT_ASSERT(len == numSamples);
if(sample.uFlags[CHN_16BIT])
{
const int16 *p = sample.sample16();
int s_old = 0;
const int s_ofs = (GetEncoding() == unsignedPCM) ? 0x80 : 0;
for(SmpLength j = 0; j < numSamples; j++)
{
int s_new = mpt::rshift_signed(*p, 8);
p++;
if(sample.uFlags[CHN_STEREO])
{
s_new = (s_new + mpt::rshift_signed(*p, 8) + 1) / 2;
p++;
}
if(GetEncoding() == deltaPCM)
{
mpt::IO::Write(fb, static_cast<int8>(s_new - s_old));
s_old = s_new;
} else
{
mpt::IO::Write(fb, static_cast<int8>(s_new + s_ofs));
}
}
} else
{
const int8 *const pSample8 = sample.sample8();
const int8 *p = pSample8;
int s_old = 0;
const int s_ofs = (GetEncoding() == unsignedPCM) ? 0x80 : 0;
for(SmpLength j = 0; j < numSamples; j++)
{
int s_new = *p;
p++;
if(sample.uFlags[CHN_STEREO])
{
s_new = (s_new + (static_cast<int>(*p)) + 1) / 2;
p++;
}
if(GetEncoding() == deltaPCM)
{
mpt::IO::Write(fb, static_cast<int8>(s_new - s_old));
s_old = s_new;
} else
{
mpt::IO::Write(fb, static_cast<int8>(s_new + s_ofs));
}
}
}
}
return len;
}
#endif // MODPLUG_NO_FILESAVE
OPENMPT_NAMESPACE_END
|