context.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifndef CORE_CONTEXT_H
  2. #define CORE_CONTEXT_H
  3. #include <array>
  4. #include <atomic>
  5. #include <bitset>
  6. #include <cstddef>
  7. #include <memory>
  8. #include <thread>
  9. #include <vector>
  10. #include "almalloc.h"
  11. #include "alsem.h"
  12. #include "alspan.h"
  13. #include "async_event.h"
  14. #include "atomic.h"
  15. #include "flexarray.h"
  16. #include "opthelpers.h"
  17. #include "vecmat.h"
  18. struct DeviceBase;
  19. struct EffectSlot;
  20. struct EffectSlotProps;
  21. struct RingBuffer;
  22. struct Voice;
  23. struct VoiceChange;
  24. struct VoicePropsItem;
  25. inline constexpr float SpeedOfSoundMetersPerSec{343.3f};
  26. inline constexpr float AirAbsorbGainHF{0.99426f}; /* -0.05dB */
  27. enum class DistanceModel : unsigned char {
  28. Disable,
  29. Inverse, InverseClamped,
  30. Linear, LinearClamped,
  31. Exponent, ExponentClamped,
  32. Default = InverseClamped
  33. };
  34. struct ContextProps {
  35. std::array<float,3> Position;
  36. std::array<float,3> Velocity;
  37. std::array<float,3> OrientAt;
  38. std::array<float,3> OrientUp;
  39. float Gain;
  40. float MetersPerUnit;
  41. float AirAbsorptionGainHF;
  42. float DopplerFactor;
  43. float DopplerVelocity;
  44. float SpeedOfSound;
  45. bool SourceDistanceModel;
  46. DistanceModel mDistanceModel;
  47. std::atomic<ContextProps*> next;
  48. };
  49. struct ContextParams {
  50. /* Pointer to the most recent property values that are awaiting an update. */
  51. std::atomic<ContextProps*> ContextUpdate{nullptr};
  52. alu::Vector Position{};
  53. alu::Matrix Matrix{alu::Matrix::Identity()};
  54. alu::Vector Velocity{};
  55. float Gain{1.0f};
  56. float MetersPerUnit{1.0f};
  57. float AirAbsorptionGainHF{AirAbsorbGainHF};
  58. float DopplerFactor{1.0f};
  59. float SpeedOfSound{SpeedOfSoundMetersPerSec}; /* in units per sec! */
  60. bool SourceDistanceModel{false};
  61. DistanceModel mDistanceModel{};
  62. };
  63. struct ContextBase {
  64. DeviceBase *const mDevice;
  65. /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
  66. * indicates if updates are currently happening).
  67. */
  68. std::atomic<unsigned int> mUpdateCount{0u};
  69. std::atomic<bool> mHoldUpdates{false};
  70. std::atomic<bool> mStopVoicesOnDisconnect{true};
  71. float mGainBoost{1.0f};
  72. /* Linked lists of unused property containers, free to use for future
  73. * updates.
  74. */
  75. std::atomic<ContextProps*> mFreeContextProps{nullptr};
  76. std::atomic<VoicePropsItem*> mFreeVoiceProps{nullptr};
  77. std::atomic<EffectSlotProps*> mFreeEffectSlotProps{nullptr};
  78. /* The voice change tail is the beginning of the "free" elements, up to and
  79. * *excluding* the current. If tail==current, there's no free elements and
  80. * new ones need to be allocated. The current voice change is the element
  81. * last processed, and any after are pending.
  82. */
  83. VoiceChange *mVoiceChangeTail{};
  84. std::atomic<VoiceChange*> mCurrentVoiceChange{};
  85. void allocVoiceChanges();
  86. void allocVoiceProps();
  87. void allocEffectSlotProps();
  88. void allocContextProps();
  89. ContextParams mParams;
  90. using VoiceArray = al::FlexArray<Voice*>;
  91. al::atomic_unique_ptr<VoiceArray> mVoices{};
  92. std::atomic<size_t> mActiveVoiceCount{};
  93. void allocVoices(size_t addcount);
  94. [[nodiscard]] auto getVoicesSpan() const noexcept -> al::span<Voice*>
  95. {
  96. return {mVoices.load(std::memory_order_relaxed)->data(),
  97. mActiveVoiceCount.load(std::memory_order_relaxed)};
  98. }
  99. [[nodiscard]] auto getVoicesSpanAcquired() const noexcept -> al::span<Voice*>
  100. {
  101. return {mVoices.load(std::memory_order_acquire)->data(),
  102. mActiveVoiceCount.load(std::memory_order_acquire)};
  103. }
  104. using EffectSlotArray = al::FlexArray<EffectSlot*>;
  105. /* This array is split in half. The front half is the list of activated
  106. * effect slots as set by the app, and the back half is the same list but
  107. * sorted to ensure later effect slots are fed by earlier ones.
  108. */
  109. al::atomic_unique_ptr<EffectSlotArray> mActiveAuxSlots;
  110. std::thread mEventThread;
  111. al::semaphore mEventSem;
  112. std::unique_ptr<RingBuffer> mAsyncEvents;
  113. using AsyncEventBitset = std::bitset<al::to_underlying(AsyncEnableBits::Count)>;
  114. std::atomic<AsyncEventBitset> mEnabledEvts{0u};
  115. /* Asynchronous voice change actions are processed as a linked list of
  116. * VoiceChange objects by the mixer, which is atomically appended to.
  117. * However, to avoid allocating each object individually, they're allocated
  118. * in clusters that are stored in a vector for easy automatic cleanup.
  119. */
  120. using VoiceChangeCluster = std::unique_ptr<std::array<VoiceChange,128>>;
  121. std::vector<VoiceChangeCluster> mVoiceChangeClusters;
  122. using VoiceCluster = std::unique_ptr<std::array<Voice,32>>;
  123. std::vector<VoiceCluster> mVoiceClusters;
  124. using VoicePropsCluster = std::unique_ptr<std::array<VoicePropsItem,32>>;
  125. std::vector<VoicePropsCluster> mVoicePropClusters;
  126. EffectSlot *getEffectSlot();
  127. using EffectSlotCluster = std::unique_ptr<std::array<EffectSlot,4>>;
  128. std::vector<EffectSlotCluster> mEffectSlotClusters;
  129. using EffectSlotPropsCluster = std::unique_ptr<std::array<EffectSlotProps,4>>;
  130. std::vector<EffectSlotPropsCluster> mEffectSlotPropClusters;
  131. /* This could be greater than 2, but there should be no way there can be
  132. * more than two context property updates in use simultaneously.
  133. */
  134. using ContextPropsCluster = std::unique_ptr<std::array<ContextProps,2>>;
  135. std::vector<ContextPropsCluster> mContextPropClusters;
  136. ContextBase(DeviceBase *device);
  137. ContextBase(const ContextBase&) = delete;
  138. ContextBase& operator=(const ContextBase&) = delete;
  139. ~ContextBase();
  140. };
  141. #endif /* CORE_CONTEXT_H */