aboutsummaryrefslogtreecommitdiffstats
path: root/core/fmt_traits.h
blob: 024730147418386fef35709e276749c16cd0c038 (plain)
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
#ifndef CORE_FMT_TRAITS_H
#define CORE_FMT_TRAITS_H

#include <cstddef>
#include <stdint.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 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 OutT to(const Type val) noexcept { return val*OutT{1.0/32768.0}; }
};
template<>
struct FmtTypeTraits<FmtInt> {
    using Type = int32_t;

    template<typename OutT>
    static constexpr OutT to(const Type val) noexcept
    { return static_cast<OutT>(val)*OutT{1.0/2147483648.0}; }
};
template<>
struct FmtTypeTraits<FmtFloat> {
    using Type = float;

    template<typename OutT>
    static constexpr OutT to(const Type val) noexcept { return val; }
};
template<>
struct FmtTypeTraits<FmtDouble> {
    using Type = double;

    template<typename OutT>
    static constexpr OutT to(const Type val) noexcept { return static_cast<OutT>(val); }
};
template<>
struct FmtTypeTraits<FmtMulaw> {
    using Type = uint8_t;

    template<typename OutT>
    static constexpr 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 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 std::byte *src, const std::size_t srcstep,
    const std::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 */