🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
6.0 KiB

  1. #ifndef CORE_VOICE_H
  2. #define CORE_VOICE_H
  3. #include <array>
  4. #include <atomic>
  5. #include <bitset>
  6. #include <memory>
  7. #include <stddef.h>
  8. #include <string>
  9. #include "albyte.h"
  10. #include "almalloc.h"
  11. #include "aloptional.h"
  12. #include "alspan.h"
  13. #include "bufferline.h"
  14. #include "buffer_storage.h"
  15. #include "devformat.h"
  16. #include "filters/biquad.h"
  17. #include "filters/nfc.h"
  18. #include "filters/splitter.h"
  19. #include "mixer/defs.h"
  20. #include "mixer/hrtfdefs.h"
  21. #include "resampler_limits.h"
  22. #include "uhjfilter.h"
  23. #include "vector.h"
  24. struct ContextBase;
  25. struct DeviceBase;
  26. struct EffectSlot;
  27. enum class DistanceModel : unsigned char;
  28. using uint = unsigned int;
  29. #define MAX_SENDS 6
  30. enum class SpatializeMode : unsigned char {
  31. Off,
  32. On,
  33. Auto
  34. };
  35. enum class DirectMode : unsigned char {
  36. Off,
  37. DropMismatch,
  38. RemixMismatch
  39. };
  40. /* Maximum number of extra source samples that may need to be loaded, for
  41. * resampling or conversion purposes.
  42. */
  43. constexpr uint MaxPostVoiceLoad{MaxResamplerEdge + UhjDecoder::sFilterDelay};
  44. enum {
  45. AF_None = 0,
  46. AF_LowPass = 1,
  47. AF_HighPass = 2,
  48. AF_BandPass = AF_LowPass | AF_HighPass
  49. };
  50. struct DirectParams {
  51. BiquadFilter LowPass;
  52. BiquadFilter HighPass;
  53. NfcFilter NFCtrlFilter;
  54. struct {
  55. HrtfFilter Old;
  56. HrtfFilter Target;
  57. alignas(16) std::array<float,HrtfHistoryLength> History;
  58. } Hrtf;
  59. struct {
  60. std::array<float,MAX_OUTPUT_CHANNELS> Current;
  61. std::array<float,MAX_OUTPUT_CHANNELS> Target;
  62. } Gains;
  63. };
  64. struct SendParams {
  65. BiquadFilter LowPass;
  66. BiquadFilter HighPass;
  67. struct {
  68. std::array<float,MAX_OUTPUT_CHANNELS> Current;
  69. std::array<float,MAX_OUTPUT_CHANNELS> Target;
  70. } Gains;
  71. };
  72. struct VoiceBufferItem {
  73. std::atomic<VoiceBufferItem*> mNext{nullptr};
  74. CallbackType mCallback{nullptr};
  75. void *mUserData{nullptr};
  76. uint mSampleLen{0u};
  77. uint mLoopStart{0u};
  78. uint mLoopEnd{0u};
  79. al::byte *mSamples{nullptr};
  80. };
  81. struct VoiceProps {
  82. float Pitch;
  83. float Gain;
  84. float OuterGain;
  85. float MinGain;
  86. float MaxGain;
  87. float InnerAngle;
  88. float OuterAngle;
  89. float RefDistance;
  90. float MaxDistance;
  91. float RolloffFactor;
  92. std::array<float,3> Position;
  93. std::array<float,3> Velocity;
  94. std::array<float,3> Direction;
  95. std::array<float,3> OrientAt;
  96. std::array<float,3> OrientUp;
  97. bool HeadRelative;
  98. DistanceModel mDistanceModel;
  99. Resampler mResampler;
  100. DirectMode DirectChannels;
  101. SpatializeMode mSpatializeMode;
  102. bool DryGainHFAuto;
  103. bool WetGainAuto;
  104. bool WetGainHFAuto;
  105. float OuterGainHF;
  106. float AirAbsorptionFactor;
  107. float RoomRolloffFactor;
  108. float DopplerFactor;
  109. std::array<float,2> StereoPan;
  110. float Radius;
  111. float EnhWidth;
  112. /** Direct filter and auxiliary send info. */
  113. struct {
  114. float Gain;
  115. float GainHF;
  116. float HFReference;
  117. float GainLF;
  118. float LFReference;
  119. } Direct;
  120. struct SendData {
  121. EffectSlot *Slot;
  122. float Gain;
  123. float GainHF;
  124. float HFReference;
  125. float GainLF;
  126. float LFReference;
  127. } Send[MAX_SENDS];
  128. };
  129. struct VoicePropsItem : public VoiceProps {
  130. std::atomic<VoicePropsItem*> next{nullptr};
  131. DEF_NEWDEL(VoicePropsItem)
  132. };
  133. enum : uint {
  134. VoiceIsStatic,
  135. VoiceIsCallback,
  136. VoiceIsAmbisonic,
  137. VoiceCallbackStopped,
  138. VoiceIsFading,
  139. VoiceHasHrtf,
  140. VoiceHasNfc,
  141. VoiceFlagCount
  142. };
  143. struct Voice {
  144. enum State {
  145. Stopped,
  146. Playing,
  147. Stopping,
  148. Pending
  149. };
  150. std::atomic<VoicePropsItem*> mUpdate{nullptr};
  151. VoiceProps mProps;
  152. std::atomic<uint> mSourceID{0u};
  153. std::atomic<State> mPlayState{Stopped};
  154. std::atomic<bool> mPendingChange{false};
  155. /**
  156. * Source offset in samples, relative to the currently playing buffer, NOT
  157. * the whole queue.
  158. */
  159. std::atomic<uint> mPosition;
  160. /** Fractional (fixed-point) offset to the next sample. */
  161. std::atomic<uint> mPositionFrac;
  162. /* Current buffer queue item being played. */
  163. std::atomic<VoiceBufferItem*> mCurrentBuffer;
  164. /* Buffer queue item to loop to at end of queue (will be NULL for non-
  165. * looping voices).
  166. */
  167. std::atomic<VoiceBufferItem*> mLoopBuffer;
  168. /* Properties for the attached buffer(s). */
  169. FmtChannels mFmtChannels;
  170. FmtType mFmtType;
  171. uint mFrequency;
  172. uint mFrameStep; /**< In steps of the sample type size. */
  173. uint mFrameSize; /**< In bytes. */
  174. AmbiLayout mAmbiLayout;
  175. AmbiScaling mAmbiScaling;
  176. uint mAmbiOrder;
  177. std::unique_ptr<UhjDecoder> mDecoder;
  178. UhjDecoder::DecoderFunc mDecoderFunc{};
  179. /** Current target parameters used for mixing. */
  180. uint mStep{0};
  181. ResamplerFunc mResampler;
  182. InterpState mResampleState;
  183. std::bitset<VoiceFlagCount> mFlags{};
  184. uint mNumCallbackSamples{0};
  185. struct TargetData {
  186. int FilterType;
  187. al::span<FloatBufferLine> Buffer;
  188. };
  189. TargetData mDirect;
  190. std::array<TargetData,MAX_SENDS> mSend;
  191. /* The first MaxResamplerPadding/2 elements are the sample history from the
  192. * previous mix, with an additional MaxResamplerPadding/2 elements that are
  193. * now current (which may be overwritten if the buffer data is still
  194. * available).
  195. */
  196. using HistoryLine = std::array<float,MaxResamplerPadding>;
  197. al::vector<HistoryLine,16> mPrevSamples{2};
  198. struct ChannelData {
  199. float mAmbiHFScale, mAmbiLFScale;
  200. BandSplitter mAmbiSplitter;
  201. DirectParams mDryParams;
  202. std::array<SendParams,MAX_SENDS> mWetParams;
  203. };
  204. al::vector<ChannelData> mChans{2};
  205. Voice() = default;
  206. ~Voice() = default;
  207. Voice(const Voice&) = delete;
  208. Voice& operator=(const Voice&) = delete;
  209. void mix(const State vstate, ContextBase *Context, const uint SamplesToDo);
  210. void prepare(DeviceBase *device);
  211. static void InitMixer(al::optional<std::string> resampler);
  212. DEF_NEWDEL(Voice)
  213. };
  214. extern Resampler ResamplerDefault;
  215. #endif /* CORE_VOICE_H */