mixer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "config.h"
  2. #include "mixer.h"
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <utility>
  6. #include "alnumbers.h"
  7. #include "core/ambidefs.h"
  8. #include "device.h"
  9. #include "mixer/defs.h"
  10. struct CTag;
  11. MixerOutFunc MixSamplesOut{Mix_<CTag>};
  12. MixerOneFunc MixSamplesOne{Mix_<CTag>};
  13. std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, const float x,
  14. const float spread)
  15. {
  16. std::array<float,MaxAmbiChannels> coeffs{CalcAmbiCoeffs(y, z, x)};
  17. if(spread > 0.0f)
  18. {
  19. /* Implement the spread by using a spherical source that subtends the
  20. * angle spread. See:
  21. * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
  22. *
  23. * When adjusted for N3D normalization instead of SN3D, these
  24. * calculations are:
  25. *
  26. * ZH0 = -sqrt(pi) * (-1+ca);
  27. * ZH1 = 0.5*sqrt(pi) * sa*sa;
  28. * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
  29. * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
  30. * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
  31. * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
  32. *
  33. * The gain of the source is compensated for size, so that the
  34. * loudness doesn't depend on the spread. Thus:
  35. *
  36. * ZH0 = 1.0f;
  37. * ZH1 = 0.5f * (ca+1.0f);
  38. * ZH2 = 0.5f * (ca+1.0f)*ca;
  39. * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
  40. * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
  41. * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
  42. */
  43. const float ca{std::cos(spread * 0.5f)};
  44. /* Increase the source volume by up to +3dB for a full spread. */
  45. const float scale{std::sqrt(1.0f + al::numbers::inv_pi_v<float>/2.0f*spread)};
  46. const float ZH0_norm{scale};
  47. const float ZH1_norm{scale * 0.5f * (ca+1.f)};
  48. const float ZH2_norm{scale * 0.5f * (ca+1.f)*ca};
  49. const float ZH3_norm{scale * 0.125f * (ca+1.f)*(5.f*ca*ca-1.f)};
  50. /* Zeroth-order */
  51. coeffs[0] *= ZH0_norm;
  52. /* First-order */
  53. coeffs[1] *= ZH1_norm;
  54. coeffs[2] *= ZH1_norm;
  55. coeffs[3] *= ZH1_norm;
  56. /* Second-order */
  57. coeffs[4] *= ZH2_norm;
  58. coeffs[5] *= ZH2_norm;
  59. coeffs[6] *= ZH2_norm;
  60. coeffs[7] *= ZH2_norm;
  61. coeffs[8] *= ZH2_norm;
  62. /* Third-order */
  63. coeffs[9] *= ZH3_norm;
  64. coeffs[10] *= ZH3_norm;
  65. coeffs[11] *= ZH3_norm;
  66. coeffs[12] *= ZH3_norm;
  67. coeffs[13] *= ZH3_norm;
  68. coeffs[14] *= ZH3_norm;
  69. coeffs[15] *= ZH3_norm;
  70. }
  71. return coeffs;
  72. }
  73. void ComputePanGains(const MixParams *mix, const al::span<const float,MaxAmbiChannels> coeffs,
  74. const float ingain, const al::span<float,MaxAmbiChannels> gains)
  75. {
  76. auto ambimap = al::span{std::as_const(mix->AmbiMap)}.first(mix->Buffer.size());
  77. auto iter = std::transform(ambimap.begin(), ambimap.end(), gains.begin(),
  78. [coeffs,ingain](const BFChannelConfig &chanmap) noexcept -> float
  79. { return chanmap.Scale * coeffs[chanmap.Index] * ingain; });
  80. std::fill(iter, gains.end(), 0.0f);
  81. }