🛠️🐜 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.

96 lines
2.9 KiB

  1. #ifndef CORE_MIXER_DEFS_H
  2. #define CORE_MIXER_DEFS_H
  3. #include <array>
  4. #include <stdlib.h>
  5. #include "alspan.h"
  6. #include "core/bufferline.h"
  7. #include "core/resampler_limits.h"
  8. struct HrtfChannelState;
  9. struct HrtfFilter;
  10. struct MixHrtfFilter;
  11. using uint = unsigned int;
  12. using float2 = std::array<float,2>;
  13. constexpr int MixerFracBits{12};
  14. constexpr int MixerFracOne{1 << MixerFracBits};
  15. constexpr int MixerFracMask{MixerFracOne - 1};
  16. constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */
  17. enum class Resampler {
  18. Point,
  19. Linear,
  20. Cubic,
  21. FastBSinc12,
  22. BSinc12,
  23. FastBSinc24,
  24. BSinc24,
  25. Max = BSinc24
  26. };
  27. /* Interpolator state. Kind of a misnomer since the interpolator itself is
  28. * stateless. This just keeps it from having to recompute scale-related
  29. * mappings for every sample.
  30. */
  31. struct BsincState {
  32. float sf; /* Scale interpolation factor. */
  33. uint m; /* Coefficient count. */
  34. uint l; /* Left coefficient offset. */
  35. /* Filter coefficients, followed by the phase, scale, and scale-phase
  36. * delta coefficients. Starting at phase index 0, each subsequent phase
  37. * index follows contiguously.
  38. */
  39. const float *filter;
  40. };
  41. union InterpState {
  42. BsincState bsinc;
  43. };
  44. using ResamplerFunc = float*(*)(const InterpState *state, float *RESTRICT src, uint frac,
  45. uint increment, const al::span<float> dst);
  46. ResamplerFunc PrepareResampler(Resampler resampler, uint increment, InterpState *state);
  47. template<typename TypeTag, typename InstTag>
  48. float *Resample_(const InterpState *state, float *RESTRICT src, uint frac, uint increment,
  49. const al::span<float> dst);
  50. template<typename InstTag>
  51. void Mix_(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
  52. float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos);
  53. template<typename InstTag>
  54. void MixHrtf_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
  55. const MixHrtfFilter *hrtfparams, const size_t BufferSize);
  56. template<typename InstTag>
  57. void MixHrtfBlend_(const float *InSamples, float2 *AccumSamples, const uint IrSize,
  58. const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize);
  59. template<typename InstTag>
  60. void MixDirectHrtf_(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
  61. const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
  62. float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize);
  63. /* Vectorized resampler helpers */
  64. template<size_t N>
  65. inline void InitPosArrays(uint frac, uint increment, uint (&frac_arr)[N], uint (&pos_arr)[N])
  66. {
  67. pos_arr[0] = 0;
  68. frac_arr[0] = frac;
  69. for(size_t i{1};i < N;i++)
  70. {
  71. const uint frac_tmp{frac_arr[i-1] + increment};
  72. pos_arr[i] = pos_arr[i-1] + (frac_tmp>>MixerFracBits);
  73. frac_arr[i] = frac_tmp&MixerFracMask;
  74. }
  75. }
  76. #endif /* CORE_MIXER_DEFS_H */