Implement a "fast" bsinc path
[openal-soft.git] / alc / mixer / hrtfbase.h
blobd2432e903b96c1288fdf16c27a92d1810accdaf5
1 #ifndef MIXER_HRTFBASE_H
2 #define MIXER_HRTFBASE_H
4 #include <algorithm>
6 #include "alu.h"
7 #include "../hrtf.h"
8 #include "opthelpers.h"
11 using ApplyCoeffsT = void(size_t Offset, float2 *RESTRICT Values, const ALuint irSize,
12 const HrirArray &Coeffs, const ALfloat left, const ALfloat right);
14 template<ApplyCoeffsT &ApplyCoeffs>
15 inline void MixHrtfBase(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
16 const ALfloat *InSamples, float2 *RESTRICT AccumSamples, const size_t OutPos,
17 const ALuint IrSize, MixHrtfFilter *hrtfparams, const size_t BufferSize)
19 ASSUME(BufferSize > 0);
21 const HrirArray &Coeffs = *hrtfparams->Coeffs;
22 const ALfloat gainstep{hrtfparams->GainStep};
23 const ALfloat gain{hrtfparams->Gain};
25 ALsizei Delay[2]{
26 HRTF_HISTORY_LENGTH - hrtfparams->Delay[0],
27 HRTF_HISTORY_LENGTH - hrtfparams->Delay[1] };
28 ASSUME(Delay[0] >= 0 && Delay[1] >= 0);
29 ALfloat stepcount{0.0f};
30 for(size_t i{0u};i < BufferSize;++i)
32 const ALfloat g{gain + gainstep*stepcount};
33 const ALfloat left{InSamples[Delay[0]++] * g};
34 const ALfloat right{InSamples[Delay[1]++] * g};
35 ApplyCoeffs(i, AccumSamples+i, IrSize, Coeffs, left, right);
37 stepcount += 1.0f;
40 for(size_t i{0u};i < BufferSize;++i)
41 LeftOut[OutPos+i] += AccumSamples[i][0];
42 for(size_t i{0u};i < BufferSize;++i)
43 RightOut[OutPos+i] += AccumSamples[i][1];
45 hrtfparams->Gain = gain + gainstep*stepcount;
48 template<ApplyCoeffsT &ApplyCoeffs>
49 inline void MixHrtfBlendBase(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
50 const ALfloat *InSamples, float2 *RESTRICT AccumSamples, const size_t OutPos,
51 const ALuint IrSize, const HrtfFilter *oldparams, MixHrtfFilter *newparams,
52 const size_t BufferSize)
54 const auto &OldCoeffs = oldparams->Coeffs;
55 const ALfloat oldGain{oldparams->Gain};
56 const ALfloat oldGainStep{-oldGain / static_cast<ALfloat>(BufferSize)};
57 const auto &NewCoeffs = *newparams->Coeffs;
58 const ALfloat newGainStep{newparams->GainStep};
60 ASSUME(BufferSize > 0);
62 ALsizei Delay[2]{
63 HRTF_HISTORY_LENGTH - oldparams->Delay[0],
64 HRTF_HISTORY_LENGTH - oldparams->Delay[1] };
65 ASSUME(Delay[0] >= 0 && Delay[1] >= 0);
66 ALfloat stepcount{0.0f};
67 for(size_t i{0u};i < BufferSize;++i)
69 const ALfloat g{oldGain + oldGainStep*stepcount};
70 const ALfloat left{InSamples[Delay[0]++] * g};
71 const ALfloat right{InSamples[Delay[1]++] * g};
72 ApplyCoeffs(i, AccumSamples+i, IrSize, OldCoeffs, left, right);
74 stepcount += 1.0f;
77 Delay[0] = HRTF_HISTORY_LENGTH - newparams->Delay[0];
78 Delay[1] = HRTF_HISTORY_LENGTH - newparams->Delay[1];
79 ASSUME(Delay[0] >= 0 && Delay[1] >= 0);
80 stepcount = 0.0f;
81 for(size_t i{0u};i < BufferSize;++i)
83 const ALfloat g{newGainStep*stepcount};
84 const ALfloat left{InSamples[Delay[0]++] * g};
85 const ALfloat right{InSamples[Delay[1]++] * g};
86 ApplyCoeffs(i, AccumSamples+i, IrSize, NewCoeffs, left, right);
88 stepcount += 1.0f;
91 for(size_t i{0u};i < BufferSize;++i)
92 LeftOut[OutPos+i] += AccumSamples[i][0];
93 for(size_t i{0u};i < BufferSize;++i)
94 RightOut[OutPos+i] += AccumSamples[i][1];
96 newparams->Gain = newGainStep*stepcount;
99 template<ApplyCoeffsT &ApplyCoeffs>
100 inline void MixDirectHrtfBase(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
101 const al::span<const FloatBufferLine> InSamples, float2 *RESTRICT AccumSamples,
102 DirectHrtfState *State, const size_t BufferSize)
104 ASSUME(BufferSize > 0);
106 const ALuint IrSize{State->IrSize};
108 auto accum_iter = std::copy_n(State->Values.begin(), State->Values.size(), AccumSamples);
109 std::fill_n(accum_iter, BufferSize, float2{});
111 auto coeff_iter = State->Coeffs.begin();
112 for(const FloatBufferLine &input : InSamples)
114 const auto &Coeffs = *(coeff_iter++);
115 for(size_t i{0u};i < BufferSize;++i)
117 const ALfloat insample{input[i]};
118 ApplyCoeffs(i, AccumSamples+i, IrSize, Coeffs, insample, insample);
121 for(size_t i{0u};i < BufferSize;++i)
122 LeftOut[i] += AccumSamples[i][0];
123 for(size_t i{0u};i < BufferSize;++i)
124 RightOut[i] += AccumSamples[i][1];
126 std::copy_n(AccumSamples + BufferSize, State->Values.size(), State->Values.begin());
129 #endif /* MIXER_HRTFBASE_H */