Properly check for struct timespec
[openal-soft.git] / Alc / mixer_inc.c
blob25dc2b588df45fcee1d87171b7eb803ab81518be
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"
9 #include "alu.h"
12 #define MAX_UPDATE_SAMPLES 128
15 static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
16 const ALuint irSize,
17 ALfloat (*restrict Coeffs)[2],
18 const ALfloat (*restrict CoeffStep)[2],
19 ALfloat left, ALfloat right);
20 static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
21 const ALuint irSize,
22 ALfloat (*restrict Coeffs)[2],
23 ALfloat left, ALfloat right);
26 void MixHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], ALuint lidx, ALuint ridx,
27 const ALfloat *data, ALuint Counter, ALuint Offset, ALuint OutPos,
28 const ALuint IrSize, const MixHrtfParams *hrtfparams, HrtfState *hrtfstate,
29 ALuint BufferSize)
31 ALfloat (*Coeffs)[2] = hrtfparams->Current->Coeffs;
32 ALuint Delay[2] = { hrtfparams->Current->Delay[0], hrtfparams->Current->Delay[1] };
33 ALfloat out[MAX_UPDATE_SAMPLES][2];
34 ALfloat left, right;
35 ALuint minsize;
36 ALuint pos, i;
38 pos = 0;
39 if(Counter == 0)
40 goto skip_stepping;
42 minsize = minu(BufferSize, Counter);
43 while(pos < minsize)
45 ALuint todo = minu(minsize-pos, MAX_UPDATE_SAMPLES);
47 for(i = 0;i < todo;i++)
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->Steps.Delay[0];
58 Delay[1] += hrtfparams->Steps.Delay[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->Steps.Coeffs, left, right);
65 out[i][0] = hrtfstate->Values[Offset&HRIR_MASK][0];
66 out[i][1] = hrtfstate->Values[Offset&HRIR_MASK][1];
69 for(i = 0;i < todo;i++)
70 OutBuffer[lidx][OutPos+i] += out[i][0];
71 for(i = 0;i < todo;i++)
72 OutBuffer[ridx][OutPos+i] += out[i][1];
73 OutPos += todo;
76 if(pos == Counter)
78 *hrtfparams->Current = *hrtfparams->Target;
79 Delay[0] = hrtfparams->Target->Delay[0];
80 Delay[1] = hrtfparams->Target->Delay[1];
82 else
84 hrtfparams->Current->Delay[0] = Delay[0];
85 hrtfparams->Current->Delay[1] = Delay[1];
88 skip_stepping:
89 Delay[0] >>= HRTFDELAY_BITS;
90 Delay[1] >>= HRTFDELAY_BITS;
91 while(pos < BufferSize)
93 ALuint todo = minu(BufferSize-pos, MAX_UPDATE_SAMPLES);
95 for(i = 0;i < todo;i++)
97 hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos++];
98 left = hrtfstate->History[(Offset-Delay[0])&HRTF_HISTORY_MASK];
99 right = hrtfstate->History[(Offset-Delay[1])&HRTF_HISTORY_MASK];
101 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
102 hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
103 Offset++;
105 ApplyCoeffs(Offset, hrtfstate->Values, IrSize, Coeffs, left, right);
106 out[i][0] = hrtfstate->Values[Offset&HRIR_MASK][0];
107 out[i][1] = hrtfstate->Values[Offset&HRIR_MASK][1];
110 for(i = 0;i < todo;i++)
111 OutBuffer[lidx][OutPos+i] += out[i][0];
112 for(i = 0;i < todo;i++)
113 OutBuffer[ridx][OutPos+i] += out[i][1];
114 OutPos += todo;
118 void MixDirectHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], ALuint lidx, ALuint ridx,
119 const ALfloat *data, ALuint Offset, const ALuint IrSize,
120 ALfloat (*restrict Coeffs)[2], ALfloat (*restrict Values)[2],
121 ALuint BufferSize)
123 ALfloat out[MAX_UPDATE_SAMPLES][2];
124 ALfloat insample;
125 ALuint pos, i;
127 for(pos = 0;pos < BufferSize;)
129 ALuint todo = minu(BufferSize-pos, MAX_UPDATE_SAMPLES);
131 for(i = 0;i < todo;i++)
133 Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
134 Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
135 Offset++;
137 insample = *(data++);
138 ApplyCoeffs(Offset, Values, IrSize, Coeffs, insample, insample);
139 out[i][0] = Values[Offset&HRIR_MASK][0];
140 out[i][1] = Values[Offset&HRIR_MASK][1];
143 for(i = 0;i < todo;i++)
144 OutBuffer[lidx][pos+i] += out[i][0];
145 for(i = 0;i < todo;i++)
146 OutBuffer[ridx][pos+i] += out[i][1];
147 pos += todo;