uhjfilter.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "config.h"
  2. #include "alu.h"
  3. #include "uhjfilter.h"
  4. /* This is the maximum number of samples processed for each inner loop
  5. * iteration. */
  6. #define MAX_UPDATE_SAMPLES 128
  7. static const ALfloat Filter1Coeff[4] = {
  8. 0.6923878f, 0.9360654322959f, 0.9882295226860f, 0.9987488452737f
  9. };
  10. static const ALfloat Filter2Coeff[4] = {
  11. 0.4021921162426f, 0.8561710882420f, 0.9722909545651f, 0.9952884791278f
  12. };
  13. static void allpass_process(AllPassState *state, ALfloat *restrict dst, const ALfloat *restrict src, const ALfloat aa, ALsizei todo)
  14. {
  15. ALsizei i;
  16. if(todo > 1)
  17. {
  18. dst[0] = aa*(src[0] + state->y[1]) - state->x[1];
  19. dst[1] = aa*(src[1] + state->y[0]) - state->x[0];
  20. for(i = 2;i < todo;i++)
  21. dst[i] = aa*(src[i] + dst[i-2]) - src[i-2];
  22. state->x[1] = src[i-2];
  23. state->x[0] = src[i-1];
  24. state->y[1] = dst[i-2];
  25. state->y[0] = dst[i-1];
  26. }
  27. else if(todo == 1)
  28. {
  29. dst[0] = aa*(src[0] + state->y[1]) - state->x[1];
  30. state->x[1] = state->x[0];
  31. state->x[0] = src[0];
  32. state->y[1] = state->y[0];
  33. state->y[0] = dst[0];
  34. }
  35. }
  36. /* NOTE: There seems to be a bit of an inconsistency in how this encoding is
  37. * supposed to work. Some references, such as
  38. *
  39. * http://members.tripod.com/martin_leese/Ambisonic/UHJ_file_format.html
  40. *
  41. * specify a pre-scaling of sqrt(2) on the W channel input, while other
  42. * references, such as
  43. *
  44. * https://en.wikipedia.org/wiki/Ambisonic_UHJ_format#Encoding.5B1.5D
  45. * and
  46. * https://wiki.xiph.org/Ambisonics#UHJ_format
  47. *
  48. * do not. The sqrt(2) scaling is in line with B-Format decoder coefficients
  49. * which include such a scaling for the W channel input, however the original
  50. * source for this equation is a 1985 paper by Michael Gerzon, which does not
  51. * apparently include the scaling. Applying the extra scaling creates a louder
  52. * result with a narrower stereo image compared to not scaling, and I don't
  53. * know which is the intended result.
  54. */
  55. void EncodeUhj2(Uhj2Encoder *enc, ALfloat *restrict LeftOut, ALfloat *restrict RightOut, ALfloat (*restrict InSamples)[BUFFERSIZE], ALsizei SamplesToDo)
  56. {
  57. ALfloat D[MAX_UPDATE_SAMPLES], S[MAX_UPDATE_SAMPLES];
  58. ALfloat temp[2][MAX_UPDATE_SAMPLES];
  59. ALsizei base, i;
  60. for(base = 0;base < SamplesToDo;)
  61. {
  62. ALsizei todo = mini(SamplesToDo - base, MAX_UPDATE_SAMPLES);
  63. /* D = 0.6554516*Y */
  64. for(i = 0;i < todo;i++)
  65. temp[0][i] = 0.6554516f*InSamples[2][base+i];
  66. allpass_process(&enc->Filter1_Y[0], temp[1], temp[0],
  67. Filter1Coeff[0]*Filter1Coeff[0], todo);
  68. allpass_process(&enc->Filter1_Y[1], temp[0], temp[1],
  69. Filter1Coeff[1]*Filter1Coeff[1], todo);
  70. allpass_process(&enc->Filter1_Y[2], temp[1], temp[0],
  71. Filter1Coeff[2]*Filter1Coeff[2], todo);
  72. /* NOTE: Filter1 requires a 1 sample delay for the final output, so
  73. * take the last processed sample from the previous run as the first
  74. * output sample.
  75. */
  76. D[0] = enc->Filter1_Y[3].y[0];
  77. allpass_process(&enc->Filter1_Y[3], temp[0], temp[1],
  78. Filter1Coeff[3]*Filter1Coeff[3], todo);
  79. for(i = 1;i < todo;i++)
  80. D[i] = temp[0][i-1];
  81. /* D += j(-0.3420201*W + 0.5098604*X) */
  82. for(i = 0;i < todo;i++)
  83. temp[0][i] = -0.3420201f*InSamples[0][base+i] +
  84. 0.5098604f*InSamples[1][base+i];
  85. allpass_process(&enc->Filter2_WX[0], temp[1], temp[0],
  86. Filter2Coeff[0]*Filter2Coeff[0], todo);
  87. allpass_process(&enc->Filter2_WX[1], temp[0], temp[1],
  88. Filter2Coeff[1]*Filter2Coeff[1], todo);
  89. allpass_process(&enc->Filter2_WX[2], temp[1], temp[0],
  90. Filter2Coeff[2]*Filter2Coeff[2], todo);
  91. allpass_process(&enc->Filter2_WX[3], temp[0], temp[1],
  92. Filter2Coeff[3]*Filter2Coeff[3], todo);
  93. for(i = 0;i < todo;i++)
  94. D[i] += temp[0][i];
  95. /* S = 0.9396926*W + 0.1855740*X */
  96. for(i = 0;i < todo;i++)
  97. temp[0][i] = 0.9396926f*InSamples[0][base+i] +
  98. 0.1855740f*InSamples[1][base+i];
  99. allpass_process(&enc->Filter1_WX[0], temp[1], temp[0],
  100. Filter1Coeff[0]*Filter1Coeff[0], todo);
  101. allpass_process(&enc->Filter1_WX[1], temp[0], temp[1],
  102. Filter1Coeff[1]*Filter1Coeff[1], todo);
  103. allpass_process(&enc->Filter1_WX[2], temp[1], temp[0],
  104. Filter1Coeff[2]*Filter1Coeff[2], todo);
  105. S[0] = enc->Filter1_WX[3].y[0];
  106. allpass_process(&enc->Filter1_WX[3], temp[0], temp[1],
  107. Filter1Coeff[3]*Filter1Coeff[3], todo);
  108. for(i = 1;i < todo;i++)
  109. S[i] = temp[0][i-1];
  110. /* Left = (S + D)/2.0 */
  111. for(i = 0;i < todo;i++)
  112. *(LeftOut++) += (S[i] + D[i]) * 0.5f;
  113. /* Right = (S - D)/2.0 */
  114. for(i = 0;i < todo;i++)
  115. *(RightOut++) += (S[i] - D[i]) * 0.5f;
  116. base += todo;
  117. }
  118. }