🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.6 KiB

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