ambidefs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef CORE_AMBIDEFS_H
  2. #define CORE_AMBIDEFS_H
  3. #include <array>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include "alnumbers.h"
  7. using uint = unsigned int;
  8. /* The maximum number of Ambisonics channels. For a given order (o), the size
  9. * needed will be (o+1)**2, thus zero-order has 1, first-order has 4, second-
  10. * order has 9, third-order has 16, and fourth-order has 25.
  11. */
  12. constexpr auto AmbiChannelsFromOrder(std::size_t order) noexcept -> std::size_t
  13. { return (order+1) * (order+1); }
  14. inline constexpr auto MaxAmbiOrder = std::uint8_t{3};
  15. inline constexpr auto MaxAmbiChannels = size_t{AmbiChannelsFromOrder(MaxAmbiOrder)};
  16. /* A bitmask of ambisonic channels for 0 to 4th order. This only specifies up
  17. * to 4th order, which is the highest order a 32-bit mask value can specify (a
  18. * 64-bit mask could handle up to 7th order).
  19. */
  20. inline constexpr uint Ambi0OrderMask{0x00000001};
  21. inline constexpr uint Ambi1OrderMask{0x0000000f};
  22. inline constexpr uint Ambi2OrderMask{0x000001ff};
  23. inline constexpr uint Ambi3OrderMask{0x0000ffff};
  24. inline constexpr uint Ambi4OrderMask{0x01ffffff};
  25. /* A bitmask of ambisonic channels with height information. If none of these
  26. * channels are used/needed, there's no height (e.g. with most surround sound
  27. * speaker setups). This is ACN ordering, with bit 0 being ACN 0, etc.
  28. */
  29. inline constexpr uint AmbiPeriphonicMask{0xfe7ce4};
  30. /* The maximum number of ambisonic channels for 2D (non-periphonic)
  31. * representation. This is 2 per each order above zero-order, plus 1 for zero-
  32. * order. Or simply, o*2 + 1.
  33. */
  34. constexpr auto Ambi2DChannelsFromOrder(std::size_t order) noexcept -> std::size_t
  35. { return order*2 + 1; }
  36. inline constexpr auto MaxAmbi2DChannels = Ambi2DChannelsFromOrder(MaxAmbiOrder);
  37. /* NOTE: These are scale factors as applied to Ambisonics content. Decoder
  38. * coefficients should be divided by these values to get proper scalings.
  39. */
  40. struct AmbiScale {
  41. static constexpr auto FromN3D = std::array<float,MaxAmbiChannels>{
  42. 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  43. 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
  44. };
  45. static constexpr auto FromSN3D = std::array<float,MaxAmbiChannels>{
  46. 1.000000000f, /* ACN 0, sqrt(1) */
  47. 1.732050808f, /* ACN 1, sqrt(3) */
  48. 1.732050808f, /* ACN 2, sqrt(3) */
  49. 1.732050808f, /* ACN 3, sqrt(3) */
  50. 2.236067978f, /* ACN 4, sqrt(5) */
  51. 2.236067978f, /* ACN 5, sqrt(5) */
  52. 2.236067978f, /* ACN 6, sqrt(5) */
  53. 2.236067978f, /* ACN 7, sqrt(5) */
  54. 2.236067978f, /* ACN 8, sqrt(5) */
  55. 2.645751311f, /* ACN 9, sqrt(7) */
  56. 2.645751311f, /* ACN 10, sqrt(7) */
  57. 2.645751311f, /* ACN 11, sqrt(7) */
  58. 2.645751311f, /* ACN 12, sqrt(7) */
  59. 2.645751311f, /* ACN 13, sqrt(7) */
  60. 2.645751311f, /* ACN 14, sqrt(7) */
  61. 2.645751311f, /* ACN 15, sqrt(7) */
  62. };
  63. static constexpr auto FromFuMa = std::array<float,MaxAmbiChannels>{
  64. 1.414213562f, /* ACN 0 (W), sqrt(2) */
  65. 1.732050808f, /* ACN 1 (Y), sqrt(3) */
  66. 1.732050808f, /* ACN 2 (Z), sqrt(3) */
  67. 1.732050808f, /* ACN 3 (X), sqrt(3) */
  68. 1.936491673f, /* ACN 4 (V), sqrt(15)/2 */
  69. 1.936491673f, /* ACN 5 (T), sqrt(15)/2 */
  70. 2.236067978f, /* ACN 6 (R), sqrt(5) */
  71. 1.936491673f, /* ACN 7 (S), sqrt(15)/2 */
  72. 1.936491673f, /* ACN 8 (U), sqrt(15)/2 */
  73. 2.091650066f, /* ACN 9 (Q), sqrt(35/8) */
  74. 1.972026594f, /* ACN 10 (O), sqrt(35)/3 */
  75. 2.231093404f, /* ACN 11 (M), sqrt(224/45) */
  76. 2.645751311f, /* ACN 12 (K), sqrt(7) */
  77. 2.231093404f, /* ACN 13 (L), sqrt(224/45) */
  78. 1.972026594f, /* ACN 14 (N), sqrt(35)/3 */
  79. 2.091650066f, /* ACN 15 (P), sqrt(35/8) */
  80. };
  81. static constexpr auto FromUHJ = std::array<float,MaxAmbiChannels>{
  82. 1.000000000f, /* ACN 0 (W), sqrt(1) */
  83. 1.224744871f, /* ACN 1 (Y), sqrt(3/2) */
  84. 1.224744871f, /* ACN 2 (Z), sqrt(3/2) */
  85. 1.224744871f, /* ACN 3 (X), sqrt(3/2) */
  86. /* Higher orders not relevant for UHJ. */
  87. 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  88. };
  89. /* Retrieves per-order HF scaling factors for "upsampling" ambisonic data. */
  90. static std::array<float,MaxAmbiOrder+1> GetHFOrderScales(const uint src_order,
  91. const uint dev_order, const bool horizontalOnly) noexcept;
  92. static const std::array<std::array<float,MaxAmbiChannels>,4> FirstOrderUp;
  93. static const std::array<std::array<float,MaxAmbiChannels>,4> FirstOrder2DUp;
  94. static const std::array<std::array<float,MaxAmbiChannels>,9> SecondOrderUp;
  95. static const std::array<std::array<float,MaxAmbiChannels>,9> SecondOrder2DUp;
  96. static const std::array<std::array<float,MaxAmbiChannels>,16> ThirdOrderUp;
  97. static const std::array<std::array<float,MaxAmbiChannels>,16> ThirdOrder2DUp;
  98. static const std::array<std::array<float,MaxAmbiChannels>,25> FourthOrder2DUp;
  99. };
  100. struct AmbiIndex {
  101. static constexpr auto FromFuMa = std::array<std::uint8_t,MaxAmbiChannels>{
  102. 0, /* W */
  103. 3, /* X */
  104. 1, /* Y */
  105. 2, /* Z */
  106. 6, /* R */
  107. 7, /* S */
  108. 5, /* T */
  109. 8, /* U */
  110. 4, /* V */
  111. 12, /* K */
  112. 13, /* L */
  113. 11, /* M */
  114. 14, /* N */
  115. 10, /* O */
  116. 15, /* P */
  117. 9, /* Q */
  118. };
  119. static constexpr auto FromFuMa2D = std::array<std::uint8_t,MaxAmbi2DChannels>{
  120. 0, /* W */
  121. 3, /* X */
  122. 1, /* Y */
  123. 8, /* U */
  124. 4, /* V */
  125. 15, /* P */
  126. 9, /* Q */
  127. };
  128. static constexpr auto FromACN = std::array<std::uint8_t,MaxAmbiChannels>{
  129. 0, 1, 2, 3, 4, 5, 6, 7,
  130. 8, 9, 10, 11, 12, 13, 14, 15
  131. };
  132. static constexpr auto FromACN2D = std::array<std::uint8_t,MaxAmbi2DChannels>{
  133. 0, 1,3, 4,8, 9,15
  134. };
  135. static constexpr auto OrderFromChannel = std::array<std::uint8_t,MaxAmbiChannels>{
  136. 0, 1,1,1, 2,2,2,2,2, 3,3,3,3,3,3,3,
  137. };
  138. static constexpr auto OrderFrom2DChannel = std::array<std::uint8_t,MaxAmbi2DChannels>{
  139. 0, 1,1, 2,2, 3,3,
  140. };
  141. };
  142. /**
  143. * Calculates ambisonic encoder coefficients using the X, Y, and Z direction
  144. * components, which must represent a normalized (unit length) vector.
  145. *
  146. * NOTE: The components use ambisonic coordinates. As a result:
  147. *
  148. * Ambisonic Y = OpenAL -X
  149. * Ambisonic Z = OpenAL Y
  150. * Ambisonic X = OpenAL -Z
  151. *
  152. * The components are ordered such that OpenAL's X, Y, and Z are the first,
  153. * second, and third parameters respectively -- simply negate X and Z.
  154. */
  155. constexpr auto CalcAmbiCoeffs(const float y, const float z, const float x)
  156. {
  157. const float xx{x*x}, yy{y*y}, zz{z*z}, xy{x*y}, yz{y*z}, xz{x*z};
  158. return std::array<float,MaxAmbiChannels>{{
  159. /* Zeroth-order */
  160. 1.0f, /* ACN 0 = 1 */
  161. /* First-order */
  162. al::numbers::sqrt3_v<float> * y, /* ACN 1 = sqrt(3) * Y */
  163. al::numbers::sqrt3_v<float> * z, /* ACN 2 = sqrt(3) * Z */
  164. al::numbers::sqrt3_v<float> * x, /* ACN 3 = sqrt(3) * X */
  165. /* Second-order */
  166. 3.872983346e+00f * xy, /* ACN 4 = sqrt(15) * X * Y */
  167. 3.872983346e+00f * yz, /* ACN 5 = sqrt(15) * Y * Z */
  168. 1.118033989e+00f * (3.0f*zz - 1.0f), /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
  169. 3.872983346e+00f * xz, /* ACN 7 = sqrt(15) * X * Z */
  170. 1.936491673e+00f * (xx - yy), /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
  171. /* Third-order */
  172. 2.091650066e+00f * (y*(3.0f*xx - yy)), /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
  173. 1.024695076e+01f * (z*xy), /* ACN 10 = sqrt(105) * Z * X * Y */
  174. 1.620185175e+00f * (y*(5.0f*zz - 1.0f)), /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
  175. 1.322875656e+00f * (z*(5.0f*zz - 3.0f)), /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
  176. 1.620185175e+00f * (x*(5.0f*zz - 1.0f)), /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
  177. 5.123475383e+00f * (z*(xx - yy)), /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
  178. 2.091650066e+00f * (x*(xx - 3.0f*yy)), /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
  179. /* Fourth-order */
  180. /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
  181. /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
  182. /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
  183. /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
  184. /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
  185. /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
  186. /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
  187. /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
  188. /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
  189. }};
  190. }
  191. #endif /* CORE_AMBIDEFS_H */