uhjfilter.c 5.1 KB

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