🛠️🐜 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.

187 lines
6.3 KiB

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