rago | ff0a51f | 2018-03-22 09:55:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | namespace dp_fx { |
| 31 | |
| 32 | using FXBuffer = SHCircularBuffer<float>; |
| 33 | |
| 34 | class ChannelBuffer { |
| 35 | public: |
| 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 | |
| 42 | //Current parameters |
| 43 | float inputGainDb; |
| 44 | struct BandParams { |
| 45 | bool enabled; |
| 46 | float freqCutoffHz; |
| 47 | size_t binStart; |
| 48 | size_t binStop; |
| 49 | }; |
| 50 | struct EqBandParams : public BandParams { |
| 51 | float gainDb; |
| 52 | }; |
| 53 | struct MbcBandParams : public BandParams { |
| 54 | float gainPreDb; |
| 55 | float gainPostDb; |
| 56 | float attackTimeMs; |
| 57 | float releaseTimeMs; |
| 58 | float ratio; |
| 59 | float thresholdDb; |
| 60 | float kneeWidthDb; |
| 61 | float noiseGateThresholdDb; |
| 62 | float expanderRatio; |
| 63 | |
| 64 | //Historic values |
| 65 | float previousEnvelope; |
| 66 | }; |
| 67 | |
| 68 | bool mPreEqInUse; |
| 69 | bool mPreEqEnabled; |
| 70 | std::vector<EqBandParams> mPreEqBands; |
| 71 | |
| 72 | bool mMbcInUse; |
| 73 | bool mMbcEnabled; |
| 74 | std::vector<MbcBandParams> mMbcBands; |
| 75 | |
| 76 | bool mPostEqInUse; |
| 77 | bool mPostEqEnabled; |
| 78 | std::vector<EqBandParams> mPostEqBands; |
| 79 | |
| 80 | bool mLimiterInUse; |
| 81 | bool mLimiterEnabled; |
| 82 | FloatVec mPreEqFactorVector; // temp pre-computed vector to shape spectrum at preEQ stage |
| 83 | FloatVec mPostEqFactorVector; // temp pre-computed vector to shape spectrum at postEQ stage |
| 84 | |
| 85 | void initBuffers(unsigned int blockSize, unsigned int overlapSize, unsigned int halfFftSize, |
| 86 | unsigned int samplingRate, DPBase &dpBase); |
| 87 | void computeBinStartStop(BandParams &bp, size_t binStart); |
| 88 | private: |
| 89 | unsigned int mSamplingRate; |
| 90 | unsigned int mBlockSize; |
| 91 | |
| 92 | }; |
| 93 | |
| 94 | class DPFrequency : public DPBase { |
| 95 | public: |
| 96 | virtual size_t processSamples(const float *in, float *out, size_t samples); |
| 97 | virtual void reset(); |
| 98 | void configure(size_t blockSize, size_t overlapSize, size_t samplingRate); |
| 99 | static size_t getMinBockSize(); |
| 100 | static size_t getMaxBockSize(); |
| 101 | |
| 102 | private: |
| 103 | void updateParameters(ChannelBuffer &cb, int channelIndex); |
| 104 | size_t processMono(ChannelBuffer &cb); |
| 105 | size_t processOneVector(FloatVec &output, FloatVec &input, ChannelBuffer &cb); |
| 106 | |
| 107 | size_t mBlockSize; |
| 108 | size_t mHalfFFTSize; |
| 109 | size_t mOverlapSize; |
| 110 | size_t mSamplingRate; |
| 111 | |
| 112 | std::vector<ChannelBuffer> mChannelBuffers; |
| 113 | |
| 114 | //dsp |
| 115 | FloatVec mVWindow; //window class. |
| 116 | Eigen::VectorXcf mComplexTemp; |
| 117 | Eigen::FFT<float> mFftServer; |
| 118 | }; |
| 119 | |
| 120 | } //namespace dp_fx |
| 121 | |
| 122 | #endif // DPFREQUENCY_H_ |