voice.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #ifndef VOICE_H
  2. #define VOICE_H
  3. #include <array>
  4. #include <atomic>
  5. #include "almalloc.h"
  6. #include "alspan.h"
  7. #include "alu.h"
  8. #include "buffer_storage.h"
  9. #include "core/bufferline.h"
  10. #include "core/devformat.h"
  11. #include "core/filters/biquad.h"
  12. #include "core/filters/nfc.h"
  13. #include "core/filters/splitter.h"
  14. #include "core/mixer/defs.h"
  15. #include "core/mixer/hrtfdefs.h"
  16. #include "vector.h"
  17. struct ALCcontext;
  18. struct EffectSlot;
  19. enum class DistanceModel : unsigned char;
  20. using uint = unsigned int;
  21. enum class SpatializeMode : unsigned char {
  22. Off,
  23. On,
  24. Auto
  25. };
  26. enum class DirectMode : unsigned char {
  27. Off,
  28. DropMismatch,
  29. RemixMismatch
  30. };
  31. enum {
  32. AF_None = 0,
  33. AF_LowPass = 1,
  34. AF_HighPass = 2,
  35. AF_BandPass = AF_LowPass | AF_HighPass
  36. };
  37. struct DirectParams {
  38. BiquadFilter LowPass;
  39. BiquadFilter HighPass;
  40. NfcFilter NFCtrlFilter;
  41. struct {
  42. HrtfFilter Old;
  43. HrtfFilter Target;
  44. alignas(16) std::array<float,HrtfHistoryLength> History;
  45. } Hrtf;
  46. struct {
  47. std::array<float,MAX_OUTPUT_CHANNELS> Current;
  48. std::array<float,MAX_OUTPUT_CHANNELS> Target;
  49. } Gains;
  50. };
  51. struct SendParams {
  52. BiquadFilter LowPass;
  53. BiquadFilter HighPass;
  54. struct {
  55. std::array<float,MAX_OUTPUT_CHANNELS> Current;
  56. std::array<float,MAX_OUTPUT_CHANNELS> Target;
  57. } Gains;
  58. };
  59. struct VoiceBufferItem {
  60. std::atomic<VoiceBufferItem*> mNext{nullptr};
  61. CallbackType mCallback{nullptr};
  62. void *mUserData{nullptr};
  63. uint mSampleLen{0u};
  64. uint mLoopStart{0u};
  65. uint mLoopEnd{0u};
  66. al::byte *mSamples{nullptr};
  67. };
  68. struct VoiceProps {
  69. float Pitch;
  70. float Gain;
  71. float OuterGain;
  72. float MinGain;
  73. float MaxGain;
  74. float InnerAngle;
  75. float OuterAngle;
  76. float RefDistance;
  77. float MaxDistance;
  78. float RolloffFactor;
  79. std::array<float,3> Position;
  80. std::array<float,3> Velocity;
  81. std::array<float,3> Direction;
  82. std::array<float,3> OrientAt;
  83. std::array<float,3> OrientUp;
  84. bool HeadRelative;
  85. DistanceModel mDistanceModel;
  86. Resampler mResampler;
  87. DirectMode DirectChannels;
  88. SpatializeMode mSpatializeMode;
  89. bool DryGainHFAuto;
  90. bool WetGainAuto;
  91. bool WetGainHFAuto;
  92. float OuterGainHF;
  93. float AirAbsorptionFactor;
  94. float RoomRolloffFactor;
  95. float DopplerFactor;
  96. std::array<float,2> StereoPan;
  97. float Radius;
  98. /** Direct filter and auxiliary send info. */
  99. struct {
  100. float Gain;
  101. float GainHF;
  102. float HFReference;
  103. float GainLF;
  104. float LFReference;
  105. } Direct;
  106. struct SendData {
  107. EffectSlot *Slot;
  108. float Gain;
  109. float GainHF;
  110. float HFReference;
  111. float GainLF;
  112. float LFReference;
  113. } Send[MAX_SENDS];
  114. };
  115. struct VoicePropsItem : public VoiceProps {
  116. std::atomic<VoicePropsItem*> next{nullptr};
  117. DEF_NEWDEL(VoicePropsItem)
  118. };
  119. constexpr uint VoiceIsStatic{ 1u<<0};
  120. constexpr uint VoiceIsCallback{ 1u<<1};
  121. constexpr uint VoiceIsAmbisonic{ 1u<<2}; /* Needs HF scaling for ambisonic upsampling. */
  122. constexpr uint VoiceCallbackStopped{1u<<3};
  123. constexpr uint VoiceIsFading{ 1u<<4}; /* Use gain stepping for smooth transitions. */
  124. constexpr uint VoiceHasHrtf{ 1u<<5};
  125. constexpr uint VoiceHasNfc{ 1u<<6};
  126. struct Voice {
  127. enum State {
  128. Stopped,
  129. Playing,
  130. Stopping,
  131. Pending
  132. };
  133. std::atomic<VoicePropsItem*> mUpdate{nullptr};
  134. VoiceProps mProps;
  135. std::atomic<uint> mSourceID{0u};
  136. std::atomic<State> mPlayState{Stopped};
  137. std::atomic<bool> mPendingChange{false};
  138. /**
  139. * Source offset in samples, relative to the currently playing buffer, NOT
  140. * the whole queue.
  141. */
  142. std::atomic<uint> mPosition;
  143. /** Fractional (fixed-point) offset to the next sample. */
  144. std::atomic<uint> mPositionFrac;
  145. /* Current buffer queue item being played. */
  146. std::atomic<VoiceBufferItem*> mCurrentBuffer;
  147. /* Buffer queue item to loop to at end of queue (will be NULL for non-
  148. * looping voices).
  149. */
  150. std::atomic<VoiceBufferItem*> mLoopBuffer;
  151. /* Properties for the attached buffer(s). */
  152. FmtChannels mFmtChannels;
  153. FmtType mFmtType;
  154. uint mFrequency;
  155. uint mSampleSize;
  156. AmbiLayout mAmbiLayout;
  157. AmbiScaling mAmbiScaling;
  158. uint mAmbiOrder;
  159. /** Current target parameters used for mixing. */
  160. uint mStep{0};
  161. ResamplerFunc mResampler;
  162. InterpState mResampleState;
  163. uint mFlags{};
  164. uint mNumCallbackSamples{0};
  165. struct TargetData {
  166. int FilterType;
  167. al::span<FloatBufferLine> Buffer;
  168. };
  169. TargetData mDirect;
  170. std::array<TargetData,MAX_SENDS> mSend;
  171. struct ChannelData {
  172. alignas(16) std::array<float,MaxResamplerPadding> mPrevSamples;
  173. float mAmbiScale;
  174. BandSplitter mAmbiSplitter;
  175. DirectParams mDryParams;
  176. std::array<SendParams,MAX_SENDS> mWetParams;
  177. };
  178. al::vector<ChannelData> mChans{2};
  179. Voice() = default;
  180. ~Voice() { delete mUpdate.exchange(nullptr, std::memory_order_acq_rel); }
  181. Voice(const Voice&) = delete;
  182. Voice& operator=(const Voice&) = delete;
  183. void mix(const State vstate, ALCcontext *Context, const uint SamplesToDo);
  184. DEF_NEWDEL(Voice)
  185. };
  186. extern Resampler ResamplerDefault;
  187. #endif /* VOICE_H */