aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/mixer/hrtfbase.h
blob: 8ad4a99c3cac6b7986de39f45386f6cd6d7f8e8b (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
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
171
172
173
174
175
176
177
178
179
#ifndef MIXER_HRTFBASE_H
#define MIXER_HRTFBASE_H

#include "alu.h"
#include "../hrtf.h"
#include "opthelpers.h"


using ApplyCoeffsT = void(ALsizei Offset, HrirArray<ALfloat> &Values, const ALsizei irSize,
    const HrirArray<ALfloat> &Coeffs, const ALfloat left, const ALfloat right);

template<ApplyCoeffsT &ApplyCoeffs>
inline void MixHrtfBase(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut, const ALfloat *data,
    ALsizei Offset, const ALsizei OutPos, const ALsizei IrSize, MixHrtfParams *hrtfparams,
    HrtfState *hrtfstate, const ALsizei BufferSize)
{
    ASSUME(OutPos >= 0);
    ASSUME(IrSize >= 4);
    ASSUME(BufferSize > 0);

    const auto &Coeffs = *hrtfparams->Coeffs;
    const ALfloat gainstep{hrtfparams->GainStep};
    const ALfloat gain{hrtfparams->Gain};
    ALfloat stepcount{0.0f};

    ALsizei Delay[2]{
        HRTF_HISTORY_LENGTH - hrtfparams->Delay[0],
        HRTF_HISTORY_LENGTH - hrtfparams->Delay[1] };
    ASSUME(Delay[0] >= 0 && Delay[1] >= 0);

    Offset &= HRIR_MASK;
    ALsizei HeadOffset{(Offset+IrSize-1)&HRIR_MASK};

    LeftOut  += OutPos;
    RightOut += OutPos;
    for(ALsizei i{0};i < BufferSize;)
    {
        /* Calculate the number of samples we can do until one of the indices
         * wraps on its buffer, or we reach the end.
         */
        const ALsizei todo_hrir{HRIR_LENGTH - maxi(HeadOffset, Offset)};
        const ALsizei todo{mini(BufferSize-i, todo_hrir) + i};
        ASSUME(todo > i);

        for(;i < todo;++i)
        {
            hrtfstate->Values[HeadOffset][0] = 0.0f;
            hrtfstate->Values[HeadOffset][1] = 0.0f;
            ++HeadOffset;

            const ALfloat g{gain + gainstep*stepcount};
            const ALfloat left{data[Delay[0]++] * g};
            const ALfloat right{data[Delay[1]++] * g};
            ApplyCoeffs(Offset, hrtfstate->Values, IrSize, Coeffs, left, right);

            *(LeftOut++)  += hrtfstate->Values[Offset][0];
            *(RightOut++) += hrtfstate->Values[Offset][1];
            ++Offset;

            stepcount += 1.0f;
        }

        HeadOffset &= HRIR_MASK;
        Offset &= HRIR_MASK;
    }
    hrtfparams->Gain = gain + gainstep*stepcount;
}

template<ApplyCoeffsT &ApplyCoeffs>
inline void MixHrtfBlendBase(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
    const ALfloat *data, ALsizei Offset, const ALsizei OutPos, const ALsizei IrSize,
    const HrtfParams *oldparams, MixHrtfParams *newparams, HrtfState *hrtfstate,
    const ALsizei BufferSize)
{
    const auto &OldCoeffs = oldparams->Coeffs;
    const ALfloat oldGain{oldparams->Gain};
    const ALfloat oldGainStep{-oldGain / static_cast<ALfloat>(BufferSize)};
    const auto &NewCoeffs = *newparams->Coeffs;
    const ALfloat newGainStep{newparams->GainStep};
    ALfloat stepcount{0.0f};

    ASSUME(OutPos >= 0);
    ASSUME(IrSize >= 4);
    ASSUME(BufferSize > 0);

    ALsizei OldDelay[2]{
        HRTF_HISTORY_LENGTH - oldparams->Delay[0],
        HRTF_HISTORY_LENGTH - oldparams->Delay[1] };
    ASSUME(OldDelay[0] >= 0 && OldDelay[1] >= 0);
    ALsizei NewDelay[2]{
        HRTF_HISTORY_LENGTH - newparams->Delay[0],
        HRTF_HISTORY_LENGTH - newparams->Delay[1] };
    ASSUME(NewDelay[0] >= 0 && NewDelay[1] >= 0);

    Offset &= HRIR_MASK;
    ALsizei HeadOffset{(Offset+IrSize-1)&HRIR_MASK};

    LeftOut  += OutPos;
    RightOut += OutPos;
    for(ALsizei i{0};i < BufferSize;)
    {
        const ALsizei todo_hrir{HRIR_LENGTH - maxi(HeadOffset, Offset)};
        const ALsizei todo{mini(BufferSize-i, todo_hrir) + i};
        ASSUME(todo > i);

        for(;i < todo;++i)
        {
            hrtfstate->Values[HeadOffset][0] = 0.0f;
            hrtfstate->Values[HeadOffset][1] = 0.0f;
            ++HeadOffset;

            ALfloat g{oldGain + oldGainStep*stepcount};
            ALfloat left{data[OldDelay[0]++] * g};
            ALfloat right{data[OldDelay[1]++] * g};
            ApplyCoeffs(Offset, hrtfstate->Values, IrSize, OldCoeffs, left, right);

            g = newGainStep*stepcount;
            left = data[NewDelay[0]++] * g;
            right = data[NewDelay[1]++] * g;
            ApplyCoeffs(Offset, hrtfstate->Values, IrSize, NewCoeffs, left, right);

            *(LeftOut++)  += hrtfstate->Values[Offset][0];
            *(RightOut++) += hrtfstate->Values[Offset][1];
            ++Offset;

            stepcount += 1.0f;
        }

        HeadOffset &= HRIR_MASK;
        Offset &= HRIR_MASK;
    }
    newparams->Gain = newGainStep*stepcount;
}

template<ApplyCoeffsT &ApplyCoeffs>
inline void MixDirectHrtfBase(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
    const ALfloat (*data)[BUFFERSIZE], DirectHrtfState *State, const ALsizei NumChans,
    const ALsizei BufferSize)
{
    ASSUME(NumChans > 0);
    ASSUME(BufferSize > 0);

    const ALsizei IrSize{State->IrSize};
    ASSUME(IrSize >= 4);

    for(ALsizei c{0};c < NumChans;++c)
    {
        const ALfloat (&input)[BUFFERSIZE] = data[c];
        const auto &Coeffs = State->Chan[c].Coeffs;
        auto &Values = State->Chan[c].Values;
        ALsizei Offset{State->Offset&HRIR_MASK};

        ALsizei HeadOffset{(Offset+IrSize-1)&HRIR_MASK};
        for(ALsizei i{0};i < BufferSize;)
        {
            const ALsizei todo_hrir{HRIR_LENGTH - maxi(HeadOffset, Offset)};
            const ALsizei todo{mini(BufferSize-i, todo_hrir) + i};
            ASSUME(todo > i);

            for(;i < todo;++i)
            {
                Values[HeadOffset][0] = 0.0f;
                Values[HeadOffset][1] = 0.0f;
                ++HeadOffset;

                const ALfloat insample{input[i]};
                ApplyCoeffs(Offset, Values, IrSize, Coeffs, insample, insample);

                LeftOut[i]  += Values[Offset][0];
                RightOut[i] += Values[Offset][1];
                ++Offset;
            }
            HeadOffset &= HRIR_MASK;
            Offset &= HRIR_MASK;
        }
    }
}

#endif /* MIXER_HRTFBASE_H */