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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#ifndef MIXER_HRTFBASE_H
#define MIXER_HRTFBASE_H
#include <algorithm>
#include "alu.h"
#include "../hrtf.h"
#include "opthelpers.h"
#include "voice.h"
using ApplyCoeffsT = void(&)(float2 *RESTRICT Values, const uint_fast32_t irSize,
const HrirArray &Coeffs, const float left, const float right);
template<ApplyCoeffsT ApplyCoeffs>
inline void MixHrtfBase(const float *InSamples, float2 *RESTRICT AccumSamples, const ALuint IrSize,
const MixHrtfFilter *hrtfparams, const size_t BufferSize)
{
ASSUME(BufferSize > 0);
const HrirArray &Coeffs = *hrtfparams->Coeffs;
const float gainstep{hrtfparams->GainStep};
const float gain{hrtfparams->Gain};
size_t Delay[2]{
HRTF_HISTORY_LENGTH - hrtfparams->Delay[0],
HRTF_HISTORY_LENGTH - hrtfparams->Delay[1] };
float stepcount{0.0f};
for(size_t i{0u};i < BufferSize;++i)
{
const float g{gain + gainstep*stepcount};
const float left{InSamples[Delay[0]++] * g};
const float right{InSamples[Delay[1]++] * g};
ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, left, right);
stepcount += 1.0f;
}
}
template<ApplyCoeffsT ApplyCoeffs>
inline void MixHrtfBlendBase(const float *InSamples, float2 *RESTRICT AccumSamples,
const ALuint IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams,
const size_t BufferSize)
{
const auto &OldCoeffs = oldparams->Coeffs;
const float oldGain{oldparams->Gain};
const float oldGainStep{-oldGain / static_cast<float>(BufferSize)};
const auto &NewCoeffs = *newparams->Coeffs;
const float newGainStep{newparams->GainStep};
ASSUME(BufferSize > 0);
size_t Delay[2]{
HRTF_HISTORY_LENGTH - oldparams->Delay[0],
HRTF_HISTORY_LENGTH - oldparams->Delay[1] };
float stepcount{0.0f};
for(size_t i{0u};i < BufferSize;++i)
{
const float g{oldGain + oldGainStep*stepcount};
const float left{InSamples[Delay[0]++] * g};
const float right{InSamples[Delay[1]++] * g};
ApplyCoeffs(AccumSamples+i, IrSize, OldCoeffs, left, right);
stepcount += 1.0f;
}
Delay[0] = HRTF_HISTORY_LENGTH - newparams->Delay[0];
Delay[1] = HRTF_HISTORY_LENGTH - newparams->Delay[1];
stepcount = 0.0f;
for(size_t i{0u};i < BufferSize;++i)
{
const float g{newGainStep*stepcount};
const float left{InSamples[Delay[0]++] * g};
const float right{InSamples[Delay[1]++] * g};
ApplyCoeffs(AccumSamples+i, IrSize, NewCoeffs, left, right);
stepcount += 1.0f;
}
}
template<ApplyCoeffsT ApplyCoeffs>
inline void MixDirectHrtfBase(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
const al::span<const FloatBufferLine> InSamples, float2 *RESTRICT AccumSamples,
DirectHrtfState *State, const size_t BufferSize)
{
ASSUME(BufferSize > 0);
const uint_fast32_t IrSize{State->mIrSize};
auto chan_iter = State->mChannels.begin();
for(const FloatBufferLine &input : InSamples)
{
/* For dual-band processing, the signal needs extra scaling applied to
* the high frequency response. The band-splitter alone creates a
* frequency-dependent phase shift, which is not ideal. To counteract
* it, combine it with a backwards phase-shift.
*/
/* Load the input signal backwards, into a temp buffer with delay
* padding. The delay serves to reduce the error caused by IIR filter's
* phase shift on a partial input.
*/
al::span<float> tempbuf{State->mTemp.data(), HRTF_DIRECT_DELAY+BufferSize};
auto tmpiter = std::reverse_copy(input.begin(), input.begin()+BufferSize, tempbuf.begin());
std::copy(chan_iter->mDelay.cbegin(), chan_iter->mDelay.cend(), tmpiter);
/* Save the unfiltered newest input samples for next time. */
std::copy_n(tempbuf.begin(), HRTF_DIRECT_DELAY, chan_iter->mDelay.begin());
/* Apply the all-pass on the reversed signal and reverse the resulting
* sample array. This produces the forward response with a backwards
* phase shift (+n degrees becomes -n degrees).
*/
chan_iter->mSplitter.applyAllpass(tempbuf);
tempbuf = tempbuf.subspan<HRTF_DIRECT_DELAY>();
std::reverse(tempbuf.begin(), tempbuf.end());
/* Now apply the band-splitter. This applies the normal phase shift,
* which cancels out with the backwards phase shift to get the original
* phase on the split signal.
*/
chan_iter->mSplitter.applyHfScale(tempbuf, chan_iter->mHfScale);
/* Now apply the HRIR coefficients to this channel. */
const auto &Coeffs = chan_iter->mCoeffs;
++chan_iter;
for(size_t i{0u};i < BufferSize;++i)
{
const float insample{tempbuf[i]};
ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, insample, insample);
}
}
/* Apply a delay to the existing signal to align with the input delay. */
auto &ldelay = State->mLeftDelay;
auto &rdelay = State->mRightDelay;
if LIKELY(BufferSize >= HRTF_DIRECT_DELAY)
{
auto buffer_end = LeftOut.begin() + BufferSize;
auto delay_end = std::rotate(LeftOut.begin(), buffer_end - HRTF_DIRECT_DELAY, buffer_end);
std::swap_ranges(LeftOut.begin(), delay_end, ldelay.begin());
buffer_end = RightOut.begin() + BufferSize;
delay_end = std::rotate(RightOut.begin(), buffer_end - HRTF_DIRECT_DELAY, buffer_end);
std::swap_ranges(RightOut.begin(), delay_end, rdelay.begin());
}
else
{
auto buffer_end = LeftOut.begin() + BufferSize;
auto delay_start = std::swap_ranges(LeftOut.begin(), buffer_end, ldelay.begin());
std::rotate(ldelay.begin(), delay_start, ldelay.end());
buffer_end = RightOut.begin() + BufferSize;
delay_start = std::swap_ranges(RightOut.begin(), buffer_end, rdelay.begin());
std::rotate(rdelay.begin(), delay_start, rdelay.end());
}
for(size_t i{0u};i < BufferSize;++i)
LeftOut[i] += AccumSamples[i][0];
for(size_t i{0u};i < BufferSize;++i)
RightOut[i] += AccumSamples[i][1];
/* Copy the new in-progress accumulation values to the front and clear the
* following samples for the next mix.
*/
auto accum_iter = std::copy_n(AccumSamples+BufferSize, HRIR_LENGTH, AccumSamples);
std::fill_n(accum_iter, BufferSize, float2{});
}
#endif /* MIXER_HRTFBASE_H */
|