blob: c502eb8b1d573dc65c8057f5d227f59b1245c79d [file] [log] [blame]
ragoff0a51f2018-03-22 09:55:50 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#ifndef DPFREQUENCY_H_
19#define DPFREQUENCY_H_
20
21#include <Eigen/Dense>
22#include <unsupported/Eigen/FFT>
23
24#include "RDsp.h"
25#include "SHCircularBuffer.h"
26
27#include "DPBase.h"
28
29
30namespace dp_fx {
31
32using FXBuffer = SHCircularBuffer<float>;
33
34class ChannelBuffer {
35public:
36 FXBuffer cBInput; // Circular Buffer input
37 FXBuffer cBOutput; // Circular Buffer output
38 FloatVec input; // time domain temp vector for input
39 FloatVec output; // time domain temp vector for output
40 FloatVec outTail; // time domain temp vector for output tail (for overlap-add method)
41
ragod55d0f32018-05-15 18:57:31 -070042 Eigen::VectorXcf complexTemp; // complex temp vector for frequency domain operations
43
ragoff0a51f2018-03-22 09:55:50 -070044 //Current parameters
45 float inputGainDb;
46 struct BandParams {
47 bool enabled;
48 float freqCutoffHz;
49 size_t binStart;
50 size_t binStop;
51 };
52 struct EqBandParams : public BandParams {
53 float gainDb;
54 };
55 struct MbcBandParams : public BandParams {
56 float gainPreDb;
57 float gainPostDb;
58 float attackTimeMs;
59 float releaseTimeMs;
60 float ratio;
61 float thresholdDb;
62 float kneeWidthDb;
63 float noiseGateThresholdDb;
64 float expanderRatio;
65
66 //Historic values
67 float previousEnvelope;
68 };
ragod55d0f32018-05-15 18:57:31 -070069 struct LimiterParams {
70 int32_t linkGroup;
71 float attackTimeMs;
72 float releaseTimeMs;
73 float ratio;
74 float thresholdDb;
75 float postGainDb;
76
77 //Historic values
78 float previousEnvelope;
79 float newFactor;
80 float linkFactor;
81 };
ragoff0a51f2018-03-22 09:55:50 -070082
83 bool mPreEqInUse;
84 bool mPreEqEnabled;
85 std::vector<EqBandParams> mPreEqBands;
86
87 bool mMbcInUse;
88 bool mMbcEnabled;
89 std::vector<MbcBandParams> mMbcBands;
90
91 bool mPostEqInUse;
92 bool mPostEqEnabled;
93 std::vector<EqBandParams> mPostEqBands;
94
95 bool mLimiterInUse;
96 bool mLimiterEnabled;
ragod55d0f32018-05-15 18:57:31 -070097 LimiterParams mLimiterParams;
ragoff0a51f2018-03-22 09:55:50 -070098 FloatVec mPreEqFactorVector; // temp pre-computed vector to shape spectrum at preEQ stage
99 FloatVec mPostEqFactorVector; // temp pre-computed vector to shape spectrum at postEQ stage
100
101 void initBuffers(unsigned int blockSize, unsigned int overlapSize, unsigned int halfFftSize,
102 unsigned int samplingRate, DPBase &dpBase);
103 void computeBinStartStop(BandParams &bp, size_t binStart);
104private:
105 unsigned int mSamplingRate;
106 unsigned int mBlockSize;
107
108};
109
ragod55d0f32018-05-15 18:57:31 -0700110using CBufferVector = std::vector<ChannelBuffer>;
111
112using GroupsMap = std::map<int32_t, IntVec>;
113
114class LinkedLimiters {
115public:
116 void reset();
117 void update(int32_t group, int index);
118 void remove(int index);
119 GroupsMap mGroupsMap;
120};
121
ragoff0a51f2018-03-22 09:55:50 -0700122class DPFrequency : public DPBase {
123public:
124 virtual size_t processSamples(const float *in, float *out, size_t samples);
125 virtual void reset();
126 void configure(size_t blockSize, size_t overlapSize, size_t samplingRate);
127 static size_t getMinBockSize();
128 static size_t getMaxBockSize();
129
130private:
131 void updateParameters(ChannelBuffer &cb, int channelIndex);
132 size_t processMono(ChannelBuffer &cb);
133 size_t processOneVector(FloatVec &output, FloatVec &input, ChannelBuffer &cb);
134
ragod55d0f32018-05-15 18:57:31 -0700135 size_t processChannelBuffers(CBufferVector &channelBuffers);
136 size_t processFirstStages(ChannelBuffer &cb);
137 size_t processLastStages(ChannelBuffer &cb);
138 void processLinkedLimiters(CBufferVector &channelBuffers);
139
ragoff0a51f2018-03-22 09:55:50 -0700140 size_t mBlockSize;
141 size_t mHalfFFTSize;
142 size_t mOverlapSize;
143 size_t mSamplingRate;
144
ragod55d0f32018-05-15 18:57:31 -0700145 float mBlocksPerSecond;
146
147 CBufferVector mChannelBuffers;
148
149 LinkedLimiters mLinkedLimiters;
ragoff0a51f2018-03-22 09:55:50 -0700150
151 //dsp
152 FloatVec mVWindow; //window class.
ragod55d0f32018-05-15 18:57:31 -0700153 float mWindowRms;
ragoff0a51f2018-03-22 09:55:50 -0700154 Eigen::FFT<float> mFftServer;
155};
156
157} //namespace dp_fx
158
159#endif // DPFREQUENCY_H_