context.h 5.6 KB

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