123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #pragma once
- #include "openmpt/all/BuildSettings.hpp"
- #include "../common/FileReaderFwd.h"
- OPENMPT_NAMESPACE_BEGIN
- struct ModSample;
- class SampleIO
- {
- public:
-
- enum Bitdepth : uint8
- {
- _8bit = 8,
- _16bit = 16,
- _24bit = 24,
- _32bit = 32,
- _64bit = 64,
- };
-
- enum Channels : uint8
- {
- mono = 1,
- stereoInterleaved,
- stereoSplit,
- };
-
- enum Endianness : uint8
- {
- littleEndian = 0,
- bigEndian = 1,
- };
-
- enum Encoding : uint8
- {
- signedPCM = 0,
- unsignedPCM,
- deltaPCM,
- floatPCM,
- IT214,
- IT215,
- AMS,
- DMF,
- MDL,
- PTM8Dto16,
- ADPCM,
- MT2,
- floatPCM15,
- floatPCM23,
- floatPCMnormalize,
- signedPCMnormalize,
- uLaw,
- aLaw,
- };
- protected:
- Bitdepth m_bitdepth;
- Channels m_channels;
- Endianness m_endianness;
- Encoding m_encoding;
- public:
- constexpr SampleIO(Bitdepth bits = _8bit, Channels channels = mono, Endianness endianness = littleEndian, Encoding encoding = signedPCM)
- : m_bitdepth(bits), m_channels(channels), m_endianness(endianness), m_encoding(encoding)
- { }
- bool operator== (const SampleIO &other) const
- {
- return memcmp(this, &other, sizeof(*this)) == 0;
- }
- bool operator!= (const SampleIO &other) const
- {
- return memcmp(this, &other, sizeof(*this)) != 0;
- }
- void operator|= (Bitdepth bits)
- {
- m_bitdepth = bits;
- }
- void operator|= (Channels channels)
- {
- m_channels = channels;
- }
- void operator|= (Endianness endianness)
- {
- m_endianness = endianness;
- }
- void operator|= (Encoding encoding)
- {
- m_encoding = encoding;
- }
- void MayNormalize()
- {
- if(GetBitDepth() >= 24)
- {
- if(GetEncoding() == SampleIO::signedPCM)
- {
- m_encoding = SampleIO::signedPCMnormalize;
- } else if(GetEncoding() == SampleIO::floatPCM)
- {
- m_encoding = SampleIO::floatPCMnormalize;
- }
- }
- }
-
- MPT_CONSTEXPRINLINE uint8 GetEncodedBitsPerSample() const
- {
- switch(GetEncoding())
- {
- case signedPCM:
- case unsignedPCM:
- case deltaPCM:
- case floatPCM:
- case MT2:
- case floatPCM15:
- case floatPCM23:
- case floatPCMnormalize:
- case signedPCMnormalize:
- return GetBitDepth();
- case IT214:
- case IT215:
- case AMS:
- case DMF:
- case MDL:
- return 0;
- case PTM8Dto16:
- return 16;
- case ADPCM:
- return 4;
- case uLaw:
- return 8;
- case aLaw:
- return 8;
- default:
- return 0;
- }
- }
-
- MPT_CONSTEXPRINLINE std::size_t GetEncodedHeaderSize() const
- {
- switch(GetEncoding())
- {
- case ADPCM:
- return 16;
- default:
- return 0;
- }
- }
-
- MPT_CONSTEXPRINLINE bool IsVariableLengthEncoded() const
- {
- return GetEncodedBitsPerSample() == 0;
- }
-
- MPT_CONSTEXPRINLINE bool UsesFileReaderForDecoding() const
- {
- switch(GetEncoding())
- {
- case IT214:
- case IT215:
- case AMS:
- case DMF:
- case MDL:
- return true;
- default:
- return false;
- }
- }
-
- constexpr uint8 GetBitDepth() const
- {
- return static_cast<uint8>(m_bitdepth);
- }
-
- constexpr Channels GetChannelFormat() const
- {
- return m_channels;
- }
-
- constexpr uint8 GetNumChannels() const
- {
- return GetChannelFormat() == mono ? 1u : 2u;
- }
-
- constexpr Endianness GetEndianness() const
- {
- return m_endianness;
- }
-
- constexpr Encoding GetEncoding() const
- {
- return m_encoding;
- }
-
- std::size_t CalculateEncodedSize(SmpLength length) const
- {
- if(IsVariableLengthEncoded())
- {
- return 0;
- }
- uint8 bps = GetEncodedBitsPerSample();
- if(bps % 8u != 0)
- {
- MPT_ASSERT(GetEncoding() == ADPCM && bps == 4);
- return GetEncodedHeaderSize() + (((length + 1) / 2) * GetNumChannels());
- }
- return GetEncodedHeaderSize() + (length * (bps / 8) * GetNumChannels());
- }
-
- size_t ReadSample(ModSample &sample, FileReader &file) const;
- #ifndef MODPLUG_NO_FILESAVE
-
- size_t WriteSample(std::ostream &f, const ModSample &sample, SmpLength maxSamples = 0) const;
- #endif
- };
- OPENMPT_NAMESPACE_END
|