voice.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "almalloc.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. 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. };
  82. struct VoiceProps {
  83. float Pitch;
  84. float Gain;
  85. float OuterGain;
  86. float MinGain;
  87. float MaxGain;
  88. float InnerAngle;
  89. float OuterAngle;
  90. float RefDistance;
  91. float MaxDistance;
  92. float RolloffFactor;
  93. std::array<float,3> Position;
  94. std::array<float,3> Velocity;
  95. std::array<float,3> Direction;
  96. std::array<float,3> OrientAt;
  97. std::array<float,3> OrientUp;
  98. bool HeadRelative;
  99. DistanceModel mDistanceModel;
  100. Resampler mResampler;
  101. DirectMode DirectChannels;
  102. SpatializeMode mSpatializeMode;
  103. bool DryGainHFAuto;
  104. bool WetGainAuto;
  105. bool WetGainHFAuto;
  106. float OuterGainHF;
  107. float AirAbsorptionFactor;
  108. float RoomRolloffFactor;
  109. float DopplerFactor;
  110. std::array<float,2> StereoPan;
  111. float Radius;
  112. float EnhWidth;
  113. float Panning;
  114. /** Direct filter and auxiliary send info. */
  115. struct DirectData {
  116. float Gain;
  117. float GainHF;
  118. float HFReference;
  119. float GainLF;
  120. float LFReference;
  121. };
  122. DirectData Direct;
  123. struct SendData {
  124. EffectSlot *Slot;
  125. float Gain;
  126. float GainHF;
  127. float HFReference;
  128. float GainLF;
  129. float LFReference;
  130. };
  131. std::array<SendData,MaxSendCount> Send;
  132. };
  133. struct VoicePropsItem : public VoiceProps {
  134. std::atomic<VoicePropsItem*> next{nullptr};
  135. };
  136. enum : uint {
  137. VoiceIsStatic,
  138. VoiceIsCallback,
  139. VoiceIsAmbisonic,
  140. VoiceCallbackStopped,
  141. VoiceIsFading,
  142. VoiceHasHrtf,
  143. VoiceHasNfc,
  144. VoiceFlagCount
  145. };
  146. struct Voice {
  147. enum State {
  148. Stopped,
  149. Playing,
  150. Stopping,
  151. Pending
  152. };
  153. std::atomic<VoicePropsItem*> mUpdate{nullptr};
  154. VoiceProps mProps{};
  155. std::atomic<uint> mSourceID{0u};
  156. std::atomic<State> mPlayState{Stopped};
  157. std::atomic<bool> mPendingChange{false};
  158. /**
  159. * Source offset in samples, relative to the currently playing buffer, NOT
  160. * the whole queue.
  161. */
  162. std::atomic<int> mPosition{};
  163. /** Fractional (fixed-point) offset to the next sample. */
  164. std::atomic<uint> mPositionFrac{};
  165. /* Current buffer queue item being played. */
  166. std::atomic<VoiceBufferItem*> mCurrentBuffer{};
  167. /* Buffer queue item to loop to at end of queue (will be NULL for non-
  168. * looping voices).
  169. */
  170. std::atomic<VoiceBufferItem*> mLoopBuffer{};
  171. std::chrono::nanoseconds mStartTime{};
  172. /* Properties for the attached buffer(s). */
  173. FmtChannels mFmtChannels{};
  174. FmtType mFmtType{};
  175. uint mFrequency{};
  176. uint mFrameStep{}; /**< In steps of the sample type size. */
  177. uint mBytesPerBlock{}; /**< Or for PCM formats, BytesPerFrame. */
  178. uint mSamplesPerBlock{}; /**< Always 1 for PCM formats. */
  179. AmbiLayout mAmbiLayout{};
  180. AmbiScaling mAmbiScaling{};
  181. uint mAmbiOrder{};
  182. std::unique_ptr<DecoderBase> mDecoder;
  183. uint mDecoderPadding{};
  184. /** Current target parameters used for mixing. */
  185. uint mStep{0};
  186. ResamplerFunc mResampler{};
  187. InterpState mResampleState{};
  188. std::bitset<VoiceFlagCount> mFlags{};
  189. uint mNumCallbackBlocks{0};
  190. uint mCallbackBlockBase{0};
  191. struct TargetData {
  192. int FilterType{};
  193. al::span<FloatBufferLine> Buffer;
  194. };
  195. TargetData mDirect;
  196. std::array<TargetData,MaxSendCount> mSend;
  197. /* The first MaxResamplerPadding/2 elements are the sample history from the
  198. * previous mix, with an additional MaxResamplerPadding/2 elements that are
  199. * now current (which may be overwritten if the buffer data is still
  200. * available).
  201. */
  202. using HistoryLine = std::array<float,MaxResamplerPadding>;
  203. al::vector<HistoryLine,16> mPrevSamples{2};
  204. struct ChannelData {
  205. float mAmbiHFScale{}, mAmbiLFScale{};
  206. BandSplitter mAmbiSplitter;
  207. DirectParams mDryParams;
  208. std::array<SendParams,MaxSendCount> mWetParams;
  209. };
  210. al::vector<ChannelData> mChans{2};
  211. Voice() = default;
  212. ~Voice() = default;
  213. Voice(const Voice&) = delete;
  214. Voice& operator=(const Voice&) = delete;
  215. void mix(const State vstate, ContextBase *Context, const std::chrono::nanoseconds deviceTime,
  216. const uint SamplesToDo);
  217. void prepare(DeviceBase *device);
  218. static void InitMixer(std::optional<std::string> resopt);
  219. };
  220. inline Resampler ResamplerDefault{Resampler::Gaussian};
  221. #endif /* CORE_VOICE_H */