-
Notifications
You must be signed in to change notification settings - Fork 515
/
hrtfbase.h
140 lines (118 loc) · 5.31 KB
/
hrtfbase.h
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
#ifndef CORE_MIXER_HRTFBASE_H
#define CORE_MIXER_HRTFBASE_H
#include <algorithm>
#include <cmath>
#include "defs.h"
#include "hrtfdefs.h"
#include "opthelpers.h"
using uint = unsigned int;
using ApplyCoeffsT = void(const al::span<float2> Values, const size_t irSize,
const ConstHrirSpan Coeffs, const float left, const float right);
template<ApplyCoeffsT ApplyCoeffs>
inline void MixHrtfBase(const al::span<const float> InSamples, const al::span<float2> AccumSamples,
const size_t IrSize, const MixHrtfFilter *hrtfparams, const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);
ASSUME(SamplesToDo <= BufferLineSize);
ASSUME(IrSize <= HrirLength);
const ConstHrirSpan Coeffs{hrtfparams->Coeffs};
const float gainstep{hrtfparams->GainStep};
const float gain{hrtfparams->Gain};
size_t ldelay{HrtfHistoryLength - hrtfparams->Delay[0]};
size_t rdelay{HrtfHistoryLength - hrtfparams->Delay[1]};
float stepcount{0.0f};
for(size_t i{0u};i < SamplesToDo;++i)
{
const float g{gain + gainstep*stepcount};
const float left{InSamples[ldelay++] * g};
const float right{InSamples[rdelay++] * g};
ApplyCoeffs(AccumSamples.subspan(i), IrSize, Coeffs, left, right);
stepcount += 1.0f;
}
}
template<ApplyCoeffsT ApplyCoeffs>
inline void MixHrtfBlendBase(const al::span<const float> InSamples,
const al::span<float2> AccumSamples, const size_t IrSize, const HrtfFilter *oldparams,
const MixHrtfFilter *newparams, const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);
ASSUME(SamplesToDo <= BufferLineSize);
ASSUME(IrSize <= HrirLength);
const ConstHrirSpan OldCoeffs{oldparams->Coeffs};
const float oldGainStep{oldparams->Gain / static_cast<float>(SamplesToDo)};
const ConstHrirSpan NewCoeffs{newparams->Coeffs};
const float newGainStep{newparams->GainStep};
if(oldparams->Gain > GainSilenceThreshold) LIKELY
{
size_t ldelay{HrtfHistoryLength - oldparams->Delay[0]};
size_t rdelay{HrtfHistoryLength - oldparams->Delay[1]};
auto stepcount = static_cast<float>(SamplesToDo);
for(size_t i{0u};i < SamplesToDo;++i)
{
const float g{oldGainStep*stepcount};
const float left{InSamples[ldelay++] * g};
const float right{InSamples[rdelay++] * g};
ApplyCoeffs(AccumSamples.subspan(i), IrSize, OldCoeffs, left, right);
stepcount -= 1.0f;
}
}
if(newGainStep*static_cast<float>(SamplesToDo) > GainSilenceThreshold) LIKELY
{
size_t ldelay{HrtfHistoryLength+1 - newparams->Delay[0]};
size_t rdelay{HrtfHistoryLength+1 - newparams->Delay[1]};
float stepcount{1.0f};
for(size_t i{1u};i < SamplesToDo;++i)
{
const float g{newGainStep*stepcount};
const float left{InSamples[ldelay++] * g};
const float right{InSamples[rdelay++] * g};
ApplyCoeffs(AccumSamples.subspan(i), IrSize, NewCoeffs, left, right);
stepcount += 1.0f;
}
}
}
template<ApplyCoeffsT ApplyCoeffs>
inline void MixDirectHrtfBase(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
const al::span<const FloatBufferLine> InSamples, const al::span<float2> AccumSamples,
const al::span<float,BufferLineSize> TempBuf, const al::span<HrtfChannelState> ChannelState,
const size_t IrSize, const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);
ASSUME(SamplesToDo <= BufferLineSize);
ASSUME(IrSize <= HrirLength);
assert(ChannelState.size() == InSamples.size());
auto ChanState = ChannelState.begin();
for(const FloatBufferLine &input : InSamples)
{
/* For dual-band processing, the signal needs extra scaling applied to
* the high frequency response. The band-splitter applies this scaling
* with a consistent phase shift regardless of the scale amount.
*/
ChanState->mSplitter.processHfScale(al::span{input}.first(SamplesToDo), TempBuf,
ChanState->mHfScale);
/* Now apply the HRIR coefficients to this channel. */
const ConstHrirSpan Coeffs{ChanState->mCoeffs};
for(size_t i{0u};i < SamplesToDo;++i)
{
const float insample{TempBuf[i]};
ApplyCoeffs(AccumSamples.subspan(i), IrSize, Coeffs, insample, insample);
}
++ChanState;
}
/* Add the HRTF signal to the existing "direct" signal. */
const auto left = al::span{al::assume_aligned<16>(LeftOut.data()), SamplesToDo};
std::transform(left.cbegin(), left.cend(), AccumSamples.cbegin(), left.begin(),
[](const float sample, const float2 &accum) noexcept -> float
{ return sample + accum[0]; });
const auto right = al::span{al::assume_aligned<16>(RightOut.data()), SamplesToDo};
std::transform(right.cbegin(), right.cend(), AccumSamples.cbegin(), right.begin(),
[](const float sample, const float2 &accum) noexcept -> float
{ return sample + accum[1]; });
/* Copy the new in-progress accumulation values to the front and clear the
* following samples for the next mix.
*/
const auto accum_inprog = AccumSamples.subspan(SamplesToDo, HrirLength);
auto accum_iter = std::copy(accum_inprog.cbegin(), accum_inprog.cend(), AccumSamples.begin());
std::fill_n(accum_iter, SamplesToDo, float2{});
}
#endif /* CORE_MIXER_HRTFBASE_H */