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