]> git.tdb.fi Git - ext/openal.git/blob - core/mastering.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / core / mastering.h
1 #ifndef CORE_MASTERING_H
2 #define CORE_MASTERING_H
3
4 #include <memory>
5
6 #include "almalloc.h"
7 #include "bufferline.h"
8
9 struct SlidingHold;
10
11 using uint = unsigned int;
12
13
14 /* General topology and basic automation was based on the following paper:
15  *
16  *   D. Giannoulis, M. Massberg and J. D. Reiss,
17  *   "Parameter Automation in a Dynamic Range Compressor,"
18  *   Journal of the Audio Engineering Society, v61 (10), Oct. 2013
19  *
20  * Available (along with supplemental reading) at:
21  *
22  *   http://c4dm.eecs.qmul.ac.uk/audioengineering/compressors/
23  */
24 struct Compressor {
25     size_t mNumChans{0u};
26
27     struct {
28         bool Knee : 1;
29         bool Attack : 1;
30         bool Release : 1;
31         bool PostGain : 1;
32         bool Declip : 1;
33     } mAuto{};
34
35     uint mLookAhead{0};
36
37     float mPreGain{0.0f};
38     float mPostGain{0.0f};
39
40     float mThreshold{0.0f};
41     float mSlope{0.0f};
42     float mKnee{0.0f};
43
44     float mAttack{0.0f};
45     float mRelease{0.0f};
46
47     alignas(16) float mSideChain[2*BufferLineSize]{};
48     alignas(16) float mCrestFactor[BufferLineSize]{};
49
50     SlidingHold *mHold{nullptr};
51     FloatBufferLine *mDelay{nullptr};
52
53     float mCrestCoeff{0.0f};
54     float mGainEstimate{0.0f};
55     float mAdaptCoeff{0.0f};
56
57     float mLastPeakSq{0.0f};
58     float mLastRmsSq{0.0f};
59     float mLastRelease{0.0f};
60     float mLastAttack{0.0f};
61     float mLastGainDev{0.0f};
62
63
64     ~Compressor();
65     void process(const uint SamplesToDo, FloatBufferLine *OutBuffer);
66     int getLookAhead() const noexcept { return static_cast<int>(mLookAhead); }
67
68     DEF_PLACE_NEWDEL()
69
70     /**
71      * The compressor is initialized with the following settings:
72      *
73      * \param NumChans      Number of channels to process.
74      * \param SampleRate    Sample rate to process.
75      * \param AutoKnee      Whether to automate the knee width parameter.
76      * \param AutoAttack    Whether to automate the attack time parameter.
77      * \param AutoRelease   Whether to automate the release time parameter.
78      * \param AutoPostGain  Whether to automate the make-up (post) gain
79      *        parameter.
80      * \param AutoDeclip    Whether to automate clipping reduction. Ignored
81      *        when not automating make-up gain.
82      * \param LookAheadTime Look-ahead time (in seconds).
83      * \param HoldTime      Peak hold-time (in seconds).
84      * \param PreGainDb     Gain applied before detection (in dB).
85      * \param PostGainDb    Make-up gain applied after compression (in dB).
86      * \param ThresholdDb   Triggering threshold (in dB).
87      * \param Ratio         Compression ratio (x:1). Set to INFINIFTY for true
88      *        limiting. Ignored when automating knee width.
89      * \param KneeDb        Knee width (in dB). Ignored when automating knee
90      *        width.
91      * \param AttackTime    Attack time (in seconds). Acts as a maximum when
92      *        automating attack time.
93      * \param ReleaseTime   Release time (in seconds). Acts as a maximum when
94      *        automating release time.
95      */
96     static std::unique_ptr<Compressor> Create(const size_t NumChans, const float SampleRate,
97         const bool AutoKnee, const bool AutoAttack, const bool AutoRelease,
98         const bool AutoPostGain, const bool AutoDeclip, const float LookAheadTime,
99         const float HoldTime, const float PreGainDb, const float PostGainDb,
100         const float ThresholdDb, const float Ratio, const float KneeDb, const float AttackTime,
101         const float ReleaseTime);
102 };
103 using CompressorPtr = std::unique_ptr<Compressor>;
104
105 #endif /* CORE_MASTERING_H */