Don't force a fade-in when resuming a paused source
[openal-soft.git] / Alc / mixer_c.c
blob87f2fe9001530174026b50defd717ed8db13db09
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 point32(const ALfloat *restrict vals, ALsizei UNUSED(frac))
12 { return vals[0]; }
13 static inline ALfloat lerp32(const ALfloat *restrict vals, ALsizei frac)
14 { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
15 static inline ALfloat fir4_32(const ALfloat *restrict vals, ALsizei frac)
16 { return resample_fir4(vals[-1], vals[0], vals[1], vals[2], frac); }
19 const ALfloat *Resample_copy32_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(Sampler) \
33 const ALfloat *Resample_##Sampler##_C(const InterpState* UNUSED(state), \
34 const ALfloat *restrict src, ALsizei frac, ALint increment, \
35 ALfloat *restrict dst, ALsizei numsamples) \
36 { \
37 ALsizei i; \
38 for(i = 0;i < numsamples;i++) \
39 { \
40 dst[i] = Sampler(src, frac); \
42 frac += increment; \
43 src += frac>>FRACTIONBITS; \
44 frac &= FRACTIONMASK; \
45 } \
46 return dst; \
49 DECL_TEMPLATE(point32)
50 DECL_TEMPLATE(lerp32)
51 DECL_TEMPLATE(fir4_32)
53 #undef DECL_TEMPLATE
55 const ALfloat *Resample_bsinc32_C(const InterpState *state, const ALfloat *restrict src,
56 ALsizei frac, ALint increment, ALfloat *restrict dst,
57 ALsizei dstlen)
59 const ALfloat *fil, *scd, *phd, *spd;
60 const ALfloat sf = state->bsinc.sf;
61 const ALsizei m = state->bsinc.m;
62 ALsizei j_f, pi, i;
63 ALfloat pf, r;
65 src += state->bsinc.l;
66 for(i = 0;i < dstlen;i++)
68 // Calculate the phase index and factor.
69 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
70 pi = frac >> FRAC_PHASE_BITDIFF;
71 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
72 #undef FRAC_PHASE_BITDIFF
74 fil = ASSUME_ALIGNED(state->bsinc.coeffs[pi].filter, 16);
75 scd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].scDelta, 16);
76 phd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].phDelta, 16);
77 spd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].spDelta, 16);
79 // Apply the scale and phase interpolated filter.
80 r = 0.0f;
81 for(j_f = 0;j_f < m;j_f++)
82 r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) * src[j_f];
83 dst[i] = r;
85 frac += increment;
86 src += frac>>FRACTIONBITS;
87 frac &= FRACTIONMASK;
89 return dst;
93 void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALsizei numsamples)
95 ALsizei i;
96 if(numsamples > 1)
98 dst[0] = filter->b0 * src[0] +
99 filter->b1 * filter->x[0] +
100 filter->b2 * filter->x[1] -
101 filter->a1 * filter->y[0] -
102 filter->a2 * filter->y[1];
103 dst[1] = filter->b0 * src[1] +
104 filter->b1 * src[0] +
105 filter->b2 * filter->x[0] -
106 filter->a1 * dst[0] -
107 filter->a2 * filter->y[0];
108 for(i = 2;i < numsamples;i++)
109 dst[i] = filter->b0 * src[i] +
110 filter->b1 * src[i-1] +
111 filter->b2 * src[i-2] -
112 filter->a1 * dst[i-1] -
113 filter->a2 * dst[i-2];
114 filter->x[0] = src[i-1];
115 filter->x[1] = src[i-2];
116 filter->y[0] = dst[i-1];
117 filter->y[1] = dst[i-2];
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;