Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DspFormat.h « src - github.com/mpc-hc/sanear.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79000cf7e015eaf7a1497e454cf142e015f27ce2 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma once

namespace SaneAudioRenderer
{
    enum class DspFormat
    {
        Unknown,
        Pcm8,
        Pcm16,
        Pcm24,
        Pcm24in32,
        Pcm32,
        Float,
        Double,
    };

    template <DspFormat OutputFormat>
    struct DspFormatTraits;

    template <>
    struct DspFormatTraits<DspFormat::Pcm8>
    {
        typedef int8_t SampleType;
    };

    template <>
    struct DspFormatTraits<DspFormat::Pcm16>
    {
        typedef int16_t SampleType;
    };

    #pragma pack(push, 1)
    typedef struct { int8_t d[3]; } int24_t;
    #pragma pack(pop)

    static_assert(sizeof(int24_t) == 3, "Failed to pack the struct properly");

    template <>
    struct DspFormatTraits<DspFormat::Pcm24>
    {
        typedef int24_t SampleType;
    };

    template <>
    struct DspFormatTraits<DspFormat::Pcm24in32>
    {
        typedef int32_t SampleType;
    };

    template <>
    struct DspFormatTraits<DspFormat::Pcm32>
    {
        typedef int32_t SampleType;
    };

    template <>
    struct DspFormatTraits<DspFormat::Float>
    {
        typedef float SampleType;
    };

    template <>
    struct DspFormatTraits<DspFormat::Double>
    {
        typedef double SampleType;
    };

    static_assert(sizeof(float) == 4, "Floats are not IEEE compliant");
    static_assert(sizeof(double) == 8, "Floats are not IEEE compliant");

    inline uint32_t DspFormatSize(DspFormat format)
    {
        return (format == DspFormat::Unknown) ? 0 :
               (format == DspFormat::Pcm8) ? 1 :
               (format == DspFormat::Pcm16) ? 2 :
               (format == DspFormat::Pcm24) ? 3 :
               (format == DspFormat::Double) ? 8 : 4;
    }

    inline DspFormat DspFormatFromWaveFormat(const WAVEFORMATEX& format)
    {
        if (format.nSamplesPerSec == 0)
            return DspFormat::Unknown;

        if (format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
        {
            switch (format.wBitsPerSample)
            {
                case 32: return DspFormat::Float;
                case 64: return DspFormat::Double;
            }
        }
        else if (format.wFormatTag == WAVE_FORMAT_PCM)
        {
            switch (format.wBitsPerSample)
            {
                case 8:  return DspFormat::Pcm8;
                case 16: return DspFormat::Pcm16;
                case 24: return DspFormat::Pcm24;
                case 32: return DspFormat::Pcm32;
            }
        }
        else if (format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
        {
            const WAVEFORMATEXTENSIBLE& formatExtensible = (const WAVEFORMATEXTENSIBLE&)format;

            if (formatExtensible.SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
            {
                switch (format.wBitsPerSample)
                {
                    case 32: return DspFormat::Float;
                    case 64: return DspFormat::Double;
                }
            }
            else if (formatExtensible.SubFormat == KSDATAFORMAT_SUBTYPE_PCM)
            {
                switch (format.wBitsPerSample)
                {
                    case 8:  return DspFormat::Pcm8;
                    case 16: return DspFormat::Pcm16;
                    case 24: return DspFormat::Pcm24;
                    case 32: return formatExtensible.Samples.wValidBitsPerSample == 24 ? DspFormat::Pcm24in32 :
                                                                                         DspFormat::Pcm32;
                }
            }
        }

        return DspFormat::Unknown;
    }
}