hrtf.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef CORE_HRTF_H
  2. #define CORE_HRTF_H
  3. #include <array>
  4. #include <cstddef>
  5. #include <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "almalloc.h"
  11. #include "alspan.h"
  12. #include "ambidefs.h"
  13. #include "bufferline.h"
  14. #include "flexarray.h"
  15. #include "intrusive_ptr.h"
  16. #include "mixer/hrtfdefs.h"
  17. struct alignas(16) HrtfStore {
  18. std::atomic<uint> mRef{};
  19. uint mSampleRate : 24;
  20. uint mIrSize : 8;
  21. struct Field {
  22. float distance;
  23. ubyte evCount;
  24. };
  25. /* NOTE: Fields are stored *backwards*. field[0] is the farthest field, and
  26. * field[fdCount-1] is the nearest.
  27. */
  28. al::span<const Field> mFields;
  29. struct Elevation {
  30. ushort azCount;
  31. ushort irOffset;
  32. };
  33. al::span<Elevation> mElev;
  34. al::span<const HrirArray> mCoeffs;
  35. al::span<const ubyte2> mDelays;
  36. void getCoeffs(float elevation, float azimuth, float distance, float spread,
  37. const HrirSpan coeffs, const al::span<uint,2> delays) const;
  38. void add_ref();
  39. void dec_ref();
  40. void *operator new(size_t) = delete;
  41. void *operator new[](size_t) = delete;
  42. void operator delete[](void*) noexcept = delete;
  43. void operator delete(gsl::owner<void*> block, void*) noexcept
  44. { ::operator delete[](block, std::align_val_t{alignof(HrtfStore)}); }
  45. void operator delete(gsl::owner<void*> block) noexcept
  46. { ::operator delete[](block, std::align_val_t{alignof(HrtfStore)}); }
  47. };
  48. using HrtfStorePtr = al::intrusive_ptr<HrtfStore>;
  49. struct EvRadians { float value; };
  50. struct AzRadians { float value; };
  51. struct AngularPoint {
  52. EvRadians Elev;
  53. AzRadians Azim;
  54. };
  55. struct DirectHrtfState {
  56. std::array<float,BufferLineSize> mTemp{};
  57. /* HRTF filter state for dry buffer content */
  58. uint mIrSize{0};
  59. al::FlexArray<HrtfChannelState> mChannels;
  60. explicit DirectHrtfState(size_t numchans) : mChannels{numchans} { }
  61. /**
  62. * Produces HRTF filter coefficients for decoding B-Format, given a set of
  63. * virtual speaker positions, a matching decoding matrix, and per-order
  64. * high-frequency gains for the decoder. The calculated impulse responses
  65. * are ordered and scaled according to the matrix input.
  66. */
  67. void build(const HrtfStore *Hrtf, const uint irSize, const bool perHrirMin,
  68. const al::span<const AngularPoint> AmbiPoints,
  69. const al::span<const std::array<float,MaxAmbiChannels>> AmbiMatrix,
  70. const float XOverFreq, const al::span<const float,MaxAmbiOrder+1> AmbiOrderHFGain);
  71. static std::unique_ptr<DirectHrtfState> Create(size_t num_chans);
  72. DEF_FAM_NEWDEL(DirectHrtfState, mChannels)
  73. };
  74. std::vector<std::string> EnumerateHrtf(std::optional<std::string> pathopt);
  75. HrtfStorePtr GetLoadedHrtf(const std::string_view name, const uint devrate);
  76. #endif /* CORE_HRTF_H */