Fix the reverb panning behavior to better fit the spec
[openal-soft.git] / Alc / mixer_c.c
blob2346080ad019af1ac64ae8d51f709d734ee24600
1 #include "config.h"
3 #include <assert.h>
5 #include "alMain.h"
6 #include "alu.h"
7 #include "alSource.h"
8 #include "alAuxEffectSlot.h"
11 static inline ALfloat do_point(const ALfloat *restrict vals, ALsizei UNUSED(frac))
12 { return vals[0]; }
13 static inline ALfloat do_lerp(const ALfloat *restrict vals, ALsizei frac)
14 { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
15 static inline ALfloat do_cubic(const ALfloat *restrict vals, ALsizei frac)
16 { return cubic(vals[0], vals[1], vals[2], vals[3], frac * (1.0f/FRACTIONONE)); }
18 const ALfloat *Resample_copy_C(const InterpState* UNUSED(state),
19 const ALfloat *restrict src, ALsizei UNUSED(frac), ALint UNUSED(increment),
20 ALfloat *restrict dst, ALsizei numsamples)
22 #if defined(HAVE_SSE) || defined(HAVE_NEON)
23 /* Avoid copying the source data if it's aligned like the destination. */
24 if((((intptr_t)src)&15) == (((intptr_t)dst)&15))
25 return src;
26 #endif
27 memcpy(dst, src, numsamples*sizeof(ALfloat));
28 return dst;
31 #define DECL_TEMPLATE(Tag, Sampler, O) \
32 const ALfloat *Resample_##Tag##_C(const InterpState* UNUSED(state), \
33 const ALfloat *restrict src, ALsizei frac, ALint increment, \
34 ALfloat *restrict dst, ALsizei numsamples) \
35 { \
36 ALsizei i; \
38 src -= O; \
39 for(i = 0;i < numsamples;i++) \
40 { \
41 dst[i] = Sampler(src, frac); \
43 frac += increment; \
44 src += frac>>FRACTIONBITS; \
45 frac &= FRACTIONMASK; \
46 } \
47 return dst; \
50 DECL_TEMPLATE(point, do_point, 0)
51 DECL_TEMPLATE(lerp, do_lerp, 0)
52 DECL_TEMPLATE(cubic, do_cubic, 1)
54 #undef DECL_TEMPLATE
56 const ALfloat *Resample_bsinc_C(const InterpState *state, const ALfloat *restrict src,
57 ALsizei frac, ALint increment, ALfloat *restrict dst,
58 ALsizei dstlen)
60 const ALfloat *fil, *scd, *phd, *spd;
61 const ALfloat *const filter = state->bsinc.filter;
62 const ALfloat sf = state->bsinc.sf;
63 const ALsizei m = state->bsinc.m;
64 ALsizei j_f, pi, i;
65 ALfloat pf, r;
67 src += state->bsinc.l;
68 for(i = 0;i < dstlen;i++)
70 // Calculate the phase index and factor.
71 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
72 pi = frac >> FRAC_PHASE_BITDIFF;
73 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
74 #undef FRAC_PHASE_BITDIFF
76 fil = ASSUME_ALIGNED(filter + m*pi*4, 16);
77 scd = ASSUME_ALIGNED(fil + m, 16);
78 phd = ASSUME_ALIGNED(scd + m, 16);
79 spd = ASSUME_ALIGNED(phd + m, 16);
81 // Apply the scale and phase interpolated filter.
82 r = 0.0f;
83 for(j_f = 0;j_f < m;j_f++)
84 r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) * src[j_f];
85 dst[i] = r;
87 frac += increment;
88 src += frac>>FRACTIONBITS;
89 frac &= FRACTIONMASK;
91 return dst;
95 void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALsizei numsamples)
97 ALsizei i;
98 if(LIKELY(numsamples > 1))
100 ALfloat x0 = filter->x[0];
101 ALfloat x1 = filter->x[1];
102 ALfloat y0 = filter->y[0];
103 ALfloat y1 = filter->y[1];
105 for(i = 0;i < numsamples;i++)
107 dst[i] = filter->b0* src[i] +
108 filter->b1*x0 + filter->b2*x1 -
109 filter->a1*y0 - filter->a2*y1;
110 y1 = y0; y0 = dst[i];
111 x1 = x0; x0 = src[i];
114 filter->x[0] = x0;
115 filter->x[1] = x1;
116 filter->y[0] = y0;
117 filter->y[1] = y1;
119 else if(numsamples == 1)
121 dst[0] = filter->b0 * src[0] +
122 filter->b1 * filter->x[0] +
123 filter->b2 * filter->x[1] -
124 filter->a1 * filter->y[0] -
125 filter->a2 * filter->y[1];
126 filter->x[1] = filter->x[0];
127 filter->x[0] = src[0];
128 filter->y[1] = filter->y[0];
129 filter->y[0] = dst[0];
134 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
135 const ALsizei IrSize,
136 const ALfloat (*restrict Coeffs)[2],
137 ALfloat left, ALfloat right)
139 ALsizei c;
140 for(c = 0;c < IrSize;c++)
142 const ALsizei off = (Offset+c)&HRIR_MASK;
143 Values[off][0] += Coeffs[c][0] * left;
144 Values[off][1] += Coeffs[c][1] * right;
148 #define MixHrtf MixHrtf_C
149 #define MixHrtfBlend MixHrtfBlend_C
150 #define MixDirectHrtf MixDirectHrtf_C
151 #include "mixer_inc.c"
152 #undef MixHrtf
155 void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
156 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
157 ALsizei BufferSize)
159 ALfloat gain, delta, step;
160 ALsizei c;
162 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
164 for(c = 0;c < OutChans;c++)
166 ALsizei pos = 0;
167 gain = CurrentGains[c];
168 step = (TargetGains[c] - gain) * delta;
169 if(fabsf(step) > FLT_EPSILON)
171 ALsizei minsize = mini(BufferSize, Counter);
172 for(;pos < minsize;pos++)
174 OutBuffer[c][OutPos+pos] += data[pos]*gain;
175 gain += step;
177 if(pos == Counter)
178 gain = TargetGains[c];
179 CurrentGains[c] = gain;
182 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
183 continue;
184 for(;pos < BufferSize;pos++)
185 OutBuffer[c][OutPos+pos] += data[pos]*gain;
189 /* Basically the inverse of the above. Rather than one input going to multiple
190 * outputs (each with its own gain), it's multiple inputs (each with its own
191 * gain) going to one output. This applies one row (vs one column) of a matrix
192 * transform. And as the matrices are more or less static once set up, no
193 * stepping is necessary.
195 void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
197 ALsizei c, i;
199 for(c = 0;c < InChans;c++)
201 ALfloat gain = Gains[c];
202 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
203 continue;
205 for(i = 0;i < BufferSize;i++)
206 OutBuffer[i] += data[c][InPos+i] * gain;