Free the args returned by CommandLineToArgvW
[openal-soft.git] / Alc / mixer_c.c
blobe468fedbb549013bc434ffd31bfc271274c7fb30
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)); }
17 const ALfloat *Resample_copy_C(const InterpState* UNUSED(state),
18 const ALfloat *restrict src, ALsizei UNUSED(frac), ALint UNUSED(increment),
19 ALfloat *restrict dst, ALsizei numsamples)
21 #if defined(HAVE_SSE) || defined(HAVE_NEON)
22 /* Avoid copying the source data if it's aligned like the destination. */
23 if((((intptr_t)src)&15) == (((intptr_t)dst)&15))
24 return src;
25 #endif
26 memcpy(dst, src, numsamples*sizeof(ALfloat));
27 return dst;
30 #define DECL_TEMPLATE(Tag, Sampler) \
31 const ALfloat *Resample_##Tag##_C(const InterpState* UNUSED(state), \
32 const ALfloat *restrict src, ALsizei frac, ALint increment, \
33 ALfloat *restrict dst, ALsizei numsamples) \
34 { \
35 ALsizei i; \
36 for(i = 0;i < numsamples;i++) \
37 { \
38 dst[i] = Sampler(src, frac); \
40 frac += increment; \
41 src += frac>>FRACTIONBITS; \
42 frac &= FRACTIONMASK; \
43 } \
44 return dst; \
47 DECL_TEMPLATE(point, do_point)
48 DECL_TEMPLATE(lerp, do_lerp)
50 #undef DECL_TEMPLATE
52 const ALfloat *Resample_fir4_C(const InterpState *state, const ALfloat *restrict src,
53 ALsizei frac, ALint increment, ALfloat *restrict dst,
54 ALsizei numsamples)
56 const ALfloat (*restrict filter)[4] = ASSUME_ALIGNED(state->sinc4.filter, 16);
57 ALsizei i;
59 src -= 1;
60 for(i = 0;i < numsamples;i++)
62 dst[i] = resample_fir4(src[0], src[1], src[2], src[3], filter[frac]);
64 frac += increment;
65 src += frac>>FRACTIONBITS;
66 frac &= FRACTIONMASK;
68 return dst;
71 const ALfloat *Resample_bsinc_C(const InterpState *state, const ALfloat *restrict src,
72 ALsizei frac, ALint increment, ALfloat *restrict dst,
73 ALsizei dstlen)
75 const ALfloat *fil, *scd, *phd, *spd;
76 const ALfloat *const filter = state->bsinc.filter;
77 const ALfloat sf = state->bsinc.sf;
78 const ALsizei m = state->bsinc.m;
79 ALsizei j_f, pi, i;
80 ALfloat pf, r;
82 src += state->bsinc.l;
83 for(i = 0;i < dstlen;i++)
85 // Calculate the phase index and factor.
86 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
87 pi = frac >> FRAC_PHASE_BITDIFF;
88 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
89 #undef FRAC_PHASE_BITDIFF
91 fil = ASSUME_ALIGNED(filter + m*pi*4, 16);
92 scd = ASSUME_ALIGNED(fil + m, 16);
93 phd = ASSUME_ALIGNED(scd + m, 16);
94 spd = ASSUME_ALIGNED(phd + m, 16);
96 // Apply the scale and phase interpolated filter.
97 r = 0.0f;
98 for(j_f = 0;j_f < m;j_f++)
99 r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) * src[j_f];
100 dst[i] = r;
102 frac += increment;
103 src += frac>>FRACTIONBITS;
104 frac &= FRACTIONMASK;
106 return dst;
110 void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALsizei numsamples)
112 ALsizei i;
113 if(numsamples > 1)
115 dst[0] = filter->b0 * src[0] +
116 filter->b1 * filter->x[0] +
117 filter->b2 * filter->x[1] -
118 filter->a1 * filter->y[0] -
119 filter->a2 * filter->y[1];
120 dst[1] = filter->b0 * src[1] +
121 filter->b1 * src[0] +
122 filter->b2 * filter->x[0] -
123 filter->a1 * dst[0] -
124 filter->a2 * filter->y[0];
125 for(i = 2;i < numsamples;i++)
126 dst[i] = filter->b0 * src[i] +
127 filter->b1 * src[i-1] +
128 filter->b2 * src[i-2] -
129 filter->a1 * dst[i-1] -
130 filter->a2 * dst[i-2];
131 filter->x[0] = src[i-1];
132 filter->x[1] = src[i-2];
133 filter->y[0] = dst[i-1];
134 filter->y[1] = dst[i-2];
136 else if(numsamples == 1)
138 dst[0] = filter->b0 * src[0] +
139 filter->b1 * filter->x[0] +
140 filter->b2 * filter->x[1] -
141 filter->a1 * filter->y[0] -
142 filter->a2 * filter->y[1];
143 filter->x[1] = filter->x[0];
144 filter->x[0] = src[0];
145 filter->y[1] = filter->y[0];
146 filter->y[0] = dst[0];
151 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
152 const ALsizei IrSize,
153 const ALfloat (*restrict Coeffs)[2],
154 ALfloat left, ALfloat right)
156 ALsizei c;
157 for(c = 0;c < IrSize;c++)
159 const ALsizei off = (Offset+c)&HRIR_MASK;
160 Values[off][0] += Coeffs[c][0] * left;
161 Values[off][1] += Coeffs[c][1] * right;
165 #define MixHrtf MixHrtf_C
166 #define MixHrtfBlend MixHrtfBlend_C
167 #define MixDirectHrtf MixDirectHrtf_C
168 #include "mixer_inc.c"
169 #undef MixHrtf
172 void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
173 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
174 ALsizei BufferSize)
176 ALfloat gain, delta, step;
177 ALsizei c;
179 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
181 for(c = 0;c < OutChans;c++)
183 ALsizei pos = 0;
184 gain = CurrentGains[c];
185 step = (TargetGains[c] - gain) * delta;
186 if(fabsf(step) > FLT_EPSILON)
188 ALsizei minsize = mini(BufferSize, Counter);
189 for(;pos < minsize;pos++)
191 OutBuffer[c][OutPos+pos] += data[pos]*gain;
192 gain += step;
194 if(pos == Counter)
195 gain = TargetGains[c];
196 CurrentGains[c] = gain;
199 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
200 continue;
201 for(;pos < BufferSize;pos++)
202 OutBuffer[c][OutPos+pos] += data[pos]*gain;
206 /* Basically the inverse of the above. Rather than one input going to multiple
207 * outputs (each with its own gain), it's multiple inputs (each with its own
208 * gain) going to one output. This applies one row (vs one column) of a matrix
209 * transform. And as the matrices are more or less static once set up, no
210 * stepping is necessary.
212 void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
214 ALsizei c, i;
216 for(c = 0;c < InChans;c++)
218 ALfloat gain = Gains[c];
219 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
220 continue;
222 for(i = 0;i < BufferSize;i++)
223 OutBuffer[i] += data[c][InPos+i] * gain;