Use a macro to reduce code duplication
[openal-soft.git] / Alc / mixer_inc.c
blobb4635b43f0619c49152139ee2b7e75f1f0e06b12
1 #include "config.h"
3 #include "alMain.h"
4 #include "alSource.h"
6 #include "hrtf.h"
7 #include "mixer_defs.h"
8 #include "align.h"
11 #define REAL_MERGE(a,b) a##b
12 #define MERGE(a,b) REAL_MERGE(a,b)
14 #define MixHrtf MERGE(MixHrtf_,SUFFIX)
17 static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
18 const ALuint irSize,
19 ALfloat (*restrict Coeffs)[2],
20 const ALfloat (*restrict CoeffStep)[2],
21 ALfloat left, ALfloat right);
22 static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
23 const ALuint irSize,
24 ALfloat (*restrict Coeffs)[2],
25 ALfloat left, ALfloat right);
28 void MixHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
29 ALuint Counter, ALuint Offset, ALuint OutPos, const ALuint IrSize,
30 const HrtfParams *hrtfparams, HrtfState *hrtfstate, ALuint BufferSize)
32 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
33 ALuint Delay[2];
34 ALfloat left, right;
35 ALuint pos;
36 ALuint c;
38 for(c = 0;c < IrSize;c++)
40 Coeffs[c][0] = hrtfparams->Coeffs[c][0] - (hrtfparams->CoeffStep[c][0]*Counter);
41 Coeffs[c][1] = hrtfparams->Coeffs[c][1] - (hrtfparams->CoeffStep[c][1]*Counter);
43 Delay[0] = hrtfparams->Delay[0] - (hrtfparams->DelayStep[0]*Counter);
44 Delay[1] = hrtfparams->Delay[1] - (hrtfparams->DelayStep[1]*Counter);
46 pos = 0;
47 for(;pos < BufferSize && pos < Counter;pos++)
49 hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos];
50 left = lerp(hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
51 hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
52 (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
53 right = lerp(hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
54 hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
55 (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
57 Delay[0] += hrtfparams->DelayStep[0];
58 Delay[1] += hrtfparams->DelayStep[1];
60 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
61 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
62 Offset++;
64 ApplyCoeffsStep(Offset, hrtfstate->Values, IrSize, Coeffs, hrtfparams->CoeffStep, left, right);
65 OutBuffer[0][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][0];
66 OutBuffer[1][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][1];
67 OutPos++;
70 Delay[0] >>= HRTFDELAY_BITS;
71 Delay[1] >>= HRTFDELAY_BITS;
72 for(;pos < BufferSize;pos++)
74 hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos];
75 left = hrtfstate->History[(Offset-Delay[0])&HRTF_HISTORY_MASK];
76 right = hrtfstate->History[(Offset-Delay[1])&HRTF_HISTORY_MASK];
78 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
79 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
80 Offset++;
82 ApplyCoeffs(Offset, hrtfstate->Values, IrSize, Coeffs, left, right);
83 OutBuffer[0][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][0];
84 OutBuffer[1][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][1];
85 OutPos++;
90 #undef MixHrtf
92 #undef MERGE
93 #undef REAL_MERGE