summaryrefslogtreecommitdiffstats
path: root/media/libeffects/dynamicsproc/dsp/DPFrequency.h
blob: be8771dee02de0f725aa072149bc70e198bf725b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef DPFREQUENCY_H_
#define DPFREQUENCY_H_

#include <Eigen/Dense>
#include <unsupported/Eigen/FFT>

#include "RDsp.h"
#include "SHCircularBuffer.h"

#include "DPBase.h"


namespace dp_fx {

using FXBuffer = SHCircularBuffer<float>;

class ChannelBuffer {
public:
    FXBuffer cBInput;   // Circular Buffer input
    FXBuffer cBOutput;  // Circular Buffer output
    FloatVec input;     // time domain temp vector for input
    FloatVec output;    // time domain temp vector for output
    FloatVec outTail;   // time domain temp vector for output tail (for overlap-add method)

    Eigen::VectorXcf complexTemp; // complex temp vector for frequency domain operations

    //Current parameters
    float inputGainDb;
    float outputGainDb;
    struct BandParams {
        bool enabled;
        float freqCutoffHz;
        size_t binStart;
        size_t binStop;
    };
    struct EqBandParams : public BandParams {
        float gainDb;
    };
    struct MbcBandParams : public BandParams {
        float gainPreDb;
        float gainPostDb;
        float attackTimeMs;
        float releaseTimeMs;
        float ratio;
        float thresholdDb;
        float kneeWidthDb;
        float noiseGateThresholdDb;
        float expanderRatio;

        //Historic values
        float previousEnvelope;
    };
    struct LimiterParams {
        int32_t linkGroup;
        float attackTimeMs;
        float releaseTimeMs;
        float ratio;
        float thresholdDb;
        float postGainDb;

        //Historic values
        float previousEnvelope;
        float newFactor;
        float linkFactor;
    };

    bool mPreEqInUse;
    bool mPreEqEnabled;
    std::vector<EqBandParams> mPreEqBands;

    bool mMbcInUse;
    bool mMbcEnabled;
    std::vector<MbcBandParams> mMbcBands;

    bool mPostEqInUse;
    bool mPostEqEnabled;
    std::vector<EqBandParams> mPostEqBands;

    bool mLimiterInUse;
    bool mLimiterEnabled;
    LimiterParams mLimiterParams;
    FloatVec mPreEqFactorVector; // temp pre-computed vector to shape spectrum at preEQ stage
    FloatVec mPostEqFactorVector; // temp pre-computed vector to shape spectrum at postEQ stage

    void initBuffers(unsigned int blockSize, unsigned int overlapSize, unsigned int halfFftSize,
            unsigned int samplingRate, DPBase &dpBase);
    void computeBinStartStop(BandParams &bp, size_t binStart);
private:
    unsigned int mSamplingRate;
    unsigned int mBlockSize;

};

using CBufferVector = std::vector<ChannelBuffer>;

using GroupsMap = std::map<int32_t, IntVec>;

class LinkedLimiters {
public:
    void reset();
    void update(int32_t group, int index);
    void remove(int index);
    GroupsMap mGroupsMap;
};

class DPFrequency : public DPBase {
public:
    virtual size_t processSamples(const float *in, float *out, size_t samples);
    virtual void reset();
    void configure(size_t blockSize, size_t overlapSize, size_t samplingRate);
    static size_t getMinBockSize();
    static size_t getMaxBockSize();

private:
    void updateParameters(ChannelBuffer &cb, int channelIndex);
    size_t processMono(ChannelBuffer &cb);
    size_t processOneVector(FloatVec &output, FloatVec &input, ChannelBuffer &cb);

    size_t processChannelBuffers(CBufferVector &channelBuffers);
    size_t processFirstStages(ChannelBuffer &cb);
    size_t processLastStages(ChannelBuffer &cb);
    void processLinkedLimiters(CBufferVector &channelBuffers);

    size_t mBlockSize;
    size_t mHalfFFTSize;
    size_t mOverlapSize;
    size_t mSamplingRate;

    float mBlocksPerSecond;

    CBufferVector mChannelBuffers;

    LinkedLimiters mLinkedLimiters;

    //dsp
    FloatVec mVWindow;  //window class.
    float mWindowRms;
    Eigen::FFT<float> mFftServer;
};

} //namespace dp_fx

#endif  // DPFREQUENCY_H_