mixer_c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "config.h"
  2. #include <assert.h>
  3. #include "alMain.h"
  4. #include "alu.h"
  5. #include "alSource.h"
  6. #include "alAuxEffectSlot.h"
  7. static inline ALfloat point32(const ALfloat *restrict vals, ALuint UNUSED(frac))
  8. { return vals[0]; }
  9. static inline ALfloat lerp32(const ALfloat *restrict vals, ALuint frac)
  10. { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
  11. static inline ALfloat fir4_32(const ALfloat *restrict vals, ALuint frac)
  12. { return resample_fir4(vals[-1], vals[0], vals[1], vals[2], frac); }
  13. static inline ALfloat fir8_32(const ALfloat *restrict vals, ALuint frac)
  14. { return resample_fir8(vals[-3], vals[-2], vals[-1], vals[0], vals[1], vals[2], vals[3], vals[4], frac); }
  15. const ALfloat *Resample_copy32_C(const BsincState* UNUSED(state), const ALfloat *restrict src, ALuint UNUSED(frac),
  16. ALuint UNUSED(increment), ALfloat *restrict dst, ALuint numsamples)
  17. {
  18. #if defined(HAVE_SSE) || defined(HAVE_NEON)
  19. /* Avoid copying the source data if it's aligned like the destination. */
  20. if((((intptr_t)src)&15) == (((intptr_t)dst)&15))
  21. return src;
  22. #endif
  23. memcpy(dst, src, numsamples*sizeof(ALfloat));
  24. return dst;
  25. }
  26. #define DECL_TEMPLATE(Sampler) \
  27. const ALfloat *Resample_##Sampler##_C(const BsincState* UNUSED(state), \
  28. const ALfloat *restrict src, ALuint frac, ALuint increment, \
  29. ALfloat *restrict dst, ALuint numsamples) \
  30. { \
  31. ALuint i; \
  32. for(i = 0;i < numsamples;i++) \
  33. { \
  34. dst[i] = Sampler(src, frac); \
  35. \
  36. frac += increment; \
  37. src += frac>>FRACTIONBITS; \
  38. frac &= FRACTIONMASK; \
  39. } \
  40. return dst; \
  41. }
  42. DECL_TEMPLATE(point32)
  43. DECL_TEMPLATE(lerp32)
  44. DECL_TEMPLATE(fir4_32)
  45. DECL_TEMPLATE(fir8_32)
  46. #undef DECL_TEMPLATE
  47. const ALfloat *Resample_bsinc32_C(const BsincState *state, const ALfloat *restrict src,
  48. ALuint frac, ALuint increment, ALfloat *restrict dst,
  49. ALuint dstlen)
  50. {
  51. const ALfloat *fil, *scd, *phd, *spd;
  52. const ALfloat sf = state->sf;
  53. const ALuint m = state->m;
  54. const ALint l = state->l;
  55. ALuint j_f, pi, i;
  56. ALfloat pf, r;
  57. ALint j_s;
  58. for(i = 0;i < dstlen;i++)
  59. {
  60. // Calculate the phase index and factor.
  61. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  62. pi = frac >> FRAC_PHASE_BITDIFF;
  63. pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
  64. #undef FRAC_PHASE_BITDIFF
  65. fil = state->coeffs[pi].filter;
  66. scd = state->coeffs[pi].scDelta;
  67. phd = state->coeffs[pi].phDelta;
  68. spd = state->coeffs[pi].spDelta;
  69. // Apply the scale and phase interpolated filter.
  70. r = 0.0f;
  71. for(j_f = 0,j_s = l;j_f < m;j_f++,j_s++)
  72. r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) *
  73. src[j_s];
  74. dst[i] = r;
  75. frac += increment;
  76. src += frac>>FRACTIONBITS;
  77. frac &= FRACTIONMASK;
  78. }
  79. return dst;
  80. }
  81. void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALuint numsamples)
  82. {
  83. ALuint i;
  84. if(numsamples > 1)
  85. {
  86. dst[0] = filter->b0 * src[0] +
  87. filter->b1 * filter->x[0] +
  88. filter->b2 * filter->x[1] -
  89. filter->a1 * filter->y[0] -
  90. filter->a2 * filter->y[1];
  91. dst[1] = filter->b0 * src[1] +
  92. filter->b1 * src[0] +
  93. filter->b2 * filter->x[0] -
  94. filter->a1 * dst[0] -
  95. filter->a2 * filter->y[0];
  96. for(i = 2;i < numsamples;i++)
  97. dst[i] = filter->b0 * src[i] +
  98. filter->b1 * src[i-1] +
  99. filter->b2 * src[i-2] -
  100. filter->a1 * dst[i-1] -
  101. filter->a2 * dst[i-2];
  102. filter->x[0] = src[i-1];
  103. filter->x[1] = src[i-2];
  104. filter->y[0] = dst[i-1];
  105. filter->y[1] = dst[i-2];
  106. }
  107. else if(numsamples == 1)
  108. {
  109. dst[0] = filter->b0 * src[0] +
  110. filter->b1 * filter->x[0] +
  111. filter->b2 * filter->x[1] -
  112. filter->a1 * filter->y[0] -
  113. filter->a2 * filter->y[1];
  114. filter->x[1] = filter->x[0];
  115. filter->x[0] = src[0];
  116. filter->y[1] = filter->y[0];
  117. filter->y[0] = dst[0];
  118. }
  119. }
  120. static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
  121. const ALuint IrSize,
  122. ALfloat (*restrict Coeffs)[2],
  123. const ALfloat (*restrict CoeffStep)[2],
  124. ALfloat left, ALfloat right)
  125. {
  126. ALuint c;
  127. for(c = 0;c < IrSize;c++)
  128. {
  129. const ALuint off = (Offset+c)&HRIR_MASK;
  130. Values[off][0] += Coeffs[c][0] * left;
  131. Values[off][1] += Coeffs[c][1] * right;
  132. Coeffs[c][0] += CoeffStep[c][0];
  133. Coeffs[c][1] += CoeffStep[c][1];
  134. }
  135. }
  136. static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
  137. const ALuint IrSize,
  138. ALfloat (*restrict Coeffs)[2],
  139. ALfloat left, ALfloat right)
  140. {
  141. ALuint c;
  142. for(c = 0;c < IrSize;c++)
  143. {
  144. const ALuint off = (Offset+c)&HRIR_MASK;
  145. Values[off][0] += Coeffs[c][0] * left;
  146. Values[off][1] += Coeffs[c][1] * right;
  147. }
  148. }
  149. #define MixHrtf MixHrtf_C
  150. #define MixDirectHrtf MixDirectHrtf_C
  151. #include "mixer_inc.c"
  152. #undef MixHrtf
  153. void Mix_C(const ALfloat *data, ALuint OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
  154. ALfloat *CurrentGains, const ALfloat *TargetGains, ALuint Counter, ALuint OutPos,
  155. ALuint BufferSize)
  156. {
  157. ALfloat gain, delta, step;
  158. ALuint c;
  159. delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
  160. for(c = 0;c < OutChans;c++)
  161. {
  162. ALuint pos = 0;
  163. gain = CurrentGains[c];
  164. step = (TargetGains[c] - gain) * delta;
  165. if(fabsf(step) > FLT_EPSILON)
  166. {
  167. ALuint minsize = minu(BufferSize, Counter);
  168. for(;pos < minsize;pos++)
  169. {
  170. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  171. gain += step;
  172. }
  173. if(pos == Counter)
  174. gain = TargetGains[c];
  175. CurrentGains[c] = gain;
  176. }
  177. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  178. continue;
  179. for(;pos < BufferSize;pos++)
  180. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  181. }
  182. }
  183. /* Basically the inverse of the above. Rather than one input going to multiple
  184. * outputs (each with its own gain), it's multiple inputs (each with its own
  185. * gain) going to one output. This applies one row (vs one column) of a matrix
  186. * transform. And as the matrices are more or less static once set up, no
  187. * stepping is necessary.
  188. */
  189. void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALuint InChans, ALuint InPos, ALuint BufferSize)
  190. {
  191. ALuint c, i;
  192. for(c = 0;c < InChans;c++)
  193. {
  194. ALfloat gain = Gains[c];
  195. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  196. continue;
  197. for(i = 0;i < BufferSize;i++)
  198. OutBuffer[i] += data[c][InPos+i] * gain;
  199. }
  200. }