Improve performance calculating reverb panning
[openal-soft.git] / core / mixer.h
blobaa7597bbae63a3699c5448fe21c56b82f26ef91d
1 #ifndef CORE_MIXER_H
2 #define CORE_MIXER_H
4 #include <array>
5 #include <cmath>
6 #include <stddef.h>
7 #include <type_traits>
9 #include "alspan.h"
10 #include "ambidefs.h"
11 #include "bufferline.h"
12 #include "devformat.h"
14 struct MixParams;
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);
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); }
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);
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); }
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).
42 * NOTE: The components use ambisonic coordinates. As a result:
44 * Ambisonic Y = OpenAL -X
45 * Ambisonic Z = OpenAL Y
46 * Ambisonic X = OpenAL -Z
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.
51 std::array<float,MaxAmbiChannels> CalcAmbiCoeffs(const float y, const float z, const float x,
52 const float spread);
54 /**
55 * CalcDirectionCoeffs
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).
61 inline std::array<float,MaxAmbiChannels> CalcDirectionCoeffs(const float (&dir)[3],
62 const float spread)
64 /* Convert from OpenAL coords to Ambisonics. */
65 return CalcAmbiCoeffs(-dir[0], dir[1], -dir[2], spread);
68 /**
69 * CalcDirectionCoeffs
71 * Calculates ambisonic coefficients based on an OpenAL direction vector. The
72 * vector must be normalized (unit length).
74 constexpr std::array<float,MaxAmbiChannels> CalcDirectionCoeffs(const float (&dir)[3])
76 /* Convert from OpenAL coords to Ambisonics. */
77 return CalcAmbiCoeffs(-dir[0], dir[1], -dir[2]);
80 /**
81 * CalcAngleCoeffs
83 * Calculates ambisonic coefficients based on azimuth and elevation. The
84 * azimuth and elevation parameters are in radians, going right and up
85 * respectively.
87 inline std::array<float,MaxAmbiChannels> CalcAngleCoeffs(const float azimuth,
88 const float elevation, const float spread)
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)};
94 return CalcAmbiCoeffs(x, y, z, spread);
98 /**
99 * ComputePanGains
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.
106 void ComputePanGains(const MixParams *mix, const float*RESTRICT coeffs, const float ingain,
107 const al::span<float,MaxAmbiChannels> gains);
109 #endif /* CORE_MIXER_H */