Make a couple more operator bools explicit
[openal-soft.git] / core / mixer.cpp
blob4618406beff388fee1f877d0800c94a064cb88b8
2 #include "config.h"
4 #include "mixer.h"
6 #include <cmath>
8 #include "alnumbers.h"
9 #include "devformat.h"
10 #include "device.h"
11 #include "mixer/defs.h"
13 struct CTag;
16 MixerFunc MixSamples{Mix_<CTag>};
19 std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, const float x,
20 const float spread)
22 std::array<float,MaxAmbiChannels> coeffs;
24 /* Zeroth-order */
25 coeffs[0] = 1.0f; /* ACN 0 = 1 */
26 /* First-order */
27 coeffs[1] = al::numbers::sqrt3_v<float> * y; /* ACN 1 = sqrt(3) * Y */
28 coeffs[2] = al::numbers::sqrt3_v<float> * z; /* ACN 2 = sqrt(3) * Z */
29 coeffs[3] = al::numbers::sqrt3_v<float> * x; /* ACN 3 = sqrt(3) * X */
30 /* Second-order */
31 const float xx{x*x}, yy{y*y}, zz{z*z}, xy{x*y}, yz{y*z}, xz{x*z};
32 coeffs[4] = 3.872983346f * xy; /* ACN 4 = sqrt(15) * X * Y */
33 coeffs[5] = 3.872983346f * yz; /* ACN 5 = sqrt(15) * Y * Z */
34 coeffs[6] = 1.118033989f * (3.0f*zz - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
35 coeffs[7] = 3.872983346f * xz; /* ACN 7 = sqrt(15) * X * Z */
36 coeffs[8] = 1.936491673f * (xx - yy); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
37 /* Third-order */
38 coeffs[9] = 2.091650066f * (y*(3.0f*xx - yy)); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
39 coeffs[10] = 10.246950766f * (z*xy); /* ACN 10 = sqrt(105) * Z * X * Y */
40 coeffs[11] = 1.620185175f * (y*(5.0f*zz - 1.0f)); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
41 coeffs[12] = 1.322875656f * (z*(5.0f*zz - 3.0f)); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
42 coeffs[13] = 1.620185175f * (x*(5.0f*zz - 1.0f)); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
43 coeffs[14] = 5.123475383f * (z*(xx - yy)); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
44 coeffs[15] = 2.091650066f * (x*(xx - 3.0f*yy)); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
45 /* Fourth-order */
46 /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
47 /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
48 /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
49 /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
50 /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
51 /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
52 /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
53 /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
54 /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
56 if(spread > 0.0f)
58 /* Implement the spread by using a spherical source that subtends the
59 * angle spread. See:
60 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
62 * When adjusted for N3D normalization instead of SN3D, these
63 * calculations are:
65 * ZH0 = -sqrt(pi) * (-1+ca);
66 * ZH1 = 0.5*sqrt(pi) * sa*sa;
67 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
68 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
69 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
70 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
72 * The gain of the source is compensated for size, so that the
73 * loudness doesn't depend on the spread. Thus:
75 * ZH0 = 1.0f;
76 * ZH1 = 0.5f * (ca+1.0f);
77 * ZH2 = 0.5f * (ca+1.0f)*ca;
78 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
79 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
80 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
82 const float ca{std::cos(spread * 0.5f)};
83 /* Increase the source volume by up to +3dB for a full spread. */
84 const float scale{std::sqrt(1.0f + al::numbers::inv_pi_v<float>/2.0f*spread)};
86 const float ZH0_norm{scale};
87 const float ZH1_norm{scale * 0.5f * (ca+1.f)};
88 const float ZH2_norm{scale * 0.5f * (ca+1.f)*ca};
89 const float ZH3_norm{scale * 0.125f * (ca+1.f)*(5.f*ca*ca-1.f)};
91 /* Zeroth-order */
92 coeffs[0] *= ZH0_norm;
93 /* First-order */
94 coeffs[1] *= ZH1_norm;
95 coeffs[2] *= ZH1_norm;
96 coeffs[3] *= ZH1_norm;
97 /* Second-order */
98 coeffs[4] *= ZH2_norm;
99 coeffs[5] *= ZH2_norm;
100 coeffs[6] *= ZH2_norm;
101 coeffs[7] *= ZH2_norm;
102 coeffs[8] *= ZH2_norm;
103 /* Third-order */
104 coeffs[9] *= ZH3_norm;
105 coeffs[10] *= ZH3_norm;
106 coeffs[11] *= ZH3_norm;
107 coeffs[12] *= ZH3_norm;
108 coeffs[13] *= ZH3_norm;
109 coeffs[14] *= ZH3_norm;
110 coeffs[15] *= ZH3_norm;
113 return coeffs;
116 void ComputePanGains(const MixParams *mix, const float*RESTRICT coeffs, const float ingain,
117 const al::span<float,MAX_OUTPUT_CHANNELS> gains)
119 auto ambimap = mix->AmbiMap.cbegin();
121 auto iter = std::transform(ambimap, ambimap+mix->Buffer.size(), gains.begin(),
122 [coeffs,ingain](const BFChannelConfig &chanmap) noexcept -> float
123 { return chanmap.Scale * coeffs[chanmap.Index] * ingain; }
125 std::fill(iter, gains.end(), 0.0f);