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 <system/audio_effects/effect_ns.h>
21/**
22 * returns true if this interface is not associated with an initialized Noise Suppression effect
23 */
24static inline bool NO_NOISESUPPRESS(IAndroidNoiseSuppression* v) {
25    return (v->mNSEffect == 0);
26}
27
28SLresult IAndroidNoiseSuppression_SetEnabled(SLAndroidNoiseSuppressionItf self, SLboolean enabled)
29{
30    SL_ENTER_INTERFACE
31
32    IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
33    interface_lock_exclusive(thiz);
34    thiz->mEnabled = (SLboolean) enabled;
35    if (NO_NOISESUPPRESS(thiz)) {
36        result = SL_RESULT_CONTROL_LOST;
37    } else {
38        android::status_t status = thiz->mNSEffect->setEnabled((bool) thiz->mEnabled);
39        result = android_fx_statusToResult(status);
40    }
41    interface_unlock_exclusive(thiz);
42
43    SL_LEAVE_INTERFACE
44}
45
46SLresult IAndroidNoiseSuppression_IsEnabled(SLAndroidNoiseSuppressionItf self, SLboolean *pEnabled)
47{
48    SL_ENTER_INTERFACE
49
50    if (NULL == pEnabled) {
51        result = SL_RESULT_PARAMETER_INVALID;
52    } else {
53        IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
54        interface_lock_exclusive(thiz);
55        if (NO_NOISESUPPRESS(thiz)) {
56            result = SL_RESULT_CONTROL_LOST;
57        } else {
58            *pEnabled = (SLboolean) thiz->mNSEffect->getEnabled();
59            result = SL_RESULT_SUCCESS;
60        }
61        interface_unlock_exclusive(thiz);
62    }
63    SL_LEAVE_INTERFACE
64}
65
66static const struct SLAndroidNoiseSuppressionItf_ IAndroidNoiseSuppression_Itf = {
67    IAndroidNoiseSuppression_SetEnabled,
68    IAndroidNoiseSuppression_IsEnabled
69};
70
71void IAndroidNoiseSuppression_init(void *self)
72{
73    IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
74    thiz->mItf = &IAndroidNoiseSuppression_Itf;
75    thiz->mEnabled = SL_BOOLEAN_FALSE;
76    memset(&thiz->mNSDescriptor, 0, sizeof(effect_descriptor_t));
77    // placement new (explicit constructor)
78    (void) new (&thiz->mNSEffect) android::sp<android::AudioEffect>();
79}
80
81void IAndroidNoiseSuppression_deinit(void *self)
82{
83    IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
84    // explicit destructor
85    thiz->mNSEffect.~sp();
86}
87
88bool IAndroidNoiseSuppression_Expose(void *self)
89{
90    IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
91    if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDNOISESUPPRESSION, &thiz->mNSDescriptor)) {
92        SL_LOGE("Noise Suppression initialization failed.");
93        return false;
94    }
95    return true;
96}
97