base.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "config.h"
  2. #include "base.h"
  3. #include <array>
  4. #include <atomic>
  5. #include <utility>
  6. #include "core/devformat.h"
  7. namespace al {
  8. auto backend_exception::make_string(fmt::string_view fmt, fmt::format_args args) -> std::string
  9. { return fmt::vformat(fmt, std::move(args)); }
  10. backend_exception::~backend_exception() = default;
  11. } // namespace al
  12. bool BackendBase::reset()
  13. { throw al::backend_exception{al::backend_error::DeviceError, "Invalid BackendBase call"}; }
  14. void BackendBase::captureSamples(std::byte*, uint)
  15. { }
  16. uint BackendBase::availableSamples()
  17. { return 0; }
  18. ClockLatency BackendBase::getClockLatency()
  19. {
  20. ClockLatency ret{};
  21. uint refcount;
  22. do {
  23. refcount = mDevice->waitForMix();
  24. ret.ClockTime = mDevice->getClockTime();
  25. std::atomic_thread_fence(std::memory_order_acquire);
  26. } while(refcount != mDevice->mMixCount.load(std::memory_order_relaxed));
  27. /* NOTE: The device will generally have about all but one periods filled at
  28. * any given time during playback. Without a more accurate measurement from
  29. * the output, this is an okay approximation.
  30. */
  31. ret.Latency = std::chrono::seconds{mDevice->mBufferSize - mDevice->mUpdateSize};
  32. ret.Latency /= mDevice->mSampleRate;
  33. return ret;
  34. }
  35. void BackendBase::setDefaultWFXChannelOrder() const
  36. {
  37. mDevice->RealOut.ChannelIndex.fill(InvalidChannelIndex);
  38. switch(mDevice->FmtChans)
  39. {
  40. case DevFmtMono:
  41. mDevice->RealOut.ChannelIndex[FrontCenter] = 0;
  42. break;
  43. case DevFmtStereo:
  44. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  45. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  46. break;
  47. case DevFmtQuad:
  48. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  49. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  50. mDevice->RealOut.ChannelIndex[BackLeft] = 2;
  51. mDevice->RealOut.ChannelIndex[BackRight] = 3;
  52. break;
  53. case DevFmtX51:
  54. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  55. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  56. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  57. mDevice->RealOut.ChannelIndex[LFE] = 3;
  58. mDevice->RealOut.ChannelIndex[SideLeft] = 4;
  59. mDevice->RealOut.ChannelIndex[SideRight] = 5;
  60. break;
  61. case DevFmtX61:
  62. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  63. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  64. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  65. mDevice->RealOut.ChannelIndex[LFE] = 3;
  66. mDevice->RealOut.ChannelIndex[BackCenter] = 4;
  67. mDevice->RealOut.ChannelIndex[SideLeft] = 5;
  68. mDevice->RealOut.ChannelIndex[SideRight] = 6;
  69. break;
  70. case DevFmtX71:
  71. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  72. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  73. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  74. mDevice->RealOut.ChannelIndex[LFE] = 3;
  75. mDevice->RealOut.ChannelIndex[BackLeft] = 4;
  76. mDevice->RealOut.ChannelIndex[BackRight] = 5;
  77. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  78. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  79. break;
  80. case DevFmtX714:
  81. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  82. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  83. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  84. mDevice->RealOut.ChannelIndex[LFE] = 3;
  85. mDevice->RealOut.ChannelIndex[BackLeft] = 4;
  86. mDevice->RealOut.ChannelIndex[BackRight] = 5;
  87. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  88. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  89. mDevice->RealOut.ChannelIndex[TopFrontLeft] = 8;
  90. mDevice->RealOut.ChannelIndex[TopFrontRight] = 9;
  91. mDevice->RealOut.ChannelIndex[TopBackLeft] = 10;
  92. mDevice->RealOut.ChannelIndex[TopBackRight] = 11;
  93. break;
  94. case DevFmtX7144:
  95. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  96. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  97. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  98. mDevice->RealOut.ChannelIndex[LFE] = 3;
  99. mDevice->RealOut.ChannelIndex[BackLeft] = 4;
  100. mDevice->RealOut.ChannelIndex[BackRight] = 5;
  101. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  102. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  103. mDevice->RealOut.ChannelIndex[TopFrontLeft] = 8;
  104. mDevice->RealOut.ChannelIndex[TopFrontRight] = 9;
  105. mDevice->RealOut.ChannelIndex[TopBackLeft] = 10;
  106. mDevice->RealOut.ChannelIndex[TopBackRight] = 11;
  107. mDevice->RealOut.ChannelIndex[BottomFrontLeft] = 12;
  108. mDevice->RealOut.ChannelIndex[BottomFrontRight] = 13;
  109. mDevice->RealOut.ChannelIndex[BottomBackLeft] = 14;
  110. mDevice->RealOut.ChannelIndex[BottomBackRight] = 15;
  111. break;
  112. case DevFmtX3D71:
  113. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  114. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  115. mDevice->RealOut.ChannelIndex[FrontCenter] = 2;
  116. mDevice->RealOut.ChannelIndex[LFE] = 3;
  117. mDevice->RealOut.ChannelIndex[Aux0] = 4;
  118. mDevice->RealOut.ChannelIndex[Aux1] = 5;
  119. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  120. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  121. break;
  122. case DevFmtAmbi3D:
  123. break;
  124. }
  125. }
  126. void BackendBase::setDefaultChannelOrder() const
  127. {
  128. mDevice->RealOut.ChannelIndex.fill(InvalidChannelIndex);
  129. switch(mDevice->FmtChans)
  130. {
  131. case DevFmtX51:
  132. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  133. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  134. mDevice->RealOut.ChannelIndex[SideLeft] = 2;
  135. mDevice->RealOut.ChannelIndex[SideRight] = 3;
  136. mDevice->RealOut.ChannelIndex[FrontCenter] = 4;
  137. mDevice->RealOut.ChannelIndex[LFE] = 5;
  138. return;
  139. case DevFmtX71:
  140. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  141. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  142. mDevice->RealOut.ChannelIndex[BackLeft] = 2;
  143. mDevice->RealOut.ChannelIndex[BackRight] = 3;
  144. mDevice->RealOut.ChannelIndex[FrontCenter] = 4;
  145. mDevice->RealOut.ChannelIndex[LFE] = 5;
  146. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  147. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  148. return;
  149. case DevFmtX714:
  150. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  151. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  152. mDevice->RealOut.ChannelIndex[BackLeft] = 2;
  153. mDevice->RealOut.ChannelIndex[BackRight] = 3;
  154. mDevice->RealOut.ChannelIndex[FrontCenter] = 4;
  155. mDevice->RealOut.ChannelIndex[LFE] = 5;
  156. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  157. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  158. mDevice->RealOut.ChannelIndex[TopFrontLeft] = 8;
  159. mDevice->RealOut.ChannelIndex[TopFrontRight] = 9;
  160. mDevice->RealOut.ChannelIndex[TopBackLeft] = 10;
  161. mDevice->RealOut.ChannelIndex[TopBackRight] = 11;
  162. break;
  163. case DevFmtX7144:
  164. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  165. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  166. mDevice->RealOut.ChannelIndex[BackLeft] = 2;
  167. mDevice->RealOut.ChannelIndex[BackRight] = 3;
  168. mDevice->RealOut.ChannelIndex[FrontCenter] = 4;
  169. mDevice->RealOut.ChannelIndex[LFE] = 5;
  170. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  171. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  172. mDevice->RealOut.ChannelIndex[TopFrontLeft] = 8;
  173. mDevice->RealOut.ChannelIndex[TopFrontRight] = 9;
  174. mDevice->RealOut.ChannelIndex[TopBackLeft] = 10;
  175. mDevice->RealOut.ChannelIndex[TopBackRight] = 11;
  176. mDevice->RealOut.ChannelIndex[BottomFrontLeft] = 12;
  177. mDevice->RealOut.ChannelIndex[BottomFrontRight] = 13;
  178. mDevice->RealOut.ChannelIndex[BottomBackLeft] = 14;
  179. mDevice->RealOut.ChannelIndex[BottomBackRight] = 15;
  180. break;
  181. case DevFmtX3D71:
  182. mDevice->RealOut.ChannelIndex[FrontLeft] = 0;
  183. mDevice->RealOut.ChannelIndex[FrontRight] = 1;
  184. mDevice->RealOut.ChannelIndex[Aux0] = 2;
  185. mDevice->RealOut.ChannelIndex[Aux1] = 3;
  186. mDevice->RealOut.ChannelIndex[FrontCenter] = 4;
  187. mDevice->RealOut.ChannelIndex[LFE] = 5;
  188. mDevice->RealOut.ChannelIndex[SideLeft] = 6;
  189. mDevice->RealOut.ChannelIndex[SideRight] = 7;
  190. return;
  191. /* Same as WFX order */
  192. case DevFmtMono:
  193. case DevFmtStereo:
  194. case DevFmtQuad:
  195. case DevFmtX61:
  196. case DevFmtAmbi3D:
  197. setDefaultWFXChannelOrder();
  198. break;
  199. }
  200. }