diff options
author | Chris Robinson <[email protected]> | 2020-12-13 21:12:03 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2020-12-13 21:12:03 -0800 |
commit | 56af137ba0a3ab847b8f57c3c4aeee606aa5403f (patch) | |
tree | 7750cfda8fc860d6d2c80b505db08852ed25575b /core/fmt_traits.h | |
parent | 783904e414024691d47e76a22d8405ca85dd04f2 (diff) |
Move fmt_traits to core
Diffstat (limited to 'core/fmt_traits.h')
-rw-r--r-- | core/fmt_traits.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/core/fmt_traits.h b/core/fmt_traits.h new file mode 100644 index 00000000..f797f836 --- /dev/null +++ b/core/fmt_traits.h @@ -0,0 +1,81 @@ +#ifndef CORE_FMT_TRAITS_H +#define CORE_FMT_TRAITS_H + +#include <stddef.h> +#include <stdint.h> + +#include "albyte.h" +#include "buffer_storage.h" + + +namespace al { + +extern const int16_t muLawDecompressionTable[256]; +extern const int16_t aLawDecompressionTable[256]; + + +template<FmtType T> +struct FmtTypeTraits { }; + +template<> +struct FmtTypeTraits<FmtUByte> { + using Type = uint8_t; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept + { return val*OutT{1.0/128.0} - OutT{1.0}; } +}; +template<> +struct FmtTypeTraits<FmtShort> { + using Type = int16_t; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept { return val*OutT{1.0/32768.0}; } +}; +template<> +struct FmtTypeTraits<FmtFloat> { + using Type = float; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept { return val; } +}; +template<> +struct FmtTypeTraits<FmtDouble> { + using Type = double; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept { return static_cast<OutT>(val); } +}; +template<> +struct FmtTypeTraits<FmtMulaw> { + using Type = uint8_t; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept + { return muLawDecompressionTable[val] * OutT{1.0/32768.0}; } +}; +template<> +struct FmtTypeTraits<FmtAlaw> { + using Type = uint8_t; + + template<typename OutT> + static constexpr inline OutT to(const Type val) noexcept + { return aLawDecompressionTable[val] * OutT{1.0/32768.0}; } +}; + + +template<FmtType SrcType, typename DstT> +inline void LoadSampleArray(DstT *RESTRICT dst, const al::byte *src, const size_t srcstep, + const size_t samples) noexcept +{ + using TypeTraits = FmtTypeTraits<SrcType>; + using SampleType = typename TypeTraits::Type; + + const SampleType *RESTRICT ssrc{reinterpret_cast<const SampleType*>(src)}; + for(size_t i{0u};i < samples;i++) + dst[i] = TypeTraits::template to<DstT>(ssrc[i*srcstep]); +} + +} // namespace al + +#endif /* CORE_FMT_TRAITS_H */ |