🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

  1. #ifndef CORE_FMT_TRAITS_H
  2. #define CORE_FMT_TRAITS_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include "albyte.h"
  6. #include "buffer_storage.h"
  7. namespace al {
  8. extern const int16_t muLawDecompressionTable[256];
  9. extern const int16_t aLawDecompressionTable[256];
  10. template<FmtType T>
  11. struct FmtTypeTraits { };
  12. template<>
  13. struct FmtTypeTraits<FmtUByte> {
  14. using Type = uint8_t;
  15. template<typename OutT>
  16. static constexpr inline OutT to(const Type val) noexcept
  17. { return val*OutT{1.0/128.0} - OutT{1.0}; }
  18. };
  19. template<>
  20. struct FmtTypeTraits<FmtShort> {
  21. using Type = int16_t;
  22. template<typename OutT>
  23. static constexpr inline OutT to(const Type val) noexcept { return val*OutT{1.0/32768.0}; }
  24. };
  25. template<>
  26. struct FmtTypeTraits<FmtFloat> {
  27. using Type = float;
  28. template<typename OutT>
  29. static constexpr inline OutT to(const Type val) noexcept { return val; }
  30. };
  31. template<>
  32. struct FmtTypeTraits<FmtDouble> {
  33. using Type = double;
  34. template<typename OutT>
  35. static constexpr inline OutT to(const Type val) noexcept { return static_cast<OutT>(val); }
  36. };
  37. template<>
  38. struct FmtTypeTraits<FmtMulaw> {
  39. using Type = uint8_t;
  40. template<typename OutT>
  41. static constexpr inline OutT to(const Type val) noexcept
  42. { return muLawDecompressionTable[val] * OutT{1.0/32768.0}; }
  43. };
  44. template<>
  45. struct FmtTypeTraits<FmtAlaw> {
  46. using Type = uint8_t;
  47. template<typename OutT>
  48. static constexpr inline OutT to(const Type val) noexcept
  49. { return aLawDecompressionTable[val] * OutT{1.0/32768.0}; }
  50. };
  51. template<FmtType SrcType, typename DstT>
  52. inline void LoadSampleArray(DstT *RESTRICT dst, const al::byte *src, const size_t srcstep,
  53. const size_t samples) noexcept
  54. {
  55. using TypeTraits = FmtTypeTraits<SrcType>;
  56. using SampleType = typename TypeTraits::Type;
  57. const SampleType *RESTRICT ssrc{reinterpret_cast<const SampleType*>(src)};
  58. for(size_t i{0u};i < samples;i++)
  59. dst[i] = TypeTraits::template to<DstT>(ssrc[i*srcstep]);
  60. }
  61. } // namespace al
  62. #endif /* CORE_FMT_TRAITS_H */