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