aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/mixer_inc.c
blob: 3e9d7f465c23fc2a441747e9dfb96d95b03b0379 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "config.h"

#include "AL/alc.h"
#include "AL/al.h"
#include "alMain.h"
#include "alSource.h"
#include "alAuxEffectSlot.h"
#include "mixer_defs.h"

#ifdef __GNUC__
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif

#define REAL_MERGE2(a,b) a##b
#define MERGE2(a,b) REAL_MERGE2(a,b)
#define REAL_MERGE4(a,b,c,d) a##b##c##d
#define MERGE4(a,b,c,d) REAL_MERGE4(a,b,c,d)


void MERGE4(MixDirect_Hrtf_,SAMPLER,_,SUFFIX)(
  ALsource *Source, ALCdevice *Device, DirectParams *params,
  const ALfloat *RESTRICT data, ALuint srcfrac,
  ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize)
{
    const ALuint NumChannels = Source->NumChannels;
    const ALint *RESTRICT DelayStep = params->Hrtf.DelayStep;
    ALfloat (*RESTRICT DryBuffer)[MaxChannels];
    ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks;
    ALfloat (*RESTRICT CoeffStep)[2] = params->Hrtf.CoeffStep;
    ALuint pos, frac;
    FILTER *DryFilter;
    ALuint BufferIdx;
    ALuint increment;
    ALfloat value;
    ALuint i,  c;

    increment = Source->Params.Step;

    DryBuffer = Device->DryBuffer;
    ClickRemoval = Device->ClickRemoval;
    PendingClicks = Device->PendingClicks;
    DryFilter = &params->iirFilter;

    for(i = 0;i < NumChannels;i++)
    {
        ALfloat (*RESTRICT TargetCoeffs)[2] = params->Hrtf.Coeffs[i];
        ALuint *RESTRICT TargetDelay = params->Hrtf.Delay[i];
        ALfloat *RESTRICT History = Source->Hrtf.History[i];
        ALfloat (*RESTRICT Values)[2] = Source->Hrtf.Values[i];
        ALint Counter = maxu(Source->Hrtf.Counter, OutPos) - OutPos;
        ALuint Offset = Source->Hrtf.Offset + OutPos;
        ALfloat Coeffs[HRIR_LENGTH][2] ALIGN(16);
        ALuint Delay[2];
        ALfloat left, right;

        pos = 0;
        frac = srcfrac;

        for(c = 0;c < HRIR_LENGTH;c++)
        {
            Coeffs[c][0] = TargetCoeffs[c][0] - (CoeffStep[c][0]*Counter);
            Coeffs[c][1] = TargetCoeffs[c][1] - (CoeffStep[c][1]*Counter);
        }

        Delay[0] = TargetDelay[0] - (DelayStep[0]*Counter);
        Delay[1] = TargetDelay[1] - (DelayStep[1]*Counter);

        if(LIKELY(OutPos == 0))
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);
            value = lpFilter2PC(DryFilter, i, value);

            History[Offset&SRC_HISTORY_MASK] = value;
            left  = lerp(History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
                         History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
                         (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
            right = lerp(History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
                         History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
                         (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));

            ClickRemoval[FrontLeft]  -= Values[(Offset+1)&HRIR_MASK][0] +
                                        Coeffs[0][0] * left;
            ClickRemoval[FrontRight] -= Values[(Offset+1)&HRIR_MASK][1] +
                                        Coeffs[0][1] * right;
        }
        for(BufferIdx = 0;BufferIdx < BufferSize && Counter > 0;BufferIdx++)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);
            value = lpFilter2P(DryFilter, i, value);

            History[Offset&SRC_HISTORY_MASK] = value;
            left  = lerp(History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
                         History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
                         (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
            right = lerp(History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
                         History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
                         (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));

            Delay[0] += DelayStep[0];
            Delay[1] += DelayStep[1];

            Values[Offset&HRIR_MASK][0] = 0.0f;
            Values[Offset&HRIR_MASK][1] = 0.0f;
            Offset++;

            ApplyCoeffsStep(Offset, Values, Coeffs, CoeffStep, left, right);
            DryBuffer[OutPos][FrontLeft]  += Values[Offset&HRIR_MASK][0];
            DryBuffer[OutPos][FrontRight] += Values[Offset&HRIR_MASK][1];

            frac += increment;
            pos  += frac>>FRACTIONBITS;
            frac &= FRACTIONMASK;
            OutPos++;
            Counter--;
        }

        Delay[0] >>= HRTFDELAY_BITS;
        Delay[1] >>= HRTFDELAY_BITS;
        for(;BufferIdx < BufferSize;BufferIdx++)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);
            value = lpFilter2P(DryFilter, i, value);

            History[Offset&SRC_HISTORY_MASK] = value;
            left = History[(Offset-Delay[0])&SRC_HISTORY_MASK];
            right = History[(Offset-Delay[1])&SRC_HISTORY_MASK];

            Values[Offset&HRIR_MASK][0] = 0.0f;
            Values[Offset&HRIR_MASK][1] = 0.0f;
            Offset++;

            ApplyCoeffs(Offset, Values, Coeffs, left, right);
            DryBuffer[OutPos][FrontLeft]  += Values[Offset&HRIR_MASK][0];
            DryBuffer[OutPos][FrontRight] += Values[Offset&HRIR_MASK][1];

            frac += increment;
            pos  += frac>>FRACTIONBITS;
            frac &= FRACTIONMASK;
            OutPos++;
        }
        if(LIKELY(OutPos == SamplesToDo))
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);
            value = lpFilter2PC(DryFilter, i, value);

            History[Offset&SRC_HISTORY_MASK] = value;
            left = History[(Offset-Delay[0])&SRC_HISTORY_MASK];
            right = History[(Offset-Delay[1])&SRC_HISTORY_MASK];

            PendingClicks[FrontLeft]  += Values[(Offset+1)&HRIR_MASK][0] +
                                         Coeffs[0][0] * left;
            PendingClicks[FrontRight] += Values[(Offset+1)&HRIR_MASK][1] +
                                         Coeffs[0][1] * right;
        }
        OutPos -= BufferSize;
    }
}


