mixer_inc.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "config.h"
  2. #include "alMain.h"
  3. #include "alSource.h"
  4. #include "hrtf.h"
  5. #include "mixer_defs.h"
  6. #include "align.h"
  7. static inline void SetupCoeffs(ALfloat (*restrict OutCoeffs)[2],
  8. const HrtfParams *hrtfparams,
  9. ALuint IrSize, ALuint Counter);
  10. static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
  11. const ALuint irSize,
  12. ALfloat (*restrict Coeffs)[2],
  13. const ALfloat (*restrict CoeffStep)[2],
  14. ALfloat left, ALfloat right);
  15. static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
  16. const ALuint irSize,
  17. ALfloat (*restrict Coeffs)[2],
  18. ALfloat left, ALfloat right);
  19. void MixHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
  20. ALuint Counter, ALuint Offset, ALuint OutPos, const ALuint IrSize,
  21. const HrtfParams *hrtfparams, HrtfState *hrtfstate, ALuint BufferSize)
  22. {
  23. alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
  24. ALuint Delay[2];
  25. ALfloat left, right;
  26. ALuint pos;
  27. SetupCoeffs(Coeffs, hrtfparams, IrSize, Counter);
  28. Delay[0] = hrtfparams->Delay[0] - (hrtfparams->DelayStep[0]*Counter);
  29. Delay[1] = hrtfparams->Delay[1] - (hrtfparams->DelayStep[1]*Counter);
  30. pos = 0;
  31. for(;pos < BufferSize && pos < Counter;pos++)
  32. {
  33. hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos];
  34. left = lerp(hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
  35. hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
  36. (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
  37. right = lerp(hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
  38. hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
  39. (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
  40. Delay[0] += hrtfparams->DelayStep[0];
  41. Delay[1] += hrtfparams->DelayStep[1];
  42. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
  43. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
  44. Offset++;
  45. ApplyCoeffsStep(Offset, hrtfstate->Values, IrSize, Coeffs, hrtfparams->CoeffStep, left, right);
  46. OutBuffer[0][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][0];
  47. OutBuffer[1][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][1];
  48. OutPos++;
  49. }
  50. Delay[0] >>= HRTFDELAY_BITS;
  51. Delay[1] >>= HRTFDELAY_BITS;
  52. for(;pos < BufferSize;pos++)
  53. {
  54. hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos];
  55. left = hrtfstate->History[(Offset-Delay[0])&HRTF_HISTORY_MASK];
  56. right = hrtfstate->History[(Offset-Delay[1])&HRTF_HISTORY_MASK];
  57. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
  58. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
  59. Offset++;
  60. ApplyCoeffs(Offset, hrtfstate->Values, IrSize, Coeffs, left, right);
  61. OutBuffer[0][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][0];
  62. OutBuffer[1][OutPos] += hrtfstate->Values[Offset&HRIR_MASK][1];
  63. OutPos++;
  64. }
  65. }