uhjfilter.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "config.h"
  2. #include "uhjfilter.h"
  3. #include <algorithm>
  4. #include <iterator>
  5. #include "AL/al.h"
  6. #include "alnumeric.h"
  7. #include "opthelpers.h"
  8. namespace {
  9. /* This is the maximum number of samples processed for each inner loop
  10. * iteration. */
  11. #define MAX_UPDATE_SAMPLES 128
  12. constexpr ALfloat Filter1CoeffSqr[4] = {
  13. 0.479400865589f, 0.876218493539f, 0.976597589508f, 0.997499255936f
  14. };
  15. constexpr ALfloat Filter2CoeffSqr[4] = {
  16. 0.161758498368f, 0.733028932341f, 0.945349700329f, 0.990599156685f
  17. };
  18. void allpass_process(AllPassState *state, ALfloat *dst, const ALfloat *src, const ALfloat aa,
  19. const size_t todo)
  20. {
  21. ALfloat z1{state->z[0]};
  22. ALfloat z2{state->z[1]};
  23. auto proc_sample = [aa,&z1,&z2](const ALfloat input) noexcept -> ALfloat
  24. {
  25. const ALfloat output{input*aa + z1};
  26. z1 = z2; z2 = output*aa - input;
  27. return output;
  28. };
  29. std::transform(src, src+todo, dst, proc_sample);
  30. state->z[0] = z1;
  31. state->z[1] = z2;
  32. }
  33. } // namespace
  34. /* NOTE: There seems to be a bit of an inconsistency in how this encoding is
  35. * supposed to work. Some references, such as
  36. *
  37. * http://members.tripod.com/martin_leese/Ambisonic/UHJ_file_format.html
  38. *
  39. * specify a pre-scaling of sqrt(2) on the W channel input, while other
  40. * references, such as
  41. *
  42. * https://en.wikipedia.org/wiki/Ambisonic_UHJ_format#Encoding.5B1.5D
  43. * and
  44. * https://wiki.xiph.org/Ambisonics#UHJ_format
  45. *
  46. * do not. The sqrt(2) scaling is in line with B-Format decoder coefficients
  47. * which include such a scaling for the W channel input, however the original
  48. * source for this equation is a 1985 paper by Michael Gerzon, which does not
  49. * apparently include the scaling. Applying the extra scaling creates a louder
  50. * result with a narrower stereo image compared to not scaling, and I don't
  51. * know which is the intended result.
  52. */
  53. void Uhj2Encoder::encode(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
  54. FloatBufferLine *InSamples, const size_t SamplesToDo)
  55. {
  56. alignas(16) ALfloat D[MAX_UPDATE_SAMPLES], S[MAX_UPDATE_SAMPLES];
  57. alignas(16) ALfloat temp[MAX_UPDATE_SAMPLES];
  58. ASSUME(SamplesToDo > 0);
  59. auto winput = InSamples[0].cbegin();
  60. auto xinput = InSamples[1].cbegin();
  61. auto yinput = InSamples[2].cbegin();
  62. for(size_t base{0};base < SamplesToDo;)
  63. {
  64. const size_t todo{minz(SamplesToDo - base, MAX_UPDATE_SAMPLES)};
  65. ASSUME(todo > 0);
  66. /* D = 0.6554516*Y */
  67. std::transform(yinput, yinput+todo, std::begin(temp),
  68. [](const float y) noexcept -> float { return 0.6554516f*y; });
  69. allpass_process(&mFilter1_Y[0], temp, temp, Filter1CoeffSqr[0], todo);
  70. allpass_process(&mFilter1_Y[1], temp, temp, Filter1CoeffSqr[1], todo);
  71. allpass_process(&mFilter1_Y[2], temp, temp, Filter1CoeffSqr[2], todo);
  72. allpass_process(&mFilter1_Y[3], temp, temp, Filter1CoeffSqr[3], todo);
  73. /* NOTE: Filter1 requires a 1 sample delay for the final output, so
  74. * take the last processed sample from the previous run as the first
  75. * output sample.
  76. */
  77. D[0] = mLastY;
  78. for(size_t i{1};i < todo;i++)
  79. D[i] = temp[i-1];
  80. mLastY = temp[todo-1];
  81. /* D += j(-0.3420201*W + 0.5098604*X) */
  82. std::transform(winput, winput+todo, xinput, std::begin(temp),
  83. [](const float w, const float x) noexcept -> float
  84. { return -0.3420201f*w + 0.5098604f*x; });
  85. allpass_process(&mFilter2_WX[0], temp, temp, Filter2CoeffSqr[0], todo);
  86. allpass_process(&mFilter2_WX[1], temp, temp, Filter2CoeffSqr[1], todo);
  87. allpass_process(&mFilter2_WX[2], temp, temp, Filter2CoeffSqr[2], todo);
  88. allpass_process(&mFilter2_WX[3], temp, temp, Filter2CoeffSqr[3], todo);
  89. for(size_t i{0};i < todo;i++)
  90. D[i] += temp[i];
  91. /* S = 0.9396926*W + 0.1855740*X */
  92. std::transform(winput, winput+todo, xinput, std::begin(temp),
  93. [](const float w, const float x) noexcept -> float
  94. { return 0.9396926f*w + 0.1855740f*x; });
  95. allpass_process(&mFilter1_WX[0], temp, temp, Filter1CoeffSqr[0], todo);
  96. allpass_process(&mFilter1_WX[1], temp, temp, Filter1CoeffSqr[1], todo);
  97. allpass_process(&mFilter1_WX[2], temp, temp, Filter1CoeffSqr[2], todo);
  98. allpass_process(&mFilter1_WX[3], temp, temp, Filter1CoeffSqr[3], todo);
  99. S[0] = mLastWX;
  100. for(size_t i{1};i < todo;i++)
  101. S[i] = temp[i-1];
  102. mLastWX = temp[todo-1];
  103. /* Left = (S + D)/2.0 */
  104. ALfloat *RESTRICT left = al::assume_aligned<16>(LeftOut.data()+base);
  105. for(size_t i{0};i < todo;i++)
  106. left[i] += (S[i] + D[i]) * 0.5f;
  107. /* Right = (S - D)/2.0 */
  108. ALfloat *RESTRICT right = al::assume_aligned<16>(RightOut.data()+base);
  109. for(size_t i{0};i < todo;i++)
  110. right[i] += (S[i] - D[i]) * 0.5f;
  111. winput += todo;
  112. xinput += todo;
  113. yinput += todo;
  114. base += todo;
  115. }
  116. }