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