voice.h 6.3 KB

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