Move the filter implementation to a separate directory
[openal-soft.git] / Alc / mixer / mixer_c.c
blobe40c2cadc54d4693356b08a7ecc5a30f5c9fba5d
1 #include "config.h"
3 #include <assert.h>
5 #include "alMain.h"
6 #include "alu.h"
7 #include "alSource.h"
8 #include "alAuxEffectSlot.h"
9 #include "defs.h"
12 static inline ALfloat do_point(const ALfloat *restrict vals, ALsizei UNUSED(frac))
13 { return vals[0]; }
14 static inline ALfloat do_lerp(const ALfloat *restrict vals, ALsizei frac)
15 { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
16 static inline ALfloat do_cubic(const ALfloat *restrict vals, ALsizei frac)
17 { return cubic(vals[0], vals[1], vals[2], vals[3], frac * (1.0f/FRACTIONONE)); }
19 const ALfloat *Resample_copy_C(const InterpState* UNUSED(state),
20 const ALfloat *restrict src, ALsizei UNUSED(frac), ALint UNUSED(increment),
21 ALfloat *restrict dst, ALsizei numsamples)
23 #if defined(HAVE_SSE) || defined(HAVE_NEON)
24 /* Avoid copying the source data if it's aligned like the destination. */
25 if((((intptr_t)src)&15) == (((intptr_t)dst)&15))
26 return src;
27 #endif
28 memcpy(dst, src, numsamples*sizeof(ALfloat));
29 return dst;
32 #define DECL_TEMPLATE(Tag, Sampler, O) \
33 const ALfloat *Resample_##Tag##_C(const InterpState* UNUSED(state), \
34 const ALfloat *restrict src, ALsizei frac, ALint increment, \
35 ALfloat *restrict dst, ALsizei numsamples) \
36 { \
37 ALsizei i; \
39 src -= O; \
40 for(i = 0;i < numsamples;i++) \
41 { \
42 dst[i] = Sampler(src, frac); \
44 frac += increment; \
45 src += frac>>FRACTIONBITS; \
46 frac &= FRACTIONMASK; \
47 } \
48 return dst; \
51 DECL_TEMPLATE(point, do_point, 0)
52 DECL_TEMPLATE(lerp, do_lerp, 0)
53 DECL_TEMPLATE(cubic, do_cubic, 1)
55 #undef DECL_TEMPLATE
57 const ALfloat *Resample_bsinc_C(const InterpState *state, const ALfloat *restrict src,
58 ALsizei frac, ALint increment, ALfloat *restrict dst,
59 ALsizei dstlen)
61 const ALfloat *fil, *scd, *phd, *spd;
62 const ALfloat *const filter = state->bsinc.filter;
63 const ALfloat sf = state->bsinc.sf;
64 const ALsizei m = state->bsinc.m;
65 ALsizei j_f, pi, i;
66 ALfloat pf, r;
68 src += state->bsinc.l;
69 for(i = 0;i < dstlen;i++)
71 // Calculate the phase index and factor.
72 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
73 pi = frac >> FRAC_PHASE_BITDIFF;
74 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
75 #undef FRAC_PHASE_BITDIFF
77 fil = ASSUME_ALIGNED(filter + m*pi*4, 16);
78 scd = ASSUME_ALIGNED(fil + m, 16);
79 phd = ASSUME_ALIGNED(scd + m, 16);
80 spd = ASSUME_ALIGNED(phd + m, 16);
82 // Apply the scale and phase interpolated filter.
83 r = 0.0f;
84 for(j_f = 0;j_f < m;j_f++)
85 r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) * src[j_f];
86 dst[i] = r;
88 frac += increment;
89 src += frac>>FRACTIONBITS;
90 frac &= FRACTIONMASK;
92 return dst;
96 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
97 const ALsizei IrSize,
98 const ALfloat (*restrict Coeffs)[2],
99 ALfloat left, ALfloat right)
101 ALsizei c;
102 for(c = 0;c < IrSize;c++)
104 const ALsizei off = (Offset+c)&HRIR_MASK;
105 Values[off][0] += Coeffs[c][0] * left;
106 Values[off][1] += Coeffs[c][1] * right;
110 #define MixHrtf MixHrtf_C
111 #define MixHrtfBlend MixHrtfBlend_C
112 #define MixDirectHrtf MixDirectHrtf_C
113 #include "hrtf_inc.c"
114 #undef MixHrtf
117 void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
118 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
119 ALsizei BufferSize)
121 ALfloat gain, delta, step;
122 ALsizei c;
124 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
126 for(c = 0;c < OutChans;c++)
128 ALsizei pos = 0;
129 gain = CurrentGains[c];
130 step = (TargetGains[c] - gain) * delta;
131 if(fabsf(step) > FLT_EPSILON)
133 ALsizei minsize = mini(BufferSize, Counter);
134 for(;pos < minsize;pos++)
136 OutBuffer[c][OutPos+pos] += data[pos]*gain;
137 gain += step;
139 if(pos == Counter)
140 gain = TargetGains[c];
141 CurrentGains[c] = gain;
144 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
145 continue;
146 for(;pos < BufferSize;pos++)
147 OutBuffer[c][OutPos+pos] += data[pos]*gain;
151 /* Basically the inverse of the above. Rather than one input going to multiple
152 * outputs (each with its own gain), it's multiple inputs (each with its own
153 * gain) going to one output. This applies one row (vs one column) of a matrix
154 * transform. And as the matrices are more or less static once set up, no
155 * stepping is necessary.
157 void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
159 ALsizei c, i;
161 for(c = 0;c < InChans;c++)
163 ALfloat gain = Gains[c];
164 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
165 continue;
167 for(i = 0;i < BufferSize;i++)
168 OutBuffer[i] += data[c][InPos+i] * gain;