Home | History | Annotate | Download | only in libopensles
      1 /*
      2  * Copyright (C) 2010 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 /* BassBoost implementation */
     18 
     19 #include "sles_allinclusive.h"
     20 
     21 #define BASSBOOST_STRENGTH_MIN 0
     22 #define BASSBOOST_STRENGTH_MAX 1000
     23 
     24 
     25 #if defined(ANDROID) && !defined(USE_BACKPORT)
     26 /**
     27  * returns true if this interface is not associated with an initialized BassBoost effect
     28  */
     29 static inline bool NO_BASSBOOST(IBassBoost* v) {
     30     return (v->mBassBoostEffect == 0);
     31 }
     32 #endif
     33 
     34 
     35 static SLresult IBassBoost_SetEnabled(SLBassBoostItf self, SLboolean enabled)
     36 {
     37     SL_ENTER_INTERFACE
     38 
     39     IBassBoost *this = (IBassBoost *) self;
     40     interface_lock_exclusive(this);
     41     this->mEnabled = (SLboolean) enabled;
     42 #if !defined(ANDROID) || defined(USE_BACKPORT)
     43     result = SL_RESULT_SUCCESS;
     44 #else
     45     if (NO_BASSBOOST(this)) {
     46         result = SL_RESULT_CONTROL_LOST;
     47     } else {
     48         android::status_t status = this->mBassBoostEffect->setEnabled((bool) this->mEnabled);
     49         result = android_fx_statusToResult(status);
     50     }
     51 #endif
     52     interface_unlock_exclusive(this);
     53 
     54     SL_LEAVE_INTERFACE
     55 }
     56 
     57 
     58 static SLresult IBassBoost_IsEnabled(SLBassBoostItf self, SLboolean *pEnabled)
     59 {
     60     SL_ENTER_INTERFACE
     61 
     62     if (NULL == pEnabled) {
     63         result = SL_RESULT_PARAMETER_INVALID;
     64     } else {
     65         IBassBoost *this = (IBassBoost *) self;
     66         interface_lock_exclusive(this);
     67         SLboolean enabled = this->mEnabled;
     68 #if !defined(ANDROID) || defined(USE_BACKPORT)
     69         *pEnabled = enabled;
     70         result = SL_RESULT_SUCCESS;
     71 #else
     72         if (NO_BASSBOOST(this)) {
     73             result = SL_RESULT_CONTROL_LOST;
     74         } else {
     75             *pEnabled = (SLboolean) this->mBassBoostEffect->getEnabled();
     76             result = SL_RESULT_SUCCESS;
     77         }
     78 #endif
     79         interface_unlock_exclusive(this);
     80     }
     81 
     82     SL_LEAVE_INTERFACE
     83 }
     84 
     85 
     86 static SLresult IBassBoost_SetStrength(SLBassBoostItf self, SLpermille strength)
     87 {
     88     SL_ENTER_INTERFACE
     89 
     90     if ((BASSBOOST_STRENGTH_MIN > strength) || (BASSBOOST_STRENGTH_MAX < strength)) {
     91         result = SL_RESULT_PARAMETER_INVALID;
     92     } else {
     93         IBassBoost *this = (IBassBoost *) self;
     94         interface_lock_exclusive(this);
     95 #if !defined(ANDROID) || defined(USE_BACKPORT)
     96         this->mStrength = strength;
     97         result = SL_RESULT_SUCCESS;
     98 #else
     99         if (NO_BASSBOOST(this)) {
    100             result = SL_RESULT_CONTROL_LOST;
    101         } else {
    102             android::status_t status =
    103                 android_bb_setParam(this->mBassBoostEffect, BASSBOOST_PARAM_STRENGTH, &strength);
    104             result = android_fx_statusToResult(status);
    105         }
    106 #endif
    107         interface_unlock_exclusive(this);
    108     }
    109 
    110     SL_LEAVE_INTERFACE
    111 }
    112 
    113 
    114 static SLresult IBassBoost_GetRoundedStrength(SLBassBoostItf self, SLpermille *pStrength)
    115 {
    116     SL_ENTER_INTERFACE
    117 
    118     if (NULL == pStrength) {
    119         result = SL_RESULT_PARAMETER_INVALID;
    120     } else {
    121         IBassBoost *this = (IBassBoost *) self;
    122         interface_lock_exclusive(this);
    123         SLpermille strength = this->mStrength;;
    124 #if !defined(ANDROID) || defined(USE_BACKPORT)
    125         result = SL_RESULT_SUCCESS;
    126 #else
    127         if (NO_BASSBOOST(this)) {
    128             result = SL_RESULT_CONTROL_LOST;
    129         } else {
    130             android::status_t status =
    131                    android_bb_getParam(this->mBassBoostEffect, BASSBOOST_PARAM_STRENGTH, &strength);
    132             result = android_fx_statusToResult(status);
    133         }
    134 #endif
    135         interface_unlock_exclusive(this);
    136         *pStrength = strength;
    137     }
    138 
    139     SL_LEAVE_INTERFACE
    140 }
    141 
    142 
    143 static SLresult IBassBoost_IsStrengthSupported(SLBassBoostItf self, SLboolean *pSupported)
    144 {
    145     SL_ENTER_INTERFACE
    146 
    147     if (NULL == pSupported) {
    148         result = SL_RESULT_PARAMETER_INVALID;
    149     } else {
    150 #if !defined(ANDROID) || defined(USE_BACKPORT)
    151         *pSupported = SL_BOOLEAN_TRUE;
    152         result = SL_RESULT_SUCCESS;
    153 #else
    154         IBassBoost *this = (IBassBoost *) self;
    155         int32_t supported = 0;
    156         interface_lock_exclusive(this);
    157         if (NO_BASSBOOST(this)) {
    158             result = SL_RESULT_CONTROL_LOST;
    159         } else {
    160             android::status_t status =
    161                 android_bb_getParam(this->mBassBoostEffect, BASSBOOST_PARAM_STRENGTH_SUPPORTED,
    162                         &supported);
    163             result = android_fx_statusToResult(status);
    164         }
    165         interface_unlock_exclusive(this);
    166         *pSupported = (SLboolean) (supported != 0);
    167 #endif
    168     }
    169 
    170     SL_LEAVE_INTERFACE
    171 }
    172 
    173 
    174 static const struct SLBassBoostItf_ IBassBoost_Itf = {
    175     IBassBoost_SetEnabled,
    176     IBassBoost_IsEnabled,
    177     IBassBoost_SetStrength,
    178     IBassBoost_GetRoundedStrength,
    179     IBassBoost_IsStrengthSupported
    180 };
    181 
    182 void IBassBoost_init(void *self)
    183 {
    184     IBassBoost *this = (IBassBoost *) self;
    185     this->mItf = &IBassBoost_Itf;
    186     this->mEnabled = SL_BOOLEAN_FALSE;
    187     this->mStrength = 0;
    188 #if defined(ANDROID) && !defined(USE_BACKPORT)
    189     memset(&this->mBassBoostDescriptor, 0, sizeof(effect_descriptor_t));
    190     // placement new (explicit constructor)
    191     (void) new (&this->mBassBoostEffect) android::sp<android::AudioEffect>();
    192 #endif
    193 }
    194 
    195 void IBassBoost_deinit(void *self)
    196 {
    197 #if defined(ANDROID) && !defined(USE_BACKPORT)
    198     IBassBoost *this = (IBassBoost *) self;
    199     // explicit destructor
    200     this->mBassBoostEffect.~sp();
    201 #endif
    202 }
    203 
    204 bool IBassBoost_Expose(void *self)
    205 {
    206 #if defined(ANDROID) && !defined(USE_BACKPORT)
    207     IBassBoost *this = (IBassBoost *) self;
    208     if (!android_fx_initEffectDescriptor(SL_IID_BASSBOOST, &this->mBassBoostDescriptor)) {
    209         SL_LOGE("BassBoost initialization failed.");
    210         return false;
    211     }
    212 #endif
    213     return true;
    214 }
    215