1/* 2 * Copyright (C) 2014 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/* Automatic Gain Control implementation */ 18#include "sles_allinclusive.h" 19 20#include <media/EffectsFactoryApi.h> 21 22#include <audio_effects/effect_agc.h> 23 24/** 25 * returns true if this interface is not associated with an initialized AGC effect 26 */ 27static inline bool NO_AUTOGAIN(IAndroidAutomaticGainControl* v) { 28 return (v->mAGCEffect == 0); 29} 30 31SLresult IAndroidAutomaticGainControl_SetEnabled(SLAndroidAutomaticGainControlItf self, 32 SLboolean enabled) 33{ 34 SL_ENTER_INTERFACE 35 36 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 37 interface_lock_exclusive(thiz); 38 thiz->mEnabled = (SLboolean) enabled; 39 if (NO_AUTOGAIN(thiz)) { 40 result = SL_RESULT_CONTROL_LOST; 41 } else { 42 android::status_t status = thiz->mAGCEffect->setEnabled((bool) thiz->mEnabled); 43 result = android_fx_statusToResult(status); 44 } 45 interface_unlock_exclusive(thiz); 46 47 SL_LEAVE_INTERFACE 48} 49 50SLresult IAndroidAutomaticGainControl_IsEnabled(SLAndroidAutomaticGainControlItf self, 51 SLboolean *pEnabled) 52{ 53 SL_ENTER_INTERFACE 54 55 if (NULL == pEnabled) { 56 result = SL_RESULT_PARAMETER_INVALID; 57 } else { 58 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 59 interface_lock_exclusive(thiz); 60 if (NO_AUTOGAIN(thiz)) { 61 result = SL_RESULT_CONTROL_LOST; 62 } else { 63 *pEnabled = (SLboolean) thiz->mAGCEffect->getEnabled(); 64 result = SL_RESULT_SUCCESS; 65 } 66 interface_unlock_exclusive(thiz); 67 } 68 SL_LEAVE_INTERFACE 69} 70 71SLresult IAndroidAutomaticGainControl_IsAvailable(SLAndroidAutomaticGainControlItf self, 72 SLboolean *pEnabled) 73{ 74 SL_ENTER_INTERFACE 75 76 *pEnabled = false; 77 78 uint32_t numEffects = 0; 79 int ret = EffectQueryNumberEffects(&numEffects); 80 if (ret != 0) { 81 ALOGE("IAndroidAutomaticGainControl_IsAvailable() error %d querying number of effects", 82 ret); 83 result = SL_RESULT_FEATURE_UNSUPPORTED; 84 } else { 85 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); 86 87 effect_descriptor_t fxDesc; 88 for (uint32_t i = 0 ; i < numEffects ; i++) { 89 if (EffectQueryEffect(i, &fxDesc) == 0) { 90 ALOGV("effect %d is called %s", i, fxDesc.name); 91 if (memcmp(&fxDesc.type, SL_IID_ANDROIDAUTOMATICGAINCONTROL, 92 sizeof(effect_uuid_t)) == 0) { 93 ALOGI("found effect \"%s\" from %s", fxDesc.name, fxDesc.implementor); 94 *pEnabled = true; 95 break; 96 } 97 } 98 } 99 result = SL_RESULT_SUCCESS; 100 } 101 SL_LEAVE_INTERFACE 102} 103 104static const struct SLAndroidAutomaticGainControlItf_ IAndroidAutomaticGainControl_Itf = { 105 IAndroidAutomaticGainControl_SetEnabled, 106 IAndroidAutomaticGainControl_IsEnabled, 107 IAndroidAutomaticGainControl_IsAvailable 108}; 109 110void IAndroidAutomaticGainControl_init(void *self) 111{ 112 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 113 thiz->mItf = &IAndroidAutomaticGainControl_Itf; 114 thiz->mEnabled = SL_BOOLEAN_FALSE; 115 memset(&thiz->mAGCDescriptor, 0, sizeof(effect_descriptor_t)); 116 // placement new (explicit constructor) 117 (void) new (&thiz->mAGCEffect) android::sp<android::AudioEffect>(); 118} 119 120void IAndroidAutomaticGainControl_deinit(void *self) 121{ 122 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 123 // explicit destructor 124 thiz->mAGCEffect.~sp(); 125} 126 127bool IAndroidAutomaticGainControl_Expose(void *self) 128{ 129 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 130 if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDAUTOMATICGAINCONTROL, 131 &thiz->mAGCDescriptor)) { 132 SL_LOGE("Automatic Gain Control initialization failed."); 133 return false; 134 } 135 return true; 136} 137