ambidefs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. };
  93. struct AmbiIndex {
  94. static auto& FromFuMa() noexcept
  95. {
  96. static constexpr const std::array<uint8_t,MaxAmbiChannels> ret{{
  97. 0, /* W */
  98. 3, /* X */
  99. 1, /* Y */
  100. 2, /* Z */
  101. 6, /* R */
  102. 7, /* S */
  103. 5, /* T */
  104. 8, /* U */
  105. 4, /* V */
  106. 12, /* K */
  107. 13, /* L */
  108. 11, /* M */
  109. 14, /* N */
  110. 10, /* O */
  111. 15, /* P */
  112. 9, /* Q */
  113. }};
  114. return ret;
  115. }
  116. static auto& FromFuMa2D() noexcept
  117. {
  118. static constexpr const std::array<uint8_t,MaxAmbi2DChannels> ret{{
  119. 0, /* W */
  120. 3, /* X */
  121. 1, /* Y */
  122. 8, /* U */
  123. 4, /* V */
  124. 15, /* P */
  125. 9, /* Q */
  126. }};
  127. return ret;
  128. }
  129. static auto& FromACN() noexcept
  130. {
  131. static constexpr const std::array<uint8_t,MaxAmbiChannels> ret{{
  132. 0, 1, 2, 3, 4, 5, 6, 7,
  133. 8, 9, 10, 11, 12, 13, 14, 15
  134. }};
  135. return ret;
  136. }
  137. static auto& FromACN2D() noexcept
  138. {
  139. static constexpr const std::array<uint8_t,MaxAmbi2DChannels> ret{{
  140. 0, 1,3, 4,8, 9,15
  141. }};
  142. return ret;
  143. }
  144. static auto& OrderFromChannel() noexcept
  145. {
  146. static constexpr const std::array<uint8_t,MaxAmbiChannels> ret{{
  147. 0, 1,1,1, 2,2,2,2,2, 3,3,3,3,3,3,3,
  148. }};
  149. return ret;
  150. }
  151. static auto& OrderFrom2DChannel() noexcept
  152. {
  153. static constexpr const std::array<uint8_t,MaxAmbi2DChannels> ret{{
  154. 0, 1,1, 2,2, 3,3,
  155. }};
  156. return ret;
  157. }
  158. };
  159. #endif /* CORE_AMBIDEFS_H */