Include fmt 11.0.2
[openal-soft.git] / core / mastering.h
blob0af9a3da6746d04087cfb550592e6b079bafd53f
1 #ifndef CORE_MASTERING_H
2 #define CORE_MASTERING_H
4 #include <array>
5 #include <memory>
7 #include "alnumeric.h"
8 #include "alspan.h"
9 #include "bufferline.h"
10 #include "opthelpers.h"
11 #include "vector.h"
13 struct SlidingHold;
15 using uint = unsigned int;
18 /* General topology and basic automation was based on the following paper:
20 * D. Giannoulis, M. Massberg and J. D. Reiss,
21 * "Parameter Automation in a Dynamic Range Compressor,"
22 * Journal of the Audio Engineering Society, v61 (10), Oct. 2013
24 * Available (along with supplemental reading) at:
26 * http://c4dm.eecs.qmul.ac.uk/audioengineering/compressors/
28 class SIMDALIGN Compressor {
29 struct AutoFlags {
30 bool Knee : 1;
31 bool Attack : 1;
32 bool Release : 1;
33 bool PostGain : 1;
34 bool Declip : 1;
36 AutoFlags mAuto{};
38 uint mLookAhead{0};
40 float mPreGain{0.0f};
41 float mPostGain{0.0f};
43 float mThreshold{0.0f};
44 float mSlope{0.0f};
45 float mKnee{0.0f};
47 float mAttack{0.0f};
48 float mRelease{0.0f};
50 alignas(16) std::array<float,BufferLineSize*2_uz> mSideChain{};
51 alignas(16) std::array<float,BufferLineSize> mCrestFactor{};
53 std::unique_ptr<SlidingHold> mHold;
54 al::vector<FloatBufferLine,16> mDelay;
56 float mCrestCoeff{0.0f};
57 float mGainEstimate{0.0f};
58 float mAdaptCoeff{0.0f};
60 float mLastPeakSq{0.0f};
61 float mLastRmsSq{0.0f};
62 float mLastRelease{0.0f};
63 float mLastAttack{0.0f};
64 float mLastGainDev{0.0f};
66 Compressor() = default;
68 void linkChannels(const uint SamplesToDo, const al::span<const FloatBufferLine> OutBuffer);
69 void crestDetector(const uint SamplesToDo);
70 void peakDetector(const uint SamplesToDo);
71 void peakHoldDetector(const uint SamplesToDo);
72 void gainCompressor(const uint SamplesToDo);
73 void signalDelay(const uint SamplesToDo, const al::span<FloatBufferLine> OutBuffer);
75 public:
76 ~Compressor();
77 void process(const uint SamplesToDo, al::span<FloatBufferLine> InOut);
78 [[nodiscard]] auto getLookAhead() const noexcept -> uint { return mLookAhead; }
80 /**
81 * The compressor is initialized with the following settings:
83 * \param NumChans Number of channels to process.
84 * \param SampleRate Sample rate to process.
85 * \param AutoKnee Whether to automate the knee width parameter.
86 * \param AutoAttack Whether to automate the attack time parameter.
87 * \param AutoRelease Whether to automate the release time parameter.
88 * \param AutoPostGain Whether to automate the make-up (post) gain
89 * parameter.
90 * \param AutoDeclip Whether to automate clipping reduction. Ignored
91 * when not automating make-up gain.
92 * \param LookAheadTime Look-ahead time (in seconds).
93 * \param HoldTime Peak hold-time (in seconds).
94 * \param PreGainDb Gain applied before detection (in dB).
95 * \param PostGainDb Make-up gain applied after compression (in dB).
96 * \param ThresholdDb Triggering threshold (in dB).
97 * \param Ratio Compression ratio (x:1). Set to INFINIFTY for true
98 * limiting. Ignored when automating knee width.
99 * \param KneeDb Knee width (in dB). Ignored when automating knee
100 * width.
101 * \param AttackTime Attack time (in seconds). Acts as a maximum when
102 * automating attack time.
103 * \param ReleaseTime Release time (in seconds). Acts as a maximum when
104 * automating release time.
106 static std::unique_ptr<Compressor> Create(const size_t NumChans, const float SampleRate,
107 const bool AutoKnee, const bool AutoAttack, const bool AutoRelease,
108 const bool AutoPostGain, const bool AutoDeclip, const float LookAheadTime,
109 const float HoldTime, const float PreGainDb, const float PostGainDb,
110 const float ThresholdDb, const float Ratio, const float KneeDb, const float AttackTime,
111 const float ReleaseTime);
113 using CompressorPtr = std::unique_ptr<Compressor>;
115 #endif /* CORE_MASTERING_H */