void MERGE4(MixDirect_,SAMPLER,_,SUFFIX)(
  ALsource *Source, ALCdevice *Device, DirectParams *params,
  const ALfloat *RESTRICT data, ALuint srcfrac,
  ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize)
{
    const ALuint NumChannels = Source->NumChannels;
    ALfloat (*RESTRICT DryBuffer)[MaxChannels];
    ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks;
    ALfloat DrySend[MaxChannels];
    FILTER *DryFilter;
    ALuint pos, frac;
    ALuint BufferIdx;
    ALuint increment;
    ALfloat value;
    ALuint i,  c;

    increment = Source->Params.Step;

    DryBuffer = Device->DryBuffer;
    ClickRemoval = Device->ClickRemoval;
    PendingClicks = Device->PendingClicks;
    DryFilter = &params->iirFilter;

    for(i = 0;i < NumChannels;i++)
    {
        for(c = 0;c < MaxChannels;c++)
            DrySend[c] = params->Gains[i][c];

        pos = 0;
        frac = srcfrac;

        if(OutPos == 0)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2PC(DryFilter, i, value);
            for(c = 0;c < MaxChannels;c++)
                ClickRemoval[c] -= value*DrySend[c];
        }
        for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2P(DryFilter, i, value);
            for(c = 0;c < MaxChannels;c++)
                DryBuffer[OutPos][c] += value*DrySend[c];

            frac += increment;
            pos  += frac>>FRACTIONBITS;
            frac &= FRACTIONMASK;
            OutPos++;
        }
        if(OutPos == SamplesToDo)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2PC(DryFilter, i, value);
            for(c = 0;c < MaxChannels;c++)
                PendingClicks[c] += value*DrySend[c];
        }
        OutPos -= BufferSize;
    }
}


void MERGE4(MixSend_,SAMPLER,_,SUFFIX)(
  ALsource *Source, ALuint sendidx, SendParams *params,
  const ALfloat *RESTRICT data, ALuint srcfrac,
  ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize)
{
    const ALuint NumChannels = Source->NumChannels;
    ALeffectslot *Slot;
    ALfloat  WetSend;
    ALfloat *WetBuffer;
    ALfloat *WetClickRemoval;
    ALfloat *WetPendingClicks;
    FILTER  *WetFilter;
    ALuint pos, frac;
    ALuint BufferIdx;
    ALuint increment;
    ALfloat value;
    ALuint i;

    increment = Source->Params.Step;

    Slot = Source->Params.Slot[sendidx];
    WetBuffer = Slot->WetBuffer;
    WetClickRemoval = Slot->ClickRemoval;
    WetPendingClicks = Slot->PendingClicks;
    WetFilter = &params->iirFilter;
    WetSend = params->Gain;

    for(i = 0;i < NumChannels;i++)
    {
        pos = 0;
        frac = srcfrac;

        if(OutPos == 0)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2PC(WetFilter, i, value);
            WetClickRemoval[0] -= value * WetSend;
        }
        for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2P(WetFilter, i, value);
            WetBuffer[OutPos] += value * WetSend;

            frac += increment;
            pos  += frac>>FRACTIONBITS;
            frac &= FRACTIONMASK;
            OutPos++;
        }
        if(OutPos == SamplesToDo)
        {
            value = SAMPLER(data + pos*NumChannels + i, NumChannels, frac);

            value = lpFilter2PC(WetFilter, i, value);
            WetPendingClicks[0] += value * WetSend;
        }
        OutPos -= BufferSize;
    }
}

#undef MERGE4
#undef REAL_MERGE4
#undef MERGE2
#undef REAL_MERGE2

#undef UNLIKELY
#undef LIKELY