mixer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef CORE_MIXER_H
  2. #define CORE_MIXER_H
  3. #include <array>
  4. #include <cmath>
  5. #include <cstddef>
  6. #include "alspan.h"
  7. #include "ambidefs.h"
  8. #include "bufferline.h"
  9. struct MixParams;
  10. /* Mixer functions that handle one input and multiple output channels. */
  11. using MixerOutFunc = void(*)(const al::span<const float> InSamples,
  12. const al::span<FloatBufferLine> OutBuffer, const al::span<float> CurrentGains,
  13. const al::span<const float> TargetGains, const std::size_t Counter, const std::size_t OutPos);
  14. extern MixerOutFunc MixSamplesOut;
  15. inline void MixSamples(const al::span<const float> InSamples,
  16. const al::span<FloatBufferLine> OutBuffer, const al::span<float> CurrentGains,
  17. const al::span<const float> TargetGains, const std::size_t Counter, const std::size_t OutPos)
  18. { MixSamplesOut(InSamples, OutBuffer, CurrentGains, TargetGains, Counter, OutPos); }
  19. /* Mixer functions that handle one input and one output channel. */
  20. using MixerOneFunc = void(*)(const al::span<const float> InSamples,const al::span<float> OutBuffer,
  21. float &CurrentGain, const float TargetGain, const std::size_t Counter);
  22. extern MixerOneFunc MixSamplesOne;
  23. inline void MixSamples(const al::span<const float> InSamples, const al::span<float> OutBuffer,
  24. float &CurrentGain, const float TargetGain, const std::size_t Counter)
  25. { MixSamplesOne(InSamples, OutBuffer, CurrentGain, TargetGain, Counter); }
  26. /**
  27. * Calculates ambisonic encoder coefficients using the X, Y, and Z direction
  28. * components, which must represent a normalized (unit length) vector, and the
  29. * spread is the angular width of the sound (0...tau).
  30. *
  31. * NOTE: The components use ambisonic coordinates. As a result:
  32. *
  33. * Ambisonic Y = OpenAL -X
  34. * Ambisonic Z = OpenAL Y
  35. * Ambisonic X = OpenAL -Z
  36. *
  37. * The components are ordered such that OpenAL's X, Y, and Z are the first,
  38. * second, and third parameters respectively -- simply negate X and Z.
  39. */
  40. std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, const float x,
  41. const float spread);
  42. /**
  43. * CalcDirectionCoeffs
  44. *
  45. * Calculates ambisonic coefficients based on an OpenAL direction vector. The
  46. * vector must be normalized (unit length), and the spread is the angular width
  47. * of the sound (0...tau).
  48. */
  49. inline std::array<float,MaxAmbiChannels> CalcDirectionCoeffs(const al::span<const float,3> dir,
  50. const float spread)
  51. {
  52. /* Convert from OpenAL coords to Ambisonics. */
  53. return CalcAmbiCoeffs(-dir[0], dir[1], -dir[2], spread);
  54. }
  55. /**
  56. * CalcDirectionCoeffs
  57. *
  58. * Calculates ambisonic coefficients based on an OpenAL direction vector. The
  59. * vector must be normalized (unit length).
  60. */
  61. constexpr std::array<float,MaxAmbiChannels> CalcDirectionCoeffs(const al::span<const float,3> dir)
  62. {
  63. /* Convert from OpenAL coords to Ambisonics. */
  64. return CalcAmbiCoeffs(-dir[0], dir[1], -dir[2]);
  65. }
  66. /**
  67. * CalcAngleCoeffs
  68. *
  69. * Calculates ambisonic coefficients based on azimuth and elevation. The
  70. * azimuth and elevation parameters are in radians, going right and up
  71. * respectively.
  72. */
  73. inline std::array<float,MaxAmbiChannels> CalcAngleCoeffs(const float azimuth,
  74. const float elevation, const float spread)
  75. {
  76. const float x{-std::sin(azimuth) * std::cos(elevation)};
  77. const float y{ std::sin(elevation)};
  78. const float z{ std::cos(azimuth) * std::cos(elevation)};
  79. return CalcAmbiCoeffs(x, y, z, spread);
  80. }
  81. /**
  82. * ComputePanGains
  83. *
  84. * Computes panning gains using the given channel decoder coefficients and the
  85. * pre-calculated direction or angle coefficients. For B-Format sources, the
  86. * coeffs are a 'slice' of a transform matrix for the input channel, used to
  87. * scale and orient the sound samples.
  88. */
  89. void ComputePanGains(const MixParams *mix, const al::span<const float,MaxAmbiChannels> coeffs,
  90. const float ingain, const al::span<float,MaxAmbiChannels> gains);
  91. #endif /* CORE_MIXER_H */