mixer_inc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "alu.h"
  8. #define MAX_UPDATE_SAMPLES 128
  9. static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
  10. const ALuint irSize,
  11. ALfloat (*restrict Coeffs)[2],
  12. const ALfloat (*restrict CoeffStep)[2],
  13. ALfloat left, ALfloat right);
  14. static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
  15. const ALuint irSize,
  16. ALfloat (*restrict Coeffs)[2],
  17. ALfloat left, ALfloat right);
  18. void MixHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], ALuint lidx, ALuint ridx,
  19. const ALfloat *data, ALuint Counter, ALuint Offset, ALuint OutPos,
  20. const ALuint IrSize, const MixHrtfParams *hrtfparams, HrtfState *hrtfstate,
  21. ALuint BufferSize)
  22. {
  23. ALfloat (*Coeffs)[2] = hrtfparams->Current->Coeffs;
  24. ALuint Delay[2] = { hrtfparams->Current->Delay[0], hrtfparams->Current->Delay[1] };
  25. ALfloat out[MAX_UPDATE_SAMPLES][2];
  26. ALfloat left, right;
  27. ALuint minsize;
  28. ALuint pos, i;
  29. pos = 0;
  30. if(Counter == 0)
  31. goto skip_stepping;
  32. minsize = minu(BufferSize, Counter);
  33. while(pos < minsize)
  34. {
  35. ALuint todo = minu(minsize-pos, MAX_UPDATE_SAMPLES);
  36. for(i = 0;i < todo;i++)
  37. {
  38. hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos++];
  39. left = lerp(hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
  40. hrtfstate->History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
  41. (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
  42. right = lerp(hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&HRTF_HISTORY_MASK],
  43. hrtfstate->History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&HRTF_HISTORY_MASK],
  44. (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
  45. Delay[0] += hrtfparams->Steps.Delay[0];
  46. Delay[1] += hrtfparams->Steps.Delay[1];
  47. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
  48. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
  49. Offset++;
  50. ApplyCoeffsStep(Offset, hrtfstate->Values, IrSize, Coeffs, hrtfparams->Steps.Coeffs, left, right);
  51. out[i][0] = hrtfstate->Values[Offset&HRIR_MASK][0];
  52. out[i][1] = hrtfstate->Values[Offset&HRIR_MASK][1];
  53. }
  54. for(i = 0;i < todo;i++)
  55. OutBuffer[lidx][OutPos+i] += out[i][0];
  56. for(i = 0;i < todo;i++)
  57. OutBuffer[ridx][OutPos+i] += out[i][1];
  58. OutPos += todo;
  59. }
  60. if(pos == Counter)
  61. {
  62. *hrtfparams->Current = *hrtfparams->Target;
  63. Delay[0] = hrtfparams->Target->Delay[0];
  64. Delay[1] = hrtfparams->Target->Delay[1];
  65. }
  66. else
  67. {
  68. hrtfparams->Current->Delay[0] = Delay[0];
  69. hrtfparams->Current->Delay[1] = Delay[1];
  70. }
  71. skip_stepping:
  72. Delay[0] >>= HRTFDELAY_BITS;
  73. Delay[1] >>= HRTFDELAY_BITS;
  74. while(pos < BufferSize)
  75. {
  76. ALuint todo = minu(BufferSize-pos, MAX_UPDATE_SAMPLES);
  77. for(i = 0;i < todo;i++)
  78. {
  79. hrtfstate->History[Offset&HRTF_HISTORY_MASK] = data[pos++];
  80. left = hrtfstate->History[(Offset-Delay[0])&HRTF_HISTORY_MASK];
  81. right = hrtfstate->History[(Offset-Delay[1])&HRTF_HISTORY_MASK];
  82. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
  83. hrtfstate->Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
  84. Offset++;
  85. ApplyCoeffs(Offset, hrtfstate->Values, IrSize, Coeffs, left, right);
  86. out[i][0] = hrtfstate->Values[Offset&HRIR_MASK][0];
  87. out[i][1] = hrtfstate->Values[Offset&HRIR_MASK][1];
  88. }
  89. for(i = 0;i < todo;i++)
  90. OutBuffer[lidx][OutPos+i] += out[i][0];
  91. for(i = 0;i < todo;i++)
  92. OutBuffer[ridx][OutPos+i] += out[i][1];
  93. OutPos += todo;
  94. }
  95. }
  96. void MixDirectHrtf(ALfloat (*restrict OutBuffer)[BUFFERSIZE], ALuint lidx, ALuint ridx,
  97. const ALfloat *data, ALuint Offset, const ALuint IrSize,
  98. ALfloat (*restrict Coeffs)[2], ALfloat (*restrict Values)[2],
  99. ALuint BufferSize)
  100. {
  101. ALfloat out[MAX_UPDATE_SAMPLES][2];
  102. ALfloat insample;
  103. ALuint pos, i;
  104. for(pos = 0;pos < BufferSize;)
  105. {
  106. ALuint todo = minu(BufferSize-pos, MAX_UPDATE_SAMPLES);
  107. for(i = 0;i < todo;i++)
  108. {
  109. Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
  110. Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
  111. Offset++;
  112. insample = *(data++);
  113. ApplyCoeffs(Offset, Values, IrSize, Coeffs, insample, insample);
  114. out[i][0] = Values[Offset&HRIR_MASK][0];
  115. out[i][1] = Values[Offset&HRIR_MASK][1];
  116. }
  117. for(i = 0;i < todo;i++)
  118. OutBuffer[lidx][pos+i] += out[i][0];
  119. for(i = 0;i < todo;i++)
  120. OutBuffer[ridx][pos+i] += out[i][1];
  121. pos += todo;
  122. }
  123. }