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