]> git.tdb.fi Git - ext/openal.git/blob - core/mixer/defs.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / core / mixer / defs.h
1 #ifndef CORE_MIXER_DEFS_H
2 #define CORE_MIXER_DEFS_H
3
4 #include <array>
5 #include <stdlib.h>
6
7 #include "alspan.h"
8 #include "core/bufferline.h"
9 #include "core/resampler_limits.h"
10
11 struct CubicCoefficients;
12 struct HrtfChannelState;
13 struct HrtfFilter;
14 struct MixHrtfFilter;
15
16 using uint = unsigned int;
17 using float2 = std::array<float,2>;
18
19
20 constexpr int MixerFracBits{16};
21 constexpr int MixerFracOne{1 << MixerFracBits};
22 constexpr int MixerFracMask{MixerFracOne - 1};
23 constexpr int MixerFracHalf{MixerFracOne >> 1};
24
25 constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */
26
27
28 enum class Resampler : uint8_t {
29     Point,
30     Linear,
31     Cubic,
32     FastBSinc12,
33     BSinc12,
34     FastBSinc24,
35     BSinc24,
36
37     Max = BSinc24
38 };
39
40 /* Interpolator state. Kind of a misnomer since the interpolator itself is
41  * stateless. This just keeps it from having to recompute scale-related
42  * mappings for every sample.
43  */
44 struct BsincState {
45     float sf; /* Scale interpolation factor. */
46     uint m; /* Coefficient count. */
47     uint l; /* Left coefficient offset. */
48     /* Filter coefficients, followed by the phase, scale, and scale-phase
49      * delta coefficients. Starting at phase index 0, each subsequent phase
50      * index follows contiguously.
51      */
52     const float *filter;
53 };
54
55 struct CubicState {
56     /* Filter coefficients, and coefficient deltas. Starting at phase index 0,
57      * each subsequent phase index follows contiguously.
58      */
59     const CubicCoefficients *filter;
60 };
61
62 union InterpState {
63     CubicState cubic;
64     BsincState bsinc;
65 };
66
67 using ResamplerFunc = void(*)(const InterpState *state, const float *RESTRICT src, uint frac,
68     const uint increment, const al::span<float> dst);
69
70 ResamplerFunc PrepareResampler(Resampler resampler, uint increment, InterpState *state);
71
72
73 template<typename TypeTag, typename InstTag>
74 void Resample_(const InterpState *state, const float *RESTRICT src, uint frac,
75     const uint increment, const al::span<float> dst);
76
77 template<typename InstTag>
78 void Mix_(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
79     float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos);
80 template<typename InstTag>
81 void Mix_(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
82     const float TargetGain, const size_t Counter);
83
84 template<typename InstTag>
85 void MixHrtf_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
86     const MixHrtfFilter *hrtfparams, const size_t BufferSize);
87 template<typename InstTag>
88 void MixHrtfBlend_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
89     const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize);
90 template<typename InstTag>
91 void MixDirectHrtf_(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
92     const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
93     float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize);
94
95 /* Vectorized resampler helpers */
96 template<size_t N>
97 inline void InitPosArrays(uint frac, uint increment, uint (&frac_arr)[N], uint (&pos_arr)[N])
98 {
99     pos_arr[0] = 0;
100     frac_arr[0] = frac;
101     for(size_t i{1};i < N;i++)
102     {
103         const uint frac_tmp{frac_arr[i-1] + increment};
104         pos_arr[i] = pos_arr[i-1] + (frac_tmp>>MixerFracBits);
105         frac_arr[i] = frac_tmp&MixerFracMask;
106     }
107 }
108
109 #endif /* CORE_MIXER_DEFS_H */