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