mixer_c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 *vals, ALuint UNUSED(frac))
  8. { return vals[0]; }
  9. static inline ALfloat lerp32(const ALfloat *vals, ALuint frac)
  10. { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
  11. static inline ALfloat fir4_32(const ALfloat *vals, ALuint frac)
  12. { return resample_fir4(vals[-1], vals[0], vals[1], vals[2], frac); }
  13. static inline ALfloat fir8_32(const ALfloat *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 *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 *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 *src, ALuint frac,
  48. ALuint increment, ALfloat *restrict dst, ALuint dstlen)
  49. {
  50. const ALfloat *fil, *scd, *phd, *spd;
  51. const ALfloat sf = state->sf;
  52. const ALuint m = state->m;
  53. const ALint l = state->l;
  54. ALuint j_f, pi, i;
  55. ALfloat pf, r;
  56. ALint j_s;
  57. for(i = 0;i < dstlen;i++)
  58. {
  59. // Calculate the phase index and factor.
  60. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  61. pi = frac >> FRAC_PHASE_BITDIFF;
  62. pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
  63. #undef FRAC_PHASE_BITDIFF
  64. fil = state->coeffs[pi].filter;
  65. scd = state->coeffs[pi].scDelta;
  66. phd = state->coeffs[pi].phDelta;
  67. spd = state->coeffs[pi].spDelta;
  68. // Apply the scale and phase interpolated filter.
  69. r = 0.0f;
  70. for(j_f = 0,j_s = l;j_f < m;j_f++,j_s++)
  71. r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) *
  72. src[j_s];
  73. dst[i] = r;
  74. frac += increment;
  75. src += frac>>FRACTIONBITS;
  76. frac &= FRACTIONMASK;
  77. }
  78. return dst;
  79. }
  80. void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *src, ALuint numsamples)
  81. {
  82. ALuint i;
  83. for(i = 0;i < numsamples;i++)
  84. *(dst++) = ALfilterState_processSingle(filter, *(src++));
  85. }
  86. static inline void SetupCoeffs(ALfloat (*restrict OutCoeffs)[2],
  87. const HrtfParams *hrtfparams,
  88. ALuint IrSize, ALuint Counter)
  89. {
  90. ALuint c;
  91. for(c = 0;c < IrSize;c++)
  92. {
  93. OutCoeffs[c][0] = hrtfparams->Coeffs[c][0] - (hrtfparams->CoeffStep[c][0]*Counter);
  94. OutCoeffs[c][1] = hrtfparams->Coeffs[c][1] - (hrtfparams->CoeffStep[c][1]*Counter);
  95. }
  96. }
  97. static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
  98. const ALuint IrSize,
  99. ALfloat (*restrict Coeffs)[2],
  100. const ALfloat (*restrict CoeffStep)[2],
  101. ALfloat left, ALfloat right)
  102. {
  103. ALuint c;
  104. for(c = 0;c < IrSize;c++)
  105. {
  106. const ALuint off = (Offset+c)&HRIR_MASK;
  107. Values[off][0] += Coeffs[c][0] * left;
  108. Values[off][1] += Coeffs[c][1] * right;
  109. Coeffs[c][0] += CoeffStep[c][0];
  110. Coeffs[c][1] += CoeffStep[c][1];
  111. }
  112. }
  113. static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
  114. const ALuint IrSize,
  115. ALfloat (*restrict Coeffs)[2],
  116. ALfloat left, ALfloat right)
  117. {
  118. ALuint c;
  119. for(c = 0;c < IrSize;c++)
  120. {
  121. const ALuint off = (Offset+c)&HRIR_MASK;
  122. Values[off][0] += Coeffs[c][0] * left;
  123. Values[off][1] += Coeffs[c][1] * right;
  124. }
  125. }
  126. #define MixHrtf MixHrtf_C
  127. #include "mixer_inc.c"
  128. #undef MixHrtf
  129. void Mix_C(const ALfloat *data, ALuint OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
  130. MixGains *Gains, ALuint Counter, ALuint OutPos, ALuint BufferSize)
  131. {
  132. ALfloat gain, step;
  133. ALuint c;
  134. for(c = 0;c < OutChans;c++)
  135. {
  136. ALuint pos = 0;
  137. gain = Gains[c].Current;
  138. step = Gains[c].Step;
  139. if(step != 0.0f && Counter > 0)
  140. {
  141. ALuint minsize = minu(BufferSize, Counter);
  142. for(;pos < minsize;pos++)
  143. {
  144. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  145. gain += step;
  146. }
  147. if(pos == Counter)
  148. gain = Gains[c].Target;
  149. Gains[c].Current = gain;
  150. }
  151. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  152. continue;
  153. for(;pos < BufferSize;pos++)
  154. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  155. }
  156. }