Home | History | Annotate | Download | only in dsp
      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 #define LOG_TAG "DPBase"
     18 //#define LOG_NDEBUG 0
     19 
     20 #include <log/log.h>
     21 #include "DPBase.h"
     22 #include "DPFrequency.h"
     23 
     24 namespace dp_fx {
     25 
     26 DPStage::DPStage() : mInUse(DP_DEFAULT_STAGE_INUSE),
     27         mEnabled(DP_DEFAULT_STAGE_ENABLED) {
     28 }
     29 
     30 void DPStage::init(bool inUse, bool enabled) {
     31     mInUse = inUse;
     32     mEnabled = enabled;
     33 }
     34 
     35 //----
     36 DPBandStage::DPBandStage() : mBandCount(0) {
     37 }
     38 
     39 void DPBandStage::init(bool inUse, bool enabled, int bandCount) {
     40     DPStage::init(inUse, enabled);
     41     mBandCount = inUse ? bandCount : 0;
     42 }
     43 
     44 //---
     45 DPBandBase::DPBandBase() {
     46     init(DP_DEFAULT_BAND_ENABLED,
     47             DP_DEFAULT_BAND_CUTOFF_FREQUENCY_HZ);
     48 }
     49 
     50 void DPBandBase::init(bool enabled, float cutoffFrequency){
     51     mEnabled = enabled;
     52     mCutoofFrequencyHz = cutoffFrequency;
     53 }
     54 
     55 //-----
     56 DPEqBand::DPEqBand() {
     57     init(DP_DEFAULT_BAND_ENABLED,
     58             DP_DEFAULT_BAND_CUTOFF_FREQUENCY_HZ,
     59             DP_DEFAULT_GAIN_DB);
     60 }
     61 
     62 void DPEqBand::init(bool enabled, float cutoffFrequency, float gain) {
     63     DPBandBase::init(enabled, cutoffFrequency);
     64     setGain(gain);
     65 }
     66 
     67 float DPEqBand::getGain() const{
     68     return mGainDb;
     69 }
     70 
     71 void DPEqBand::setGain(float gain) {
     72     mGainDb = gain;
     73 }
     74 
     75 //------
     76 DPMbcBand::DPMbcBand() {
     77     init(DP_DEFAULT_BAND_ENABLED,
     78             DP_DEFAULT_BAND_CUTOFF_FREQUENCY_HZ,
     79             DP_DEFAULT_ATTACK_TIME_MS,
     80             DP_DEFAULT_RELEASE_TIME_MS,
     81             DP_DEFAULT_RATIO,
     82             DP_DEFAULT_THRESHOLD_DB,
     83             DP_DEFAULT_KNEE_WIDTH_DB,
     84             DP_DEFAULT_NOISE_GATE_THRESHOLD_DB,
     85             DP_DEFAULT_EXPANDER_RATIO,
     86             DP_DEFAULT_GAIN_DB,
     87             DP_DEFAULT_GAIN_DB);
     88 }
     89 
     90 void DPMbcBand::init(bool enabled, float cutoffFrequency, float attackTime, float releaseTime,
     91         float ratio, float threshold, float kneeWidth, float noiseGateThreshold,
     92         float expanderRatio, float preGain, float postGain) {
     93     DPBandBase::init(enabled, cutoffFrequency);
     94     setAttackTime(attackTime);
     95     setReleaseTime(releaseTime);
     96     setRatio(ratio);
     97     setThreshold(threshold);
     98     setKneeWidth(kneeWidth);
     99     setNoiseGateThreshold(noiseGateThreshold);
    100     setExpanderRatio(expanderRatio);
    101     setPreGain(preGain);
    102     setPostGain(postGain);
    103 }
    104 
    105 //------
    106 DPEq::DPEq() {
    107 }
    108 
    109 void DPEq::init(bool inUse, bool enabled, uint32_t bandCount) {
    110     DPBandStage::init(inUse, enabled, bandCount);
    111     mBands.resize(getBandCount());
    112 }
    113 
    114 DPEqBand * DPEq::getBand(uint32_t band) {
    115     if (band < getBandCount()) {
    116         return &mBands[band];
    117     }
    118     return NULL;
    119 }
    120 
    121 void DPEq::setBand(uint32_t band, DPEqBand &src) {
    122     if (band < getBandCount()) {
    123         mBands[band] = src;
    124     }
    125 }
    126 
    127 //------
    128 DPMbc::DPMbc() {
    129 }
    130 
    131 void DPMbc::init(bool inUse, bool enabled, uint32_t bandCount) {
    132     DPBandStage::init(inUse, enabled, bandCount);
    133     if (isInUse()) {
    134         mBands.resize(bandCount);
    135     } else {
    136         mBands.resize(0);
    137     }
    138 }
    139 
    140 DPMbcBand * DPMbc::getBand(uint32_t band) {
    141     if (band < getBandCount()) {
    142         return &mBands[band];
    143     }
    144     return NULL;
    145 }
    146 
    147 void DPMbc::setBand(uint32_t band, DPMbcBand &src) {
    148     if (band < getBandCount()) {
    149         mBands[band] = src;
    150     }
    151 }
    152 
    153 //------
    154 DPLimiter::DPLimiter() {
    155     init(DP_DEFAULT_STAGE_INUSE,
    156             DP_DEFAULT_STAGE_ENABLED,
    157             DP_DEFAULT_LINK_GROUP,
    158             DP_DEFAULT_ATTACK_TIME_MS,
    159             DP_DEFAULT_RELEASE_TIME_MS,
    160             DP_DEFAULT_RATIO,
    161             DP_DEFAULT_THRESHOLD_DB,
    162             DP_DEFAULT_GAIN_DB);
    163 }
    164 
    165 void DPLimiter::init(bool inUse, bool enabled, uint32_t linkGroup, float attackTime, float releaseTime,
    166         float ratio, float threshold, float postGain) {
    167     DPStage::init(inUse, enabled);
    168     setLinkGroup(linkGroup);
    169     setAttackTime(attackTime);
    170     setReleaseTime(releaseTime);
    171     setRatio(ratio);
    172     setThreshold(threshold);
    173     setPostGain(postGain);
    174 }
    175 
    176 //----
    177 DPChannel::DPChannel() : mInitialized(false), mInputGainDb(0), mOutputGainDb(0),
    178         mPreEqInUse(false), mMbcInUse(false), mPostEqInUse(false), mLimiterInUse(false) {
    179 }
    180 
    181 void DPChannel::init(float inputGain, bool preEqInUse, uint32_t preEqBandCount,
    182         bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
    183         bool limiterInUse) {
    184     setInputGain(inputGain);
    185     mPreEqInUse = preEqInUse;
    186     mMbcInUse = mbcInUse;
    187     mPostEqInUse = postEqInUse;
    188     mLimiterInUse = limiterInUse;
    189 
    190     mPreEq.init(mPreEqInUse, false, preEqBandCount);
    191     mMbc.init(mMbcInUse, false, mbcBandCount);
    192     mPostEq.init(mPostEqInUse, false, postEqBandCount);
    193     mLimiter.init(mLimiterInUse, false, 0, 50, 120, 2, -30, 0);
    194     mInitialized = true;
    195 }
    196 
    197 DPEq* DPChannel::getPreEq() {
    198     if (!mInitialized) {
    199         return NULL;
    200     }
    201     return &mPreEq;
    202 }
    203 
    204 DPMbc* DPChannel::getMbc() {
    205     if (!mInitialized) {
    206         return NULL;
    207     }
    208     return &mMbc;
    209 }
    210 
    211 DPEq* DPChannel::getPostEq() {
    212     if (!mInitialized) {
    213         return NULL;
    214     }
    215     return &mPostEq;
    216 }
    217 
    218 DPLimiter* DPChannel::getLimiter() {
    219     if (!mInitialized) {
    220         return NULL;
    221     }
    222     return &mLimiter;
    223 }
    224 
    225 void DPChannel::setLimiter(DPLimiter &limiter) {
    226     if (!mInitialized) {
    227         return;
    228     }
    229     mLimiter = limiter;
    230 }
    231 
    232 //----
    233 DPBase::DPBase() : mInitialized(false), mChannelCount(0), mPreEqInUse(false), mPreEqBandCount(0),
    234         mMbcInUse(false), mMbcBandCount(0), mPostEqInUse(false), mPostEqBandCount(0),
    235         mLimiterInUse(false) {
    236 }
    237 
    238 void DPBase::init(uint32_t channelCount, bool preEqInUse, uint32_t preEqBandCount,
    239         bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
    240         bool limiterInUse) {
    241     ALOGV("DPBase::init");
    242     mChannelCount = channelCount;
    243     mPreEqInUse = preEqInUse;
    244     mPreEqBandCount = preEqBandCount;
    245     mMbcInUse = mbcInUse;
    246     mMbcBandCount = mbcBandCount;
    247     mPostEqInUse = postEqInUse;
    248     mPostEqBandCount = postEqBandCount;
    249     mLimiterInUse = limiterInUse;
    250     mChannel.resize(mChannelCount);
    251     for (size_t ch = 0; ch < mChannelCount; ch++) {
    252         mChannel[ch].init(0, preEqInUse, preEqBandCount, mbcInUse, mbcBandCount,
    253                 postEqInUse, postEqBandCount, limiterInUse);
    254     }
    255     mInitialized = true;
    256 }
    257 
    258 DPChannel* DPBase::getChannel(uint32_t channelIndex) {
    259     if (!mInitialized || channelIndex < 0 || channelIndex >= mChannel.size()) {
    260         return NULL;
    261     }
    262     return & mChannel[channelIndex];
    263 }
    264 
    265 } //namespace dp_fx
    266