uhjfilter.c 4.4 KB

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